blob: d9c6781592c43b08541733eb56c70feb5fbef326 [file] [log] [blame]
Ronald Cron3d580bf2022-02-18 17:24:56 +01001/*
2 * TLS 1.2 and 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
24#if defined(MBEDTLS_SSL_CLI_C)
25#if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
26
Ronald Cron58b80382022-02-18 18:41:08 +010027#include "mbedtls/platform.h"
Ronald Cron58b80382022-02-18 18:41:08 +010028
Ronald Cron3d580bf2022-02-18 17:24:56 +010029#include <string.h>
30
31#include "mbedtls/debug.h"
32#include "mbedtls/error.h"
Ronald Cron58b80382022-02-18 18:41:08 +010033#if defined(MBEDTLS_HAVE_TIME)
34#include "mbedtls/platform_time.h"
35#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +010036
37#include "ssl_client.h"
38#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010039#include "ssl_tls13_keys.h"
40#include "ssl_debug_helpers.h"
41
Ronald Cronfbd9f992022-03-17 15:22:07 +010042#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020043MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +010044static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
45 unsigned char *buf,
46 const unsigned char *end,
47 size_t *olen )
48{
49 unsigned char *p = buf;
50 size_t hostname_len;
51
52 *olen = 0;
53
54 if( ssl->hostname == NULL )
55 return( 0 );
56
57 MBEDTLS_SSL_DEBUG_MSG( 3,
58 ( "client hello, adding server name extension: %s",
59 ssl->hostname ) );
60
61 hostname_len = strlen( ssl->hostname );
62
63 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
64
65 /*
66 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
67 *
68 * In order to provide any of the server names, clients MAY include an
69 * extension of type "server_name" in the (extended) client hello. The
70 * "extension_data" field of this extension SHALL contain
71 * "ServerNameList" where:
72 *
73 * struct {
74 * NameType name_type;
75 * select (name_type) {
76 * case host_name: HostName;
77 * } name;
78 * } ServerName;
79 *
80 * enum {
81 * host_name(0), (255)
82 * } NameType;
83 *
84 * opaque HostName<1..2^16-1>;
85 *
86 * struct {
87 * ServerName server_name_list<1..2^16-1>
88 * } ServerNameList;
89 *
90 */
91 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
92 p += 2;
93
94 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
95 p += 2;
96
97 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
98 p += 2;
99
100 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
101
102 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
103 p += 2;
104
105 memcpy( p, ssl->hostname, hostname_len );
106
107 *olen = hostname_len + 9;
108
109 return( 0 );
110}
111#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
112
Ronald Cron3d580bf2022-02-18 17:24:56 +0100113#if defined(MBEDTLS_SSL_ALPN)
114/*
Ronald Cron71c23322022-02-18 17:29:39 +0100115 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100116 *
117 * Structure of the application_layer_protocol_negotiation extension in
118 * ClientHello:
119 *
120 * opaque ProtocolName<1..2^8-1>;
121 *
122 * struct {
123 * ProtocolName protocol_name_list<2..2^16-1>
124 * } ProtocolNameList;
125 *
126 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200127MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100128static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
129 unsigned char *buf,
130 const unsigned char *end,
131 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100132{
133 unsigned char *p = buf;
134
135 *out_len = 0;
136
137 if( ssl->conf->alpn_list == NULL )
138 return( 0 );
139
140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
141
142
143 /* Check we have enough space for the extension type (2 bytes), the
144 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
145 */
146 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
147 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
148 /* Skip writing extension and list length for now */
149 p += 6;
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 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
159 {
160 /*
161 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
162 * protocol names is less than 255.
163 */
164 size_t protocol_name_len = strlen( *cur );
165
166 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
167 *p++ = (unsigned char)protocol_name_len;
168 memcpy( p, *cur, protocol_name_len );
169 p += protocol_name_len;
170 }
171
172 *out_len = p - buf;
173
174 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
175 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
176
177 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
178 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
179
180 return( 0 );
181}
182#endif /* MBEDTLS_SSL_ALPN */
183
Ronald Cronfbd9f992022-03-17 15:22:07 +0100184#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
185 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
186/*
187 * Function for writing a supported groups (TLS 1.3) or supported elliptic
188 * curves (TLS 1.2) extension.
189 *
190 * The "extension_data" field of a supported groups extension contains a
191 * "NamedGroupList" value (TLS 1.3 RFC8446):
192 * enum {
193 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
194 * x25519(0x001D), x448(0x001E),
195 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
196 * ffdhe6144(0x0103), ffdhe8192(0x0104),
197 * ffdhe_private_use(0x01FC..0x01FF),
198 * ecdhe_private_use(0xFE00..0xFEFF),
199 * (0xFFFF)
200 * } NamedGroup;
201 * struct {
202 * NamedGroup named_group_list<2..2^16-1>;
203 * } NamedGroupList;
204 *
205 * The "extension_data" field of a supported elliptic curves extension contains
206 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
207 * enum {
208 * deprecated(1..22),
209 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
210 * x25519(29), x448(30),
211 * reserved (0xFE00..0xFEFF),
212 * deprecated(0xFF01..0xFF02),
213 * (0xFFFF)
214 * } NamedCurve;
215 * struct {
216 * NamedCurve named_curve_list<2..2^16-1>
217 * } NamedCurveList;
218 *
219 * The TLS 1.3 supported groups extension was defined to be a compatible
220 * generalization of the TLS 1.2 supported elliptic curves extension. They both
221 * share the same extension identifier.
222 *
223 * DHE groups are not supported yet.
224 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200225MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +0100226static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
227 unsigned char *buf,
228 const unsigned char *end,
229 size_t *out_len )
230{
231 unsigned char *p = buf ;
232 unsigned char *named_group_list; /* Start of named_group_list */
233 size_t named_group_list_len; /* Length of named_group_list */
234 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
235
236 *out_len = 0;
237
238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
239
240 /* Check if we have space for header and length fields:
241 * - extension_type (2 bytes)
242 * - extension_data_length (2 bytes)
243 * - named_group_list_length (2 bytes)
244 */
245 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
246 p += 6;
247
248 named_group_list = p;
249
250 if( group_list == NULL )
251 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
252
253 for( ; *group_list != 0; group_list++ )
254 {
255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
256
257#if defined(MBEDTLS_ECP_C)
258 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
259 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
260 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
261 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
262 {
263 const mbedtls_ecp_curve_info *curve_info;
264 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
265 if( curve_info == NULL )
266 continue;
267 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
268 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
269 p += 2;
270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
271 curve_info->name, *group_list ) );
272 }
273#endif /* MBEDTLS_ECP_C */
274 /* Add DHE groups here */
275
276 }
277
278 /* Length of named_group_list */
279 named_group_list_len = p - named_group_list;
280 if( named_group_list_len == 0 )
281 {
282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
284 }
285
286 /* Write extension_type */
287 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
288 /* Write extension_data_length */
289 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
290 /* Write length of named_group_list */
291 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
292
293 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
294 buf + 4, named_group_list_len + 2 );
295
296 *out_len = p - buf;
297
298#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
299 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
300#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
301
302 return( 0 );
303}
304
305#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
306 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
307
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200308MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100309static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100310 mbedtls_ssl_context *ssl,
311 unsigned char *buf,
312 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100313 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100314 size_t *out_len )
315{
316 unsigned char *p = buf;
317 const int *ciphersuite_list;
318 unsigned char *cipher_suites; /* Start of the cipher_suites list */
319 size_t cipher_suites_len;
320
Ronald Crond491c2d2022-02-19 18:30:46 +0100321 *tls12_uses_ec = 0;
322 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100323
324 /*
325 * Ciphersuite list
326 *
327 * This is a list of the symmetric cipher options supported by
328 * the client, specifically the record protection algorithm
329 * ( including secret key length ) and a hash to be used with
330 * HKDF, in descending order of client preference.
331 */
332 ciphersuite_list = ssl->conf->ciphersuite_list;
333
334 /* Check there is space for the cipher suite list length (2 bytes). */
335 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
336 p += 2;
337
Ronald Cron64767262022-03-31 14:13:57 +0200338 /* Write cipher_suites
339 * CipherSuite cipher_suites<2..2^16-2>;
340 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100341 cipher_suites = p;
342 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
343 {
344 int cipher_suite = ciphersuite_list[i];
345 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
346
347 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100348
Ronald Cron757a2ab2022-03-30 13:57:40 +0200349 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400350 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400351 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100352 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100353
354#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
355 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
356 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
357 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
358#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100359
360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
361 (unsigned int) cipher_suite,
362 ciphersuite_info->name ) );
363
364 /* Check there is space for the cipher suite identifier (2 bytes). */
365 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
366 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
367 p += 2;
368 }
369
Ronald Crond491c2d2022-02-19 18:30:46 +0100370 /*
371 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
372 */
David Horstmann4a285632022-10-06 18:30:10 +0100373 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100374#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann4a285632022-10-06 18:30:10 +0100375 renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
Ronald Crond491c2d2022-02-19 18:30:46 +0100376#endif
David Horstmann4a285632022-10-06 18:30:10 +0100377 if( !renegotiating )
Ronald Crond491c2d2022-02-19 18:30:46 +0100378 {
379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
380 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
381 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
382 p += 2;
383 }
384
Ronald Cron3d580bf2022-02-18 17:24:56 +0100385 /* Write the cipher_suites length in number of bytes */
386 cipher_suites_len = p - cipher_suites;
387 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
388 MBEDTLS_SSL_DEBUG_MSG( 3,
389 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
390 cipher_suites_len/2 ) );
391
392 /* Output the total length of cipher_suites field. */
393 *out_len = p - buf;
394
395 return( 0 );
396}
397
398/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100399 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100400 *
401 * struct {
402 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
403 * Random random;
404 * opaque legacy_session_id<0..32>;
405 * CipherSuite cipher_suites<2..2^16-2>;
406 * opaque legacy_compression_methods<1..2^8-1>;
407 * Extension extensions<8..2^16-1>;
408 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100409 *
410 * Structure of the (D)TLS 1.2 ClientHello message:
411 *
412 * struct {
413 * ProtocolVersion client_version;
414 * Random random;
415 * SessionID session_id;
416 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
417 * CipherSuite cipher_suites<2..2^16-2>;
418 * CompressionMethod compression_methods<1..2^8-1>;
419 * select (extensions_present) {
420 * case false:
421 * struct {};
422 * case true:
423 * Extension extensions<0..2^16-1>;
424 * };
425 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100426 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200427MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100428static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
429 unsigned char *buf,
430 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000431 size_t *out_len,
432 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100433{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100434 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100435 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100436 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100437 unsigned char *p_extensions_len; /* Pointer to extensions length */
438 size_t output_len; /* Length of buffer used by function */
439 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100440 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100441
Ronald Cron3d580bf2022-02-18 17:24:56 +0100442 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000443 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100444
Ronald Cron4079abc2022-02-20 10:35:26 +0100445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200446 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400447 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200448 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400449 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100450#endif
451#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200452 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400453 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200454 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400455 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100456#endif
457
Ronald Cron3d580bf2022-02-18 17:24:56 +0100458 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100459 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100460 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100461 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100462 */
463 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400464 mbedtls_ssl_write_version( p, ssl->conf->transport,
465 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100466 p += 2;
467
Ronald Cron58b80382022-02-18 18:41:08 +0100468 /* ...
469 * Random random;
470 * ...
471 *
Ronald Cron58b80382022-02-18 18:41:08 +0100472 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100473 * the handshake->randbytes buffer and are copied here into the output
474 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100475 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100476 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100477 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100478 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
479 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
480 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
481
Ronald Cron021b1782022-02-19 17:32:53 +0100482 /* TLS 1.2:
483 * ...
484 * SessionID session_id;
485 * ...
486 * with
487 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100488 *
Ronald Cron021b1782022-02-19 17:32:53 +0100489 * TLS 1.3:
490 * ...
491 * opaque legacy_session_id<0..32>;
492 * ...
493 *
Ronald Cronda41b382022-03-30 09:57:11 +0200494 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100495 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
496 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100497 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100498 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
499 *p++ = (unsigned char)ssl->session_negotiate->id_len;
500 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
501 p += ssl->session_negotiate->id_len;
502
503 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
504 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100505
Ronald Crona874aa82022-02-19 18:11:26 +0100506 /* DTLS 1.2 ONLY
507 * ...
508 * opaque cookie<0..2^8-1>;
509 * ...
510 */
511#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
513 {
514 unsigned char cookie_len = 0;
515
Ronald Cron4079abc2022-02-20 10:35:26 +0100516 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100517 {
518 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100519 handshake->cookie,
520 handshake->verify_cookie_len );
521 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100522 }
523
524 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
525 *p++ = cookie_len;
526 if( cookie_len > 0 )
527 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100528 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100529 p += cookie_len;
530 }
531 }
532#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
533
Ronald Cron3d580bf2022-02-18 17:24:56 +0100534 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100535 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
536 &tls12_uses_ec,
537 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100538 if( ret != 0 )
539 return( ret );
540 p += output_len;
541
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100542 /* Write legacy_compression_methods (TLS 1.3) or
543 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100544 *
545 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
546 * one byte set to zero, which corresponds to the 'null' compression
547 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100548 *
549 * For TLS 1.2 ClientHello, for security reasons we do not support
550 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100551 */
552 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
553 *p++ = 1;
554 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
555
556 /* Write extensions */
557
558#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
559 /* Keeping track of the included extensions */
Ronald Cron4079abc2022-02-20 10:35:26 +0100560 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100561#endif
562
563 /* First write extensions, then the total length */
564 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
565 p_extensions_len = p;
566 p += 2;
567
Ronald Crondf823bf2022-03-29 18:57:54 +0200568#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
569 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100570 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100571 if( ret != 0 )
572 return( ret );
573 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200574#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100575
576#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100577 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100578 if( ret != 0 )
579 return( ret );
580 p += output_len;
581#endif /* MBEDTLS_SSL_ALPN */
582
583#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100584 if( propose_tls13 )
585 {
586 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
587 &output_len );
588 if( ret != 0 )
589 return( ret );
590 p += output_len;
591 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200592#endif
593
Ronald Cron4079abc2022-02-20 10:35:26 +0100594#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
595 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
596 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200597#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100598 ( propose_tls13 &&
599 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
600#endif
601#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
602 ( propose_tls12 && tls12_uses_ec ) ||
603#endif
604 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100605 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100606 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100607 if( ret != 0 )
608 return( ret );
609 p += output_len;
610 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100611#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100612
Ronald Crone68ab4f2022-10-05 12:46:29 +0200613#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100614 if(
615#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
616 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
617#endif
618#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
619 propose_tls12 ||
620#endif
621 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100622 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000623 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100624 if( ret != 0 )
625 return( ret );
626 p += output_len;
627 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200628#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100629
Ronald Cron4079abc2022-02-20 10:35:26 +0100630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
631 if( propose_tls12 )
632 {
633 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
634 tls12_uses_ec,
635 &output_len );
636 if( ret != 0 )
637 return( ret );
638 p += output_len;
639 }
640#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100641
Ronald Cron41a443a2022-10-04 16:38:25 +0200642#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000643 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
644 * MUST be the last extension in the ClientHello.
645 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000646 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
647 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000648 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000649 ssl, p, end, &output_len, binders_len );
650 if( ret != 0 )
651 return( ret );
652 p += output_len;
653 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200654#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000655
Ronald Cron3d580bf2022-02-18 17:24:56 +0100656 /* Write the length of the list of extensions. */
657 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100658
659 if( extensions_len == 0 )
660 p = p_extensions_len;
661 else
662 {
663 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
665 MBEDTLS_PRINTF_SIZET, extensions_len ) );
666 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
667 p_extensions_len, extensions_len );
668 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100669
670 *out_len = p - buf;
671 return( 0 );
672}
673
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200674MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100675static int ssl_generate_random( mbedtls_ssl_context *ssl )
676{
677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
678 unsigned char *randbytes = ssl->handshake->randbytes;
679 size_t gmt_unix_time_len = 0;
680
681 /*
682 * Generate the random bytes
683 *
684 * TLS 1.2 case:
685 * struct {
686 * uint32 gmt_unix_time;
687 * opaque random_bytes[28];
688 * } Random;
689 *
690 * TLS 1.3 case:
691 * opaque Random[32];
692 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400693 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100694 {
695#if defined(MBEDTLS_HAVE_TIME)
696 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
697 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
698 gmt_unix_time_len = 4;
699
700 MBEDTLS_SSL_DEBUG_MSG( 3,
701 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
702 (long long) gmt_unix_time ) );
703#endif /* MBEDTLS_HAVE_TIME */
704 }
705
706 ret = ssl->conf->f_rng( ssl->conf->p_rng,
707 randbytes + gmt_unix_time_len,
708 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
709 return( ret );
710}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000711
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200712MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100713static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100714{
715 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100716 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800717 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
718
719 if( session_negotiate == NULL )
720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100721
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800722#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
723 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
724 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800725
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800726 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800727 if( ssl->handshake->resume != 0 &&
728 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
729 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800730 {
731 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800732 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
733 if( session_negotiate->ticket_received > now ||
734 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800735 {
736 /* Without valid ticket, disable session resumption.*/
737 MBEDTLS_SSL_DEBUG_MSG(
738 3, ( "Ticket expired, disable session resumption" ) );
739 ssl->handshake->resume = 0;
740 }
741 }
742#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
743 MBEDTLS_SSL_SESSION_TICKETS &&
744 MBEDTLS_HAVE_TIME */
745
Ronald Cron3d580bf2022-02-18 17:24:56 +0100746 if( ssl->conf->f_rng == NULL )
747 {
748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
749 return( MBEDTLS_ERR_SSL_NO_RNG );
750 }
751
Ronald Cron86a477f2022-02-18 17:45:10 +0100752 /* Bet on the highest configured version if we are not in a TLS 1.2
753 * renegotiation or session resumption.
754 */
755#if defined(MBEDTLS_SSL_RENEGOTIATION)
756 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400757 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100758 else
759#endif
760 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100761 if( ssl->handshake->resume )
762 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800763 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400764 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100765 }
766 else
767 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400768 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400769 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100770 }
771 }
772
Ronald Cron58b80382022-02-18 18:41:08 +0100773 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200774 * Generate the random bytes, except when responding to a verify request
775 * where we MUST reuse the previoulsy generated random bytes
776 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100777 */
778#if defined(MBEDTLS_SSL_PROTO_DTLS)
779 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
780 ( ssl->handshake->cookie == NULL ) )
781#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100782 {
Ronald Cron58b80382022-02-18 18:41:08 +0100783 ret = ssl_generate_random( ssl );
784 if( ret != 0 )
785 {
786 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
787 return( ret );
788 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100789 }
790
Ronald Cron3d580bf2022-02-18 17:24:56 +0100791 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200792 * Prepare session identifier. At that point, the length of the session
793 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
794 * to zero, except in the case of a TLS 1.2 session renegotiation or
795 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100796 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800797 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100798
799#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400800 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100801 {
Ronald Cron021b1782022-02-19 17:32:53 +0100802 if( session_id_len < 16 || session_id_len > 32 ||
803#if defined(MBEDTLS_SSL_RENEGOTIATION)
804 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
805#endif
806 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100807 {
Ronald Cron021b1782022-02-19 17:32:53 +0100808 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100809 }
Ronald Cron021b1782022-02-19 17:32:53 +0100810
811#if defined(MBEDTLS_SSL_SESSION_TICKETS)
812 /*
813 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
814 * generate and include a Session ID in the TLS ClientHello."
815 */
David Horstmann4a285632022-10-06 18:30:10 +0100816 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100817#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann7aee0ec2022-10-25 10:38:25 +0100818 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
819 renegotiating = 1;
Ronald Cron021b1782022-02-19 17:32:53 +0100820#endif
David Horstmann4a285632022-10-06 18:30:10 +0100821 if( !renegotiating )
Ronald Cron021b1782022-02-19 17:32:53 +0100822 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800823 if( ( session_negotiate->ticket != NULL ) &&
824 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100825 {
826 session_id_len = 32;
827 }
828 }
829#endif /* MBEDTLS_SSL_SESSION_TICKETS */
830 }
831#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
832
833#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400834 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100835 {
836 /*
837 * Create a legacy session identifier for the purpose of middlebox
838 * compatibility only if one has not been created already, which is
839 * the case if we are here for the TLS 1.3 second ClientHello.
840 *
841 * Versions of TLS before TLS 1.3 supported a "session resumption"
842 * feature which has been merged with pre-shared keys in TLS 1.3
843 * version. A client which has a cached session ID set by a pre-TLS 1.3
844 * server SHOULD set this field to that value. In compatibility mode,
845 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
846 * session MUST generate a new 32-byte value. This value need not be
847 * random but SHOULD be unpredictable to avoid implementations fixating
848 * on a specific value (also known as ossification). Otherwise, it MUST
849 * be set as a zero-length vector ( i.e., a zero-valued single byte
850 * length field ).
851 */
852 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100853 }
854#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
855
Jerry Yu22c18c12022-10-11 15:58:51 +0800856 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100857 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800858 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100859 if( session_id_len > 0 )
860 {
861 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800862 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100863 session_id_len );
864 if( ret != 0 )
865 {
866 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
867 return( ret );
868 }
869 }
870 }
871
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000872#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000873 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000874 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000875 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
876 ssl->handshake->resume )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000877 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000878 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000879 session_negotiate->hostname != NULL;
880 if( ssl->hostname != NULL && session_negotiate->hostname != NULL )
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000881 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000882 hostname_mismatch = strcmp(
Xiaokang Qian307a7302022-10-12 11:14:32 +0000883 ssl->hostname, session_negotiate->hostname ) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000884 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000885
886 if( hostname_mismatch )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000887 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000888 MBEDTLS_SSL_DEBUG_MSG(
889 1, ( "Hostname mismatch the session ticket, "
890 "disable session resumption." ) );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000892 }
893 }
894 else
Xiaokang Qian03409292022-10-12 02:49:52 +0000895 {
Xiaokang Qian307a7302022-10-12 11:14:32 +0000896 return mbedtls_ssl_session_set_hostname( session_negotiate,
Xiaokang Qian03409292022-10-12 02:49:52 +0000897 ssl->hostname );
898 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000899#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000900 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000901 MBEDTLS_SSL_SERVER_NAME_INDICATION */
902
Ronald Cron3d580bf2022-02-18 17:24:56 +0100903 return( 0 );
904}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100905/*
906 * Write ClientHello handshake message.
907 * Handler for MBEDTLS_SSL_CLIENT_HELLO
908 */
909int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
910{
911 int ret = 0;
912 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000913 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100914
915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
916
Ronald Cron71c23322022-02-18 17:29:39 +0100917 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100918
919 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
920 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
921 &buf, &buf_len ) );
922
Ronald Cron71c23322022-02-18 17:29:39 +0100923 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
924 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000925 &msg_len,
926 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100927
Ronald Cron5f4e9122022-02-21 09:50:36 +0100928#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
929 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
930 {
931 ssl->out_msglen = msg_len + 4;
932 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100933
Ronald Cron8ecd9932022-03-29 12:26:54 +0200934 /*
935 * The two functions below may try to send data on the network and
936 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
937 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200938 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200939 * set the handshake state to the next state at that point to ensure
940 * that we will not write and send again a ClientHello when we
941 * eventually succeed in sending the pending data.
942 */
943 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
944
Ronald Cron5f4e9122022-02-21 09:50:36 +0100945 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
946 {
947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
948 return( ret );
949 }
950
Ronald Cron8ecd9932022-03-29 12:26:54 +0200951 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100952 {
953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
954 return( ret );
955 }
956 }
957 else
958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
959 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000960
XiaokangQianadab9a62022-07-18 07:41:26 +0000961 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
962 msg_len );
963 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
Ronald Cron41a443a2022-10-04 16:38:25 +0200964#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000965 if( binders_len > 0 )
966 {
967 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000968 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000969 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000970 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
971 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000972 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200973#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000974
Ronald Cron5f4e9122022-02-21 09:50:36 +0100975 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
976 buf_len,
977 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200978 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100979 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100980
Ronald Cron3d580bf2022-02-18 17:24:56 +0100981
982cleanup:
983
984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
985 return ret;
986}
987
988#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
989#endif /* MBEDTLS_SSL_CLI_C */