blob: 5b6aee1f83bb26d43fe53cf6c30c48ca53a647e5 [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
222 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
223 return( 0 );
224
Jerry Yub60e3cf2021-09-08 16:41:02 +0800225 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800226 * - extension_type (2 bytes)
227 * - extension_data_length (2 bytes)
228 * - client_shares_length (2 bytes)
229 */
230 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
231 p += 6;
232
233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
234
235 /* HRR could already have requested something else. */
236 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800237 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
238 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800239 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800240 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800241 &group_id ) );
242 }
243
244 /*
245 * Dispatch to type-specific key generation function.
246 *
247 * So far, we're only supporting ECDHE. With the introduction
248 * of PQC KEMs, we'll want to have multiple branches, one per
249 * type of KEM, and dispatch to the corresponding crypto. And
250 * only one key share entry is allowed.
251 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000252 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800253#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800254 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800256 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000257 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800258 /* Length of key_exchange */
259 size_t key_exchange_len;
260
261 /* Check there is space for header of KeyShareEntry
262 * - group (2 bytes)
263 * - key_exchange_length (2 bytes)
264 */
265 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
266 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800267 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
268 p, end,
269 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800270 p += key_exchange_len;
271 if( ret != 0 )
272 return( ret );
273
274 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000275 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000277 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800278 }
279 else
280#endif /* MBEDTLS_ECDH_C */
281 if( 0 /* other KEMs? */ )
282 {
283 /* Do something */
284 }
285 else
286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
287
Jerry Yub60e3cf2021-09-08 16:41:02 +0800288 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000289 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800290 if( client_shares_len == 0)
291 {
292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800294 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800295 /* Write extension_type */
296 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
297 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800298 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800300 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800301
302 /* Update offered_group_id field */
303 ssl->handshake->offered_group_id = group_id;
304
305 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000306 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800307
Xiaofei Baid25fab62021-12-02 06:36:27 +0000308 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800309
310 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
311
312cleanup:
313
314 return( ret );
315}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800316
Jerry Yue1b9c292021-09-10 10:08:31 +0800317#if defined(MBEDTLS_ECDH_C)
318
Jerry Yuc068b662021-10-11 22:30:19 +0800319static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800320{
321 const mbedtls_ecp_curve_info *curve_info;
322 mbedtls_ecp_group_id grp_id;
323#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
324 grp_id = ssl->handshake->ecdh_ctx.grp.id;
325#else
326 grp_id = ssl->handshake->ecdh_ctx.grp_id;
327#endif
328
329 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
330 if( curve_info == NULL )
331 {
332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
334 }
335
336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
337
Jerry Yue1b9c292021-09-10 10:08:31 +0800338 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800339 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800340
341 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
342 MBEDTLS_DEBUG_ECDH_QP );
343
344 return( 0 );
345}
346
Jerry Yuc068b662021-10-11 22:30:19 +0800347static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
348 const unsigned char *buf,
349 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800350{
351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
352
Jerry Yuc068b662021-10-11 22:30:19 +0800353 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800354 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800355 if( ret != 0 )
356 {
357 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800358
359 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
360 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
361 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800362 }
363
Jerry Yuc068b662021-10-11 22:30:19 +0800364 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800365 {
Jerry Yuc068b662021-10-11 22:30:19 +0800366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800367
Jerry Yu4a173382021-10-11 21:45:31 +0800368 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
369 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
370 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800371 }
372
373 return( 0 );
374}
Jerry Yu4a173382021-10-11 21:45:31 +0800375#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800376
377/*
Jerry Yub85277e2021-10-13 13:36:05 +0800378 * ssl_tls13_parse_key_share_ext()
379 * Parse key_share extension in Server Hello
380 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800381 * struct {
382 * KeyShareEntry server_share;
383 * } KeyShareServerHello;
384 * struct {
385 * NamedGroup group;
386 * opaque key_exchange<1..2^16-1>;
387 * } KeyShareEntry;
388 */
Jerry Yu4a173382021-10-11 21:45:31 +0800389static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800390 const unsigned char *buf,
391 const unsigned char *end )
392{
Jerry Yub85277e2021-10-13 13:36:05 +0800393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800394 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800395 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800396
Jerry Yu4a173382021-10-11 21:45:31 +0800397 /* ...
398 * NamedGroup group; (2 bytes)
399 * ...
400 */
401 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
402 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800403 p += 2;
404
Jerry Yu4a173382021-10-11 21:45:31 +0800405 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800406 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800407 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800408 {
409 MBEDTLS_SSL_DEBUG_MSG( 1,
410 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800411 (unsigned) offered_group, (unsigned) group ) );
412 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
413 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
414 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800415 }
416
417#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800418 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800419 {
420 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800421 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800422 if( ret != 0 )
423 return( ret );
424 }
Jerry Yub85277e2021-10-13 13:36:05 +0800425 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800426#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800427 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800428 {
429 /* Do something */
430 }
431 else
432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
433
434 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
435 return( ret );
436}
437
Jerry Yubc20bdd2021-08-24 15:59:48 +0800438#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
439
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800440/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800441 * CipherSuite cipher_suites<2..2^16-2>;
442 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800443static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800444 mbedtls_ssl_context *ssl,
445 unsigned char *buf,
446 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000447 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800448{
Jerry Yufec982e2021-09-07 17:26:06 +0800449 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800450 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000451 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800452 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800453
Xiaofei Baid25fab62021-12-02 06:36:27 +0000454 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800455
456 /*
457 * Ciphersuite list
458 *
459 * This is a list of the symmetric cipher options supported by
460 * the client, specifically the record protection algorithm
461 * ( including secret key length ) and a hash to be used with
462 * HKDF, in descending order of client preference.
463 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800464 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800465
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800466 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800467 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
468 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800469
Jerry Yu0c63af62021-09-02 12:59:12 +0800470 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000471 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800472 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800473 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800474 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800476
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800477 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800478 if( ciphersuite_info == NULL )
479 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800480 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
481 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800482 continue;
483
484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800485 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800486 ciphersuite_info->name ) );
487
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800488 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800489 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
490 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
491 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800492 }
493
Jerry Yu0c63af62021-09-02 12:59:12 +0800494 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000495 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800496 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800497 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800498 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
499 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800500
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800501 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000502 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800503
Jerry Yu6a643102021-08-31 14:40:36 +0800504 return( 0 );
505}
506
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800507/*
508 * Structure of ClientHello message:
509 *
510 * struct {
511 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
512 * Random random;
513 * opaque legacy_session_id<0..32>;
514 * CipherSuite cipher_suites<2..2^16-2>;
515 * opaque legacy_compression_methods<1..2^8-1>;
516 * Extension extensions<8..2^16-1>;
517 * } ClientHello;
518 */
Jerry Yu08906d02021-08-31 11:05:27 +0800519static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800520 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800521 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000522 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800523{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800524
Jerry Yubc20bdd2021-08-24 15:59:48 +0800525 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000526 unsigned char *p_extensions_len; /* Pointer to extensions length */
527 size_t output_len; /* Length of buffer used by function */
528 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800529
Jerry Yubc20bdd2021-08-24 15:59:48 +0800530 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800531 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800532
Xiaofei Baid25fab62021-12-02 06:36:27 +0000533 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800534
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800535 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800536 ssl->major_ver = ssl->conf->min_major_ver;
537 ssl->minor_ver = ssl->conf->min_minor_ver;
538
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800539 /*
540 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800541 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800542 *
543 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800544 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800545 */
Jerry Yufec982e2021-09-07 17:26:06 +0800546 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800547 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800548 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800549
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800550 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800551 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
552 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800553 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800554 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
555 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800556
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800557 /*
558 * Write legacy_session_id
559 *
560 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
561 * which has been merged with pre-shared keys in this version. A client
562 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
563 * this field to that value. In compatibility mode, this field MUST be
564 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
565 * a new 32-byte value. This value need not be random but SHOULD be
566 * unpredictable to avoid implementations fixating on a specific value
567 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
568 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800569 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100570#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
571 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
572 *p++ = (unsigned char)ssl->session_negotiate->id_len;
573 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
574 p += ssl->session_negotiate->id_len;
575
576 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
577 ssl->session_negotiate->id_len );
578#else
Jerry Yubbe09522021-09-06 21:17:54 +0800579 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
580 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100581#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800582
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800583 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800584 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800585 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800586 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800587 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800588
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800589 /* Write legacy_compression_methods
590 *
591 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800592 * one byte set to zero, which corresponds to the 'null' compression
593 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800594 */
Jerry Yubbe09522021-09-06 21:17:54 +0800595 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
596 *p++ = 1;
597 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800598
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800599 /* Write extensions */
600
601 /* Keeping track of the included extensions */
602 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800603
604 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800605 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000606 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800607 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800608
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800609 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800610 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800611 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800612 */
Jerry Yubbe09522021-09-06 21:17:54 +0800613 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800614 if( ret != 0 )
615 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800616 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800617
618#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800619 /* Write supported_groups extension
620 *
621 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800622 */
Jerry Yu7581c112021-12-20 22:25:41 +0800623 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800624 if( ret != 0 )
625 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800626 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800627
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800628 /* Write key_share extension
629 *
630 * We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800631 * 1) A certificate-based ciphersuite is being offered. In this case
632 * supported_groups and supported_signature extensions have been
633 * successfully added.
634 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800635 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800636 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
637 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800638 */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800639 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800640 if( ret != 0 )
641 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800642 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800643
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800644 /* Write signature_algorithms extension
645 *
646 * It is REQUIRED for certificate authenticated cipher_suites.
647 */
Jerry Yubbe09522021-09-06 21:17:54 +0800648 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800649 if( ret != 0 )
650 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800651 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800652
Jerry Yubc20bdd2021-08-24 15:59:48 +0800653#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
654
Xiaofei Bai15a56812021-11-05 10:52:12 +0000655#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000656 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000657 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
658 if( ret != 0 )
659 return( ret );
660 p += output_len;
661#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
662
Jerry Yubc20bdd2021-08-24 15:59:48 +0800663 /* Add more extensions here */
664
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800665 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000666 extensions_len = p - p_extensions_len - 2;
667 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800669 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000670 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800671
Xiaofei Baid25fab62021-12-02 06:36:27 +0000672 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800673 return( 0 );
674}
675
Jerry Yu335aca92021-09-12 20:18:56 +0800676static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800677{
Jerry Yu92c6b402021-08-27 16:59:09 +0800678 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
679 return( 0 );
680}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800681
Jerry Yu92c6b402021-08-27 16:59:09 +0800682static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
683{
684 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800685
Jerry Yu92c6b402021-08-27 16:59:09 +0800686 if( ssl->conf->f_rng == NULL )
687 {
688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
689 return( MBEDTLS_ERR_SSL_NO_RNG );
690 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800691
Jerry Yu92c6b402021-08-27 16:59:09 +0800692 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
693 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800694 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800695 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800696 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800697 return( ret );
698 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800699
Ronald Cron49ad6192021-11-24 16:25:31 +0100700#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
701 /*
702 * Create a session identifier for the purpose of middlebox compatibility
703 * only if one has not been created already.
704 */
705 if( ssl->session_negotiate->id_len == 0 )
706 {
707 /* Creating a session id with 32 byte length */
708 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
709 ssl->session_negotiate->id, 32 ) ) != 0 )
710 {
711 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
712 return( ret );
713 }
714 ssl->session_negotiate->id_len = 32;
715 }
716#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
717
Jerry Yu6f13f642021-08-26 17:18:15 +0800718 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800719}
720
Jerry Yu92c6b402021-08-27 16:59:09 +0800721/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800722 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800723 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800724 */
725static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800726{
Jerry Yu92c6b402021-08-27 16:59:09 +0800727 int ret = 0;
728 unsigned char *buf;
729 size_t buf_len, msg_len;
730
731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
732
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800733 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800734
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800735 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
736 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
737 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800738
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800739 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800740 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800741 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800742
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800743 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
744 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800745 msg_len );
746 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800747
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800748 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
749 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
750 buf_len,
751 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800752
753cleanup:
754
755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
756 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800757}
758
Jerry Yu687101b2021-09-14 16:03:56 +0800759/*
Jerry Yu4a173382021-10-11 21:45:31 +0800760 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800761 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800762/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800763 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
764 * - SSL_SERVER_HELLO_COORDINATE_HRR
765 * to indicate which message is expected and to be parsed next. */
766#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
767#define SSL_SERVER_HELLO_COORDINATE_HRR 1
768static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
769 const unsigned char *buf,
770 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800771{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800772 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800773 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
774 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
775 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
776 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
777
778 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
779 *
Jerry Yu4a173382021-10-11 21:45:31 +0800780 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800781 * special value of the SHA-256 of "HelloRetryRequest".
782 *
783 * struct {
784 * ProtocolVersion legacy_version = 0x0303;
785 * Random random;
786 * opaque legacy_session_id_echo<0..32>;
787 * CipherSuite cipher_suite;
788 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800789 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800790 * } ServerHello;
791 *
792 */
Jerry Yub85277e2021-10-13 13:36:05 +0800793 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800794
795 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800796 {
Jerry Yub85277e2021-10-13 13:36:05 +0800797 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800798 }
799
Jerry Yub85277e2021-10-13 13:36:05 +0800800 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800801}
802
Jerry Yu745bb612021-10-13 22:01:04 +0800803/* Fetch and preprocess
804 * Returns a negative value on failure, and otherwise
805 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
806 * - SSL_SERVER_HELLO_COORDINATE_HRR
807 */
Jerry Yub85277e2021-10-13 13:36:05 +0800808static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
809 unsigned char **buf,
810 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800811{
Jerry Yu4a173382021-10-11 21:45:31 +0800812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800813
814 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
815
Jerry Yue1b9c292021-09-10 10:08:31 +0800816 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
817 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
818 {
819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
820
821 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
822 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
823 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
824 }
825
826 *buf = ssl->in_msg + 4;
827 *buf_len = ssl->in_hslen - 4;
828
Jerry Yub85277e2021-10-13 13:36:05 +0800829 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
830 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800831 {
Jerry Yu745bb612021-10-13 22:01:04 +0800832 case SSL_SERVER_HELLO_COORDINATE_HELLO:
833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
834 break;
835 case SSL_SERVER_HELLO_COORDINATE_HRR:
836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
837 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800838 }
839
840cleanup:
841
842 return( ret );
843}
844
Jerry Yu4a173382021-10-11 21:45:31 +0800845static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
846 const unsigned char **buf,
847 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800848{
849 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800850 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800851
Jerry Yude4fb2c2021-09-19 18:05:08 +0800852 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800853 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800854
Jerry Yu4a173382021-10-11 21:45:31 +0800855 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800856
857 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800858 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
859 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800860 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800861 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
862 ssl->session_negotiate->id,
863 ssl->session_negotiate->id_len );
864 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800865 legacy_session_id_echo_len );
866
867 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
868 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
869
Jerry Yue1b9c292021-09-10 10:08:31 +0800870 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
871 }
872
Jerry Yu4a173382021-10-11 21:45:31 +0800873 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800874 *buf = p;
875
876 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800877 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800878 return( 0 );
879}
880
Jerry Yu4a173382021-10-11 21:45:31 +0800881static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
882 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800883{
Jerry Yu4a173382021-10-11 21:45:31 +0800884 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
885
Jerry Yue1b9c292021-09-10 10:08:31 +0800886 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800887 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800888 {
Jerry Yu4a173382021-10-11 21:45:31 +0800889 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800890 {
891 return( 1 );
892 }
893 }
894 return( 0 );
895}
896
897/* Parse ServerHello message and configure context
898 *
899 * struct {
900 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
901 * Random random;
902 * opaque legacy_session_id_echo<0..32>;
903 * CipherSuite cipher_suite;
904 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800905 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 * } ServerHello;
907 */
Jerry Yuc068b662021-10-11 22:30:19 +0800908static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
909 const unsigned char *buf,
910 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800911{
Jerry Yub85277e2021-10-13 13:36:05 +0800912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800913 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +0800914 size_t extensions_len;
915 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800916 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800917 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +0800918
919 /*
920 * Check there is space for minimal fields
921 *
922 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800923 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800924 * - legacy_session_id_echo ( 1 byte ), minimum size
925 * - cipher_suite ( 2 bytes)
926 * - legacy_compression_method ( 1 byte )
927 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800928 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800929
930 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800931 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
932
Jerry Yu4a173382021-10-11 21:45:31 +0800933 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800934 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800935 * ...
936 * with ProtocolVersion defined as:
937 * uint16 ProtocolVersion;
938 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800939 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
940 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
941 {
942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
943 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
944 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
945 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
946 }
947 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800948
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800949 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800950 * Random random;
951 * ...
952 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800953 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800954 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800955 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
956 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +0800957 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800958 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
959 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800960
Jerry Yu4a173382021-10-11 21:45:31 +0800961 /* ...
962 * opaque legacy_session_id_echo<0..32>;
963 * ...
964 */
965 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800966 {
967 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
968 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
969 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
970 }
971
Jerry Yu4a173382021-10-11 21:45:31 +0800972 /* ...
973 * CipherSuite cipher_suite;
974 * ...
975 * with CipherSuite defined as:
976 * uint8 CipherSuite[2];
977 */
978 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800979 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
980 p += 2;
981
Jerry Yu4a173382021-10-11 21:45:31 +0800982
983 /*
984 * Check whether this ciphersuite is supported and offered.
985 * Via the force_ciphersuite version we may have instructed the client
986 * to use a different ciphersuite.
987 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800988 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +0800989 if( ciphersuite_info == NULL ||
990 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800991 {
Jerry Yub85277e2021-10-13 13:36:05 +0800992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +0800993 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +0800994
Jerry Yu4a173382021-10-11 21:45:31 +0800995 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
996 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
997 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800998 }
999
Jerry Yue1b9c292021-09-10 10:08:31 +08001000
Jerry Yu4a173382021-10-11 21:45:31 +08001001 /* Configure ciphersuites */
1002 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1003
1004 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001005 ssl->session_negotiate->ciphersuite = cipher_suite;
1006
Jerry Yue1b9c292021-09-10 10:08:31 +08001007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1008 cipher_suite, ciphersuite_info->name ) );
1009
1010#if defined(MBEDTLS_HAVE_TIME)
1011 ssl->session_negotiate->start = time( NULL );
1012#endif /* MBEDTLS_HAVE_TIME */
1013
Jerry Yu4a173382021-10-11 21:45:31 +08001014 /* ...
1015 * uint8 legacy_compression_method = 0;
1016 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001017 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001018 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001019 if( p[0] != 0 )
1020 {
Jerry Yub85277e2021-10-13 13:36:05 +08001021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001022 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1023 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1024 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1025 }
1026 p++;
1027
Jerry Yub85277e2021-10-13 13:36:05 +08001028 /* ...
1029 * Extension extensions<6..2^16-1>;
1030 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001031 * struct {
1032 * ExtensionType extension_type; (2 bytes)
1033 * opaque extension_data<0..2^16-1>;
1034 * } Extension;
1035 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001036 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001037 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001038 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001039
Jerry Yu4a173382021-10-11 21:45:31 +08001040 /* Check extensions do not go beyond the buffer of data. */
1041 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1042 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001043
Jerry Yu4a173382021-10-11 21:45:31 +08001044 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001045
Jerry Yu4a173382021-10-11 21:45:31 +08001046 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001047 {
1048 unsigned int extension_type;
1049 size_t extension_data_len;
1050
Jerry Yu4a173382021-10-11 21:45:31 +08001051 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001052 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1053 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1054 p += 4;
1055
Jerry Yu4a173382021-10-11 21:45:31 +08001056 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001057
1058 switch( extension_type )
1059 {
1060 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1061 MBEDTLS_SSL_DEBUG_MSG( 3,
1062 ( "found supported_versions extension" ) );
1063
Jerry Yuc068b662021-10-11 22:30:19 +08001064 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001065 p,
1066 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001067 if( ret != 0 )
1068 return( ret );
1069 break;
1070
1071 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001074
1075 MBEDTLS_SSL_PEND_FATAL_ALERT(
1076 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1077 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1078 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001079
1080#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1081 case MBEDTLS_TLS_EXT_KEY_SHARE:
1082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001083 if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +08001084 p, p + extension_data_len ) ) != 0 )
1085 {
1086 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001087 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001088 ret );
1089 return( ret );
1090 }
1091 break;
1092#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1093
1094 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001095 MBEDTLS_SSL_DEBUG_MSG(
1096 3,
1097 ( "unknown extension found: %u ( ignoring )",
1098 extension_type ) );
1099
1100 MBEDTLS_SSL_PEND_FATAL_ALERT(
1101 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1102 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1103 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001104 }
1105
1106 p += extension_data_len;
1107 }
1108
1109 return( 0 );
1110}
1111
Jerry Yuc068b662021-10-11 22:30:19 +08001112static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001113{
Jerry Yub85277e2021-10-13 13:36:05 +08001114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001115 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001116 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001117 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001118
Jerry Yub85277e2021-10-13 13:36:05 +08001119 /* Determine the key exchange mode:
1120 * 1) If both the pre_shared_key and key_share extensions were received
1121 * then the key exchange mode is PSK with EPHEMERAL.
1122 * 2) If only the pre_shared_key extension was received then the key
1123 * exchange mode is PSK-only.
1124 * 3) If only the key_share extension was received then the key
1125 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001126 */
Jerry Yub85277e2021-10-13 13:36:05 +08001127 switch( handshake->extensions_present &
1128 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001129 {
Jerry Yu745bb612021-10-13 22:01:04 +08001130 /* Only the pre_shared_key extension was received */
1131 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001132 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001133 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001134
Jerry Yu745bb612021-10-13 22:01:04 +08001135 /* Only the key_share extension was received */
1136 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001137 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001138 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001139
Jerry Yu745bb612021-10-13 22:01:04 +08001140 /* Both the pre_shared_key and key_share extensions were received */
1141 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001142 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001143 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001144
Jerry Yu745bb612021-10-13 22:01:04 +08001145 /* Neither pre_shared_key nor key_share extension was received */
1146 default:
1147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1148 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1149 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001150 }
1151
1152 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1153 *
1154 * TODO: We don't have to do this in case we offered 0-RTT and the
1155 * server accepted it. In this case, we could skip generating
1156 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001157 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001158 if( ret != 0 )
1159 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001160 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001161 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001162 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001163 }
1164
1165 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001166 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001167 if( ret != 0 )
1168 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001170 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001171 }
1172
1173 /* Next evolution in key schedule: Establish handshake secret and
1174 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001175 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001176 if( ret != 0 )
1177 {
Jerry Yuc068b662021-10-11 22:30:19 +08001178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1179 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001180 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001181 }
1182
Jerry Yub85277e2021-10-13 13:36:05 +08001183 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001184 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001185 {
1186 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1187 goto cleanup;
1188 }
Jerry Yu0b177842021-09-05 19:41:30 +08001189
1190 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1191 ssl->conf->endpoint,
1192 ssl->session_negotiate->ciphersuite,
1193 &traffic_keys,
1194 ssl );
1195 if( ret != 0 )
1196 {
1197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001198 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001199 }
1200
Jerry Yub85277e2021-10-13 13:36:05 +08001201 handshake->transform_handshake = transform_handshake;
1202 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001203
1204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1205 ssl->session_in = ssl->session_negotiate;
1206
1207 /*
1208 * State machine update
1209 */
1210 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1211
Jerry Yu4a173382021-10-11 21:45:31 +08001212cleanup:
1213
Jerry Yu0b177842021-09-05 19:41:30 +08001214 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001215 if( ret != 0 )
1216 {
Jerry Yub85277e2021-10-13 13:36:05 +08001217 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001218
Jerry Yu4a173382021-10-11 21:45:31 +08001219 MBEDTLS_SSL_PEND_FATAL_ALERT(
1220 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1221 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1222 }
1223 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001224}
1225
1226/*
Jerry Yu4a173382021-10-11 21:45:31 +08001227 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001228 * Handler for MBEDTLS_SSL_SERVER_HELLO
1229 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001230static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001231{
Jerry Yu4a173382021-10-11 21:45:31 +08001232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001233 unsigned char *buf;
1234 size_t buf_len;
1235
1236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1237
1238 /* Coordination step
1239 * - Fetch record
1240 * - Make sure it's either a ServerHello or a HRR.
1241 * - Switch processing routine in case of HRR
1242 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001243 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1244 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1245
Jerry Yub85277e2021-10-13 13:36:05 +08001246 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001247 /* Parsing step
1248 * We know what message to expect by now and call
1249 * the respective parsing function.
1250 */
1251 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1252 {
Jerry Yuc068b662021-10-11 22:30:19 +08001253 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1254 buf + buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001255
Xiaofei Bai746f9482021-11-12 08:53:56 +00001256 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1257 MBEDTLS_SSL_HS_SERVER_HELLO,
1258 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001259
Jerry Yuc068b662021-10-11 22:30:19 +08001260 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001261 }
1262 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1263 {
Jerry Yu4a173382021-10-11 21:45:31 +08001264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR not supported" ) );
1265 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ,
1266 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1267 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yue1b9c292021-09-10 10:08:31 +08001268 }
1269
1270cleanup:
1271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1272 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001273}
1274
1275/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001276 *
1277 * EncryptedExtensions message
1278 *
1279 * The EncryptedExtensions message contains any extensions which
1280 * should be protected, i.e., any which are not needed to establish
1281 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001282 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001283
1284/*
1285 * Overview
1286 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001287
1288/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001289static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001290
XiaokangQian97799ac2021-10-11 10:05:54 +00001291static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1292 const unsigned char *buf,
1293 const unsigned char *end );
1294static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001295
1296/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001297 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001298 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001299static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001300{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001301 int ret;
1302 unsigned char *buf;
1303 size_t buf_len;
1304
1305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1306
Xiaofei Bai746f9482021-11-12 08:53:56 +00001307 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001308 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001309 &buf, &buf_len ) );
1310
1311 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001312 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001313 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001314
Xiaofei Bai746f9482021-11-12 08:53:56 +00001315 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001316 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001317
XiaokangQian97799ac2021-10-11 10:05:54 +00001318 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001319
1320cleanup:
1321
1322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1323 return( ret );
1324
1325}
1326
XiaokangQian08da26c2021-10-09 10:12:11 +00001327/* Parse EncryptedExtensions message
1328 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001329 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001330 * } EncryptedExtensions;
1331 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001332static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1333 const unsigned char *buf,
1334 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001335{
1336 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001337 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001338 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001339 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001340
XiaokangQian08da26c2021-10-09 10:12:11 +00001341 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001342 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001343 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001344
XiaokangQian97799ac2021-10-11 10:05:54 +00001345 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001346 extensions_end = p + extensions_len;
1347 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001348
XiaokangQian8db25ff2021-10-13 05:56:18 +00001349 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001350 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001351 unsigned int extension_type;
1352 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001353
XiaokangQian08da26c2021-10-09 10:12:11 +00001354 /*
1355 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001356 * ExtensionType extension_type; (2 bytes)
1357 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001358 * } Extension;
1359 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001360 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001361 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1362 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1363 p += 4;
1364
XiaokangQian8db25ff2021-10-13 05:56:18 +00001365 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001366
XiaokangQian97799ac2021-10-11 10:05:54 +00001367 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001368 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001369 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001370 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001371 switch( extension_type )
1372 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001373
XiaokangQian08da26c2021-10-09 10:12:11 +00001374 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1376 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001377
XiaokangQian08da26c2021-10-09 10:12:11 +00001378 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001379 MBEDTLS_SSL_DEBUG_MSG(
1380 3, ( "unsupported extension found: %u ", extension_type) );
1381 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001382 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001383 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1384 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001385 }
1386
XiaokangQian08da26c2021-10-09 10:12:11 +00001387 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001388 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001389
XiaokangQian97799ac2021-10-11 10:05:54 +00001390 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001391 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001392 {
1393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001394 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001395 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001396 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001397 }
1398
1399 return( ret );
1400}
1401
XiaokangQian97799ac2021-10-11 10:05:54 +00001402static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001403{
Jerry Yua93ac112021-10-27 16:31:48 +08001404#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001405 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001406 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1407 else
Jerry Yud2674312021-10-29 10:08:19 +08001408 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001409#else
1410 ((void) ssl);
1411 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1412#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001413 return( 0 );
1414}
1415
Jerry Yua93ac112021-10-27 16:31:48 +08001416#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001417/*
Jerry Yud2674312021-10-29 10:08:19 +08001418 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1419 */
1420static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1421{
1422 int ret = mbedtls_ssl_read_record( ssl, 0 );
1423
1424 if( ret != 0 )
1425 {
1426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1427 return( ret );
1428 }
1429
1430 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1431 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1432 {
1433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1434 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1435 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1436 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1437 }
1438
1439 ssl->keep_current_message = 1;
1440 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1441
1442 return( 0 );
1443}
1444
1445/*
Jerry Yu687101b2021-09-14 16:03:56 +08001446 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1447 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001448static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001449{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001450 int ret;
1451
1452 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001453 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001454 return( ret );
1455
Jerry Yu687101b2021-09-14 16:03:56 +08001456 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1457 return( 0 );
1458}
1459
1460/*
1461 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1462 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001463static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001464{
Jerry Yu30b071c2021-09-12 20:16:03 +08001465 int ret;
1466
1467 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1468 if( ret != 0 )
1469 return( ret );
1470
Jerry Yu687101b2021-09-14 16:03:56 +08001471 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1472 return( 0 );
1473}
Jerry Yua93ac112021-10-27 16:31:48 +08001474#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001475
Jerry Yu687101b2021-09-14 16:03:56 +08001476/*
1477 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1478 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001479static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001480{
XiaokangQianac0385c2021-11-03 06:40:11 +00001481 int ret;
1482
XiaokangQianc5c39d52021-11-09 11:55:10 +00001483 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001484 if( ret != 0 )
1485 return( ret );
1486
Ronald Cron49ad6192021-11-24 16:25:31 +01001487#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1488 mbedtls_ssl_handshake_set_state(
1489 ssl,
1490 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1491#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001492 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001493#endif
1494
XiaokangQianac0385c2021-11-03 06:40:11 +00001495 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001496}
1497
1498/*
Ronald Crond4c64022021-12-06 09:06:46 +01001499 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1500 */
1501#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1502static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1503{
1504 int ret;
1505
1506 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1507 if( ret != 0 )
1508 return( ret );
1509
1510 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1511
1512 return( 0 );
1513}
1514#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1515
1516/*
Jerry Yu687101b2021-09-14 16:03:56 +08001517 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1518 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001519static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001520{
XiaokangQian0fa66432021-11-15 03:33:57 +00001521 int ret;
1522
Ronald Cron49ad6192021-11-24 16:25:31 +01001523 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1524
XiaokangQian0fa66432021-11-15 03:33:57 +00001525 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1526 if( ret != 0 )
1527 return( ret );
1528
1529 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1530 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001531}
1532
1533/*
1534 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1535 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001536static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001537{
Jerry Yu378254d2021-10-30 21:44:47 +08001538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001539 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001540 return( 0 );
1541}
1542
1543/*
1544 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1545 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001546static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001547{
Jerry Yu378254d2021-10-30 21:44:47 +08001548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1549 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1550
1551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1552 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1553
1554 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1555
1556 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1557 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001558}
1559
Jerry Yu92c6b402021-08-27 16:59:09 +08001560int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001561{
Jerry Yu92c6b402021-08-27 16:59:09 +08001562 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001563
Jerry Yue3b34122021-09-28 17:53:35 +08001564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1565 mbedtls_ssl_states_str( ssl->state ),
1566 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001567
1568 switch( ssl->state )
1569 {
1570 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001571 * ssl->state is initialized as HELLO_REQUEST. It is the same
1572 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001573 */
1574 case MBEDTLS_SSL_HELLO_REQUEST:
1575 case MBEDTLS_SSL_CLIENT_HELLO:
1576 ret = ssl_tls13_write_client_hello( ssl );
1577 break;
1578
1579 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001580 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001581 break;
1582
1583 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001584 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001585 break;
1586
Jerry Yua93ac112021-10-27 16:31:48 +08001587#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001588 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1589 ret = ssl_tls13_process_certificate_request( ssl );
1590 break;
1591
Jerry Yu687101b2021-09-14 16:03:56 +08001592 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001593 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001594 break;
1595
1596 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001597 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001598 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001599#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001600
1601 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001602 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001603 break;
1604
Jerry Yu687101b2021-09-14 16:03:56 +08001605 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001606 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001607 break;
1608
1609 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001610 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001611 break;
1612
1613 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001614 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001615 break;
1616
Ronald Cron49ad6192021-11-24 16:25:31 +01001617 /*
1618 * Injection of dummy-CCS's for middlebox compatibility
1619 */
1620#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1621 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Ronald Crond4c64022021-12-06 09:06:46 +01001622 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001623 break;
1624#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1625
Jerry Yu92c6b402021-08-27 16:59:09 +08001626 default:
1627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1629 }
1630
1631 return( ret );
1632}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001633
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001634#endif /* MBEDTLS_SSL_CLI_C */
1635
Ronald Cron6f135e12021-12-08 16:57:54 +01001636#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */