blob: 99c12cd7aef89f186a7fe46623159993bd23003e [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
lhuang0486cacac2022-01-21 07:34:27 -0800116#if defined(MBEDTLS_SSL_ALPN)
117/*
118 * ssl_tls13_write_alpn_ext( ) structure:
119 *
120 * opaque ProtocolName<1..2^8-1>;
121 *
122 * struct {
123 * ProtocolName protocol_name_list<2..2^16-1>
124 * } ProtocolNameList;
125 *
126 */
127static int ssl_tls13_write_alpn_ext( mbedtls_ssl_context *ssl,
128 unsigned char *buf,
129 const unsigned char *end,
130 size_t *olen )
131{
132 unsigned char *p = buf;
133 size_t alpnlen = 0;
134 const char **cur;
135
136 *olen = 0;
137
138 if( ssl->conf->alpn_list == NULL )
139 return( 0 );
140
141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
142
143 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
144 alpnlen += strlen( *cur ) + 1;
145
146 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
147
148 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
149 p += 2;
150
151 /*
152 * opaque ProtocolName<1..2^8-1>;
153 *
154 * struct {
155 * ProtocolName protocol_name_list<2..2^16-1>
156 * } ProtocolNameList;
157 */
158
159 /* Skip writing extension and list length for now */
160 p += 4;
161
162 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
163 {
164 /*
165 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
166 * protocol names is less than 255.
167 */
168 *p = (unsigned char)strlen( *cur );
169 memcpy( p + 1, *cur, *p );
170 p += 1 + *p;
171 }
172
173 *olen = p - buf;
174
175 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
176 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
177
178 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
179 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
180
181 return( 0 );
182}
183
184static int ssl_tls13_parse_alpn_ext( mbedtls_ssl_context *ssl,
185 const unsigned char *buf, size_t len )
186{
187 size_t list_len, name_len;
188 const unsigned char *p = buf;
189 const unsigned char *end = buf + len;
190
191 /* If we didn't send it, the server shouldn't send it */
192 if( ssl->conf->alpn_list == NULL )
193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
194
195 /*
196 * opaque ProtocolName<1..2^8-1>;
197 *
198 * struct {
199 * ProtocolName protocol_name_list<2..2^16-1>
200 * } ProtocolNameList;
201 *
202 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
203 */
204
205 /* Min length is 2 ( list_len ) + 1 ( name_len ) + 1 ( name ) */
206 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
207
208 list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
209 p += 2;
210 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len );
211
212 name_len = *p++;
213 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len - 1 );
214
215 /* Check that the server chosen protocol was in our list and save it */
216 for ( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
217 {
218 if( name_len == strlen( *alpn ) &&
219 memcmp( buf + 3, *alpn, name_len ) == 0 )
220 {
221 ssl->alpn_chosen = *alpn;
222 return( 0 );
223 }
224 }
225
226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
227}
228#endif /* MBEDTLS_SSL_ALPN */
229
Jerry Yubc20bdd2021-08-24 15:59:48 +0800230#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
231
XiaokangQian16acd4b2022-01-14 07:35:47 +0000232static int ssl_tls13_reset_key_share( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +0000233{
234 uint16_t group_id = ssl->handshake->offered_group_id;
235 if( group_id == 0 )
236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
237
XiaokangQian355e09a2022-01-20 11:14:50 +0000238#if defined(MBEDTLS_ECDH_C)
XiaokangQian647719a2021-12-07 09:16:29 +0000239 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
XiaokangQian78b1fa72022-01-19 06:56:30 +0000240 {
241 mbedtls_ecdh_free( &ssl->handshake->ecdh_ctx );
242 return( 0 );
243 }
XiaokangQian355e09a2022-01-20 11:14:50 +0000244 else
245#endif /* MBEDTLS_ECDH_C */
246 if( 0 /* other KEMs? */ )
XiaokangQian647719a2021-12-07 09:16:29 +0000247 {
248 /* Do something */
249 }
250
251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
252}
253
254/*
Jerry Yu56fc07f2021-09-01 17:48:49 +0800255 * Functions for writing key_share extension.
256 */
257#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800258static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800259 mbedtls_ssl_context *ssl,
260 uint16_t named_group,
261 unsigned char *buf,
262 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000263 size_t *out_len )
Jerry Yu92c6b402021-08-27 16:59:09 +0800264{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800266 const mbedtls_ecp_curve_info *curve_info =
267 mbedtls_ecp_curve_info_from_tls_id( named_group );
268
269 if( curve_info == NULL )
270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
271
272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
273
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800274 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
275 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800276 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800278 return( ret );
279 }
280
Xiaofei Baid25fab62021-12-02 06:36:27 +0000281 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, out_len,
282 buf, end - buf,
283 ssl->conf->f_rng, ssl->conf->p_rng );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800284 if( ret != 0 )
285 {
286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
287 return( ret );
288 }
289
290 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
291 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800292 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800293}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800294#endif /* MBEDTLS_ECDH_C */
295
Jerry Yub60e3cf2021-09-08 16:41:02 +0800296static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
297 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800298{
299 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
300
Jerry Yu56fc07f2021-09-01 17:48:49 +0800301
Jerry Yu56fc07f2021-09-01 17:48:49 +0800302#if defined(MBEDTLS_ECDH_C)
Brett Warren14efd332021-10-06 09:32:11 +0100303 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800304 /* Pick first available ECDHE group compatible with TLS 1.3 */
Brett Warren14efd332021-10-06 09:32:11 +0100305 if( group_list == NULL )
Jerry Yu388bd0d2021-09-15 18:41:02 +0800306 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
307
Brett Warren14efd332021-10-06 09:32:11 +0100308 for ( ; *group_list != 0; group_list++ )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800309 {
Xiaofei Baieef15042021-11-18 07:29:56 +0000310 const mbedtls_ecp_curve_info *curve_info;
311 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
312 if( curve_info != NULL &&
Brett Warren14efd332021-10-06 09:32:11 +0100313 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800314 {
Brett Warren14efd332021-10-06 09:32:11 +0100315 *group_id = *group_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800316 return( 0 );
317 }
318 }
319#else
320 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800321 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800322#endif /* MBEDTLS_ECDH_C */
323
324 /*
325 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800326 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800327 */
328
329 return( ret );
330}
331
332/*
333 * ssl_tls13_write_key_share_ext
334 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800335 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800336 *
337 * struct {
338 * NamedGroup group;
339 * opaque key_exchange<1..2^16-1>;
340 * } KeyShareEntry;
341 * struct {
342 * KeyShareEntry client_shares<0..2^16-1>;
343 * } KeyShareClientHello;
344 */
345static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
346 unsigned char *buf,
347 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000348 size_t *out_len )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800349{
350 unsigned char *p = buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000351 unsigned char *client_shares; /* Start of client_shares */
352 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800353 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800354 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
355
Xiaofei Baid25fab62021-12-02 06:36:27 +0000356 *out_len = 0;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800357
Jerry Yub60e3cf2021-09-08 16:41:02 +0800358 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800359 * - extension_type (2 bytes)
360 * - extension_data_length (2 bytes)
361 * - client_shares_length (2 bytes)
362 */
363 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
364 p += 6;
365
366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
367
368 /* HRR could already have requested something else. */
369 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800370 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
371 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800372 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800373 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800374 &group_id ) );
375 }
376
377 /*
378 * Dispatch to type-specific key generation function.
379 *
380 * So far, we're only supporting ECDHE. With the introduction
381 * of PQC KEMs, we'll want to have multiple branches, one per
382 * type of KEM, and dispatch to the corresponding crypto. And
383 * only one key share entry is allowed.
384 */
Xiaofei Baieef15042021-11-18 07:29:56 +0000385 client_shares = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800386#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800387 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800388 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800389 /* Pointer to group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000390 unsigned char *group = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800391 /* Length of key_exchange */
392 size_t key_exchange_len;
393
394 /* Check there is space for header of KeyShareEntry
395 * - group (2 bytes)
396 * - key_exchange_length (2 bytes)
397 */
398 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
399 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800400 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
401 p, end,
402 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800403 p += key_exchange_len;
404 if( ret != 0 )
405 return( ret );
406
407 /* Write group */
Xiaofei Baieef15042021-11-18 07:29:56 +0000408 MBEDTLS_PUT_UINT16_BE( group_id, group, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800409 /* Write key_exchange_length */
Xiaofei Baieef15042021-11-18 07:29:56 +0000410 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800411 }
412 else
413#endif /* MBEDTLS_ECDH_C */
414 if( 0 /* other KEMs? */ )
415 {
416 /* Do something */
417 }
418 else
419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
420
Jerry Yub60e3cf2021-09-08 16:41:02 +0800421 /* Length of client_shares */
Xiaofei Baieef15042021-11-18 07:29:56 +0000422 client_shares_len = p - client_shares;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800423 if( client_shares_len == 0)
424 {
425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
426 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800427 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800428 /* Write extension_type */
429 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
430 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800431 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800432 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800433 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800434
435 /* Update offered_group_id field */
436 ssl->handshake->offered_group_id = group_id;
437
438 /* Output the total length of key_share extension. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000439 *out_len = p - buf;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800440
Xiaofei Baid25fab62021-12-02 06:36:27 +0000441 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *out_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800442
443 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
444
445cleanup:
446
447 return( ret );
448}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800449
Jerry Yue1b9c292021-09-10 10:08:31 +0800450#if defined(MBEDTLS_ECDH_C)
451
Jerry Yuc068b662021-10-11 22:30:19 +0800452static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +0800453{
454 const mbedtls_ecp_curve_info *curve_info;
455 mbedtls_ecp_group_id grp_id;
456#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
457 grp_id = ssl->handshake->ecdh_ctx.grp.id;
458#else
459 grp_id = ssl->handshake->ecdh_ctx.grp_id;
460#endif
461
462 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
463 if( curve_info == NULL )
464 {
465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
467 }
468
469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
470
Jerry Yue1b9c292021-09-10 10:08:31 +0800471 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jerry Yub85277e2021-10-13 13:36:05 +0800472 return( -1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800473
474 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
475 MBEDTLS_DEBUG_ECDH_QP );
476
477 return( 0 );
478}
479
Jerry Yuc068b662021-10-11 22:30:19 +0800480static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
481 const unsigned char *buf,
482 size_t buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +0800483{
484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
485
Jerry Yuc068b662021-10-11 22:30:19 +0800486 ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
Jerry Yu4a173382021-10-11 21:45:31 +0800487 buf, buf_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800488 if( ret != 0 )
489 {
490 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
Jerry Yub85277e2021-10-13 13:36:05 +0800491
492 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
493 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
494 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800495 }
496
Jerry Yuc068b662021-10-11 22:30:19 +0800497 if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +0800498 {
Jerry Yuc068b662021-10-11 22:30:19 +0800499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
Jerry Yub85277e2021-10-13 13:36:05 +0800500
Jerry Yu4a173382021-10-11 21:45:31 +0800501 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
502 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
503 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Jerry Yue1b9c292021-09-10 10:08:31 +0800504 }
505
506 return( 0 );
507}
Jerry Yu4a173382021-10-11 21:45:31 +0800508#endif /* MBEDTLS_ECDH_C */
Jerry Yue1b9c292021-09-10 10:08:31 +0800509
XiaokangQiand59be772022-01-24 10:12:51 +0000510/*
511 * ssl_tls13_parse_hrr_key_share_ext()
512 * Parse key_share extension in Hello Retry Request
513 *
514 * struct {
515 * NamedGroup selected_group;
516 * } KeyShareHelloRetryRequest;
517 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000518static int ssl_tls13_parse_hrr_key_share_ext( mbedtls_ssl_context *ssl,
XiaokangQianb851da82022-01-14 04:03:11 +0000519 const unsigned char *buf,
520 const unsigned char *end )
521{
XiaokangQianb851da82022-01-14 04:03:11 +0000522 const mbedtls_ecp_curve_info *curve_info = NULL;
523 const unsigned char *p = buf;
XiaokangQiand59be772022-01-24 10:12:51 +0000524 int selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000525 int found = 0;
526
527 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
528 if( group_list == NULL )
529 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
530
531 MBEDTLS_SSL_DEBUG_BUF( 3, "key_share extension", p, end - buf );
532
533 /* Read selected_group */
XiaokangQianb48894e2022-01-17 02:05:52 +0000534 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQiand59be772022-01-24 10:12:51 +0000535 selected_group = MBEDTLS_GET_UINT16_BE( p, 0 );
536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected_group ( %d )", selected_group ) );
XiaokangQianb851da82022-01-14 04:03:11 +0000537
538 /* Upon receipt of this extension in a HelloRetryRequest, the client
539 * MUST first verify that the selected_group field corresponds to a
540 * group which was provided in the "supported_groups" extension in the
541 * original ClientHello.
542 * The supported_group was based on the info in ssl->conf->group_list.
543 *
544 * If the server provided a key share that was not sent in the ClientHello
545 * then the client MUST abort the handshake with an "illegal_parameter" alert.
546 */
XiaokangQiand9e068e2022-01-18 06:23:32 +0000547 for( ; *group_list != 0; group_list++ )
XiaokangQianb851da82022-01-14 04:03:11 +0000548 {
549 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
XiaokangQiand59be772022-01-24 10:12:51 +0000550 if( curve_info == NULL || curve_info->tls_id != selected_group )
XiaokangQianb851da82022-01-14 04:03:11 +0000551 continue;
552
553 /* We found a match */
554 found = 1;
555 break;
556 }
557
558 /* Client MUST verify that the selected_group field does not
559 * correspond to a group which was provided in the "key_share"
560 * extension in the original ClientHello. If the server sent an
561 * HRR message with a key share already provided in the
562 * ClientHello then the client MUST abort the handshake with
563 * an "illegal_parameter" alert.
564 */
XiaokangQiand59be772022-01-24 10:12:51 +0000565 if( found == 0 || selected_group == ssl->handshake->offered_group_id )
XiaokangQianb851da82022-01-14 04:03:11 +0000566 {
567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid key share in HRR" ) );
568 MBEDTLS_SSL_PEND_FATAL_ALERT(
569 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
570 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
571 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
572 }
573
574 /* Remember server's preference for next ClientHello */
XiaokangQiand59be772022-01-24 10:12:51 +0000575 ssl->handshake->offered_group_id = selected_group;
XiaokangQianb851da82022-01-14 04:03:11 +0000576
577 return( 0 );
578}
579
Jerry Yue1b9c292021-09-10 10:08:31 +0800580/*
Jerry Yub85277e2021-10-13 13:36:05 +0800581 * ssl_tls13_parse_key_share_ext()
582 * Parse key_share extension in Server Hello
583 *
Jerry Yue1b9c292021-09-10 10:08:31 +0800584 * struct {
585 * KeyShareEntry server_share;
586 * } KeyShareServerHello;
587 * struct {
588 * NamedGroup group;
589 * opaque key_exchange<1..2^16-1>;
590 * } KeyShareEntry;
591 */
Jerry Yu4a173382021-10-11 21:45:31 +0800592static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
Jerry Yue1b9c292021-09-10 10:08:31 +0800593 const unsigned char *buf,
594 const unsigned char *end )
595{
Jerry Yub85277e2021-10-13 13:36:05 +0800596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +0800597 const unsigned char *p = buf;
Jerry Yu4a173382021-10-11 21:45:31 +0800598 uint16_t group, offered_group;
Jerry Yue1b9c292021-09-10 10:08:31 +0800599
Jerry Yu4a173382021-10-11 21:45:31 +0800600 /* ...
601 * NamedGroup group; (2 bytes)
602 * ...
603 */
604 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
605 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800606 p += 2;
607
Jerry Yu4a173382021-10-11 21:45:31 +0800608 /* Check that the chosen group matches the one we offered. */
Jerry Yue1b9c292021-09-10 10:08:31 +0800609 offered_group = ssl->handshake->offered_group_id;
Jerry Yu4a173382021-10-11 21:45:31 +0800610 if( offered_group != group )
Jerry Yue1b9c292021-09-10 10:08:31 +0800611 {
612 MBEDTLS_SSL_DEBUG_MSG( 1,
613 ( "Invalid server key share, our group %u, their group %u",
Jerry Yu4a173382021-10-11 21:45:31 +0800614 (unsigned) offered_group, (unsigned) group ) );
615 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
616 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
617 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yue1b9c292021-09-10 10:08:31 +0800618 }
619
620#if defined(MBEDTLS_ECDH_C)
Jerry Yu4a173382021-10-11 21:45:31 +0800621 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
Jerry Yue1b9c292021-09-10 10:08:31 +0800622 {
623 /* Complete ECDHE key agreement */
Jerry Yuc068b662021-10-11 22:30:19 +0800624 ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +0800625 if( ret != 0 )
626 return( ret );
627 }
Jerry Yub85277e2021-10-13 13:36:05 +0800628 else
Jerry Yue1b9c292021-09-10 10:08:31 +0800629#endif /* MBEDTLS_ECDH_C */
Jerry Yub85277e2021-10-13 13:36:05 +0800630 if( 0 /* other KEMs? */ )
Jerry Yue1b9c292021-09-10 10:08:31 +0800631 {
632 /* Do something */
633 }
634 else
635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
636
637 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
638 return( ret );
639}
640
Jerry Yubc20bdd2021-08-24 15:59:48 +0800641#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
642
XiaokangQiand59be772022-01-24 10:12:51 +0000643/*
644 * ssl_tls13_parse_cookie_ext()
645 * Parse cookie extension in Hello Retry Request
646 *
647 * struct {
648 * opaque cookie<1..2^16-1>;
649 * } Cookie;
650 *
651 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
652 * extension to the client (this is an exception to the usual rule that
653 * the only extensions that may be sent are those that appear in the
654 * ClientHello). When sending the new ClientHello, the client MUST copy
655 * the contents of the extension received in the HelloRetryRequest into
656 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
657 * cookies in their initial ClientHello in subsequent connections.
658 */
XiaokangQian43550bd2022-01-21 04:32:58 +0000659static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl,
660 const unsigned char *buf,
661 const unsigned char *end )
662{
663 size_t cookie_len;
664 const unsigned char *p = buf;
665 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
666
667 /* Retrieve length field of cookie */
668 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
669 cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 );
670 p += 2;
671
672 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len );
673 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie extension", p, cookie_len );
674
675 mbedtls_free( handshake->verify_cookie );
XiaokangQian34909742022-01-27 02:25:04 +0000676 handshake->verify_cookie_len = 0;
XiaokangQian43550bd2022-01-21 04:32:58 +0000677 handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
678 if( handshake->verify_cookie == NULL )
679 {
680 MBEDTLS_SSL_DEBUG_MSG( 1,
681 ( "alloc failed ( %" MBEDTLS_PRINTF_SIZET " bytes )",
682 cookie_len ) );
683 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
684 }
685
686 memcpy( handshake->verify_cookie, p, cookie_len );
687 handshake->verify_cookie_len = (unsigned char) cookie_len;
688
689 return( 0 );
690}
XiaokangQian43550bd2022-01-21 04:32:58 +0000691
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800692/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800693 * CipherSuite cipher_suites<2..2^16-2>;
694 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800695static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800696 mbedtls_ssl_context *ssl,
697 unsigned char *buf,
698 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000699 size_t *out_len )
Jerry Yu6a643102021-08-31 14:40:36 +0800700{
Jerry Yufec982e2021-09-07 17:26:06 +0800701 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800702 const int *ciphersuite_list;
Xiaofei Baieef15042021-11-18 07:29:56 +0000703 unsigned char *cipher_suites; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800704 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800705
Xiaofei Baid25fab62021-12-02 06:36:27 +0000706 *out_len = 0 ;
Jerry Yu6a643102021-08-31 14:40:36 +0800707
708 /*
709 * Ciphersuite list
710 *
711 * This is a list of the symmetric cipher options supported by
712 * the client, specifically the record protection algorithm
713 * ( including secret key length ) and a hash to be used with
714 * HKDF, in descending order of client preference.
715 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800716 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800717
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800718 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800719 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
720 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800721
Jerry Yu0c63af62021-09-02 12:59:12 +0800722 /* Write cipher_suites */
Xiaofei Baieef15042021-11-18 07:29:56 +0000723 cipher_suites = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800724 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800725 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800726 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800727 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800728
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800729 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800730 if( ciphersuite_info == NULL )
731 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800732 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
733 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800734 continue;
735
736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800737 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800738 ciphersuite_info->name ) );
739
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800740 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800741 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
742 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
743 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800744 }
745
Jerry Yu0c63af62021-09-02 12:59:12 +0800746 /* Write the cipher_suites length in number of bytes */
Xiaofei Baieef15042021-11-18 07:29:56 +0000747 cipher_suites_len = p - cipher_suites;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800748 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800749 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800750 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
751 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800752
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800753 /* Output the total length of cipher_suites field. */
Xiaofei Baid25fab62021-12-02 06:36:27 +0000754 *out_len = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800755
Jerry Yu6a643102021-08-31 14:40:36 +0800756 return( 0 );
757}
758
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800759/*
760 * Structure of ClientHello message:
761 *
762 * struct {
763 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
764 * Random random;
765 * opaque legacy_session_id<0..32>;
766 * CipherSuite cipher_suites<2..2^16-2>;
767 * opaque legacy_compression_methods<1..2^8-1>;
768 * Extension extensions<8..2^16-1>;
769 * } ClientHello;
770 */
Jerry Yu08906d02021-08-31 11:05:27 +0800771static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800772 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800773 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000774 size_t *out_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800775{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800776
Jerry Yubc20bdd2021-08-24 15:59:48 +0800777 int ret;
Xiaofei Baieef15042021-11-18 07:29:56 +0000778 unsigned char *p_extensions_len; /* Pointer to extensions length */
779 size_t output_len; /* Length of buffer used by function */
780 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800781
Jerry Yubc20bdd2021-08-24 15:59:48 +0800782 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800783 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800784
Xiaofei Baid25fab62021-12-02 06:36:27 +0000785 *out_len = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800786
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800787 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800788 ssl->major_ver = ssl->conf->min_major_ver;
789 ssl->minor_ver = ssl->conf->min_minor_ver;
790
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800791 /*
792 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800793 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800794 *
795 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800796 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800797 */
Jerry Yufec982e2021-09-07 17:26:06 +0800798 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800799 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800800 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800801
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800802 /* Write the random bytes ( random ).*/
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800803 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
804 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800805 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800806 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
807 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800808
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800809 /*
810 * Write legacy_session_id
811 *
812 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
813 * which has been merged with pre-shared keys in this version. A client
814 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
815 * this field to that value. In compatibility mode, this field MUST be
816 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
817 * a new 32-byte value. This value need not be random but SHOULD be
818 * unpredictable to avoid implementations fixating on a specific value
819 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
820 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800821 */
Ronald Cron49ad6192021-11-24 16:25:31 +0100822#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
823 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
824 *p++ = (unsigned char)ssl->session_negotiate->id_len;
825 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
826 p += ssl->session_negotiate->id_len;
827
828 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
829 ssl->session_negotiate->id_len );
830#else
Jerry Yubbe09522021-09-06 21:17:54 +0800831 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
832 *p++ = 0; /* session id length set to zero */
Ronald Cron49ad6192021-11-24 16:25:31 +0100833#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800834
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800835 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800836 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800837 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800838 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800839 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800840
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800841 /* Write legacy_compression_methods
842 *
843 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800844 * one byte set to zero, which corresponds to the 'null' compression
845 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800846 */
Jerry Yubbe09522021-09-06 21:17:54 +0800847 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
848 *p++ = 1;
849 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800850
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800851 /* Write extensions */
852
853 /* Keeping track of the included extensions */
854 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800855
856 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800857 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Xiaofei Baieef15042021-11-18 07:29:56 +0000858 p_extensions_len = p;
Jerry Yubbe09522021-09-06 21:17:54 +0800859 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800860
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800861 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800862 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800863 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800864 */
Jerry Yubbe09522021-09-06 21:17:54 +0800865 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800866 if( ret != 0 )
867 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800868 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800869
lhuang0486cacac2022-01-21 07:34:27 -0800870#if defined(MBEDTLS_SSL_ALPN)
871 ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
872 if( ret != 0 )
873 return( ret );
874 p += output_len;
875#endif /* MBEDTLS_SSL_ALPN */
876
Jerry Yubc20bdd2021-08-24 15:59:48 +0800877#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800878
Jerry Yub925f212022-01-12 11:17:02 +0800879 /*
880 * Add the extensions related to (EC)DHE ephemeral key establishment only if
881 * enabled as per the configuration.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800882 */
Jerry Yuf46b0162022-01-11 16:28:00 +0800883 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
884 {
885 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
886 if( ret != 0 )
887 return( ret );
888 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800889
Jerry Yuf46b0162022-01-11 16:28:00 +0800890 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
891 if( ret != 0 )
892 return( ret );
893 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800894
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yuf46b0162022-01-11 16:28:00 +0800896 if( ret != 0 )
897 return( ret );
898 p += output_len;
899 }
Jerry Yubc20bdd2021-08-24 15:59:48 +0800900#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
901
Xiaofei Bai15a56812021-11-05 10:52:12 +0000902#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai58afdba2021-11-09 03:10:05 +0000903 /* Write server name extension */
Xiaofei Bai15a56812021-11-05 10:52:12 +0000904 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
905 if( ret != 0 )
906 return( ret );
907 p += output_len;
908#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
909
Jerry Yubc20bdd2021-08-24 15:59:48 +0800910 /* Add more extensions here */
911
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800912 /* Write the length of the list of extensions. */
Xiaofei Baieef15042021-11-18 07:29:56 +0000913 extensions_len = p - p_extensions_len - 2;
914 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800916 extensions_len ) );
Xiaofei Baieef15042021-11-18 07:29:56 +0000917 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800918
Xiaofei Baid25fab62021-12-02 06:36:27 +0000919 *out_len = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800920 return( 0 );
921}
922
Jerry Yu335aca92021-09-12 20:18:56 +0800923static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800924{
Jerry Yu92c6b402021-08-27 16:59:09 +0800925 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
926 return( 0 );
927}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800928
Jerry Yu92c6b402021-08-27 16:59:09 +0800929static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
930{
931 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800932
Jerry Yu92c6b402021-08-27 16:59:09 +0800933 if( ssl->conf->f_rng == NULL )
934 {
935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
936 return( MBEDTLS_ERR_SSL_NO_RNG );
937 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800938
Jerry Yu92c6b402021-08-27 16:59:09 +0800939 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
940 ssl->handshake->randbytes,
Jerry Yue6d7e5c2021-10-26 10:44:32 +0800941 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800942 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800943 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800944 return( ret );
945 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800946
Ronald Cron49ad6192021-11-24 16:25:31 +0100947#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
948 /*
949 * Create a session identifier for the purpose of middlebox compatibility
950 * only if one has not been created already.
951 */
952 if( ssl->session_negotiate->id_len == 0 )
953 {
954 /* Creating a session id with 32 byte length */
955 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
956 ssl->session_negotiate->id, 32 ) ) != 0 )
957 {
958 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
959 return( ret );
960 }
961 ssl->session_negotiate->id_len = 32;
962 }
963#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
964
Jerry Yu6f13f642021-08-26 17:18:15 +0800965 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800966}
967
Jerry Yu92c6b402021-08-27 16:59:09 +0800968/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800969 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800970 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800971 */
972static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800973{
Jerry Yu92c6b402021-08-27 16:59:09 +0800974 int ret = 0;
975 unsigned char *buf;
976 size_t buf_len, msg_len;
977
978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
979
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800980 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800981
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800982 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
983 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
984 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800985
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800986 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800987 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800988 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800989
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800990 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
991 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800992 msg_len );
993 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800994
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800995 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
996 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
997 buf_len,
998 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800999
1000cleanup:
1001
1002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1003 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001004}
1005
Jerry Yu687101b2021-09-14 16:03:56 +08001006/*
Jerry Yu4a173382021-10-11 21:45:31 +08001007 * Functions for parsing and processing Server Hello
Jerry Yue1b9c292021-09-10 10:08:31 +08001008 */
Jerry Yu7a186a02021-10-15 18:46:14 +08001009/* Returns a negative value on failure, and otherwise
Jerry Yub85277e2021-10-13 13:36:05 +08001010 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1011 * - SSL_SERVER_HELLO_COORDINATE_HRR
XiaokangQian51eff222021-12-10 10:33:56 +00001012 * to indicate which message is expected and to be parsed next.
1013 */
Jerry Yub85277e2021-10-13 13:36:05 +08001014#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
1015#define SSL_SERVER_HELLO_COORDINATE_HRR 1
1016static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
1017 const unsigned char *buf,
1018 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001019{
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001020 static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
Jerry Yue1b9c292021-09-10 10:08:31 +08001021 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
1022 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
1023 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
1024 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
1025
1026 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1027 *
Jerry Yu4a173382021-10-11 21:45:31 +08001028 * Server Hello and HRR are only distinguished by Random set to the
Jerry Yue1b9c292021-09-10 10:08:31 +08001029 * special value of the SHA-256 of "HelloRetryRequest".
1030 *
1031 * struct {
1032 * ProtocolVersion legacy_version = 0x0303;
1033 * Random random;
1034 * opaque legacy_session_id_echo<0..32>;
1035 * CipherSuite cipher_suite;
1036 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001037 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001038 * } ServerHello;
1039 *
1040 */
Jerry Yub85277e2021-10-13 13:36:05 +08001041 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001042
1043 if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001044 {
Jerry Yub85277e2021-10-13 13:36:05 +08001045 return( SSL_SERVER_HELLO_COORDINATE_HRR );
Jerry Yue1b9c292021-09-10 10:08:31 +08001046 }
1047
Jerry Yub85277e2021-10-13 13:36:05 +08001048 return( SSL_SERVER_HELLO_COORDINATE_HELLO );
Jerry Yue1b9c292021-09-10 10:08:31 +08001049}
1050
Jerry Yu745bb612021-10-13 22:01:04 +08001051/* Fetch and preprocess
1052 * Returns a negative value on failure, and otherwise
1053 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
1054 * - SSL_SERVER_HELLO_COORDINATE_HRR
1055 */
Jerry Yub85277e2021-10-13 13:36:05 +08001056static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
1057 unsigned char **buf,
1058 size_t *buf_len )
Jerry Yue1b9c292021-09-10 10:08:31 +08001059{
Jerry Yu4a173382021-10-11 21:45:31 +08001060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001061
XiaokangQian355e09a2022-01-20 11:14:50 +00001062 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1063 MBEDTLS_SSL_HS_SERVER_HELLO,
1064 buf, buf_len ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001065
Jerry Yub85277e2021-10-13 13:36:05 +08001066 ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
1067 switch( ret )
Jerry Yue1b9c292021-09-10 10:08:31 +08001068 {
Jerry Yu745bb612021-10-13 22:01:04 +08001069 case SSL_SERVER_HELLO_COORDINATE_HELLO:
1070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
1071 break;
1072 case SSL_SERVER_HELLO_COORDINATE_HRR:
1073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
XiaokangQian16acd4b2022-01-14 07:35:47 +00001074 /* If a client receives a second
1075 * HelloRetryRequest in the same connection (i.e., where the ClientHello
1076 * was itself in response to a HelloRetryRequest), it MUST abort the
1077 * handshake with an "unexpected_message" alert.
1078 */
XiaokangQiand9e068e2022-01-18 06:23:32 +00001079 if( ssl->handshake->hello_retry_request_count > 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001080 {
1081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Multiple HRRs received" ) );
1082 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1083 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1084 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1085 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001086 /*
1087 * Clients must abort the handshake with an "illegal_parameter"
1088 * alert if the HelloRetryRequest would not result in any change
1089 * in the ClientHello.
1090 * In a PSK only key exchange that what we expect.
1091 */
1092 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1093 {
1094 MBEDTLS_SSL_DEBUG_MSG( 1,
1095 ( "Unexpected HRR in pure PSK key exchange." ) );
1096 MBEDTLS_SSL_PEND_FATAL_ALERT(
1097 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1098 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1099 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1100 }
XiaokangQian16acd4b2022-01-14 07:35:47 +00001101
XiaokangQiand9e068e2022-01-18 06:23:32 +00001102 ssl->handshake->hello_retry_request_count++;
XiaokangQian16acd4b2022-01-14 07:35:47 +00001103
Jerry Yu745bb612021-10-13 22:01:04 +08001104 break;
Jerry Yue1b9c292021-09-10 10:08:31 +08001105 }
1106
1107cleanup:
1108
1109 return( ret );
1110}
1111
Jerry Yu4a173382021-10-11 21:45:31 +08001112static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
1113 const unsigned char **buf,
1114 const unsigned char *end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001115{
1116 const unsigned char *p = *buf;
Jerry Yu4a173382021-10-11 21:45:31 +08001117 size_t legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001118
Jerry Yude4fb2c2021-09-19 18:05:08 +08001119 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yu4a173382021-10-11 21:45:31 +08001120 legacy_session_id_echo_len = *p++ ;
Jerry Yue1b9c292021-09-10 10:08:31 +08001121
Jerry Yu4a173382021-10-11 21:45:31 +08001122 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001123
1124 /* legacy_session_id_echo */
Jerry Yu4a173382021-10-11 21:45:31 +08001125 if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1126 memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001127 {
Jerry Yue1b9c292021-09-10 10:08:31 +08001128 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1129 ssl->session_negotiate->id,
1130 ssl->session_negotiate->id_len );
1131 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
Jerry Yu4a173382021-10-11 21:45:31 +08001132 legacy_session_id_echo_len );
1133
1134 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1135 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1136
Jerry Yue1b9c292021-09-10 10:08:31 +08001137 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1138 }
1139
Jerry Yu4a173382021-10-11 21:45:31 +08001140 p += legacy_session_id_echo_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001141 *buf = p;
1142
1143 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
Jerry Yu4a173382021-10-11 21:45:31 +08001144 ssl->session_negotiate->id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001145 return( 0 );
1146}
1147
Jerry Yu4a173382021-10-11 21:45:31 +08001148static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1149 int cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001150{
Jerry Yu4a173382021-10-11 21:45:31 +08001151 const int *ciphersuite_list = ssl->conf->ciphersuite_list;
1152
Jerry Yue1b9c292021-09-10 10:08:31 +08001153 /* Check whether we have offered this ciphersuite */
Jerry Yu4a173382021-10-11 21:45:31 +08001154 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yue1b9c292021-09-10 10:08:31 +08001155 {
Jerry Yu4a173382021-10-11 21:45:31 +08001156 if( ciphersuite_list[i] == cipher_suite )
Jerry Yue1b9c292021-09-10 10:08:31 +08001157 {
1158 return( 1 );
1159 }
1160 }
1161 return( 0 );
1162}
1163
1164/* Parse ServerHello message and configure context
1165 *
1166 * struct {
1167 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1168 * Random random;
1169 * opaque legacy_session_id_echo<0..32>;
1170 * CipherSuite cipher_suite;
1171 * uint8 legacy_compression_method = 0;
Jerry Yub85277e2021-10-13 13:36:05 +08001172 * Extension extensions<6..2^16-1>;
Jerry Yue1b9c292021-09-10 10:08:31 +08001173 * } ServerHello;
1174 */
Jerry Yuc068b662021-10-11 22:30:19 +08001175static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
1176 const unsigned char *buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001177 const unsigned char *end,
1178 int is_hrr )
Jerry Yue1b9c292021-09-10 10:08:31 +08001179{
Jerry Yub85277e2021-10-13 13:36:05 +08001180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue1b9c292021-09-10 10:08:31 +08001181 const unsigned char *p = buf;
XiaokangQian355e09a2022-01-20 11:14:50 +00001182 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yub85277e2021-10-13 13:36:05 +08001183 size_t extensions_len;
1184 const unsigned char *extensions_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001185 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001186 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001187 int supported_versions_ext_found = 0;
XiaokangQianb119a352022-01-26 03:29:10 +00001188 int fatal_alert = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001189
1190 /*
1191 * Check there is space for minimal fields
1192 *
1193 * - legacy_version ( 2 bytes)
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001194 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
Jerry Yue1b9c292021-09-10 10:08:31 +08001195 * - legacy_session_id_echo ( 1 byte ), minimum size
1196 * - cipher_suite ( 2 bytes)
1197 * - legacy_compression_method ( 1 byte )
1198 */
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001199 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001200
1201 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
Jerry Yue1b9c292021-09-10 10:08:31 +08001202 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1203
Jerry Yu4a173382021-10-11 21:45:31 +08001204 /* ...
Jerry Yub85277e2021-10-13 13:36:05 +08001205 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
Jerry Yu4a173382021-10-11 21:45:31 +08001206 * ...
1207 * with ProtocolVersion defined as:
1208 * uint16 ProtocolVersion;
1209 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001210 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1211 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1212 {
1213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1214 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1215 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQiand59be772022-01-24 10:12:51 +00001216 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1217 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001218 }
1219 p += 2;
Jerry Yue1b9c292021-09-10 10:08:31 +08001220
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001221 /* ...
Jerry Yu4a173382021-10-11 21:45:31 +08001222 * Random random;
1223 * ...
1224 * with Random defined as:
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001225 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu4a173382021-10-11 21:45:31 +08001226 */
XiaokangQian53f20b72022-01-18 10:47:33 +00001227 if( !is_hrr )
1228 {
XiaokangQian355e09a2022-01-20 11:14:50 +00001229 memcpy( &handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
XiaokangQian53f20b72022-01-18 10:47:33 +00001230 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1231 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1232 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1233 }
Jerry Yue6d7e5c2021-10-26 10:44:32 +08001234 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
Jerry Yue1b9c292021-09-10 10:08:31 +08001235
Jerry Yu4a173382021-10-11 21:45:31 +08001236 /* ...
1237 * opaque legacy_session_id_echo<0..32>;
1238 * ...
1239 */
1240 if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001241 {
XiaokangQian52da5582022-01-26 09:49:29 +00001242 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001243 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001244 }
1245
Jerry Yu4a173382021-10-11 21:45:31 +08001246 /* ...
1247 * CipherSuite cipher_suite;
1248 * ...
1249 * with CipherSuite defined as:
1250 * uint8 CipherSuite[2];
1251 */
1252 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001253 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1254 p += 2;
1255
Jerry Yu4a173382021-10-11 21:45:31 +08001256
XiaokangQian355e09a2022-01-20 11:14:50 +00001257 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu4a173382021-10-11 21:45:31 +08001258 /*
1259 * Check whether this ciphersuite is supported and offered.
1260 * Via the force_ciphersuite version we may have instructed the client
1261 * to use a different ciphersuite.
1262 */
Jerry Yu4a173382021-10-11 21:45:31 +08001263 if( ciphersuite_info == NULL ||
1264 ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001265 {
XiaokangQian52da5582022-01-26 09:49:29 +00001266 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
Jerry Yue1b9c292021-09-10 10:08:31 +08001267 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001268 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001269 * If we received an HRR before and that the proposed selected
1270 * ciphersuite in this server hello is not the same as the one
1271 * proposed in the HRR, we abort the handshake and send an
1272 * "illegal_parameter" alert.
XiaokangQian53f20b72022-01-18 10:47:33 +00001273 */
XiaokangQian355e09a2022-01-20 11:14:50 +00001274 else if( ( !is_hrr ) && ( handshake->hello_retry_request_count > 0 ) &&
1275 ( cipher_suite != ssl->session_negotiate->ciphersuite ) )
XiaokangQian53f20b72022-01-18 10:47:33 +00001276 {
XiaokangQian52da5582022-01-26 09:49:29 +00001277 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQian355e09a2022-01-20 11:14:50 +00001278 }
XiaokangQian53f20b72022-01-18 10:47:33 +00001279
XiaokangQian52da5582022-01-26 09:49:29 +00001280 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQian355e09a2022-01-20 11:14:50 +00001281 {
1282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid ciphersuite(%04x) parameter",
1283 cipher_suite ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001284 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001285 }
Jerry Yue1b9c292021-09-10 10:08:31 +08001286
Jerry Yu4a173382021-10-11 21:45:31 +08001287 /* Configure ciphersuites */
1288 mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
1289
XiaokangQian355e09a2022-01-20 11:14:50 +00001290 handshake->ciphersuite_info = ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001291 ssl->session_negotiate->ciphersuite = cipher_suite;
1292
Jerry Yue1b9c292021-09-10 10:08:31 +08001293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1294 cipher_suite, ciphersuite_info->name ) );
1295
1296#if defined(MBEDTLS_HAVE_TIME)
1297 ssl->session_negotiate->start = time( NULL );
1298#endif /* MBEDTLS_HAVE_TIME */
1299
Jerry Yu4a173382021-10-11 21:45:31 +08001300 /* ...
1301 * uint8 legacy_compression_method = 0;
1302 * ...
Jerry Yue1b9c292021-09-10 10:08:31 +08001303 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001304 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001305 if( p[0] != 0 )
1306 {
Jerry Yub85277e2021-10-13 13:36:05 +08001307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001308 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001309 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001310 }
1311 p++;
1312
Jerry Yub85277e2021-10-13 13:36:05 +08001313 /* ...
1314 * Extension extensions<6..2^16-1>;
1315 * ...
Jerry Yu4a173382021-10-11 21:45:31 +08001316 * struct {
1317 * ExtensionType extension_type; (2 bytes)
1318 * opaque extension_data<0..2^16-1>;
1319 * } Extension;
1320 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001321 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yu4a173382021-10-11 21:45:31 +08001322 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001323 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001324
Jerry Yu4a173382021-10-11 21:45:31 +08001325 /* Check extensions do not go beyond the buffer of data. */
1326 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1327 extensions_end = p + extensions_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001328
Jerry Yu4a173382021-10-11 21:45:31 +08001329 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001330
Jerry Yu4a173382021-10-11 21:45:31 +08001331 while( p < extensions_end )
Jerry Yue1b9c292021-09-10 10:08:31 +08001332 {
1333 unsigned int extension_type;
1334 size_t extension_data_len;
XiaokangQian43550bd2022-01-21 04:32:58 +00001335 const unsigned char *extension_data_end;
Jerry Yue1b9c292021-09-10 10:08:31 +08001336
Jerry Yu4a173382021-10-11 21:45:31 +08001337 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001338 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1339 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1340 p += 4;
1341
Jerry Yu4a173382021-10-11 21:45:31 +08001342 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian43550bd2022-01-21 04:32:58 +00001343 extension_data_end = p + extension_data_len;
Jerry Yue1b9c292021-09-10 10:08:31 +08001344
1345 switch( extension_type )
1346 {
XiaokangQianb851da82022-01-14 04:03:11 +00001347 case MBEDTLS_TLS_EXT_COOKIE:
1348
XiaokangQiand9e068e2022-01-18 06:23:32 +00001349 if( !is_hrr )
1350 {
XiaokangQian52da5582022-01-26 09:49:29 +00001351 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001352 goto cleanup;
XiaokangQiand9e068e2022-01-18 06:23:32 +00001353 }
1354
XiaokangQian43550bd2022-01-21 04:32:58 +00001355 ret = ssl_tls13_parse_cookie_ext( ssl,
1356 p, extension_data_end );
1357 if( ret != 0 )
XiaokangQianb851da82022-01-14 04:03:11 +00001358 {
XiaokangQian43550bd2022-01-21 04:32:58 +00001359 MBEDTLS_SSL_DEBUG_RET( 1,
1360 "ssl_tls13_parse_cookie_ext",
1361 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001362 goto cleanup;
XiaokangQianb851da82022-01-14 04:03:11 +00001363 }
XiaokangQianb851da82022-01-14 04:03:11 +00001364 break;
XiaokangQianb851da82022-01-14 04:03:11 +00001365
Jerry Yue1b9c292021-09-10 10:08:31 +08001366 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
XiaokangQian78b1fa72022-01-19 06:56:30 +00001367 supported_versions_ext_found = 1;
Jerry Yue1b9c292021-09-10 10:08:31 +08001368 MBEDTLS_SSL_DEBUG_MSG( 3,
1369 ( "found supported_versions extension" ) );
1370
Jerry Yuc068b662021-10-11 22:30:19 +08001371 ret = ssl_tls13_parse_supported_versions_ext( ssl,
Jerry Yub85277e2021-10-13 13:36:05 +08001372 p,
XiaokangQian43550bd2022-01-21 04:32:58 +00001373 extension_data_end );
Jerry Yue1b9c292021-09-10 10:08:31 +08001374 if( ret != 0 )
XiaokangQiand59be772022-01-24 10:12:51 +00001375 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001376 break;
1377
1378 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001381
XiaokangQian52da5582022-01-26 09:49:29 +00001382 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001383 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001384
1385#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1386 case MBEDTLS_TLS_EXT_KEY_SHARE:
1387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
XiaokangQiand59be772022-01-24 10:12:51 +00001388 if( ! mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1389 {
XiaokangQian52da5582022-01-26 09:49:29 +00001390 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001391 goto cleanup;
1392 }
1393
XiaokangQian53f20b72022-01-18 10:47:33 +00001394 if( is_hrr )
XiaokangQiand9e068e2022-01-18 06:23:32 +00001395 ret = ssl_tls13_parse_hrr_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001396 p, extension_data_end );
XiaokangQian53f20b72022-01-18 10:47:33 +00001397 else
XiaokangQianb851da82022-01-14 04:03:11 +00001398 ret = ssl_tls13_parse_key_share_ext( ssl,
XiaokangQian43550bd2022-01-21 04:32:58 +00001399 p, extension_data_end );
XiaokangQianb851da82022-01-14 04:03:11 +00001400 if( ret != 0 )
Jerry Yue1b9c292021-09-10 10:08:31 +08001401 {
1402 MBEDTLS_SSL_DEBUG_RET( 1,
Jerry Yu4a173382021-10-11 21:45:31 +08001403 "ssl_tls13_parse_key_share_ext",
Jerry Yue1b9c292021-09-10 10:08:31 +08001404 ret );
XiaokangQiand59be772022-01-24 10:12:51 +00001405 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001406 }
1407 break;
1408#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1409
1410 default:
Jerry Yu4a173382021-10-11 21:45:31 +08001411 MBEDTLS_SSL_DEBUG_MSG(
1412 3,
1413 ( "unknown extension found: %u ( ignoring )",
1414 extension_type ) );
1415
XiaokangQian52da5582022-01-26 09:49:29 +00001416 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
XiaokangQiand59be772022-01-24 10:12:51 +00001417 goto cleanup;
Jerry Yue1b9c292021-09-10 10:08:31 +08001418 }
1419
1420 p += extension_data_len;
1421 }
1422
XiaokangQian78b1fa72022-01-19 06:56:30 +00001423 if( !supported_versions_ext_found )
XiaokangQian53f20b72022-01-18 10:47:33 +00001424 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "supported_versions not found" ) );
XiaokangQian52da5582022-01-26 09:49:29 +00001426 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
XiaokangQiand59be772022-01-24 10:12:51 +00001427 goto cleanup;
XiaokangQian53f20b72022-01-18 10:47:33 +00001428 }
1429
XiaokangQiand59be772022-01-24 10:12:51 +00001430cleanup:
1431
XiaokangQian52da5582022-01-26 09:49:29 +00001432 if( fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT )
XiaokangQiand59be772022-01-24 10:12:51 +00001433 {
1434 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1435 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1436 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1437 }
XiaokangQian52da5582022-01-26 09:49:29 +00001438 else if ( fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER )
XiaokangQiand59be772022-01-24 10:12:51 +00001439 {
1440 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1441 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1442 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1443 }
1444 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001445}
1446
XiaokangQian355e09a2022-01-20 11:14:50 +00001447static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001448{
Jerry Yub85277e2021-10-13 13:36:05 +08001449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu0b177842021-09-05 19:41:30 +08001450 mbedtls_ssl_key_set traffic_keys;
Jerry Yu4a173382021-10-11 21:45:31 +08001451 mbedtls_ssl_transform *transform_handshake = NULL;
Jerry Yub85277e2021-10-13 13:36:05 +08001452 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu0b177842021-09-05 19:41:30 +08001453
Jerry Yub85277e2021-10-13 13:36:05 +08001454 /* Determine the key exchange mode:
1455 * 1) If both the pre_shared_key and key_share extensions were received
1456 * then the key exchange mode is PSK with EPHEMERAL.
1457 * 2) If only the pre_shared_key extension was received then the key
1458 * exchange mode is PSK-only.
1459 * 3) If only the key_share extension was received then the key
1460 * exchange mode is EPHEMERAL-only.
Jerry Yu0b177842021-09-05 19:41:30 +08001461 */
Jerry Yub85277e2021-10-13 13:36:05 +08001462 switch( handshake->extensions_present &
1463 ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
Jerry Yu0b177842021-09-05 19:41:30 +08001464 {
Jerry Yu745bb612021-10-13 22:01:04 +08001465 /* Only the pre_shared_key extension was received */
1466 case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001467 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Jerry Yu745bb612021-10-13 22:01:04 +08001468 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001469
Jerry Yu745bb612021-10-13 22:01:04 +08001470 /* Only the key_share extension was received */
1471 case MBEDTLS_SSL_EXT_KEY_SHARE:
Xiaofei Baid25fab62021-12-02 06:36:27 +00001472 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001473 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001474
Jerry Yu745bb612021-10-13 22:01:04 +08001475 /* Both the pre_shared_key and key_share extensions were received */
1476 case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
Xiaofei Baid25fab62021-12-02 06:36:27 +00001477 handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Jerry Yu745bb612021-10-13 22:01:04 +08001478 break;
Jerry Yub85277e2021-10-13 13:36:05 +08001479
Jerry Yu745bb612021-10-13 22:01:04 +08001480 /* Neither pre_shared_key nor key_share extension was received */
1481 default:
1482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1483 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1484 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001485 }
1486
1487 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1488 *
1489 * TODO: We don't have to do this in case we offered 0-RTT and the
1490 * server accepted it. In this case, we could skip generating
1491 * the early secret. */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001492 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001493 if( ret != 0 )
1494 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_key_schedule_stage_early_data",
Jerry Yu0b177842021-09-05 19:41:30 +08001496 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001497 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001498 }
1499
1500 /* Compute handshake secret */
Jerry Yuf0ac2352021-10-11 17:47:07 +08001501 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
Jerry Yu0b177842021-09-05 19:41:30 +08001502 if( ret != 0 )
1503 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001505 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001506 }
1507
1508 /* Next evolution in key schedule: Establish handshake secret and
1509 * key material. */
Jerry Yuc068b662021-10-11 22:30:19 +08001510 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
Jerry Yu0b177842021-09-05 19:41:30 +08001511 if( ret != 0 )
1512 {
Jerry Yuc068b662021-10-11 22:30:19 +08001513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1514 ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001515 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001516 }
1517
Jerry Yub85277e2021-10-13 13:36:05 +08001518 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
Jerry Yu0b177842021-09-05 19:41:30 +08001519 if( transform_handshake == NULL )
Jerry Yu4a173382021-10-11 21:45:31 +08001520 {
1521 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1522 goto cleanup;
1523 }
Jerry Yu0b177842021-09-05 19:41:30 +08001524
1525 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1526 ssl->conf->endpoint,
1527 ssl->session_negotiate->ciphersuite,
1528 &traffic_keys,
1529 ssl );
1530 if( ret != 0 )
1531 {
1532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
Jerry Yu4a173382021-10-11 21:45:31 +08001533 goto cleanup;
Jerry Yu0b177842021-09-05 19:41:30 +08001534 }
1535
Jerry Yub85277e2021-10-13 13:36:05 +08001536 handshake->transform_handshake = transform_handshake;
1537 mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
Jerry Yu0b177842021-09-05 19:41:30 +08001538
1539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1540 ssl->session_in = ssl->session_negotiate;
1541
1542 /*
1543 * State machine update
1544 */
1545 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1546
Jerry Yu4a173382021-10-11 21:45:31 +08001547cleanup:
1548
Jerry Yu0b177842021-09-05 19:41:30 +08001549 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
Jerry Yu4a173382021-10-11 21:45:31 +08001550 if( ret != 0 )
1551 {
Jerry Yub85277e2021-10-13 13:36:05 +08001552 mbedtls_free( transform_handshake );
Jerry Yuc068b662021-10-11 22:30:19 +08001553
Jerry Yu4a173382021-10-11 21:45:31 +08001554 MBEDTLS_SSL_PEND_FATAL_ALERT(
1555 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1556 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1557 }
1558 return( ret );
Jerry Yue1b9c292021-09-10 10:08:31 +08001559}
1560
XiaokangQian355e09a2022-01-20 11:14:50 +00001561static int ssl_tls13_postprocess_hrr( mbedtls_ssl_context *ssl )
XiaokangQian647719a2021-12-07 09:16:29 +00001562{
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001563#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian647719a2021-12-07 09:16:29 +00001564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1565
XiaokangQian647719a2021-12-07 09:16:29 +00001566#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1567 /* If not offering early data, the client sends a dummy CCS record
1568 * immediately before its second flight. This may either be before
XiaokangQian51eff222021-12-10 10:33:56 +00001569 * its second ClientHello or before its encrypted handshake flight.
1570 */
XiaokangQian647719a2021-12-07 09:16:29 +00001571 mbedtls_ssl_handshake_set_state( ssl,
1572 MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO );
1573#else
1574 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1575#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1576
XiaokangQian78b1fa72022-01-19 06:56:30 +00001577 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
XiaokangQian647719a2021-12-07 09:16:29 +00001578
XiaokangQian78b1fa72022-01-19 06:56:30 +00001579 /*
XiaokangQian355e09a2022-01-20 11:14:50 +00001580 * We are going to re-generate a shared secret corresponding to the group
1581 * selected by the server, which is different from the group for which we
1582 * generated a shared secret in the first client hello.
1583 * Thus, reset the shared secret.
XiaokangQian51eff222021-12-10 10:33:56 +00001584 */
XiaokangQian16acd4b2022-01-14 07:35:47 +00001585 ret = ssl_tls13_reset_key_share( ssl );
XiaokangQian647719a2021-12-07 09:16:29 +00001586 if( ret != 0 )
1587 return( ret );
XiaokangQian43550bd2022-01-21 04:32:58 +00001588#else
1589 ((void) ssl);
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001590#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian647719a2021-12-07 09:16:29 +00001591
1592 return( 0 );
1593}
1594
Jerry Yue1b9c292021-09-10 10:08:31 +08001595/*
Jerry Yu4a173382021-10-11 21:45:31 +08001596 * Wait and parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001597 * Handler for MBEDTLS_SSL_SERVER_HELLO
1598 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001599static int ssl_tls13_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001600{
Jerry Yu4a173382021-10-11 21:45:31 +08001601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian647719a2021-12-07 09:16:29 +00001602 unsigned char *buf = NULL;
1603 size_t buf_len = 0;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001604 int is_hrr = 0;
Jerry Yue1b9c292021-09-10 10:08:31 +08001605
1606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1607
1608 /* Coordination step
1609 * - Fetch record
1610 * - Make sure it's either a ServerHello or a HRR.
1611 * - Switch processing routine in case of HRR
1612 */
Jerry Yue1b9c292021-09-10 10:08:31 +08001613 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1614 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1615
XiaokangQian16acd4b2022-01-14 07:35:47 +00001616 ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001617 if( ret < 0 )
XiaokangQian16acd4b2022-01-14 07:35:47 +00001618 goto cleanup;
1619 else
XiaokangQiand9e068e2022-01-18 06:23:32 +00001620 is_hrr = ( ret == SSL_SERVER_HELLO_COORDINATE_HRR );
XiaokangQiand59be772022-01-24 10:12:51 +00001621
XiaokangQianb851da82022-01-14 04:03:11 +00001622 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
XiaokangQiand9e068e2022-01-18 06:23:32 +00001623 buf + buf_len,
1624 is_hrr ) );
1625 if( is_hrr )
XiaokangQian647719a2021-12-07 09:16:29 +00001626 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_reset_transcript_for_hrr( ssl ) );
1627
XiaokangQianb851da82022-01-14 04:03:11 +00001628 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1629 MBEDTLS_SSL_HS_SERVER_HELLO,
1630 buf, buf_len );
XiaokangQian647719a2021-12-07 09:16:29 +00001631
XiaokangQiand9e068e2022-01-18 06:23:32 +00001632 if( is_hrr )
XiaokangQian355e09a2022-01-20 11:14:50 +00001633 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_hrr( ssl ) );
XiaokangQiand9e068e2022-01-18 06:23:32 +00001634 else
XiaokangQian355e09a2022-01-20 11:14:50 +00001635 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001636
1637cleanup:
XiaokangQiana9090612022-01-27 03:48:27 +00001638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s ( %s )", __func__,
1639 is_hrr?"HelloRetryRequest":"ServerHello" ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001640 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001641}
1642
1643/*
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001644 *
1645 * EncryptedExtensions message
1646 *
1647 * The EncryptedExtensions message contains any extensions which
1648 * should be protected, i.e., any which are not needed to establish
1649 * the cryptographic context.
Jerry Yu687101b2021-09-14 16:03:56 +08001650 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001651
1652/*
1653 * Overview
1654 */
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001655
1656/* Main entry point; orchestrates the other functions */
XiaokangQian97799ac2021-10-11 10:05:54 +00001657static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001658
XiaokangQian97799ac2021-10-11 10:05:54 +00001659static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1660 const unsigned char *buf,
1661 const unsigned char *end );
1662static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001663
1664/*
XiaokangQianc1fe0002021-09-16 03:02:14 +00001665 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001666 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001667static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001668{
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001669 int ret;
1670 unsigned char *buf;
1671 size_t buf_len;
1672
1673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
1674
Xiaofei Bai746f9482021-11-12 08:53:56 +00001675 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianab7f50d2021-10-21 06:23:29 +00001676 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001677 &buf, &buf_len ) );
1678
1679 /* Process the message contents */
XiaokangQian97799ac2021-10-11 10:05:54 +00001680 MBEDTLS_SSL_PROC_CHK(
XiaokangQian8db25ff2021-10-13 05:56:18 +00001681 ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001682
Xiaofei Bai746f9482021-11-12 08:53:56 +00001683 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
XiaokangQianab7f50d2021-10-21 06:23:29 +00001684 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001685
XiaokangQian97799ac2021-10-11 10:05:54 +00001686 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001687
1688cleanup:
1689
1690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
1691 return( ret );
1692
1693}
1694
XiaokangQian08da26c2021-10-09 10:12:11 +00001695/* Parse EncryptedExtensions message
1696 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001697 * Extension extensions<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001698 * } EncryptedExtensions;
1699 */
XiaokangQian97799ac2021-10-11 10:05:54 +00001700static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
1701 const unsigned char *buf,
1702 const unsigned char *end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001703{
1704 int ret = 0;
XiaokangQian08da26c2021-10-09 10:12:11 +00001705 size_t extensions_len;
XiaokangQian140f0452021-10-08 08:05:53 +00001706 const unsigned char *p = buf;
XiaokangQian8db25ff2021-10-13 05:56:18 +00001707 const unsigned char *extensions_end;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001708
XiaokangQian08da26c2021-10-09 10:12:11 +00001709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian97799ac2021-10-11 10:05:54 +00001710 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001711 p += 2;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001712
XiaokangQian97799ac2021-10-11 10:05:54 +00001713 MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
XiaokangQian8db25ff2021-10-13 05:56:18 +00001714 extensions_end = p + extensions_len;
1715 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001716
XiaokangQian8db25ff2021-10-13 05:56:18 +00001717 while( p < extensions_end )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001718 {
XiaokangQian08da26c2021-10-09 10:12:11 +00001719 unsigned int extension_type;
1720 size_t extension_data_len;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001721
XiaokangQian08da26c2021-10-09 10:12:11 +00001722 /*
1723 * struct {
XiaokangQian97799ac2021-10-11 10:05:54 +00001724 * ExtensionType extension_type; (2 bytes)
1725 * opaque extension_data<0..2^16-1>;
XiaokangQian08da26c2021-10-09 10:12:11 +00001726 * } Extension;
1727 */
XiaokangQian8db25ff2021-10-13 05:56:18 +00001728 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian08da26c2021-10-09 10:12:11 +00001729 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1730 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1731 p += 4;
1732
XiaokangQian8db25ff2021-10-13 05:56:18 +00001733 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001734
XiaokangQian97799ac2021-10-11 10:05:54 +00001735 /* The client MUST check EncryptedExtensions for the
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001736 * presence of any forbidden extensions and if any are found MUST abort
XiaokangQian08da26c2021-10-09 10:12:11 +00001737 * the handshake with an "unsupported_extension" alert.
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001738 */
XiaokangQian08da26c2021-10-09 10:12:11 +00001739 switch( extension_type )
1740 {
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001741
XiaokangQian08da26c2021-10-09 10:12:11 +00001742 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
1744 break;
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001745
lhuang0486cacac2022-01-21 07:34:27 -08001746#if defined(MBEDTLS_SSL_ALPN)
1747 case MBEDTLS_TLS_EXT_ALPN:
1748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1749
1750 if( ( ret = ssl_tls13_parse_alpn_ext( ssl, p, (size_t)extension_data_len ) ) != 0 )
1751 {
1752 return( ret );
1753 }
1754
1755 break;
1756#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian08da26c2021-10-09 10:12:11 +00001757 default:
XiaokangQian97799ac2021-10-11 10:05:54 +00001758 MBEDTLS_SSL_DEBUG_MSG(
1759 3, ( "unsupported extension found: %u ", extension_type) );
1760 MBEDTLS_SSL_PEND_FATAL_ALERT(
Jerry Yu7aa71862021-10-28 21:41:30 +08001761 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
XiaokangQian97799ac2021-10-11 10:05:54 +00001762 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
1763 return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
XiaokangQian08da26c2021-10-09 10:12:11 +00001764 }
1765
XiaokangQian08da26c2021-10-09 10:12:11 +00001766 p += extension_data_len;
XiaokangQian97799ac2021-10-11 10:05:54 +00001767 }
XiaokangQian08da26c2021-10-09 10:12:11 +00001768
XiaokangQian97799ac2021-10-11 10:05:54 +00001769 /* Check that we consumed all the message. */
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001770 if( p != end )
XiaokangQian97799ac2021-10-11 10:05:54 +00001771 {
1772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
Jerry Yu7aa71862021-10-28 21:41:30 +08001773 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQian7b2d4ef2021-10-13 10:19:02 +00001774 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian97799ac2021-10-11 10:05:54 +00001775 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001776 }
1777
1778 return( ret );
1779}
1780
XiaokangQian97799ac2021-10-11 10:05:54 +00001781static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
XiaokangQian2d5c72b2021-09-13 07:30:09 +00001782{
Jerry Yua93ac112021-10-27 16:31:48 +08001783#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai746f9482021-11-12 08:53:56 +00001784 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Jerry Yua93ac112021-10-27 16:31:48 +08001785 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1786 else
Jerry Yud2674312021-10-29 10:08:19 +08001787 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yua93ac112021-10-27 16:31:48 +08001788#else
1789 ((void) ssl);
1790 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1791#endif
Jerry Yu687101b2021-09-14 16:03:56 +08001792 return( 0 );
1793}
1794
Jerry Yua93ac112021-10-27 16:31:48 +08001795#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu687101b2021-09-14 16:03:56 +08001796/*
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001797 *
1798 * STATE HANDLING: CertificateRequest
1799 *
Jerry Yud2674312021-10-29 10:08:19 +08001800 */
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001801#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
1802#define SSL_CERTIFICATE_REQUEST_SKIP 1
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001803/* Coordination:
1804 * Deals with the ambiguity of not knowing if a CertificateRequest
1805 * will be sent. Returns a negative code on failure, or
1806 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
1807 * - SSL_CERTIFICATE_REQUEST_SKIP
1808 * indicating if a Certificate Request is expected or not.
1809 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001810static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
Jerry Yud2674312021-10-29 10:08:19 +08001811{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud2674312021-10-29 10:08:19 +08001813
Xiaofei Bai7c8b6a92022-02-08 15:21:13 +00001814 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001815 {
1816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
1817 return( SSL_CERTIFICATE_REQUEST_SKIP );
1818 }
1819
1820 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
Jerry Yud2674312021-10-29 10:08:19 +08001821 {
1822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1823 return( ret );
1824 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001825 ssl->keep_current_message = 1;
Jerry Yud2674312021-10-29 10:08:19 +08001826
1827 if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
1828 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
1829 {
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001830 return( SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST );
Jerry Yud2674312021-10-29 10:08:19 +08001831 }
1832
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001833 return( SSL_CERTIFICATE_REQUEST_SKIP );
1834}
1835
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001836/*
1837 * ssl_tls13_parse_certificate_request()
1838 * Parse certificate request
1839 * struct {
1840 * opaque certificate_request_context<0..2^8-1>;
1841 * Extension extensions<2..2^16-1>;
1842 * } CertificateRequest;
1843 */
1844static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
1845 const unsigned char *buf,
1846 const unsigned char *end )
1847{
1848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1849 const unsigned char *p = buf;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001850 size_t certificate_request_context_len = 0;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001851 size_t extensions_len = 0;
1852 const unsigned char *extensions_end;
1853 unsigned char sig_alg_ext_found = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001854
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001855 /* ...
1856 * opaque certificate_request_context<0..2^8-1>
1857 * ...
1858 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001859 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
1860 certificate_request_context_len = (size_t) p[0];
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001861 p += 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001862
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001863 if( certificate_request_context_len > 0 )
1864 {
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001865 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001866 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
1867 p, certificate_request_context_len );
1868
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001869 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001870 handshake->certificate_request_context =
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001871 mbedtls_calloc( 1, certificate_request_context_len );
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001872 if( handshake->certificate_request_context == NULL )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001873 {
1874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1875 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1876 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001877 memcpy( handshake->certificate_request_context, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001878 certificate_request_context_len );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001879 p += certificate_request_context_len;
1880 }
1881
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001882 /* ...
1883 * Extension extensions<2..2^16-1>;
1884 * ...
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001885 */
1886 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
1887 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1888 p += 2;
1889
1890 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
1891 extensions_end = p + extensions_len;
1892
1893 while( p < extensions_end )
1894 {
1895 unsigned int extension_type;
1896 size_t extension_data_len;
1897
1898 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
1899 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1900 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1901 p += 4;
1902
1903 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
1904
1905 switch( extension_type )
1906 {
1907 case MBEDTLS_TLS_EXT_SIG_ALG:
1908 MBEDTLS_SSL_DEBUG_MSG( 3,
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001909 ( "found signature algorithms extension" ) );
1910 ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p,
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001911 p + extension_data_len );
1912 if( ret != 0 )
1913 return( ret );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001914 if( ! sig_alg_ext_found )
1915 sig_alg_ext_found = 1;
1916 else
1917 {
1918 MBEDTLS_SSL_DEBUG_MSG( 3,
1919 ( "Duplicate signature algorithms extensions found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001920 goto decode_error;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001921 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001922 break;
1923
1924 default:
1925 MBEDTLS_SSL_DEBUG_MSG(
1926 3,
1927 ( "unknown extension found: %u ( ignoring )",
1928 extension_type ) );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001929 break;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001930 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001931 p += extension_data_len;
1932 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001933 /* Check that we consumed all the message. */
1934 if( p != end )
1935 {
1936 MBEDTLS_SSL_DEBUG_MSG( 1,
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001937 ( "CertificateRequest misaligned" ) );
1938 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001939 }
1940 /* Check that we found signature algorithms extension */
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +00001941 if( ! sig_alg_ext_found )
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001942 {
Xiaofei Bai6d42bb42022-01-28 08:52:13 +00001943 MBEDTLS_SSL_DEBUG_MSG( 3,
1944 ( "no signature algorithms extension found" ) );
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001945 goto decode_error;
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001946 }
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001947
Jerry Yu7840f812022-01-29 10:26:51 +08001948 ssl->handshake->client_auth = 1;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001949 return( 0 );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001950
Xiaofei Baic234ecf2022-02-08 09:59:23 +00001951decode_error:
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001952 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1953 MBEDTLS_ERR_SSL_DECODE_ERROR );
1954 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001955}
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001956
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001957/*
1958 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1959 */
1960static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001961{
Xiaofei Baide3f13e2022-01-18 05:47:05 +00001962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001963
1964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1965
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001966 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1967
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001968 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
1969 {
1970 unsigned char *buf;
1971 size_t buf_len;
1972
1973 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1974 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
1975 &buf, &buf_len ) );
1976
1977 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
1978 buf, buf + buf_len ) );
1979
1980 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1981 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
1982 }
Xiaofei Bai69fcd392022-01-20 08:25:00 +00001983 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001984 {
1985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Xiaofei Baif6d36962022-01-16 14:54:35 +00001986 ret = 0;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001987 }
1988 else
1989 {
1990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Xiaofei Bai51f515a2022-02-08 07:28:04 +00001991 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1992 goto cleanup;
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001993 }
1994
1995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yu7840f812022-01-29 10:26:51 +08001996 ssl->handshake->client_auth ? "a" : "no" ) );
Xiaofei Baia0ab7772022-01-16 12:14:45 +00001997
Jerry Yud2674312021-10-29 10:08:19 +08001998 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1999
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002000cleanup:
2001
Xiaofei Baia0ab7772022-01-16 12:14:45 +00002002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2003 return( ret );
Jerry Yud2674312021-10-29 10:08:19 +08002004}
2005
2006/*
Jerry Yu687101b2021-09-14 16:03:56 +08002007 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2008 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002009static int ssl_tls13_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002010{
Xiaofei Bai947571e2021-09-29 09:12:03 +00002011 int ret;
2012
2013 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00002014 if( ret != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00002015 return( ret );
2016
Jerry Yu687101b2021-09-14 16:03:56 +08002017 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
2018 return( 0 );
2019}
2020
2021/*
2022 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2023 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002024static int ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002025{
Jerry Yu30b071c2021-09-12 20:16:03 +08002026 int ret;
2027
2028 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
2029 if( ret != 0 )
2030 return( ret );
2031
Jerry Yu687101b2021-09-14 16:03:56 +08002032 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
2033 return( 0 );
2034}
Jerry Yua93ac112021-10-27 16:31:48 +08002035#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Crond4c64022021-12-06 09:06:46 +01002036
Jerry Yu687101b2021-09-14 16:03:56 +08002037/*
2038 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2039 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002040static int ssl_tls13_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002041{
XiaokangQianac0385c2021-11-03 06:40:11 +00002042 int ret;
2043
XiaokangQianc5c39d52021-11-09 11:55:10 +00002044 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
XiaokangQianac0385c2021-11-03 06:40:11 +00002045 if( ret != 0 )
2046 return( ret );
2047
Ronald Cron49ad6192021-11-24 16:25:31 +01002048#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2049 mbedtls_ssl_handshake_set_state(
2050 ssl,
2051 MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED );
2052#else
XiaokangQianac0385c2021-11-03 06:40:11 +00002053 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron49ad6192021-11-24 16:25:31 +01002054#endif
2055
XiaokangQianac0385c2021-11-03 06:40:11 +00002056 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002057}
2058
2059/*
Ronald Crond4c64022021-12-06 09:06:46 +01002060 * Handler for MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED
2061 */
2062#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2063static int ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
2064{
2065 int ret;
2066
2067 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
2068 if( ret != 0 )
2069 return( ret );
2070
Ronald Crond4c64022021-12-06 09:06:46 +01002071 return( 0 );
2072}
2073#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2074
2075/*
Jerry Yu687101b2021-09-14 16:03:56 +08002076 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2077 */
XiaokangQian74af2a82021-09-22 07:40:30 +00002078static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002079{
XiaokangQian0fa66432021-11-15 03:33:57 +00002080 int ret;
2081
Ronald Cron49ad6192021-11-24 16:25:31 +01002082 mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
2083
XiaokangQian0fa66432021-11-15 03:33:57 +00002084 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
2085 if( ret != 0 )
2086 return( ret );
2087
2088 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
2089 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002090}
2091
2092/*
2093 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2094 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002095static int ssl_tls13_flush_buffers( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002096{
Jerry Yu378254d2021-10-30 21:44:47 +08002097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08002098 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08002099 return( 0 );
2100}
2101
2102/*
2103 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2104 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00002105static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08002106{
Jerry Yu378254d2021-10-30 21:44:47 +08002107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
2108 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
2109
2110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
2111 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
2112
2113 mbedtls_ssl_tls13_handshake_wrapup( ssl );
2114
2115 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
2116 return( 0 );
Jerry Yu687101b2021-09-14 16:03:56 +08002117}
2118
Jerry Yu92c6b402021-08-27 16:59:09 +08002119int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08002120{
Jerry Yu92c6b402021-08-27 16:59:09 +08002121 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08002122
Jerry Yue3b34122021-09-28 17:53:35 +08002123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 client state: %s(%d)",
2124 mbedtls_ssl_states_str( ssl->state ),
2125 ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08002126
2127 switch( ssl->state )
2128 {
2129 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08002130 * ssl->state is initialized as HELLO_REQUEST. It is the same
2131 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08002132 */
2133 case MBEDTLS_SSL_HELLO_REQUEST:
2134 case MBEDTLS_SSL_CLIENT_HELLO:
2135 ret = ssl_tls13_write_client_hello( ssl );
2136 break;
2137
2138 case MBEDTLS_SSL_SERVER_HELLO:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002139 ret = ssl_tls13_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002140 break;
2141
2142 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
XiaokangQian97799ac2021-10-11 10:05:54 +00002143 ret = ssl_tls13_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002144 break;
2145
Jerry Yua93ac112021-10-27 16:31:48 +08002146#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yud2674312021-10-29 10:08:19 +08002147 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
2148 ret = ssl_tls13_process_certificate_request( ssl );
2149 break;
2150
Jerry Yu687101b2021-09-14 16:03:56 +08002151 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002152 ret = ssl_tls13_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002153 break;
2154
2155 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002156 ret = ssl_tls13_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002157 break;
Jerry Yua93ac112021-10-27 16:31:48 +08002158#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu687101b2021-09-14 16:03:56 +08002159
2160 case MBEDTLS_SSL_SERVER_FINISHED:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002161 ret = ssl_tls13_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002162 break;
2163
Jerry Yu687101b2021-09-14 16:03:56 +08002164 case MBEDTLS_SSL_CLIENT_FINISHED:
XiaokangQian74af2a82021-09-22 07:40:30 +00002165 ret = ssl_tls13_write_client_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002166 break;
2167
2168 case MBEDTLS_SSL_FLUSH_BUFFERS:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002169 ret = ssl_tls13_flush_buffers( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08002170 break;
2171
2172 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Xiaofei Bai746f9482021-11-12 08:53:56 +00002173 ret = ssl_tls13_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08002174 break;
2175
Ronald Cron49ad6192021-11-24 16:25:31 +01002176 /*
2177 * Injection of dummy-CCS's for middlebox compatibility
2178 */
2179#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2180 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
XiaokangQian0b56a8f2021-12-22 02:39:32 +00002181 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
Ronald Crond4c64022021-12-06 09:06:46 +01002182 ret = ssl_tls13_write_change_cipher_spec( ssl );
Ronald Cron49ad6192021-11-24 16:25:31 +01002183 break;
2184#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2185
Jerry Yu92c6b402021-08-27 16:59:09 +08002186 default:
2187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2189 }
2190
2191 return( ret );
2192}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08002193
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08002194#endif /* MBEDTLS_SSL_CLI_C */
2195
Ronald Cron6f135e12021-12-08 16:57:54 +01002196#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */