blob: 9f80d8c4d0017149d6c9d1aff7079a4384fc5dd1 [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
792 * to indicate which message is expected and to be parsed next. */
793#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
794#define SSL_SERVER_HELLO_COORDINATE_HRR 1
795static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
796 const unsigned char *buf,
797 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800798{
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800799 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +0800800 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
801 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
802 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
803 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
804
805 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
806 *
Jerry Yu4a173382021-10-11 21:45:31 +0800807 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +0800808 * special value of the SHA-256 of "HelloRetryRequest".
809 *
810 * struct {
811 * ProtocolVersion legacy_version = 0x0303;
812 * Random random;
813 * opaque legacy_session_id_echo<0..32>;
814 * CipherSuite cipher_suite;
815 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800816 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800817 * } ServerHello;
818 *
819 */
Jerry Yub85277e2021-10-13 13:36:05 +0800820 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +0800821
822 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800823 {
Jerry Yub85277e2021-10-13 13:36:05 +0800824 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +0800825 }
826
Jerry Yub85277e2021-10-13 13:36:05 +0800827 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +0800828}
829
Jerry Yu745bb612021-10-13 22:01:04 +0800830/* Fetch and preprocess
831 * Returns a negative value on failure, and otherwise
832 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
833 * - SSL_SERVER_HELLO_COORDINATE_HRR
834 */
Jerry Yub85277e2021-10-13 13:36:05 +0800835static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
836 unsigned char **buf,
837 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800838{
Jerry Yu4a173382021-10-11 21:45:31 +0800839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800840
841 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
842
Jerry Yue1b9c292021-09-10 10:08:31 +0800843 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
844 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
845 {
846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
847
848 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
849 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
850 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
851 }
852
853 *buf = ssl->in_msg + 4;
854 *buf_len = ssl->in_hslen - 4;
855
Jerry Yub85277e2021-10-13 13:36:05 +0800856 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
857 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +0800858 {
Jerry Yu745bb612021-10-13 22:01:04 +0800859 case SSL_SERVER_HELLO_COORDINATE_HELLO:
860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
861 break;
862 case SSL_SERVER_HELLO_COORDINATE_HRR:
863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
864 break;
Jerry Yue1b9c292021-09-10 10:08:31 +0800865 }
866
867cleanup:
868
869 return( ret );
870}
871
Jerry Yu4a173382021-10-11 21:45:31 +0800872static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
873 const unsigned char **buf,
874 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800875{
876 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800877 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800878
Jerry Yude4fb2c2021-09-19 18:05:08 +0800879 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +0800880 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +0800881
Jerry Yu4a173382021-10-11 21:45:31 +0800882 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800883
884 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +0800885 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
886 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800887 {
Jerry Yue1b9c292021-09-10 10:08:31 +0800888 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
889 ssl->session_negotiate->id,
890 ssl->session_negotiate->id_len );
891 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +0800892 legacy_session_id_echo_len );
893
894 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
895 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
896
Jerry Yue1b9c292021-09-10 10:08:31 +0800897 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
898 }
899
Jerry Yu4a173382021-10-11 21:45:31 +0800900 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +0800901 *buf = p;
902
903 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +0800904 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800905 return( 0 );
906}
907
Jerry Yu4a173382021-10-11 21:45:31 +0800908static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
909 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800910{
Jerry Yu4a173382021-10-11 21:45:31 +0800911 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
912
Jerry Yue1b9c292021-09-10 10:08:31 +0800913 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +0800914 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800915 {
Jerry Yu4a173382021-10-11 21:45:31 +0800916 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +0800917 {
918 return( 1 );
919 }
920 }
921 return( 0 );
922}
923
924/* Parse ServerHello message and configure context
925 *
926 * struct {
927 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
928 * Random random;
929 * opaque legacy_session_id_echo<0..32>;
930 * CipherSuite cipher_suite;
931 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +0800932 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +0800933 * } ServerHello;
934 */
Jerry Yuc068b662021-10-11 22:30:19 +0800935static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
936 const unsigned char *buf,
937 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +0800938{
Jerry Yub85277e2021-10-13 13:36:05 +0800939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800940 const unsigned char *p = buf;
Jerry Yub85277e2021-10-13 13:36:05 +0800941 size_t extensions_len;
942 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +0800943 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +0800944 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +0800945
946 /*
947 * Check there is space for minimal fields
948 *
949 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800950 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +0800951 * - legacy_session_id_echo ( 1 byte ), minimum size
952 * - cipher_suite ( 2 bytes)
953 * - legacy_compression_method ( 1 byte )
954 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800955 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800956
957 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800958 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
959
Jerry Yu4a173382021-10-11 21:45:31 +0800960 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +0800961 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +0800962 * ...
963 * with ProtocolVersion defined as:
964 * uint16 ProtocolVersion;
965 */
Jerry Yue1b9c292021-09-10 10:08:31 +0800966 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
967 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
968 {
969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
970 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
971 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
972 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
973 }
974 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +0800975
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800976 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +0800977 * Random random;
978 * ...
979 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800980 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +0800981 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800982 memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
983 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yue1b9c292021-09-10 10:08:31 +0800984 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800985 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
986 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +0800987
Jerry Yu4a173382021-10-11 21:45:31 +0800988 /* ...
989 * opaque legacy_session_id_echo<0..32>;
990 * ...
991 */
992 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800993 {
994 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
995 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
996 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
997 }
998
Jerry Yu4a173382021-10-11 21:45:31 +0800999 /* ...
1000 * CipherSuite cipher_suite;
1001 * ...
1002 * with CipherSuite defined as:
1003 * uint8 CipherSuite[2];
1004 */
1005 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001006 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1007 p += 2;
1008
Jerry Yu4a173382021-10-11 21:45:31 +08001009
1010 /*
1011 * Check whether this ciphersuite is supported and offered.
1012 * Via the force_ciphersuite version we may have instructed the client
1013 * to use a different ciphersuite.
1014 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001015 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001016 if( ciphersuite_info == NULL ||
1017 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001018 {
Jerry Yub85277e2021-10-13 13:36:05 +08001019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
Jerry Yue1b9c292021-09-10 10:08:31 +08001020 cipher_suite ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001021
Jerry Yu4a173382021-10-11 21:45:31 +08001022 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1023 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1024 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +08001025 }
1026
Jerry Yue1b9c292021-09-10 10:08:31 +08001027
Jerry Yu4a173382021-10-11 21:45:31 +08001028 /* Configure ciphersuites */
1029 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1030
1031 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001032 ssl->session_negotiate->ciphersuite = cipher_suite;
1033
Jerry Yue1b9c292021-09-10 10:08:31 +08001034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1035 cipher_suite, ciphersuite_info->name ) );
1036
1037#if defined(MBEDTLS_HAVE_TIME)
1038 ssl->session_negotiate->start = time( NULL );
1039#endif /* MBEDTLS_HAVE_TIME */
1040
Jerry Yu4a173382021-10-11 21:45:31 +08001041 /* ...
1042 * uint8 legacy_compression_method = 0;
1043 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001044 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001045 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 if( p[0] != 0 )
1047 {
Jerry Yub85277e2021-10-13 13:36:05 +08001048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001049 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1050 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1051 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1052 }
1053 p++;
1054
Jerry Yub85277e2021-10-13 13:36:05 +08001055 /* ...
1056 * Extension extensions<6..2^16-1>;
1057 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001058 * struct {
1059 * ExtensionType extension_type; (2 bytes)
1060 * opaque extension_data<0..2^16-1>;
1061 * } Extension;
1062 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001063 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001064 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001065 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001066
Jerry Yu4a173382021-10-11 21:45:31 +08001067 /* Check extensions do not go beyond the buffer of data. */
1068 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1069 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001070
Jerry Yu4a173382021-10-11 21:45:31 +08001071 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001072
Jerry Yu4a173382021-10-11 21:45:31 +08001073 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001074 {
1075 unsigned int extension_type;
1076 size_t extension_data_len;
1077
Jerry Yu4a173382021-10-11 21:45:31 +08001078 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001079 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1080 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1081 p += 4;
1082
Jerry Yu4a173382021-10-11 21:45:31 +08001083 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001084
1085 switch( extension_type )
1086 {
1087 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1088 MBEDTLS_SSL_DEBUG_MSG( 3,
1089 ( "found supported_versions extension" ) );
1090
Jerry Yuc068b662021-10-11 22:30:19 +08001091 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001092 p,
1093 p + extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001094 if( ret != 0 )
1095 return( ret );
1096 break;
1097
1098 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001101
1102 MBEDTLS_SSL_PEND_FATAL_ALERT(
1103 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1104 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1105 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001106
1107#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1108 case MBEDTLS_TLS_EXT_KEY_SHARE:
1109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001110 if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +08001111 p, p + extension_data_len ) ) != 0 )
1112 {
1113 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001114 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001115 ret );
1116 return( ret );
1117 }
1118 break;
1119#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1120
1121 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001122 MBEDTLS_SSL_DEBUG_MSG(
1123 3,
1124 ( "unknown extension found: %u ( ignoring )",
1125 extension_type ) );
1126
1127 MBEDTLS_SSL_PEND_FATAL_ALERT(
1128 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1129 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1130 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Jerry Yue1b9c292021-09-10 10:08:31 +08001131 }
1132
1133 p += extension_data_len;
1134 }
1135
1136 return( 0 );
1137}
1138
Jerry Yuc068b662021-10-11 22:30:19 +08001139static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001140{
Jerry Yub85277e2021-10-13 13:36:05 +08001141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001142 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001143 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001144 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001145
Jerry Yub85277e2021-10-13 13:36:05 +08001146 /* Determine the key exchange mode:
1147 * 1) If both the pre_shared_key and key_share extensions were received
1148 * then the key exchange mode is PSK with EPHEMERAL.
1149 * 2) If only the pre_shared_key extension was received then the key
1150 * exchange mode is PSK-only.
1151 * 3) If only the key_share extension was received then the key
1152 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001153 */
Jerry Yub85277e2021-10-13 13:36:05 +08001154 switch( handshake->extensions_present &
1155 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001156 {
Jerry Yu745bb612021-10-13 22:01:04 +08001157 /* Only the pre_shared_key extension was received */
1158 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001159 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001160 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001161
Jerry Yu745bb612021-10-13 22:01:04 +08001162 /* Only the key_share extension was received */
1163 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001164 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001165 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001166
Jerry Yu745bb612021-10-13 22:01:04 +08001167 /* Both the pre_shared_key and key_share extensions were received */
1168 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001169 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001170 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001171
Jerry Yu745bb612021-10-13 22:01:04 +08001172 /* Neither pre_shared_key nor key_share extension was received */
1173 default:
1174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1175 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1176 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001177 }
1178
1179 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1180 *
1181 * TODO: We don't have to do this in case we offered 0-RTT and the
1182 * server accepted it. In this case, we could skip generating
1183 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001184 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001185 if( ret != 0 )
1186 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001188 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001189 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001190 }
1191
1192 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001193 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001194 if( ret != 0 )
1195 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001197 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001198 }
1199
1200 /* Next evolution in key schedule: Establish handshake secret and
1201 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001202 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001203 if( ret != 0 )
1204 {
Jerry Yuc068b662021-10-11 22:30:19 +08001205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1206 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001207 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001208 }
1209
Jerry Yub85277e2021-10-13 13:36:05 +08001210 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001211 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001212 {
1213 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1214 goto cleanup;
1215 }
Jerry Yu0b177842021-09-05 19:41:30 +08001216
1217 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1218 ssl->conf->endpoint,
1219 ssl->session_negotiate->ciphersuite,
1220 &traffic_keys,
1221 ssl );
1222 if( ret != 0 )
1223 {
1224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001225 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001226 }
1227
Jerry Yub85277e2021-10-13 13:36:05 +08001228 handshake->transform_handshake = transform_handshake;
1229 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001230
1231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1232 ssl->session_in = ssl->session_negotiate;
1233
1234 /*
1235 * State machine update
1236 */
1237 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1238
Jerry Yu4a173382021-10-11 21:45:31 +08001239cleanup:
1240
Jerry Yu0b177842021-09-05 19:41:30 +08001241 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001242 if( ret != 0 )
1243 {
Jerry Yub85277e2021-10-13 13:36:05 +08001244 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001245
Jerry Yu4a173382021-10-11 21:45:31 +08001246 MBEDTLS_SSL_PEND_FATAL_ALERT(
1247 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1248 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1249 }
1250 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001251}
1252
XiaokangQian647719a2021-12-07 09:16:29 +00001253static int ssl_hrr_parse( mbedtls_ssl_context *ssl,
1254 const unsigned char *buf, size_t buflen )
1255{
1256 int ret; /* return value */
1257 int i; /* scratch value */
1258 /* pointer to the end of the buffer for length checks */
1259 const unsigned char* msg_end = buf + buflen;
1260
1261 size_t ext_len; /* stores length of all extensions */
1262 unsigned int ext_id; /* id of an extension */
1263 const unsigned char* ext; /* pointer to an individual extension */
1264 unsigned int ext_size; /* size of an individual extension */
1265
1266 const mbedtls_ssl_ciphersuite_t* suite_info; /* pointer to ciphersuite */
1267
1268#if defined(MBEDTLS_SSL_COOKIE_C)
1269 size_t cookie_len;
1270 unsigned char *cookie;
1271#endif /* MBEDTLS_SSL_COOKIE_C */
1272
1273 /* Check for minimal length */
1274 /* struct {
1275 * ProtocolVersion legacy_version = 0x0303;
1276 * 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 *
1283 *
1284 * 38 = 32 ( random bytes ) + 2 ( ciphersuite ) + 2 ( version ) +
1285 * 1 ( legacy_compression_method ) + 1 ( minimum for legacy_session_id_echo )
1286 */
1287 if( buflen < 38 )
1288 {
1289 MBEDTLS_SSL_DEBUG_MSG( 1,
1290 ( "bad hello retry request message - min size not reached" ) );
1291 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1292 MBEDTLS_ERR_SSL_DECODE_ERROR );
1293 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1294 }
1295
1296 MBEDTLS_SSL_DEBUG_BUF( 4, "hello retry request", buf, buflen );
1297
1298 MBEDTLS_SSL_DEBUG_BUF( 3, "hello retry request, version", buf + 0, 2 );
1299 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1300 ssl->conf->transport, buf + 0 );
1301
1302 /* The version field must contain 0x303 */
1303 if( buf[0] != 0x03 || buf[1] != 0x03 )
1304 {
1305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1306 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1307 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1308 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1309 }
1310
1311 /* skip version */
1312 buf += 2;
1313
1314 /* Internally we use the correct 1.3 version */
1315 ssl->major_ver = 0x03;
1316 ssl->minor_ver = 0x04;
1317
1318 /* store server-provided random values */
1319 memcpy( ssl->handshake->randbytes + 32, buf, 32 );
1320 MBEDTLS_SSL_DEBUG_BUF( 3, "hello retry request, random bytes", buf + 2, 32 );
1321
1322 /* skip random bytes */
1323 buf += 32;
1324
1325 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &buf, msg_end ) != 0 )
1326 {
1327 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1328 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1329 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1330 }
1331
1332 /* read server-selected ciphersuite, which follows random bytes */
1333 i = ( buf[0] << 8 ) | buf[1];
1334
1335 /* skip ciphersuite */
1336 buf += 2;
1337
1338 /* TBD: Check whether we have offered this ciphersuite */
1339 /* Via the force_ciphersuite version we may have instructed the client */
1340 /* to use a difference ciphersuite. */
1341
1342 /* Configure ciphersuites */
1343 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1344
1345 if( ssl->handshake->ciphersuite_info == NULL )
1346 {
1347 MBEDTLS_SSL_DEBUG_MSG( 1,
1348 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
1349 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
1350 MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1352 }
1353
1354 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
1355
1356 ssl->session_negotiate->ciphersuite = i;
1357
1358 suite_info = mbedtls_ssl_ciphersuite_from_id(
1359 ssl->session_negotiate->ciphersuite );
1360 if( suite_info == NULL )
1361 {
1362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1363 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1364 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1365 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1366 }
1367
1368 MBEDTLS_SSL_DEBUG_MSG( 3,
1369 ( "hello retry request, chosen ciphersuite: ( %04x ) - %s",
1370 (unsigned int)i, suite_info->name ) );
1371
1372#if defined(MBEDTLS_HAVE_TIME)
1373 ssl->session_negotiate->start = time( NULL );
1374#endif /* MBEDTLS_HAVE_TIME */
1375
1376 i = 0;
1377 while ( 1 )
1378 {
1379 if( ssl->conf->ciphersuite_list[i] == 0 )
1380 {
1381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1382 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1383 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1384 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1385 }
1386
1387 if( ssl->conf->ciphersuite_list[i++] ==
1388 ssl->session_negotiate->ciphersuite )
1389 {
1390 break;
1391 }
1392 }
1393
1394 /* Ensure that compression method is set to zero */
1395 if( buf[0] != 0 )
1396 {
1397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1398 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1399 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1400 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1401 }
1402
1403 /* skip compression */
1404 buf++;
1405
1406 /* Are we reading beyond the message buffer? */
1407 if( ( buf + 2 ) > msg_end )
1408 {
1409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1410 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1411 MBEDTLS_ERR_SSL_DECODE_ERROR );
1412 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1413 }
1414
1415 ext_len = ( ( buf[0] << 8 ) | ( buf[1] ) );
1416 buf += 2; /* skip extension length */
1417
1418 /* Are we reading beyond the message buffer? */
1419 if( ( buf + ext_len ) > msg_end )
1420 {
1421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1422 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1423 MBEDTLS_ERR_SSL_DECODE_ERROR );
1424 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1425 }
1426
1427 ext = buf;
1428
1429 MBEDTLS_SSL_DEBUG_MSG( 3,
1430 ( "hello retry request, total extension length: %"
1431 MBEDTLS_PRINTF_SIZET , ext_len ) );
1432
1433 MBEDTLS_SSL_DEBUG_BUF( 3, "extensions", ext, ext_len );
1434
1435 while ( ext_len )
1436 {
1437 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1438 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
1439
1440 if( ext_size + 4 > ext_len )
1441 {
1442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1443 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1444 MBEDTLS_ERR_SSL_DECODE_ERROR );
1445 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1446 }
1447
1448 switch( ext_id )
1449 {
1450#if defined(MBEDTLS_SSL_COOKIE_C)
1451 case MBEDTLS_TLS_EXT_COOKIE:
1452
1453 /* Retrieve length field of cookie */
1454 if( ext_size >= 2 )
1455 {
1456 cookie = (unsigned char *) ( ext + 4 );
1457 cookie_len = ( cookie[0] << 8 ) | cookie[1];
1458 cookie += 2;
1459 }
1460 else
1461 {
1462 MBEDTLS_SSL_DEBUG_MSG( 1,
1463 ( "bad HRR message - cookie length mismatch" ) );
1464 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1465 }
1466
1467 if( ( cookie_len + 2 ) != ext_size )
1468 {
1469 MBEDTLS_SSL_DEBUG_MSG( 1,
1470 ( "bad HRR message - cookie length mismatch" ) );
1471 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1472 }
1473
1474 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", cookie, cookie_len );
1475
1476 mbedtls_free( ssl->handshake->verify_cookie );
1477
1478 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
1479 if( ssl->handshake->verify_cookie == NULL )
1480 {
1481 MBEDTLS_SSL_DEBUG_MSG( 1,
1482 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
1483 cookie_len ) );
1484 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1485 }
1486
1487 memcpy( ssl->handshake->verify_cookie, cookie, cookie_len );
1488 ssl->handshake->verify_cookie_len = (unsigned char) cookie_len;
1489 break;
1490#endif /* MBEDTLS_SSL_COOKIE_C */
1491
1492 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1493 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_versions extension" ) );
1494
1495 ret = ssl_tls13_parse_supported_versions_ext( ssl,
1496 ext + 4,
1497 ext + 4 + ext_size );
1498 if( ret != 0 )
1499 return( ret );
1500 break;
1501
1502#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1503 case MBEDTLS_TLS_EXT_KEY_SHARE:
1504 {
1505 /* Variables for parsing the key_share */
1506 const mbedtls_ecp_group_id* grp_id;
1507 const mbedtls_ecp_curve_info *curve_info = NULL;
1508 int tls_id;
1509 int found = 0;
1510
1511 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", ext + 4, ext_size );
1512
1513 /* Read selected_group */
1514 tls_id = ( ( ext[4] << 8 ) | ( ext[5] ) );
1515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", tls_id ) );
1516
1517 /* Upon receipt of this extension in a HelloRetryRequest, the client
1518 * MUST first verify that the selected_group field corresponds to a
1519 * group which was provided in the "supported_groups" extension in the
1520 * original ClientHello.
1521 * The supported_group was based on the info in ssl->conf->curve_list.
1522 *
1523 * If the server provided a key share that was not sent in the ClientHello
1524 * then the client MUST abort the handshake with an "illegal_parameter" alert. */
1525 for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
1526 {
1527 curve_info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
1528 if( curve_info == NULL || curve_info->tls_id != tls_id )
1529 continue;
1530
1531 /* We found a match */
1532 found = 1;
1533 break;
1534 }
1535
1536 /* Client MUST verify that the selected_group field does not
1537 * correspond to a group which was provided in the "key_share"
1538 * extension in the original ClientHello. If the server sent an
1539 * HRR message with a key share already provided in the
1540 * ClientHello then the client MUST abort the handshake with
1541 * an "illegal_parameter" alert. */
1542 if( found == 0 || tls_id == ssl->handshake->offered_group_id )
1543 {
1544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
1545 MBEDTLS_SSL_PEND_FATAL_ALERT(
1546 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1547 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1548 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1549 }
1550
1551 /* Remember server's preference for next ClientHello */
1552 ssl->handshake->offered_group_id= tls_id;
1553 break;
1554 }
1555
1556#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
1557 default:
1558 MBEDTLS_SSL_DEBUG_MSG( 3,
1559 ( "unknown extension found: %u ( ignoring )",
1560 ext_id ) );
1561 }
1562
1563 /* Jump to next extension */
1564 ext_len -= 4 + ext_size;
1565 ext += 4 + ext_size;
1566
1567 if( ext_len > 0 && ext_len < 4 )
1568 {
1569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad hello retry request message" ) );
1570 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1571 }
1572
1573 }
1574
1575 return( 0 );
1576}
1577
1578static int ssl_hrr_postprocess( mbedtls_ssl_context *ssl )
1579{
1580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1581
1582 if( ssl->handshake->hello_retry_requests_received > 0 )
1583 {
1584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1585 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1586 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1587 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1588 }
1589
1590 ssl->handshake->hello_retry_requests_received++;
1591
1592#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1593 /* If not offering early data, the client sends a dummy CCS record
1594 * immediately before its second flight. This may either be before
1595 * its second ClientHello or before its encrypted handshake flight. */
1596 mbedtls_ssl_handshake_set_state( ssl,
1597 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1598#else
1599 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1600#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1601
1602 mbedtls_ssl_tls13_session_reset_msg_layer( ssl, 0 );
1603
1604 /* Reset everything that's going to be re-generated in the new ClientHello.
1605 *
1606 * Currently, we're always resetting the key share, even if the server
1607 * was fine with it. Once we have separated key share generation from
1608 * key share writing, we can confine this to the case where the server
1609 * requested a different share. */
1610 ret = ssl_reset_key_share( ssl );
1611 if( ret != 0 )
1612 return( ret );
1613
1614 return( 0 );
1615}
1616
Jerry Yue1b9c292021-09-10 10:08:31 +08001617/*
Jerry Yu4a173382021-10-11 21:45:31 +08001618 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001619 * Handler for MBEDTLS_SSL_SERVER_HELLO
1620 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001621static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001622{
Jerry Yu4a173382021-10-11 21:45:31 +08001623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001624 unsigned char *buf = NULL;
1625 size_t buf_len = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001626
1627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1628
1629 /* Coordination step
1630 * - Fetch record
1631 * - Make sure it's either a ServerHello or a HRR.
1632 * - Switch processing routine in case of HRR
1633 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001634 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1635 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1636
Jerry Yub85277e2021-10-13 13:36:05 +08001637 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001638 /* Parsing step
1639 * We know what message to expect by now and call
1640 * the respective parsing function.
1641 */
1642 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1643 {
Jerry Yuc068b662021-10-11 22:30:19 +08001644 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
1645 buf + buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001646
Xiaofei Bai746f9482021-11-12 08:53:56 +00001647 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1648 MBEDTLS_SSL_HS_SERVER_HELLO,
1649 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001650
Jerry Yuc068b662021-10-11 22:30:19 +08001651 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001652 }
1653 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1654 {
XiaokangQian647719a2021-12-07 09:16:29 +00001655 MBEDTLS_SSL_PROC_CHK( ssl_hrr_parse( ssl, buf, buf_len ) );
1656 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1657
1658 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1659 buf, buf_len );
1660
1661 MBEDTLS_SSL_PROC_CHK( ssl_hrr_postprocess( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001662 }
1663
1664cleanup:
1665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1666 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001667}
1668
1669/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001670 *
1671 * EncryptedExtensions message
1672 *
1673 * The EncryptedExtensions message contains any extensions which
1674 * should be protected, i.e., any which are not needed to establish
1675 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001676 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001677
1678/*
1679 * Overview
1680 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001681
1682/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001683static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001684
XiaokangQian97799ac2021-10-11 10:05:54 +00001685static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1686 const unsigned char *buf,
1687 const unsigned char *end );
1688static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001689
1690/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001691 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001692 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001693static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001694{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001695 int ret;
1696 unsigned char *buf;
1697 size_t buf_len;
1698
1699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1700
Xiaofei Bai746f9482021-11-12 08:53:56 +00001701 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001702 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001703 &buf, &buf_len ) );
1704
1705 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001706 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001707 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001708
Xiaofei Bai746f9482021-11-12 08:53:56 +00001709 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001710 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001711
XiaokangQian97799ac2021-10-11 10:05:54 +00001712 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001713
1714cleanup:
1715
1716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1717 return( ret );
1718
1719}
1720
XiaokangQian08da26c2021-10-09 10:12:11 +00001721/* Parse EncryptedExtensions message
1722 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001723 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001724 * } EncryptedExtensions;
1725 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001726static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1727 const unsigned char *buf,
1728 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001729{
1730 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001731 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001732 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001733 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001734
XiaokangQian08da26c2021-10-09 10:12:11 +00001735 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001736 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001737 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001738
XiaokangQian97799ac2021-10-11 10:05:54 +00001739 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001740 extensions_end = p + extensions_len;
1741 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001742
XiaokangQian8db25ff2021-10-13 05:56:18 +00001743 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001744 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001745 unsigned int extension_type;
1746 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001747
XiaokangQian08da26c2021-10-09 10:12:11 +00001748 /*
1749 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001750 * ExtensionType extension_type; (2 bytes)
1751 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001752 * } Extension;
1753 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001754 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001755 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1756 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1757 p += 4;
1758
XiaokangQian8db25ff2021-10-13 05:56:18 +00001759 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001760
XiaokangQian97799ac2021-10-11 10:05:54 +00001761 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001762 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001763 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001764 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001765 switch( extension_type )
1766 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001767
XiaokangQian08da26c2021-10-09 10:12:11 +00001768 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1770 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001771
XiaokangQian08da26c2021-10-09 10:12:11 +00001772 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001773 MBEDTLS_SSL_DEBUG_MSG(
1774 3, ( "unsupported extension found: %u ", extension_type) );
1775 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001776 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001777 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1778 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001779 }
1780
XiaokangQian08da26c2021-10-09 10:12:11 +00001781 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001782 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001783
XiaokangQian97799ac2021-10-11 10:05:54 +00001784 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001785 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001786 {
1787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001788 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001789 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001790 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001791 }
1792
1793 return( ret );
1794}
1795
XiaokangQian97799ac2021-10-11 10:05:54 +00001796static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001797{
Jerry Yua93ac112021-10-27 16:31:48 +08001798#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001799 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001800 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1801 else
Jerry Yud2674312021-10-29 10:08:19 +08001802 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001803#else
1804 ((void) ssl);
1805 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1806#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001807 return( 0 );
1808}
1809
Jerry Yua93ac112021-10-27 16:31:48 +08001810#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001811/*
Jerry Yud2674312021-10-29 10:08:19 +08001812 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1813 */
1814static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
1815{
1816 int ret = mbedtls_ssl_read_record( ssl, 0 );
1817
1818 if( ret != 0 )
1819 {
1820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1821 return( ret );
1822 }
1823
1824 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1825 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1826 {
1827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
1828 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1829 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1830 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1831 }
1832
1833 ssl->keep_current_message = 1;
1834 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1835
1836 return( 0 );
1837}
1838
1839/*
Jerry Yu687101b2021-09-14 16:03:56 +08001840 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1841 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001842static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001843{
Xiaofei Bai947571e2021-09-29 09:12:03 +00001844 int ret;
1845
1846 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001847 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001848 return( ret );
1849
Jerry Yu687101b2021-09-14 16:03:56 +08001850 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1851 return( 0 );
1852}
1853
1854/*
1855 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1856 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001857static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001858{
Jerry Yu30b071c2021-09-12 20:16:03 +08001859 int ret;
1860
1861 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1862 if( ret != 0 )
1863 return( ret );
1864
Jerry Yu687101b2021-09-14 16:03:56 +08001865 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1866 return( 0 );
1867}
Jerry Yua93ac112021-10-27 16:31:48 +08001868#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01001869
Jerry Yu687101b2021-09-14 16:03:56 +08001870/*
1871 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1872 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001873static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001874{
XiaokangQianac0385c2021-11-03 06:40:11 +00001875 int ret;
1876
XiaokangQianc5c39d52021-11-09 11:55:10 +00001877 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00001878 if( ret != 0 )
1879 return( ret );
1880
Ronald Cron49ad6192021-11-24 16:25:31 +01001881#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1882 mbedtls_ssl_handshake_set_state(
1883 ssl,
1884 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
1885#else
XiaokangQianac0385c2021-11-03 06:40:11 +00001886 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01001887#endif
1888
XiaokangQianac0385c2021-11-03 06:40:11 +00001889 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001890}
1891
1892/*
Ronald Crond4c64022021-12-06 09:06:46 +01001893 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
1894 */
1895#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1896static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1897{
1898 int ret;
1899
1900 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1901 if( ret != 0 )
1902 return( ret );
1903
1904 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1905
1906 return( 0 );
1907}
1908#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1909
1910/*
Jerry Yu687101b2021-09-14 16:03:56 +08001911 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1912 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001913static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001914{
XiaokangQian0fa66432021-11-15 03:33:57 +00001915 int ret;
1916
Ronald Cron49ad6192021-11-24 16:25:31 +01001917 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
1918
XiaokangQian0fa66432021-11-15 03:33:57 +00001919 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1920 if( ret != 0 )
1921 return( ret );
1922
1923 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
1924 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001925}
1926
1927/*
1928 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1929 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001930static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001931{
Jerry Yu378254d2021-10-30 21:44:47 +08001932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001933 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001934 return( 0 );
1935}
1936
1937/*
1938 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1939 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001940static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001941{
Jerry Yu378254d2021-10-30 21:44:47 +08001942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1943 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1944
1945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1946 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1947
1948 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1949
1950 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1951 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08001952}
1953
Jerry Yu92c6b402021-08-27 16:59:09 +08001954int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001955{
Jerry Yu92c6b402021-08-27 16:59:09 +08001956 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001957
Jerry Yue3b34122021-09-28 17:53:35 +08001958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
1959 mbedtls_ssl_states_str( ssl->state ),
1960 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001961
1962 switch( ssl->state )
1963 {
1964 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001965 * ssl->state is initialized as HELLO_REQUEST. It is the same
1966 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001967 */
1968 case MBEDTLS_SSL_HELLO_REQUEST:
1969 case MBEDTLS_SSL_CLIENT_HELLO:
1970 ret = ssl_tls13_write_client_hello( ssl );
1971 break;
1972
1973 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001974 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001975 break;
1976
1977 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00001978 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001979 break;
1980
Jerry Yua93ac112021-10-27 16:31:48 +08001981#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08001982 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
1983 ret = ssl_tls13_process_certificate_request( ssl );
1984 break;
1985
Jerry Yu687101b2021-09-14 16:03:56 +08001986 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001987 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001988 break;
1989
1990 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001991 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001992 break;
Jerry Yua93ac112021-10-27 16:31:48 +08001993#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08001994
1995 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00001996 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001997 break;
1998
Jerry Yu687101b2021-09-14 16:03:56 +08001999 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002000 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002001 break;
2002
2003 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002004 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002005 break;
2006
2007 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002008 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002009 break;
2010
Ronald Cron49ad6192021-11-24 16:25:31 +01002011 /*
2012 * Injection of dummy-CCS's for middlebox compatibility
2013 */
2014#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2015 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Ronald Crond4c64022021-12-06 09:06:46 +01002016 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01002017 break;
2018#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2019
Jerry Yu92c6b402021-08-27 16:59:09 +08002020 default:
2021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2023 }
2024
2025 return( ret );
2026}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002027
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002028#endif /* MBEDTLS_SSL_CLI_C */
2029
Ronald Cron6f135e12021-12-08 16:57:54 +01002030#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */