blob: 9541fc33b5e9f37656316a953642946fef5149a7 [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/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800119 * Functions for writing key_share extension.
120 */
121#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800122static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800123 mbedtls_ssl_context *ssl,
124 uint16_t named_group,
125 unsigned char *buf,
126 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000127 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800128{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800130 const mbedtls_ecp_curve_info *curve_info =
131 mbedtls_ecp_curve_info_from_tls_id( named_group );
132
133 if( curve_info == NULL )
134 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
135
136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
137
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800138 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
139 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800140 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800142 return( ret );
143 }
144
Xiaofei Baid25fab62021-12-02 06:36:27 +0000145 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
146 buf, end - buf,
147 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800148 if( ret != 0 )
149 {
150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
151 return( ret );
152 }
153
154 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
155 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800156 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800157}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800158#endif /* MBEDTLS_ECDH_C */
159
Jerry Yub60e3cf2021-09-08 16:41:02 +0800160static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
161 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800162{
163 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
164
Jerry Yu56fc07f2021-09-01 17:48:49 +0800165
Jerry Yu56fc07f2021-09-01 17:48:49 +0800166#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100167 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800168 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100169 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800170 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
171
Brett Warren14efd332021-10-06 09:32:11 +0100172 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800173 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000174 const mbedtls_ecp_curve_info *curve_info;
175 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
176 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100177 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800178 {
Brett Warren14efd332021-10-06 09:32:11 +0100179 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800180 return( 0 );
181 }
182 }
183#else
184 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800185 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800186#endif /* MBEDTLS_ECDH_C */
187
188 /*
189 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800190 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800191 */
192
193 return( ret );
194}
195
196/*
197 * ssl_tls13_write_key_share_ext
198 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800199 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800200 *
201 * struct {
202 * NamedGroup group;
203 * opaque key_exchange<1..2^16-1>;
204 * } KeyShareEntry;
205 * struct {
206 * KeyShareEntry client_shares<0..2^16-1>;
207 * } KeyShareClientHello;
208 */
209static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
210 unsigned char *buf,
211 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000212 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800213{
214 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000215 unsigned char *client_shares; /* Start of client_shares */
216 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800217 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800218 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
219
Xiaofei Baid25fab62021-12-02 06:36:27 +0000220 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800221
Jerry Yub60e3cf2021-09-08 16:41:02 +0800222 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800223 * - extension_type (2 bytes)
224 * - extension_data_length (2 bytes)
225 * - client_shares_length (2 bytes)
226 */
227 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
228 p += 6;
229
230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
231
232 /* HRR could already have requested something else. */
233 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800234 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
235 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800236 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800237 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800238 &group_id ) );
239 }
240
241 /*
242 * Dispatch to type-specific key generation function.
243 *
244 * So far, we're only supporting ECDHE. With the introduction
245 * of PQC KEMs, we'll want to have multiple branches, one per
246 * type of KEM, and dispatch to the corresponding crypto. And
247 * only one key share entry is allowed.
248 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000249 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800250#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800251 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800252 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800253 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000254 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255 /* Length of key_exchange */
256 size_t key_exchange_len;
257
258 /* Check there is space for header of KeyShareEntry
259 * - group (2 bytes)
260 * - key_exchange_length (2 bytes)
261 */
262 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
263 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800264 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
265 p, end,
266 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800267 p += key_exchange_len;
268 if( ret != 0 )
269 return( ret );
270
271 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000272 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800273 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000274 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800275 }
276 else
277#endif /* MBEDTLS_ECDH_C */
278 if( 0 /* other KEMs? */ )
279 {
280 /* Do something */
281 }
282 else
283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
284
Jerry Yub60e3cf2021-09-08 16:41:02 +0800285 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000286 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800287 if( client_shares_len == 0)
288 {
289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800291 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292 /* Write extension_type */
293 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
294 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800295 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800296 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800297 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800298
299 /* Update offered_group_id field */
300 ssl->handshake->offered_group_id = group_id;
301
302 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000303 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800304
Xiaofei Baid25fab62021-12-02 06:36:27 +0000305 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800306
307 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
308
309cleanup:
310
311 return( ret );
312}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800313
Jerry Yue1b9c292021-09-10 10:08:31 +0800314#if defined(MBEDTLS_ECDH_C)
315
Jerry Yuc068b662021-10-11 22:30:19 +0800316static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800317{
318 const mbedtls_ecp_curve_info *curve_info;
319 mbedtls_ecp_group_id grp_id;
320#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
321 grp_id = ssl->handshake->ecdh_ctx.grp.id;
322#else
323 grp_id = ssl->handshake->ecdh_ctx.grp_id;
324#endif
325
326 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
327 if( curve_info == NULL )
328 {
329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
331 }
332
333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
334
Jerry Yue1b9c292021-09-10 10:08:31 +0800335 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800336 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800337
338 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
339 MBEDTLS_DEBUG_ECDH_QP );
340
341 return( 0 );
342}
343
Jerry Yuc068b662021-10-11 22:30:19 +0800344static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
345 const unsigned char *buf,
346 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800347{
348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
349
Jerry Yuc068b662021-10-11 22:30:19 +0800350 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800351 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800352 if( ret != 0 )
353 {
354 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800355
356 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
357 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
358 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800359 }
360
Jerry Yuc068b662021-10-11 22:30:19 +0800361 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800362 {
Jerry Yuc068b662021-10-11 22:30:19 +0800363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800364
Jerry Yu4a173382021-10-11 21:45:31 +0800365 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
366 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
367 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800368 }
369
370 return( 0 );
371}
Jerry Yu4a173382021-10-11 21:45:31 +0800372#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800373
374/*
Jerry Yub85277e2021-10-13 13:36:05 +0800375 * ssl_tls13_parse_key_share_ext()
376 * Parse key_share extension in Server Hello
377 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800378 * struct {
379 * KeyShareEntry server_share;
380 * } KeyShareServerHello;
381 * struct {
382 * NamedGroup group;
383 * opaque key_exchange<1..2^16-1>;
384 * } KeyShareEntry;
385 */
Jerry Yu4a173382021-10-11 21:45:31 +0800386static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800387 const unsigned char *buf,
388 const unsigned char *end )
389{
Jerry Yub85277e2021-10-13 13:36:05 +0800390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800391 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800392 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800393
Jerry Yu4a173382021-10-11 21:45:31 +0800394 /* ...
395 * NamedGroup group; (2 bytes)
396 * ...
397 */
398 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
399 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800400 p += 2;
401
Jerry Yu4a173382021-10-11 21:45:31 +0800402 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800403 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800404 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800405 {
406 MBEDTLS_SSL_DEBUG_MSG( 1,
407 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800408 (unsigned) offered_group, (unsigned) group ) );
409 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
410 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
411 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800412 }
413
414#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800415 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800416 {
417 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800418 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800419 if( ret != 0 )
420 return( ret );
421 }
Jerry Yub85277e2021-10-13 13:36:05 +0800422 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800423#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800424 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800425 {
426 /* Do something */
427 }
428 else
429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
430
431 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
432 return( ret );
433}
434
Jerry Yubc20bdd2021-08-24 15:59:48 +0800435#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
436
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800437/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800438 * CipherSuite cipher_suites<2..2^16-2>;
439 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800440static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800441 mbedtls_ssl_context *ssl,
442 unsigned char *buf,
443 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000444 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800445{
Jerry Yufec982e2021-09-07 17:26:06 +0800446 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800447 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000448 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800449 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800450
Xiaofei Baid25fab62021-12-02 06:36:27 +0000451 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800452
453 /*
454 * Ciphersuite list
455 *
456 * This is a list of the symmetric cipher options supported by
457 * the client, specifically the record protection algorithm
458 * ( including secret key length ) and a hash to be used with
459 * HKDF, in descending order of client preference.
460 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800461 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800462
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800463 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800464 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
465 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800466
Jerry Yu0c63af62021-09-02 12:59:12 +0800467 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000468 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800469 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800470 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800471 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800472 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800473
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800474 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800475 if( ciphersuite_info == NULL )
476 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800477 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
478 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800479 continue;
480
481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800482 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800483 ciphersuite_info->name ) );
484
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800485 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800486 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
487 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
488 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800489 }
490
Jerry Yu0c63af62021-09-02 12:59:12 +0800491 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000492 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800493 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800494 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800495 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
496 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800497
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800498 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000499 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800500
Jerry Yu6a643102021-08-31 14:40:36 +0800501 return( 0 );
502}
503
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800504/*
505 * Structure of ClientHello message:
506 *
507 * struct {
508 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
509 * Random random;
510 * opaque legacy_session_id<0..32>;
511 * CipherSuite cipher_suites<2..2^16-2>;
512 * opaque legacy_compression_methods<1..2^8-1>;
513 * Extension extensions<8..2^16-1>;
514 * } ClientHello;
515 */
Jerry Yu08906d02021-08-31 11:05:27 +0800516static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800517 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800518 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000519 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800520{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800521
Jerry Yubc20bdd2021-08-24 15:59:48 +0800522 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000523 unsigned char *p_extensions_len; /* Pointer to extensions length */
524 size_t output_len; /* Length of buffer used by function */
525 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800526
Jerry Yubc20bdd2021-08-24 15:59:48 +0800527 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800528 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800529
Xiaofei Baid25fab62021-12-02 06:36:27 +0000530 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800531
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800532 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800533 ssl->major_ver = ssl->conf->min_major_ver;
534 ssl->minor_ver = ssl->conf->min_minor_ver;
535
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800536 /*
537 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800538 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800539 *
540 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800541 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800542 */
Jerry Yufec982e2021-09-07 17:26:06 +0800543 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800544 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800545 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800546
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800547 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800548 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
549 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800550 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800551 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
552 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800553
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800554 /*
555 * Write legacy_session_id
556 *
557 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
558 * which has been merged with pre-shared keys in this version. A client
559 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
560 * this field to that value. In compatibility mode, this field MUST be
561 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
562 * a new 32-byte value. This value need not be random but SHOULD be
563 * unpredictable to avoid implementations fixating on a specific value
564 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
565 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800566 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100567#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
568 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
569 *p++ = (unsigned char)ssl->session_negotiate->id_len;
570 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
571 p += ssl->session_negotiate->id_len;
572
573 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
574 ssl->session_negotiate->id_len );
575#else
Jerry Yubbe09522021-09-06 21:17:54 +0800576 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
577 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100578#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800579
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800580 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800581 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800582 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800583 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800584 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800585
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800586 /* Write legacy_compression_methods
587 *
588 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800589 * one byte set to zero, which corresponds to the 'null' compression
590 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800591 */
Jerry Yubbe09522021-09-06 21:17:54 +0800592 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
593 *p++ = 1;
594 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800595
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800596 /* Write extensions */
597
598 /* Keeping track of the included extensions */
599 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800600
601 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800602 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000603 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800604 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800605
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800606 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800607 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800608 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800609 */
Jerry Yubbe09522021-09-06 21:17:54 +0800610 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800611 if( ret != 0 )
612 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800613 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800614
615#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800616 /* Write supported_groups extension
617 *
618 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800619 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800620 /* Skip the extensions on the client if all allowed key exchanges
621 * are PSK-based. */
622 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
623 {
624 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
625 if( ret != 0 )
626 return( ret );
627 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800628
Jerry Yuf46b0162022-01-11 16:28:00 +0800629 /* Write key_share extension
630 *
631 * We need to send the key shares under three conditions:
632 * 1) A certificate-based ciphersuite is being offered. In this case
633 * supported_groups and supported_signature extensions have been
634 * successfully added.
635 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
636 * psk_key_exchange_modes has been added as the last extension.
637 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
638 * from above )
639 */
640 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
641 if( ret != 0 )
642 return( ret );
643 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800644
Jerry Yuf46b0162022-01-11 16:28:00 +0800645 /* Write signature_algorithms extension
646 *
647 * It is REQUIRED for certificate authenticated cipher_suites.
648 */
649 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
650 if( ret != 0 )
651 return( ret );
652 p += output_len;
653 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800654#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
655
Xiaofei Bai15a56812021-11-05 10:52:12 +0000656#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000657 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000658 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
659 if( ret != 0 )
660 return( ret );
661 p += output_len;
662#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
663
Jerry Yubc20bdd2021-08-24 15:59:48 +0800664 /* Add more extensions here */
665
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800666 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000667 extensions_len = p - p_extensions_len - 2;
668 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800670 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000671 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800672
Xiaofei Baid25fab62021-12-02 06:36:27 +0000673 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800674 return( 0 );
675}
676
Jerry Yu335aca92021-09-12 20:18:56 +0800677static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800678{
Jerry Yu92c6b402021-08-27 16:59:09 +0800679 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
680 return( 0 );
681}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800682
Jerry Yu92c6b402021-08-27 16:59:09 +0800683static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
684{
685 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800686
Jerry Yu92c6b402021-08-27 16:59:09 +0800687 if( ssl->conf->f_rng == NULL )
688 {
689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
690 return( MBEDTLS_ERR_SSL_NO_RNG );
691 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800692
Jerry Yu92c6b402021-08-27 16:59:09 +0800693 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
694 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800695 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800696 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800697 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800698 return( ret );
699 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800700
Ronald Cron49ad6192021-11-24 16:25:31 +0100701#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
702 /*
703 * Create a session identifier for the purpose of middlebox compatibility
704 * only if one has not been created already.
705 */
706 if( ssl->session_negotiate->id_len == 0 )
707 {
708 /* Creating a session id with 32 byte length */
709 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
710 ssl->session_negotiate->id, 32 ) ) != 0 )
711 {
712 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
713 return( ret );
714 }
715 ssl->session_negotiate->id_len = 32;
716 }
717#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
718
Jerry Yu6f13f642021-08-26 17:18:15 +0800719 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800720}
721
Jerry Yu92c6b402021-08-27 16:59:09 +0800722/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800723 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800724 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800725 */
726static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800727{
Jerry Yu92c6b402021-08-27 16:59:09 +0800728 int ret = 0;
729 unsigned char *buf;
730 size_t buf_len, msg_len;
731
732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
733
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800734 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800735
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800736 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
737 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
738 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800739
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800740 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800741 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800742 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800743
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800744 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
745 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800746 msg_len );
747 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800748
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800749 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
750 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
751 buf_len,
752 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800753
754cleanup:
755
756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
757 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800758}
759
Jerry Yu687101b2021-09-14 16:03:56 +0800760/*
Jerry Yu4a173382021-10-11 21:45:31 +0800761 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800762 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800763/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800764 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
765 * - SSL_SERVER_HELLO_COORDINATE_HRR
766 * to indicate which message is expected and to be parsed next. */
767#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
768#define SSL_SERVER_HELLO_COORDINATE_HRR 1
769static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
770 const unsigned char *buf,
771 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800772{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800773 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800774 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
775 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
776 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
777 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
778
779 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
780 *
Jerry Yu4a173382021-10-11 21:45:31 +0800781 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800782 * special value of the SHA-256 of "HelloRetryRequest".
783 *
784 * struct {
785 * ProtocolVersion legacy_version = 0x0303;
786 * Random random;
787 * opaque legacy_session_id_echo<0..32>;
788 * CipherSuite cipher_suite;
789 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800790 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800791 * } ServerHello;
792 *
793 */
Jerry Yub85277e2021-10-13 13:36:05 +0800794 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800795
796 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800797 {
Jerry Yub85277e2021-10-13 13:36:05 +0800798 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800799 }
800
Jerry Yub85277e2021-10-13 13:36:05 +0800801 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800802}
803
Jerry Yu745bb612021-10-13 22:01:04 +0800804/* Fetch and preprocess
805 * Returns a negative value on failure, and otherwise
806 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
807 * - SSL_SERVER_HELLO_COORDINATE_HRR
808 */
Jerry Yub85277e2021-10-13 13:36:05 +0800809static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
810 unsigned char **buf,
811 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800812{
Jerry Yu4a173382021-10-11 21:45:31 +0800813 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800814
815 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
816
Jerry Yue1b9c292021-09-10 10:08:31 +0800817 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
818 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
819 {
820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
821
822 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
823 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
824 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
825 }
826
827 *buf = ssl->in_msg + 4;
828 *buf_len = ssl->in_hslen - 4;
829
Jerry Yub85277e2021-10-13 13:36:05 +0800830 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
831 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800832 {
Jerry Yu745bb612021-10-13 22:01:04 +0800833 case SSL_SERVER_HELLO_COORDINATE_HELLO:
834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
835 break;
836 case SSL_SERVER_HELLO_COORDINATE_HRR:
837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
838 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800839 }
840
841cleanup:
842
843 return( ret );
844}
845
Jerry Yu4a173382021-10-11 21:45:31 +0800846static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
847 const unsigned char **buf,
848 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800849{
850 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800851 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800852
Jerry Yude4fb2c2021-09-19 18:05:08 +0800853 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800854 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800855
Jerry Yu4a173382021-10-11 21:45:31 +0800856 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800857
858 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800859 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
860 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800861 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800862 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
863 ssl->session_negotiate->id,
864 ssl->session_negotiate->id_len );
865 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800866 legacy_session_id_echo_len );
867
868 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
869 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
870
Jerry Yue1b9c292021-09-10 10:08:31 +0800871 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
872 }
873
Jerry Yu4a173382021-10-11 21:45:31 +0800874 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800875 *buf = p;
876
877 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800878 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800879 return( 0 );
880}
881
Jerry Yu4a173382021-10-11 21:45:31 +0800882static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
883 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800884{
Jerry Yu4a173382021-10-11 21:45:31 +0800885 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
886
Jerry Yue1b9c292021-09-10 10:08:31 +0800887 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800888 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800889 {
Jerry Yu4a173382021-10-11 21:45:31 +0800890 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800891 {
892 return( 1 );
893 }
894 }
895 return( 0 );
896}
897
898/* Parse ServerHello message and configure context
899 *
900 * struct {
901 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
902 * Random random;
903 * opaque legacy_session_id_echo<0..32>;
904 * CipherSuite cipher_suite;
905 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800906 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800907 * } ServerHello;
908 */
Jerry Yuc068b662021-10-11 22:30:19 +0800909static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
910 const unsigned char *buf,
911 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800912{
Jerry Yub85277e2021-10-13 13:36:05 +0800913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800914 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +0800915 size_t extensions_len;
916 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800917 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800918 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +0800919
920 /*
921 * Check there is space for minimal fields
922 *
923 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800924 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800925 * - legacy_session_id_echo ( 1 byte ), minimum size
926 * - cipher_suite ( 2 bytes)
927 * - legacy_compression_method ( 1 byte )
928 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800929 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800930
931 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800932 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
933
Jerry Yu4a173382021-10-11 21:45:31 +0800934 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800935 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800936 * ...
937 * with ProtocolVersion defined as:
938 * uint16 ProtocolVersion;
939 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800940 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
941 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
942 {
943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
944 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
945 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
946 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
947 }
948 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800949
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800950 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800951 * Random random;
952 * ...
953 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800954 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800955 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800956 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
957 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +0800958 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800959 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
960 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800961
Jerry Yu4a173382021-10-11 21:45:31 +0800962 /* ...
963 * opaque legacy_session_id_echo<0..32>;
964 * ...
965 */
966 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800967 {
968 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
969 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
970 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
971 }
972
Jerry Yu4a173382021-10-11 21:45:31 +0800973 /* ...
974 * CipherSuite cipher_suite;
975 * ...
976 * with CipherSuite defined as:
977 * uint8 CipherSuite[2];
978 */
979 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800980 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
981 p += 2;
982
Jerry Yu4a173382021-10-11 21:45:31 +0800983
984 /*
985 * Check whether this ciphersuite is supported and offered.
986 * Via the force_ciphersuite version we may have instructed the client
987 * to use a different ciphersuite.
988 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800989 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +0800990 if( ciphersuite_info == NULL ||
991 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800992 {
Jerry Yub85277e2021-10-13 13:36:05 +0800993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +0800994 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800995
Jerry Yu4a173382021-10-11 21:45:31 +0800996 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
997 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
998 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800999 }
1000
Jerry Yue1b9c292021-09-10 10:08:31 +08001001
Jerry Yu4a173382021-10-11 21:45:31 +08001002 /* Configure ciphersuites */
1003 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1004
1005 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001006 ssl->session_negotiate->ciphersuite = cipher_suite;
1007
Jerry Yue1b9c292021-09-10 10:08:31 +08001008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1009 cipher_suite, ciphersuite_info->name ) );
1010
1011#if defined(MBEDTLS_HAVE_TIME)
1012 ssl->session_negotiate->start = time( NULL );
1013#endif /* MBEDTLS_HAVE_TIME */
1014
Jerry Yu4a173382021-10-11 21:45:31 +08001015 /* ...
1016 * uint8 legacy_compression_method = 0;
1017 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001019 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 if( p[0] != 0 )
1021 {
Jerry Yub85277e2021-10-13 13:36:05 +08001022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001023 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1024 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1025 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1026 }
1027 p++;
1028
Jerry Yub85277e2021-10-13 13:36:05 +08001029 /* ...
1030 * Extension extensions<6..2^16-1>;
1031 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001032 * struct {
1033 * ExtensionType extension_type; (2 bytes)
1034 * opaque extension_data<0..2^16-1>;
1035 * } Extension;
1036 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001037 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001038 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001039 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001040
Jerry Yu4a173382021-10-11 21:45:31 +08001041 /* Check extensions do not go beyond the buffer of data. */
1042 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1043 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001044
Jerry Yu4a173382021-10-11 21:45:31 +08001045 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001046
Jerry Yu4a173382021-10-11 21:45:31 +08001047 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001048 {
1049 unsigned int extension_type;
1050 size_t extension_data_len;
1051
Jerry Yu4a173382021-10-11 21:45:31 +08001052 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001053 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1054 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1055 p += 4;
1056
Jerry Yu4a173382021-10-11 21:45:31 +08001057 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001058
1059 switch( extension_type )
1060 {
1061 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1062 MBEDTLS_SSL_DEBUG_MSG( 3,
1063 ( "found supported_versions extension" ) );
1064
Jerry Yuc068b662021-10-11 22:30:19 +08001065 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001066 p,
1067 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 if( ret != 0 )
1069 return( ret );
1070 break;
1071
1072 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001075
1076 MBEDTLS_SSL_PEND_FATAL_ALERT(
1077 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1078 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1079 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001080
1081#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1082 case MBEDTLS_TLS_EXT_KEY_SHARE:
1083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001084 if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +08001085 p, p + extension_data_len ) ) != 0 )
1086 {
1087 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001088 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001089 ret );
1090 return( ret );
1091 }
1092 break;
1093#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1094
1095 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001096 MBEDTLS_SSL_DEBUG_MSG(
1097 3,
1098 ( "unknown extension found: %u ( ignoring )",
1099 extension_type ) );
1100
1101 MBEDTLS_SSL_PEND_FATAL_ALERT(
1102 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1103 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1104 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001105 }
1106
1107 p += extension_data_len;
1108 }
1109
1110 return( 0 );
1111}
1112
Jerry Yuc068b662021-10-11 22:30:19 +08001113static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001114{
Jerry Yub85277e2021-10-13 13:36:05 +08001115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001116 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001117 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001118 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001119
Jerry Yub85277e2021-10-13 13:36:05 +08001120 /* Determine the key exchange mode:
1121 * 1) If both the pre_shared_key and key_share extensions were received
1122 * then the key exchange mode is PSK with EPHEMERAL.
1123 * 2) If only the pre_shared_key extension was received then the key
1124 * exchange mode is PSK-only.
1125 * 3) If only the key_share extension was received then the key
1126 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001127 */
Jerry Yub85277e2021-10-13 13:36:05 +08001128 switch( handshake->extensions_present &
1129 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001130 {
Jerry Yu745bb612021-10-13 22:01:04 +08001131 /* Only the pre_shared_key extension was received */
1132 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001133 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001134 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001135
Jerry Yu745bb612021-10-13 22:01:04 +08001136 /* Only the key_share extension was received */
1137 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001138 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001139 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001140
Jerry Yu745bb612021-10-13 22:01:04 +08001141 /* Both the pre_shared_key and key_share extensions were received */
1142 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001143 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001144 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001145
Jerry Yu745bb612021-10-13 22:01:04 +08001146 /* Neither pre_shared_key nor key_share extension was received */
1147 default:
1148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1149 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1150 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001151 }
1152
1153 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1154 *
1155 * TODO: We don't have to do this in case we offered 0-RTT and the
1156 * server accepted it. In this case, we could skip generating
1157 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001158 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001159 if( ret != 0 )
1160 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001162 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001163 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001164 }
1165
1166 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001167 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001168 if( ret != 0 )
1169 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001171 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001172 }
1173
1174 /* Next evolution in key schedule: Establish handshake secret and
1175 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001176 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001177 if( ret != 0 )
1178 {
Jerry Yuc068b662021-10-11 22:30:19 +08001179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1180 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001181 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001182 }
1183
Jerry Yub85277e2021-10-13 13:36:05 +08001184 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001185 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001186 {
1187 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1188 goto cleanup;
1189 }
Jerry Yu0b177842021-09-05 19:41:30 +08001190
1191 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1192 ssl->conf->endpoint,
1193 ssl->session_negotiate->ciphersuite,
1194 &traffic_keys,
1195 ssl );
1196 if( ret != 0 )
1197 {
1198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001199 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001200 }
1201
Jerry Yub85277e2021-10-13 13:36:05 +08001202 handshake->transform_handshake = transform_handshake;
1203 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001204
1205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1206 ssl->session_in = ssl->session_negotiate;
1207
1208 /*
1209 * State machine update
1210 */
1211 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1212
Jerry Yu4a173382021-10-11 21:45:31 +08001213cleanup:
1214
Jerry Yu0b177842021-09-05 19:41:30 +08001215 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001216 if( ret != 0 )
1217 {
Jerry Yub85277e2021-10-13 13:36:05 +08001218 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001219
Jerry Yu4a173382021-10-11 21:45:31 +08001220 MBEDTLS_SSL_PEND_FATAL_ALERT(
1221 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1222 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1223 }
1224 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001225}
1226
1227/*
Jerry Yu4a173382021-10-11 21:45:31 +08001228 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001229 * Handler for MBEDTLS_SSL_SERVER_HELLO
1230 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001231static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001232{
Jerry Yu4a173382021-10-11 21:45:31 +08001233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001234 unsigned char *buf;
1235 size_t buf_len;
1236
1237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1238
1239 /* Coordination step
1240 * - Fetch record
1241 * - Make sure it's either a ServerHello or a HRR.
1242 * - Switch processing routine in case of HRR
1243 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001244 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1245 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1246
Jerry Yub85277e2021-10-13 13:36:05 +08001247 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001248 /* Parsing step
1249 * We know what message to expect by now and call
1250 * the respective parsing function.
1251 */
1252 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1253 {
Jerry Yuc068b662021-10-11 22:30:19 +08001254 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1255 buf + buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001256
Xiaofei Bai746f9482021-11-12 08:53:56 +00001257 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1258 MBEDTLS_SSL_HS_SERVER_HELLO,
1259 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001260
Jerry Yuc068b662021-10-11 22:30:19 +08001261 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001262 }
1263 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1264 {
Jerry Yu4a173382021-10-11 21:45:31 +08001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR not supported" ) );
1266 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ,
1267 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1268 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yue1b9c292021-09-10 10:08:31 +08001269 }
1270
1271cleanup:
1272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1273 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001274}
1275
1276/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001277 *
1278 * EncryptedExtensions message
1279 *
1280 * The EncryptedExtensions message contains any extensions which
1281 * should be protected, i.e., any which are not needed to establish
1282 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001283 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001284
1285/*
1286 * Overview
1287 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001288
1289/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001290static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001291
XiaokangQian97799ac2021-10-11 10:05:54 +00001292static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1293 const unsigned char *buf,
1294 const unsigned char *end );
1295static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001296
1297/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001298 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001299 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001300static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001301{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001302 int ret;
1303 unsigned char *buf;
1304 size_t buf_len;
1305
1306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1307
Xiaofei Bai746f9482021-11-12 08:53:56 +00001308 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001309 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001310 &buf, &buf_len ) );
1311
1312 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001313 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001314 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001315
Xiaofei Bai746f9482021-11-12 08:53:56 +00001316 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001317 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001318
XiaokangQian97799ac2021-10-11 10:05:54 +00001319 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001320
1321cleanup:
1322
1323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1324 return( ret );
1325
1326}
1327
XiaokangQian08da26c2021-10-09 10:12:11 +00001328/* Parse EncryptedExtensions message
1329 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001330 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001331 * } EncryptedExtensions;
1332 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001333static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1334 const unsigned char *buf,
1335 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001336{
1337 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001338 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001339 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001340 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001341
XiaokangQian08da26c2021-10-09 10:12:11 +00001342 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001343 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001344 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001345
XiaokangQian97799ac2021-10-11 10:05:54 +00001346 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001347 extensions_end = p + extensions_len;
1348 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001349
XiaokangQian8db25ff2021-10-13 05:56:18 +00001350 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001351 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001352 unsigned int extension_type;
1353 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001354
XiaokangQian08da26c2021-10-09 10:12:11 +00001355 /*
1356 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001357 * ExtensionType extension_type; (2 bytes)
1358 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001359 * } Extension;
1360 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001361 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001362 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1363 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1364 p += 4;
1365
XiaokangQian8db25ff2021-10-13 05:56:18 +00001366 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001367
XiaokangQian97799ac2021-10-11 10:05:54 +00001368 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001369 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001370 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001371 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001372 switch( extension_type )
1373 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001374
XiaokangQian08da26c2021-10-09 10:12:11 +00001375 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1377 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001378
XiaokangQian08da26c2021-10-09 10:12:11 +00001379 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001380 MBEDTLS_SSL_DEBUG_MSG(
1381 3, ( "unsupported extension found: %u ", extension_type) );
1382 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001383 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001384 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1385 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001386 }
1387
XiaokangQian08da26c2021-10-09 10:12:11 +00001388 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001389 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001390
XiaokangQian97799ac2021-10-11 10:05:54 +00001391 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001392 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001393 {
1394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001395 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001396 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001397 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001398 }
1399
1400 return( ret );
1401}
1402
XiaokangQian97799ac2021-10-11 10:05:54 +00001403static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001404{
Jerry Yua93ac112021-10-27 16:31:48 +08001405#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001406 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001407 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1408 else
Jerry Yud2674312021-10-29 10:08:19 +08001409 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001410#else
1411 ((void) ssl);
1412 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1413#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001414 return( 0 );
1415}
1416
Jerry Yua93ac112021-10-27 16:31:48 +08001417#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001418/*
Jerry Yud2674312021-10-29 10:08:19 +08001419 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1420 */
1421static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1422{
1423 int ret = mbedtls_ssl_read_record( ssl, 0 );
1424
1425 if( ret != 0 )
1426 {
1427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1428 return( ret );
1429 }
1430
1431 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1432 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1433 {
1434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1435 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1436 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1437 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1438 }
1439
1440 ssl->keep_current_message = 1;
1441 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1442
1443 return( 0 );
1444}
1445
1446/*
Jerry Yu687101b2021-09-14 16:03:56 +08001447 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1448 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001449static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001450{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001451 int ret;
1452
1453 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001454 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001455 return( ret );
1456
Jerry Yu687101b2021-09-14 16:03:56 +08001457 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1458 return( 0 );
1459}
1460
1461/*
1462 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1463 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001464static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001465{
Jerry Yu30b071c2021-09-12 20:16:03 +08001466 int ret;
1467
1468 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1469 if( ret != 0 )
1470 return( ret );
1471
Jerry Yu687101b2021-09-14 16:03:56 +08001472 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1473 return( 0 );
1474}
Jerry Yua93ac112021-10-27 16:31:48 +08001475#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001476
Jerry Yu687101b2021-09-14 16:03:56 +08001477/*
1478 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1479 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001480static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001481{
XiaokangQianac0385c2021-11-03 06:40:11 +00001482 int ret;
1483
XiaokangQianc5c39d52021-11-09 11:55:10 +00001484 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001485 if( ret != 0 )
1486 return( ret );
1487
Ronald Cron49ad6192021-11-24 16:25:31 +01001488#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1489 mbedtls_ssl_handshake_set_state(
1490 ssl,
1491 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1492#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001493 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001494#endif
1495
XiaokangQianac0385c2021-11-03 06:40:11 +00001496 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001497}
1498
1499/*
Ronald Crond4c64022021-12-06 09:06:46 +01001500 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1501 */
1502#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1503static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1504{
1505 int ret;
1506
1507 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1508 if( ret != 0 )
1509 return( ret );
1510
1511 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1512
1513 return( 0 );
1514}
1515#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1516
1517/*
Jerry Yu687101b2021-09-14 16:03:56 +08001518 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1519 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001520static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001521{
XiaokangQian0fa66432021-11-15 03:33:57 +00001522 int ret;
1523
Ronald Cron49ad6192021-11-24 16:25:31 +01001524 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1525
XiaokangQian0fa66432021-11-15 03:33:57 +00001526 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1527 if( ret != 0 )
1528 return( ret );
1529
1530 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1531 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001532}
1533
1534/*
1535 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1536 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001537static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001538{
Jerry Yu378254d2021-10-30 21:44:47 +08001539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001540 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001541 return( 0 );
1542}
1543
1544/*
1545 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1546 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001547static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001548{
Jerry Yu378254d2021-10-30 21:44:47 +08001549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1550 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1551
1552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1553 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1554
1555 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1556
1557 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1558 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001559}
1560
Jerry Yu92c6b402021-08-27 16:59:09 +08001561int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001562{
Jerry Yu92c6b402021-08-27 16:59:09 +08001563 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001564
Jerry Yue3b34122021-09-28 17:53:35 +08001565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1566 mbedtls_ssl_states_str( ssl->state ),
1567 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001568
1569 switch( ssl->state )
1570 {
1571 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001572 * ssl->state is initialized as HELLO_REQUEST. It is the same
1573 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001574 */
1575 case MBEDTLS_SSL_HELLO_REQUEST:
1576 case MBEDTLS_SSL_CLIENT_HELLO:
1577 ret = ssl_tls13_write_client_hello( ssl );
1578 break;
1579
1580 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001581 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001582 break;
1583
1584 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001585 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001586 break;
1587
Jerry Yua93ac112021-10-27 16:31:48 +08001588#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001589 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1590 ret = ssl_tls13_process_certificate_request( ssl );
1591 break;
1592
Jerry Yu687101b2021-09-14 16:03:56 +08001593 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001594 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001595 break;
1596
1597 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001598 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001599 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001600#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001601
1602 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001603 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001604 break;
1605
Jerry Yu687101b2021-09-14 16:03:56 +08001606 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001607 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001608 break;
1609
1610 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001611 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001612 break;
1613
1614 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001615 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001616 break;
1617
Ronald Cron49ad6192021-11-24 16:25:31 +01001618 /*
1619 * Injection of dummy-CCS's for middlebox compatibility
1620 */
1621#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1622 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Ronald Crond4c64022021-12-06 09:06:46 +01001623 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001624 break;
1625#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1626
Jerry Yu92c6b402021-08-27 16:59:09 +08001627 default:
1628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1630 }
1631
1632 return( ret );
1633}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001634
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001635#endif /* MBEDTLS_SSL_CLI_C */
1636
Ronald Cron6f135e12021-12-08 16:57:54 +01001637#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */