blob: 18b9074e6900f8d8b985224cb2d973591455b941 [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"
Gilles Peskine923d5c92021-12-15 12:56:54 +010037#include "ssl_debug_helpers.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/*
XiaokangQian647719a2021-12-07 09:16:29 +0000119 * Key Shares Extension
120 *
121 * enum {
122 * ... (0xFFFF)
123 * } NamedGroup;
124 *
125 * struct {
126 * NamedGroup group;
127 * opaque key_exchange<1..2^16-1>;
128 * } KeyShareEntry;
129 *
130 * struct {
131 * select(role) {
132 * case client:
133 * KeyShareEntry client_shares<0..2^16-1>;
134 * }
135 * } KeyShare;
136 */
137
138static int ssl_reset_ecdhe_share( mbedtls_ssl_context *ssl )
139{
140 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
141 return( 0 );
142}
143
144static int ssl_reset_key_share( mbedtls_ssl_context *ssl )
145{
146 uint16_t group_id = ssl->handshake->offered_group_id;
147 if( group_id == 0 )
148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
149
150 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
151 return( ssl_reset_ecdhe_share( ssl ) );
152 else if( 0 /* other KEMs? */ )
153 {
154 /* Do something */
155 }
156
157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
158}
159
160/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800161 * Functions for writing key_share extension.
162 */
163#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800164static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800165 mbedtls_ssl_context *ssl,
166 uint16_t named_group,
167 unsigned char *buf,
168 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000169 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800170{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800172 const mbedtls_ecp_curve_info *curve_info =
173 mbedtls_ecp_curve_info_from_tls_id( named_group );
174
175 if( curve_info == NULL )
176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
177
178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
179
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800180 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
181 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800182 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800184 return( ret );
185 }
186
Xiaofei Baid25fab62021-12-02 06:36:27 +0000187 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
188 buf, end - buf,
189 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800190 if( ret != 0 )
191 {
192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
193 return( ret );
194 }
195
196 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
197 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800198 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800199}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800200#endif /* MBEDTLS_ECDH_C */
201
Jerry Yub60e3cf2021-09-08 16:41:02 +0800202static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
203 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800204{
205 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
206
Jerry Yu56fc07f2021-09-01 17:48:49 +0800207
Jerry Yu56fc07f2021-09-01 17:48:49 +0800208#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100209 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800210 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100211 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800212 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
213
Brett Warren14efd332021-10-06 09:32:11 +0100214 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800215 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000216 const mbedtls_ecp_curve_info *curve_info;
217 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
218 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100219 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800220 {
Brett Warren14efd332021-10-06 09:32:11 +0100221 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800222 return( 0 );
223 }
224 }
225#else
226 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800227 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800228#endif /* MBEDTLS_ECDH_C */
229
230 /*
231 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800232 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800233 */
234
235 return( ret );
236}
237
238/*
239 * ssl_tls13_write_key_share_ext
240 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800241 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800242 *
243 * struct {
244 * NamedGroup group;
245 * opaque key_exchange<1..2^16-1>;
246 * } KeyShareEntry;
247 * struct {
248 * KeyShareEntry client_shares<0..2^16-1>;
249 * } KeyShareClientHello;
250 */
251static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
252 unsigned char *buf,
253 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000254 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255{
256 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000257 unsigned char *client_shares; /* Start of client_shares */
258 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800259 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800260 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
261
Xiaofei Baid25fab62021-12-02 06:36:27 +0000262 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800263
Jerry Yub60e3cf2021-09-08 16:41:02 +0800264 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 * - extension_type (2 bytes)
266 * - extension_data_length (2 bytes)
267 * - client_shares_length (2 bytes)
268 */
269 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
270 p += 6;
271
272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
273
274 /* HRR could already have requested something else. */
275 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800276 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
277 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800278 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800279 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800280 &group_id ) );
281 }
282
283 /*
284 * Dispatch to type-specific key generation function.
285 *
286 * So far, we're only supporting ECDHE. With the introduction
287 * of PQC KEMs, we'll want to have multiple branches, one per
288 * type of KEM, and dispatch to the corresponding crypto. And
289 * only one key share entry is allowed.
290 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000291 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800292#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800293 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800294 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800295 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000296 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800297 /* Length of key_exchange */
298 size_t key_exchange_len;
299
300 /* Check there is space for header of KeyShareEntry
301 * - group (2 bytes)
302 * - key_exchange_length (2 bytes)
303 */
304 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
305 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800306 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
307 p, end,
308 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800309 p += key_exchange_len;
310 if( ret != 0 )
311 return( ret );
312
313 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000314 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800315 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000316 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800317 }
318 else
319#endif /* MBEDTLS_ECDH_C */
320 if( 0 /* other KEMs? */ )
321 {
322 /* Do something */
323 }
324 else
325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
326
Jerry Yub60e3cf2021-09-08 16:41:02 +0800327 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000328 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800329 if( client_shares_len == 0)
330 {
331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800333 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800334 /* Write extension_type */
335 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
336 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800337 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800338 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800339 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800340
341 /* Update offered_group_id field */
342 ssl->handshake->offered_group_id = group_id;
343
344 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000345 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800346
Xiaofei Baid25fab62021-12-02 06:36:27 +0000347 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800348
349 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
350
351cleanup:
352
353 return( ret );
354}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800355
Jerry Yue1b9c292021-09-10 10:08:31 +0800356#if defined(MBEDTLS_ECDH_C)
357
Jerry Yuc068b662021-10-11 22:30:19 +0800358static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800359{
360 const mbedtls_ecp_curve_info *curve_info;
361 mbedtls_ecp_group_id grp_id;
362#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
363 grp_id = ssl->handshake->ecdh_ctx.grp.id;
364#else
365 grp_id = ssl->handshake->ecdh_ctx.grp_id;
366#endif
367
368 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
369 if( curve_info == NULL )
370 {
371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
372 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
373 }
374
375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
376
Jerry Yue1b9c292021-09-10 10:08:31 +0800377 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800378 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800379
380 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
381 MBEDTLS_DEBUG_ECDH_QP );
382
383 return( 0 );
384}
385
Jerry Yuc068b662021-10-11 22:30:19 +0800386static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
387 const unsigned char *buf,
388 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800389{
390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
391
Jerry Yuc068b662021-10-11 22:30:19 +0800392 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800393 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800394 if( ret != 0 )
395 {
396 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800397
398 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
399 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
400 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800401 }
402
Jerry Yuc068b662021-10-11 22:30:19 +0800403 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800404 {
Jerry Yuc068b662021-10-11 22:30:19 +0800405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800406
Jerry Yu4a173382021-10-11 21:45:31 +0800407 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
408 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
409 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800410 }
411
412 return( 0 );
413}
Jerry Yu4a173382021-10-11 21:45:31 +0800414#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800415
416/*
Jerry Yub85277e2021-10-13 13:36:05 +0800417 * ssl_tls13_parse_key_share_ext()
418 * Parse key_share extension in Server Hello
419 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800420 * struct {
421 * KeyShareEntry server_share;
422 * } KeyShareServerHello;
423 * struct {
424 * NamedGroup group;
425 * opaque key_exchange<1..2^16-1>;
426 * } KeyShareEntry;
427 */
Jerry Yu4a173382021-10-11 21:45:31 +0800428static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800429 const unsigned char *buf,
430 const unsigned char *end )
431{
Jerry Yub85277e2021-10-13 13:36:05 +0800432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800433 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800434 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800435
Jerry Yu4a173382021-10-11 21:45:31 +0800436 /* ...
437 * NamedGroup group; (2 bytes)
438 * ...
439 */
440 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
441 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800442 p += 2;
443
Jerry Yu4a173382021-10-11 21:45:31 +0800444 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800445 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800446 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800447 {
448 MBEDTLS_SSL_DEBUG_MSG( 1,
449 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800450 (unsigned) offered_group, (unsigned) group ) );
451 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
452 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
453 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800454 }
455
456#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800457 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800458 {
459 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800460 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800461 if( ret != 0 )
462 return( ret );
463 }
Jerry Yub85277e2021-10-13 13:36:05 +0800464 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800465#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800466 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800467 {
468 /* Do something */
469 }
470 else
471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
472
473 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
474 return( ret );
475}
476
Jerry Yubc20bdd2021-08-24 15:59:48 +0800477#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
478
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800479/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800480 * CipherSuite cipher_suites<2..2^16-2>;
481 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800482static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800483 mbedtls_ssl_context *ssl,
484 unsigned char *buf,
485 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000486 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800487{
Jerry Yufec982e2021-09-07 17:26:06 +0800488 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800489 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000490 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800491 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800492
Xiaofei Baid25fab62021-12-02 06:36:27 +0000493 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800494
495 /*
496 * Ciphersuite list
497 *
498 * This is a list of the symmetric cipher options supported by
499 * the client, specifically the record protection algorithm
500 * ( including secret key length ) and a hash to be used with
501 * HKDF, in descending order of client preference.
502 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800503 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800504
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800505 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800506 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
507 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800508
Jerry Yu0c63af62021-09-02 12:59:12 +0800509 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000510 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800511 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800512 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800513 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800514 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800515
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800516 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800517 if( ciphersuite_info == NULL )
518 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800519 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
520 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800521 continue;
522
523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800524 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800525 ciphersuite_info->name ) );
526
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800527 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800528 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
529 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
530 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800531 }
532
Jerry Yu0c63af62021-09-02 12:59:12 +0800533 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000534 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800535 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800536 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800537 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
538 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800539
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800540 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000541 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800542
Jerry Yu6a643102021-08-31 14:40:36 +0800543 return( 0 );
544}
545
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800546/*
547 * Structure of ClientHello message:
548 *
549 * struct {
550 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
551 * Random random;
552 * opaque legacy_session_id<0..32>;
553 * CipherSuite cipher_suites<2..2^16-2>;
554 * opaque legacy_compression_methods<1..2^8-1>;
555 * Extension extensions<8..2^16-1>;
556 * } ClientHello;
557 */
Jerry Yu08906d02021-08-31 11:05:27 +0800558static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800559 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800560 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000561 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800562{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800563
Jerry Yubc20bdd2021-08-24 15:59:48 +0800564 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000565 unsigned char *p_extensions_len; /* Pointer to extensions length */
566 size_t output_len; /* Length of buffer used by function */
567 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800568
Jerry Yubc20bdd2021-08-24 15:59:48 +0800569 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800570 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800571
Xiaofei Baid25fab62021-12-02 06:36:27 +0000572 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800573
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800574 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800575 ssl->major_ver = ssl->conf->min_major_ver;
576 ssl->minor_ver = ssl->conf->min_minor_ver;
577
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800578 /*
579 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800580 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800581 *
582 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800583 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800584 */
Jerry Yufec982e2021-09-07 17:26:06 +0800585 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800586 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800587 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800588
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800589 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800590 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
591 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800592 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800593 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
594 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800595
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800596 /*
597 * Write legacy_session_id
598 *
599 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
600 * which has been merged with pre-shared keys in this version. A client
601 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
602 * this field to that value. In compatibility mode, this field MUST be
603 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
604 * a new 32-byte value. This value need not be random but SHOULD be
605 * unpredictable to avoid implementations fixating on a specific value
606 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
607 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800608 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100609#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
610 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
611 *p++ = (unsigned char)ssl->session_negotiate->id_len;
612 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
613 p += ssl->session_negotiate->id_len;
614
615 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
616 ssl->session_negotiate->id_len );
617#else
Jerry Yubbe09522021-09-06 21:17:54 +0800618 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
619 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100620#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800621
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800622 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800623 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800624 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800625 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 legacy_compression_methods
629 *
630 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800631 * one byte set to zero, which corresponds to the 'null' compression
632 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800633 */
Jerry Yubbe09522021-09-06 21:17:54 +0800634 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
635 *p++ = 1;
636 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800637
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800638 /* Write extensions */
639
640 /* Keeping track of the included extensions */
641 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800642
643 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800644 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000645 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800646 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800647
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800648 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800649 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800650 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800651 */
Jerry Yubbe09522021-09-06 21:17:54 +0800652 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800653 if( ret != 0 )
654 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800655 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800656
657#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800658
Jerry Yub925f212022-01-12 11:17:02 +0800659 /*
660 * Add the extensions related to (EC)DHE ephemeral key establishment only if
661 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800662 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800663 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
664 {
665 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
666 if( ret != 0 )
667 return( ret );
668 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800669
Jerry Yuf46b0162022-01-11 16:28:00 +0800670 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
671 if( ret != 0 )
672 return( ret );
673 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800674
Jerry Yuf017ee42022-01-12 15:49:48 +0800675 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800676 if( ret != 0 )
677 return( ret );
678 p += output_len;
679 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800680#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
681
Xiaofei Bai15a56812021-11-05 10:52:12 +0000682#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000683 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000684 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
685 if( ret != 0 )
686 return( ret );
687 p += output_len;
688#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
689
Jerry Yubc20bdd2021-08-24 15:59:48 +0800690 /* Add more extensions here */
691
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800692 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000693 extensions_len = p - p_extensions_len - 2;
694 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800696 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000697 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800698
Xiaofei Baid25fab62021-12-02 06:36:27 +0000699 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800700 return( 0 );
701}
702
Jerry Yu335aca92021-09-12 20:18:56 +0800703static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800704{
Jerry Yu92c6b402021-08-27 16:59:09 +0800705 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
706 return( 0 );
707}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800708
Jerry Yu92c6b402021-08-27 16:59:09 +0800709static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
710{
711 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800712
Jerry Yu92c6b402021-08-27 16:59:09 +0800713 if( ssl->conf->f_rng == NULL )
714 {
715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
716 return( MBEDTLS_ERR_SSL_NO_RNG );
717 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800718
Jerry Yu92c6b402021-08-27 16:59:09 +0800719 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
720 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800721 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800722 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800723 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800724 return( ret );
725 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800726
Ronald Cron49ad6192021-11-24 16:25:31 +0100727#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
728 /*
729 * Create a session identifier for the purpose of middlebox compatibility
730 * only if one has not been created already.
731 */
732 if( ssl->session_negotiate->id_len == 0 )
733 {
734 /* Creating a session id with 32 byte length */
735 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
736 ssl->session_negotiate->id, 32 ) ) != 0 )
737 {
738 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
739 return( ret );
740 }
741 ssl->session_negotiate->id_len = 32;
742 }
743#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
744
Jerry Yu6f13f642021-08-26 17:18:15 +0800745 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800746}
747
Jerry Yu92c6b402021-08-27 16:59:09 +0800748/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800749 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800750 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800751 */
752static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800753{
Jerry Yu92c6b402021-08-27 16:59:09 +0800754 int ret = 0;
755 unsigned char *buf;
756 size_t buf_len, msg_len;
757
758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
759
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800760 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800761
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800762 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
763 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
764 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800765
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800766 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800767 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800768 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800769
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800770 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
771 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800772 msg_len );
773 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800774
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800775 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
776 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
777 buf_len,
778 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800779
780cleanup:
781
782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
783 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800784}
785
Jerry Yu687101b2021-09-14 16:03:56 +0800786/*
Jerry Yu4a173382021-10-11 21:45:31 +0800787 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +0800788 */
Jerry Yu7a186a02021-10-15 18:46:14 +0800789/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +0800790 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
791 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +0000792 * to indicate which message is expected and to be parsed next.
793 */
Jerry Yub85277e2021-10-13 13:36:05 +0800794#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
795#define SSL_SERVER_HELLO_COORDINATE_HRR 1
796static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
797 const unsigned char *buf,
798 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800799{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800800 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800801 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
802 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
803 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
804 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
805
806 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
807 *
Jerry Yu4a173382021-10-11 21:45:31 +0800808 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800809 * special value of the SHA-256 of "HelloRetryRequest".
810 *
811 * struct {
812 * ProtocolVersion legacy_version = 0x0303;
813 * Random random;
814 * opaque legacy_session_id_echo<0..32>;
815 * CipherSuite cipher_suite;
816 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800817 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800818 * } ServerHello;
819 *
820 */
Jerry Yub85277e2021-10-13 13:36:05 +0800821 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800822
823 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800824 {
Jerry Yub85277e2021-10-13 13:36:05 +0800825 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800826 }
827
Jerry Yub85277e2021-10-13 13:36:05 +0800828 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800829}
830
Jerry Yu745bb612021-10-13 22:01:04 +0800831/* Fetch and preprocess
832 * Returns a negative value on failure, and otherwise
833 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
834 * - SSL_SERVER_HELLO_COORDINATE_HRR
835 */
Jerry Yub85277e2021-10-13 13:36:05 +0800836static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
837 unsigned char **buf,
838 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800839{
Jerry Yu4a173382021-10-11 21:45:31 +0800840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800841
842 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
843
Jerry Yue1b9c292021-09-10 10:08:31 +0800844 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
845 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
846 {
847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
848
849 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
850 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
851 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
852 }
853
854 *buf = ssl->in_msg + 4;
855 *buf_len = ssl->in_hslen - 4;
856
Jerry Yub85277e2021-10-13 13:36:05 +0800857 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
858 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800859 {
Jerry Yu745bb612021-10-13 22:01:04 +0800860 case SSL_SERVER_HELLO_COORDINATE_HELLO:
861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
862 break;
863 case SSL_SERVER_HELLO_COORDINATE_HRR:
864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
865 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800866 }
867
868cleanup:
869
870 return( ret );
871}
872
Jerry Yu4a173382021-10-11 21:45:31 +0800873static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
874 const unsigned char **buf,
875 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800876{
877 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800878 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800879
Jerry Yude4fb2c2021-09-19 18:05:08 +0800880 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800881 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800882
Jerry Yu4a173382021-10-11 21:45:31 +0800883 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800884
885 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800886 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
887 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800888 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800889 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
890 ssl->session_negotiate->id,
891 ssl->session_negotiate->id_len );
892 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800893 legacy_session_id_echo_len );
894
895 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
896 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
897
Jerry Yue1b9c292021-09-10 10:08:31 +0800898 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
899 }
900
Jerry Yu4a173382021-10-11 21:45:31 +0800901 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800902 *buf = p;
903
904 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800905 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800906 return( 0 );
907}
908
Jerry Yu4a173382021-10-11 21:45:31 +0800909static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
910 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800911{
Jerry Yu4a173382021-10-11 21:45:31 +0800912 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
913
Jerry Yue1b9c292021-09-10 10:08:31 +0800914 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800915 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800916 {
Jerry Yu4a173382021-10-11 21:45:31 +0800917 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800918 {
919 return( 1 );
920 }
921 }
922 return( 0 );
923}
924
925/* Parse ServerHello message and configure context
926 *
927 * struct {
928 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
929 * Random random;
930 * opaque legacy_session_id_echo<0..32>;
931 * CipherSuite cipher_suite;
932 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800933 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800934 * } ServerHello;
935 */
Jerry Yuc068b662021-10-11 22:30:19 +0800936static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
937 const unsigned char *buf,
938 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800939{
Jerry Yub85277e2021-10-13 13:36:05 +0800940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800941 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +0800942 size_t extensions_len;
943 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800944 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800945 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +0800946
947 /*
948 * Check there is space for minimal fields
949 *
950 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800951 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800952 * - legacy_session_id_echo ( 1 byte ), minimum size
953 * - cipher_suite ( 2 bytes)
954 * - legacy_compression_method ( 1 byte )
955 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800956 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800957
958 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800959 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
960
Jerry Yu4a173382021-10-11 21:45:31 +0800961 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800962 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800963 * ...
964 * with ProtocolVersion defined as:
965 * uint16 ProtocolVersion;
966 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800967 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
968 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
969 {
970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
971 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
972 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
973 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
974 }
975 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800976
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800977 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800978 * Random random;
979 * ...
980 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800981 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800982 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800983 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
984 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +0800985 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800986 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
987 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800988
Jerry Yu4a173382021-10-11 21:45:31 +0800989 /* ...
990 * opaque legacy_session_id_echo<0..32>;
991 * ...
992 */
993 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800994 {
995 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
996 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
997 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
998 }
999
Jerry Yu4a173382021-10-11 21:45:31 +08001000 /* ...
1001 * CipherSuite cipher_suite;
1002 * ...
1003 * with CipherSuite defined as:
1004 * uint8 CipherSuite[2];
1005 */
1006 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001007 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1008 p += 2;
1009
Jerry Yu4a173382021-10-11 21:45:31 +08001010
1011 /*
1012 * Check whether this ciphersuite is supported and offered.
1013 * Via the force_ciphersuite version we may have instructed the client
1014 * to use a different ciphersuite.
1015 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001016 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001017 if( ciphersuite_info == NULL ||
1018 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001019 {
Jerry Yub85277e2021-10-13 13:36:05 +08001020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +08001021 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001022
Jerry Yu4a173382021-10-11 21:45: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 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001026 }
1027
Jerry Yue1b9c292021-09-10 10:08:31 +08001028
Jerry Yu4a173382021-10-11 21:45:31 +08001029 /* Configure ciphersuites */
1030 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1031
1032 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001033 ssl->session_negotiate->ciphersuite = cipher_suite;
1034
Jerry Yue1b9c292021-09-10 10:08:31 +08001035 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1036 cipher_suite, ciphersuite_info->name ) );
1037
1038#if defined(MBEDTLS_HAVE_TIME)
1039 ssl->session_negotiate->start = time( NULL );
1040#endif /* MBEDTLS_HAVE_TIME */
1041
Jerry Yu4a173382021-10-11 21:45:31 +08001042 /* ...
1043 * uint8 legacy_compression_method = 0;
1044 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001046 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001047 if( p[0] != 0 )
1048 {
Jerry Yub85277e2021-10-13 13:36:05 +08001049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001050 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1051 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1052 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1053 }
1054 p++;
1055
Jerry Yub85277e2021-10-13 13:36:05 +08001056 /* ...
1057 * Extension extensions<6..2^16-1>;
1058 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001059 * struct {
1060 * ExtensionType extension_type; (2 bytes)
1061 * opaque extension_data<0..2^16-1>;
1062 * } Extension;
1063 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001064 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001065 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001066 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001067
Jerry Yu4a173382021-10-11 21:45:31 +08001068 /* Check extensions do not go beyond the buffer of data. */
1069 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1070 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001071
Jerry Yu4a173382021-10-11 21:45:31 +08001072 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001073
Jerry Yu4a173382021-10-11 21:45:31 +08001074 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001075 {
1076 unsigned int extension_type;
1077 size_t extension_data_len;
1078
Jerry Yu4a173382021-10-11 21:45:31 +08001079 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001080 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1081 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1082 p += 4;
1083
Jerry Yu4a173382021-10-11 21:45:31 +08001084 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001085
1086 switch( extension_type )
1087 {
1088 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1089 MBEDTLS_SSL_DEBUG_MSG( 3,
1090 ( "found supported_versions extension" ) );
1091
Jerry Yuc068b662021-10-11 22:30:19 +08001092 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001093 p,
1094 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001095 if( ret != 0 )
1096 return( ret );
1097 break;
1098
1099 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001102
1103 MBEDTLS_SSL_PEND_FATAL_ALERT(
1104 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1105 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1106 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001107
1108#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1109 case MBEDTLS_TLS_EXT_KEY_SHARE:
1110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001111 if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +08001112 p, p + extension_data_len ) ) != 0 )
1113 {
1114 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001115 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001116 ret );
1117 return( ret );
1118 }
1119 break;
1120#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1121
1122 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001123 MBEDTLS_SSL_DEBUG_MSG(
1124 3,
1125 ( "unknown extension found: %u ( ignoring )",
1126 extension_type ) );
1127
1128 MBEDTLS_SSL_PEND_FATAL_ALERT(
1129 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1130 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1131 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001132 }
1133
1134 p += extension_data_len;
1135 }
1136
1137 return( 0 );
1138}
1139
Jerry Yuc068b662021-10-11 22:30:19 +08001140static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001141{
Jerry Yub85277e2021-10-13 13:36:05 +08001142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001143 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001144 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001145 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001146
Jerry Yub85277e2021-10-13 13:36:05 +08001147 /* Determine the key exchange mode:
1148 * 1) If both the pre_shared_key and key_share extensions were received
1149 * then the key exchange mode is PSK with EPHEMERAL.
1150 * 2) If only the pre_shared_key extension was received then the key
1151 * exchange mode is PSK-only.
1152 * 3) If only the key_share extension was received then the key
1153 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001154 */
Jerry Yub85277e2021-10-13 13:36:05 +08001155 switch( handshake->extensions_present &
1156 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001157 {
Jerry Yu745bb612021-10-13 22:01:04 +08001158 /* Only the pre_shared_key extension was received */
1159 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001160 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001161 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001162
Jerry Yu745bb612021-10-13 22:01:04 +08001163 /* Only the key_share extension was received */
1164 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001165 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001166 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001167
Jerry Yu745bb612021-10-13 22:01:04 +08001168 /* Both the pre_shared_key and key_share extensions were received */
1169 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001170 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001171 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001172
Jerry Yu745bb612021-10-13 22:01:04 +08001173 /* Neither pre_shared_key nor key_share extension was received */
1174 default:
1175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1176 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1177 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001178 }
1179
1180 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1181 *
1182 * TODO: We don't have to do this in case we offered 0-RTT and the
1183 * server accepted it. In this case, we could skip generating
1184 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001185 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001186 if( ret != 0 )
1187 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001189 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001190 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001191 }
1192
1193 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001194 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001195 if( ret != 0 )
1196 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001198 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001199 }
1200
1201 /* Next evolution in key schedule: Establish handshake secret and
1202 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001203 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001204 if( ret != 0 )
1205 {
Jerry Yuc068b662021-10-11 22:30:19 +08001206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1207 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001208 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001209 }
1210
Jerry Yub85277e2021-10-13 13:36:05 +08001211 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001212 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001213 {
1214 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1215 goto cleanup;
1216 }
Jerry Yu0b177842021-09-05 19:41:30 +08001217
1218 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1219 ssl->conf->endpoint,
1220 ssl->session_negotiate->ciphersuite,
1221 &traffic_keys,
1222 ssl );
1223 if( ret != 0 )
1224 {
1225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001226 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001227 }
1228
Jerry Yub85277e2021-10-13 13:36:05 +08001229 handshake->transform_handshake = transform_handshake;
1230 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001231
1232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1233 ssl->session_in = ssl->session_negotiate;
1234
1235 /*
1236 * State machine update
1237 */
1238 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1239
Jerry Yu4a173382021-10-11 21:45:31 +08001240cleanup:
1241
Jerry Yu0b177842021-09-05 19:41:30 +08001242 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001243 if( ret != 0 )
1244 {
Jerry Yub85277e2021-10-13 13:36:05 +08001245 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001246
Jerry Yu4a173382021-10-11 21:45:31 +08001247 MBEDTLS_SSL_PEND_FATAL_ALERT(
1248 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1249 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1250 }
1251 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001252}
1253
XiaokangQian647719a2021-12-07 09:16:29 +00001254static int ssl_hrr_parse( mbedtls_ssl_context *ssl,
XiaokangQian51eff222021-12-10 10:33:56 +00001255 const unsigned char *buf,
1256 const unsigned char *end )
XiaokangQian647719a2021-12-07 09:16:29 +00001257{
XiaokangQian51eff222021-12-10 10:33:56 +00001258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1259 int cipher_suite;
1260
XiaokangQian647719a2021-12-07 09:16:29 +00001261 /* pointer to the end of the buffer for length checks */
XiaokangQian51eff222021-12-10 10:33:56 +00001262 const unsigned char *p = buf;
1263 const unsigned char *extensions_end;
1264 size_t extensions_len; /* stores length of all extensions */
XiaokangQian647719a2021-12-07 09:16:29 +00001265
XiaokangQian51eff222021-12-10 10:33:56 +00001266 const mbedtls_ssl_ciphersuite_t* ciphersuite_info; /* pointer to ciphersuite */
XiaokangQian647719a2021-12-07 09:16:29 +00001267
1268#if defined(MBEDTLS_SSL_COOKIE_C)
1269 size_t cookie_len;
1270 unsigned char *cookie;
1271#endif /* MBEDTLS_SSL_COOKIE_C */
1272
XiaokangQian51eff222021-12-10 10:33:56 +00001273 /* Check for minimal length
1274 * struct {
1275 * ProtocolVersion legacy_version = 0x0303;
XiaokangQian647719a2021-12-07 09:16:29 +00001276 * Random random;
1277 * opaque legacy_session_id_echo<0..32>;
1278 * CipherSuite cipher_suite;
1279 * uint8 legacy_compression_method = 0;
1280 * Extension extensions<6..2 ^ 16 - 1>;
1281 * } ServerHello;
1282 *
XiaokangQian647719a2021-12-07 09:16:29 +00001283 * 38 = 32 ( random bytes ) + 2 ( ciphersuite ) + 2 ( version ) +
XiaokangQian51eff222021-12-10 10:33:56 +00001284 * 1 ( legacy_compression_method ) +
1285 * 1 ( minimum for legacy_session_id_echo )
XiaokangQian647719a2021-12-07 09:16:29 +00001286 */
XiaokangQian51eff222021-12-10 10:33:56 +00001287 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
XiaokangQian647719a2021-12-07 09:16:29 +00001288
XiaokangQian51eff222021-12-10 10:33:56 +00001289 MBEDTLS_SSL_DEBUG_BUF( 4, "hello retry request", p, end - p );
XiaokangQian647719a2021-12-07 09:16:29 +00001290
XiaokangQian51eff222021-12-10 10:33:56 +00001291 MBEDTLS_SSL_DEBUG_BUF( 3, "hello retry request, version", p, 2 );
XiaokangQian647719a2021-12-07 09:16:29 +00001292
1293 /* The version field must contain 0x303 */
XiaokangQian51eff222021-12-10 10:33:56 +00001294 if( MBEDTLS_GET_UINT16_BE( p, 0 ) != 0x303 )
XiaokangQian647719a2021-12-07 09:16:29 +00001295 {
1296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1297 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
XiaokangQian51eff222021-12-10 10:33:56 +00001298 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian647719a2021-12-07 09:16:29 +00001299 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1300 }
1301
1302 /* skip version */
XiaokangQian51eff222021-12-10 10:33:56 +00001303 p += 2;
XiaokangQian647719a2021-12-07 09:16:29 +00001304
1305 /* Internally we use the correct 1.3 version */
XiaokangQian51eff222021-12-10 10:33:56 +00001306 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1307 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
XiaokangQian647719a2021-12-07 09:16:29 +00001308
1309 /* store server-provided random values */
XiaokangQian51eff222021-12-10 10:33:56 +00001310 memcpy( ssl->handshake->randbytes + MBEDTLS_SERVER_HELLO_RANDOM_LEN,
1311 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1312 MBEDTLS_SSL_DEBUG_BUF( 3, "hello retry request, random bytes",
1313 p + 2, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
XiaokangQian647719a2021-12-07 09:16:29 +00001314
1315 /* skip random bytes */
XiaokangQian51eff222021-12-10 10:33:56 +00001316 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
XiaokangQian647719a2021-12-07 09:16:29 +00001317
XiaokangQian51eff222021-12-10 10:33:56 +00001318 /* ...
1319 * opaque legacy_session_id_echo<0..32>;
1320 * ...
1321 */
1322 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
XiaokangQian647719a2021-12-07 09:16:29 +00001323 {
1324 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
XiaokangQian51eff222021-12-10 10:33:56 +00001325 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian647719a2021-12-07 09:16:29 +00001326 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1327 }
1328
1329 /* read server-selected ciphersuite, which follows random bytes */
XiaokangQian51eff222021-12-10 10:33:56 +00001330 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001331
1332 /* skip ciphersuite */
XiaokangQian51eff222021-12-10 10:33:56 +00001333 p += 2;
XiaokangQian647719a2021-12-07 09:16:29 +00001334
XiaokangQian51eff222021-12-10 10:33:56 +00001335 /*
1336 * Check whether we have offered this ciphersuite
1337 * Via the force_ciphersuite version we may have instructed the client
1338 * to use a difference ciphersuite.
1339 */
1340 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1341 if( ciphersuite_info == NULL ||
1342 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
1343 {
1344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
1345 (unsigned int)cipher_suite ) );
1346
1347 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1348 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1349 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1350 }
XiaokangQian647719a2021-12-07 09:16:29 +00001351
1352 /* Configure ciphersuites */
XiaokangQian51eff222021-12-10 10:33:56 +00001353 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1354 ssl->handshake->ciphersuite_info = ciphersuite_info;
1355 ssl->session_negotiate->ciphersuite = cipher_suite;
XiaokangQian647719a2021-12-07 09:16:29 +00001356
1357 MBEDTLS_SSL_DEBUG_MSG( 3,
1358 ( "hello retry request, chosen ciphersuite: ( %04x ) - %s",
XiaokangQian51eff222021-12-10 10:33:56 +00001359 (unsigned int)cipher_suite, ciphersuite_info->name ) );
XiaokangQian647719a2021-12-07 09:16:29 +00001360
1361#if defined(MBEDTLS_HAVE_TIME)
1362 ssl->session_negotiate->start = time( NULL );
1363#endif /* MBEDTLS_HAVE_TIME */
1364
XiaokangQian647719a2021-12-07 09:16:29 +00001365 /* Ensure that compression method is set to zero */
XiaokangQian51eff222021-12-10 10:33:56 +00001366 if( p[0] != 0 )
XiaokangQian647719a2021-12-07 09:16:29 +00001367 {
1368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1369 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1370 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1371 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1372 }
1373
1374 /* skip compression */
XiaokangQian51eff222021-12-10 10:33:56 +00001375 p++;
XiaokangQian647719a2021-12-07 09:16:29 +00001376
1377 /* Are we reading beyond the message buffer? */
XiaokangQian51eff222021-12-10 10:33:56 +00001378 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian647719a2021-12-07 09:16:29 +00001379
XiaokangQian51eff222021-12-10 10:33:56 +00001380 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1381 p += 2; /* skip extension length */
XiaokangQian647719a2021-12-07 09:16:29 +00001382
1383 /* Are we reading beyond the message buffer? */
XiaokangQian51eff222021-12-10 10:33:56 +00001384 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1385 extensions_end = p + extensions_len;
XiaokangQian647719a2021-12-07 09:16:29 +00001386
1387 MBEDTLS_SSL_DEBUG_MSG( 3,
1388 ( "hello retry request, total extension length: %"
XiaokangQian51eff222021-12-10 10:33:56 +00001389 MBEDTLS_PRINTF_SIZET , extensions_len ) );
1390 MBEDTLS_SSL_DEBUG_BUF( 3, "extensions", p, extensions_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001391
XiaokangQian51eff222021-12-10 10:33:56 +00001392 while ( p < extensions_end )
XiaokangQian647719a2021-12-07 09:16:29 +00001393 {
XiaokangQian51eff222021-12-10 10:33:56 +00001394 unsigned int extension_type;
1395 const unsigned char *extensions_data_end;
1396 unsigned int extension_data_len; /* size of an individual extension */
XiaokangQian647719a2021-12-07 09:16:29 +00001397
XiaokangQian51eff222021-12-10 10:33:56 +00001398 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1399 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1400 extension_data_len = MBEDTLS_GET_UINT16_BE( p + 2, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001401
XiaokangQian51eff222021-12-10 10:33:56 +00001402 p += 4;
1403 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1404 extensions_data_end = p + extension_data_len;
1405
1406 switch( extension_type )
XiaokangQian647719a2021-12-07 09:16:29 +00001407 {
1408#if defined(MBEDTLS_SSL_COOKIE_C)
1409 case MBEDTLS_TLS_EXT_COOKIE:
1410
1411 /* Retrieve length field of cookie */
XiaokangQian51eff222021-12-10 10:33:56 +00001412 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_data_end, 2 );
1413 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1414 cookie = (unsigned char *) ( p + 2 );
XiaokangQian647719a2021-12-07 09:16:29 +00001415
XiaokangQian51eff222021-12-10 10:33:56 +00001416 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_data_end, cookie_len + 2 );
XiaokangQian647719a2021-12-07 09:16:29 +00001417 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", cookie, cookie_len );
1418
1419 mbedtls_free( ssl->handshake->verify_cookie );
XiaokangQian647719a2021-12-07 09:16:29 +00001420 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
1421 if( ssl->handshake->verify_cookie == NULL )
1422 {
1423 MBEDTLS_SSL_DEBUG_MSG( 1,
1424 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
1425 cookie_len ) );
1426 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1427 }
1428
1429 memcpy( ssl->handshake->verify_cookie, cookie, cookie_len );
1430 ssl->handshake->verify_cookie_len = (unsigned char) cookie_len;
1431 break;
1432#endif /* MBEDTLS_SSL_COOKIE_C */
1433
1434 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_versions extension" ) );
1436
1437 ret = ssl_tls13_parse_supported_versions_ext( ssl,
XiaokangQian51eff222021-12-10 10:33:56 +00001438 p,
1439 p + extension_data_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001440 if( ret != 0 )
1441 return( ret );
1442 break;
1443
1444#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1445 case MBEDTLS_TLS_EXT_KEY_SHARE:
1446 {
1447 /* Variables for parsing the key_share */
1448 const mbedtls_ecp_group_id* grp_id;
1449 const mbedtls_ecp_curve_info *curve_info = NULL;
1450 int tls_id;
1451 int found = 0;
1452
XiaokangQian51eff222021-12-10 10:33:56 +00001453 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, extension_data_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001454
1455 /* Read selected_group */
XiaokangQian51eff222021-12-10 10:33:56 +00001456 tls_id = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", tls_id ) );
1458
1459 /* Upon receipt of this extension in a HelloRetryRequest, the client
1460 * MUST first verify that the selected_group field corresponds to a
1461 * group which was provided in the "supported_groups" extension in the
1462 * original ClientHello.
1463 * The supported_group was based on the info in ssl->conf->curve_list.
1464 *
1465 * If the server provided a key share that was not sent in the ClientHello
1466 * then the client MUST abort the handshake with an "illegal_parameter" alert. */
1467 for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
1468 {
1469 curve_info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
1470 if( curve_info == NULL || curve_info->tls_id != tls_id )
1471 continue;
1472
1473 /* We found a match */
1474 found = 1;
1475 break;
1476 }
1477
1478 /* Client MUST verify that the selected_group field does not
1479 * correspond to a group which was provided in the "key_share"
1480 * extension in the original ClientHello. If the server sent an
1481 * HRR message with a key share already provided in the
1482 * ClientHello then the client MUST abort the handshake with
1483 * an "illegal_parameter" alert. */
1484 if( found == 0 || tls_id == ssl->handshake->offered_group_id )
1485 {
1486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
1487 MBEDTLS_SSL_PEND_FATAL_ALERT(
1488 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1489 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1490 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1491 }
1492
1493 /* Remember server's preference for next ClientHello */
1494 ssl->handshake->offered_group_id= tls_id;
1495 break;
1496 }
1497
1498#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
1499 default:
1500 MBEDTLS_SSL_DEBUG_MSG( 3,
1501 ( "unknown extension found: %u ( ignoring )",
XiaokangQian51eff222021-12-10 10:33:56 +00001502 extension_type ) );
XiaokangQian647719a2021-12-07 09:16:29 +00001503 }
1504
1505 /* Jump to next extension */
XiaokangQian51eff222021-12-10 10:33:56 +00001506 //extensions_len -= 4 + extension_data_len;
1507 //ext += 4 + extension_data_len;
1508 p += extension_data_len;
XiaokangQian647719a2021-12-07 09:16:29 +00001509 }
1510
1511 return( 0 );
1512}
1513
1514static int ssl_hrr_postprocess( mbedtls_ssl_context *ssl )
1515{
1516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1517
1518 if( ssl->handshake->hello_retry_requests_received > 0 )
1519 {
1520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1521 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1522 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1523 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1524 }
1525
1526 ssl->handshake->hello_retry_requests_received++;
1527
1528#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1529 /* If not offering early data, the client sends a dummy CCS record
1530 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001531 * its second ClientHello or before its encrypted handshake flight.
1532 */
XiaokangQian647719a2021-12-07 09:16:29 +00001533 mbedtls_ssl_handshake_set_state( ssl,
1534 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1535#else
1536 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1537#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1538
1539 mbedtls_ssl_tls13_session_reset_msg_layer( ssl, 0 );
1540
1541 /* Reset everything that's going to be re-generated in the new ClientHello.
1542 *
1543 * Currently, we're always resetting the key share, even if the server
1544 * was fine with it. Once we have separated key share generation from
1545 * key share writing, we can confine this to the case where the server
XiaokangQian51eff222021-12-10 10:33:56 +00001546 * requested a different share.
1547 */
XiaokangQian647719a2021-12-07 09:16:29 +00001548 ret = ssl_reset_key_share( ssl );
1549 if( ret != 0 )
1550 return( ret );
1551
1552 return( 0 );
1553}
1554
Jerry Yue1b9c292021-09-10 10:08:31 +08001555/*
Jerry Yu4a173382021-10-11 21:45:31 +08001556 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001557 * Handler for MBEDTLS_SSL_SERVER_HELLO
1558 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001559static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001560{
Jerry Yu4a173382021-10-11 21:45:31 +08001561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001562 unsigned char *buf = NULL;
1563 size_t buf_len = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001564
1565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1566
1567 /* Coordination step
1568 * - Fetch record
1569 * - Make sure it's either a ServerHello or a HRR.
1570 * - Switch processing routine in case of HRR
1571 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001572 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1573 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1574
Jerry Yub85277e2021-10-13 13:36:05 +08001575 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001576 /* Parsing step
1577 * We know what message to expect by now and call
1578 * the respective parsing function.
1579 */
1580 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1581 {
Jerry Yuc068b662021-10-11 22:30:19 +08001582 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1583 buf + buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001584
Xiaofei Bai746f9482021-11-12 08:53:56 +00001585 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1586 MBEDTLS_SSL_HS_SERVER_HELLO,
1587 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001588
Jerry Yuc068b662021-10-11 22:30:19 +08001589 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001590 }
1591 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1592 {
XiaokangQian51eff222021-12-10 10:33:56 +00001593 MBEDTLS_SSL_PROC_CHK( ssl_hrr_parse( ssl, buf, buf + buf_len ) );
XiaokangQian647719a2021-12-07 09:16:29 +00001594 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1595
XiaokangQian51eff222021-12-10 10:33:56 +00001596 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1597 MBEDTLS_SSL_HS_SERVER_HELLO,
1598 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001599
1600 MBEDTLS_SSL_PROC_CHK( ssl_hrr_postprocess( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001601 }
1602
1603cleanup:
1604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1605 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001606}
1607
1608/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001609 *
1610 * EncryptedExtensions message
1611 *
1612 * The EncryptedExtensions message contains any extensions which
1613 * should be protected, i.e., any which are not needed to establish
1614 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001615 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001616
1617/*
1618 * Overview
1619 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001620
1621/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001622static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001623
XiaokangQian97799ac2021-10-11 10:05:54 +00001624static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1625 const unsigned char *buf,
1626 const unsigned char *end );
1627static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001628
1629/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001630 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001631 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001632static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001633{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001634 int ret;
1635 unsigned char *buf;
1636 size_t buf_len;
1637
1638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1639
Xiaofei Bai746f9482021-11-12 08:53:56 +00001640 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001641 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001642 &buf, &buf_len ) );
1643
1644 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001645 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001646 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001647
Xiaofei Bai746f9482021-11-12 08:53:56 +00001648 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001649 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001650
XiaokangQian97799ac2021-10-11 10:05:54 +00001651 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001652
1653cleanup:
1654
1655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1656 return( ret );
1657
1658}
1659
XiaokangQian08da26c2021-10-09 10:12:11 +00001660/* Parse EncryptedExtensions message
1661 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001662 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001663 * } EncryptedExtensions;
1664 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001665static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1666 const unsigned char *buf,
1667 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001668{
1669 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001670 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001671 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001672 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001673
XiaokangQian08da26c2021-10-09 10:12:11 +00001674 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001675 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001676 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001677
XiaokangQian97799ac2021-10-11 10:05:54 +00001678 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001679 extensions_end = p + extensions_len;
1680 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001681
XiaokangQian8db25ff2021-10-13 05:56:18 +00001682 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001683 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001684 unsigned int extension_type;
1685 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001686
XiaokangQian08da26c2021-10-09 10:12:11 +00001687 /*
1688 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001689 * ExtensionType extension_type; (2 bytes)
1690 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001691 * } Extension;
1692 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001693 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001694 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1695 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1696 p += 4;
1697
XiaokangQian8db25ff2021-10-13 05:56:18 +00001698 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001699
XiaokangQian97799ac2021-10-11 10:05:54 +00001700 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001701 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001702 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001703 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001704 switch( extension_type )
1705 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001706
XiaokangQian08da26c2021-10-09 10:12:11 +00001707 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1709 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001710
XiaokangQian08da26c2021-10-09 10:12:11 +00001711 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001712 MBEDTLS_SSL_DEBUG_MSG(
1713 3, ( "unsupported extension found: %u ", extension_type) );
1714 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001715 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001716 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1717 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001718 }
1719
XiaokangQian08da26c2021-10-09 10:12:11 +00001720 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001721 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001722
XiaokangQian97799ac2021-10-11 10:05:54 +00001723 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001724 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001725 {
1726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001727 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001728 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001729 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001730 }
1731
1732 return( ret );
1733}
1734
XiaokangQian97799ac2021-10-11 10:05:54 +00001735static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001736{
Jerry Yua93ac112021-10-27 16:31:48 +08001737#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001738 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001739 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1740 else
Jerry Yud2674312021-10-29 10:08:19 +08001741 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001742#else
1743 ((void) ssl);
1744 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1745#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001746 return( 0 );
1747}
1748
Jerry Yua93ac112021-10-27 16:31:48 +08001749#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001750/*
Jerry Yud2674312021-10-29 10:08:19 +08001751 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1752 */
1753static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1754{
1755 int ret = mbedtls_ssl_read_record( ssl, 0 );
1756
1757 if( ret != 0 )
1758 {
1759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1760 return( ret );
1761 }
1762
1763 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1764 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1765 {
1766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1767 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1768 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1769 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1770 }
1771
1772 ssl->keep_current_message = 1;
1773 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1774
1775 return( 0 );
1776}
1777
1778/*
Jerry Yu687101b2021-09-14 16:03:56 +08001779 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1780 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001781static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001782{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001783 int ret;
1784
1785 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001786 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001787 return( ret );
1788
Jerry Yu687101b2021-09-14 16:03:56 +08001789 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1790 return( 0 );
1791}
1792
1793/*
1794 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1795 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001796static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001797{
Jerry Yu30b071c2021-09-12 20:16:03 +08001798 int ret;
1799
1800 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1801 if( ret != 0 )
1802 return( ret );
1803
Jerry Yu687101b2021-09-14 16:03:56 +08001804 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1805 return( 0 );
1806}
Jerry Yua93ac112021-10-27 16:31:48 +08001807#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001808
Jerry Yu687101b2021-09-14 16:03:56 +08001809/*
1810 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1811 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001812static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001813{
XiaokangQianac0385c2021-11-03 06:40:11 +00001814 int ret;
1815
XiaokangQianc5c39d52021-11-09 11:55:10 +00001816 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001817 if( ret != 0 )
1818 return( ret );
1819
Ronald Cron49ad6192021-11-24 16:25:31 +01001820#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1821 mbedtls_ssl_handshake_set_state(
1822 ssl,
1823 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1824#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001825 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001826#endif
1827
XiaokangQianac0385c2021-11-03 06:40:11 +00001828 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001829}
1830
1831/*
Ronald Crond4c64022021-12-06 09:06:46 +01001832 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1833 */
1834#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1835static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1836{
1837 int ret;
1838
1839 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1840 if( ret != 0 )
1841 return( ret );
1842
1843 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1844
1845 return( 0 );
1846}
1847#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1848
1849/*
Jerry Yu687101b2021-09-14 16:03:56 +08001850 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1851 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001852static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001853{
XiaokangQian0fa66432021-11-15 03:33:57 +00001854 int ret;
1855
Ronald Cron49ad6192021-11-24 16:25:31 +01001856 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1857
XiaokangQian0fa66432021-11-15 03:33:57 +00001858 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1859 if( ret != 0 )
1860 return( ret );
1861
1862 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1863 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001864}
1865
1866/*
1867 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1868 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001869static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001870{
Jerry Yu378254d2021-10-30 21:44:47 +08001871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001872 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001873 return( 0 );
1874}
1875
1876/*
1877 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1878 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001879static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001880{
Jerry Yu378254d2021-10-30 21:44:47 +08001881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1882 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1883
1884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1885 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1886
1887 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1888
1889 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1890 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001891}
1892
Jerry Yu92c6b402021-08-27 16:59:09 +08001893int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001894{
Jerry Yu92c6b402021-08-27 16:59:09 +08001895 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001896
Jerry Yue3b34122021-09-28 17:53:35 +08001897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1898 mbedtls_ssl_states_str( ssl->state ),
1899 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001900
1901 switch( ssl->state )
1902 {
1903 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001904 * ssl->state is initialized as HELLO_REQUEST. It is the same
1905 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001906 */
1907 case MBEDTLS_SSL_HELLO_REQUEST:
1908 case MBEDTLS_SSL_CLIENT_HELLO:
1909 ret = ssl_tls13_write_client_hello( ssl );
1910 break;
1911
1912 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001913 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001914 break;
1915
1916 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001917 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001918 break;
1919
Jerry Yua93ac112021-10-27 16:31:48 +08001920#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001921 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1922 ret = ssl_tls13_process_certificate_request( ssl );
1923 break;
1924
Jerry Yu687101b2021-09-14 16:03:56 +08001925 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001926 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001927 break;
1928
1929 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001930 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001931 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001932#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001933
1934 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001935 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001936 break;
1937
Jerry Yu687101b2021-09-14 16:03:56 +08001938 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00001939 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001940 break;
1941
1942 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001943 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001944 break;
1945
1946 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001947 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001948 break;
1949
Ronald Cron49ad6192021-11-24 16:25:31 +01001950 /*
1951 * Injection of dummy-CCS's for middlebox compatibility
1952 */
1953#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1954 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Ronald Crond4c64022021-12-06 09:06:46 +01001955 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01001956 break;
1957#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1958
Jerry Yu92c6b402021-08-27 16:59:09 +08001959 default:
1960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1962 }
1963
1964 return( ret );
1965}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001966
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001967#endif /* MBEDTLS_SSL_CLI_C */
1968
Ronald Cron6f135e12021-12-08 16:57:54 +01001969#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */