blob: 10566deaa9488a1f66d4c5b2b9bcaced886dafad [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#if defined(MBEDTLS_PLATFORM_C)
28#include "mbedtls/platform.h"
29#else
30#include <stdlib.h>
31#define mbedtls_calloc calloc
32#define mbedtls_free free
33#endif
34
Ronald Cron3d580bf2022-02-18 17:24:56 +010035#include <string.h>
36
37#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Ronald Cron58b80382022-02-18 18:41:08 +010039#if defined(MBEDTLS_HAVE_TIME)
40#include "mbedtls/platform_time.h"
41#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +010042
43#include "ssl_client.h"
44#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010045#include "ssl_tls13_keys.h"
46#include "ssl_debug_helpers.h"
47
Ronald Cronfbd9f992022-03-17 15:22:07 +010048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020049MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +010050static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
51 unsigned char *buf,
52 const unsigned char *end,
53 size_t *olen )
54{
55 unsigned char *p = buf;
56 size_t hostname_len;
57
58 *olen = 0;
59
60 if( ssl->hostname == NULL )
61 return( 0 );
62
63 MBEDTLS_SSL_DEBUG_MSG( 3,
64 ( "client hello, adding server name extension: %s",
65 ssl->hostname ) );
66
67 hostname_len = strlen( ssl->hostname );
68
69 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
70
71 /*
72 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
73 *
74 * In order to provide any of the server names, clients MAY include an
75 * extension of type "server_name" in the (extended) client hello. The
76 * "extension_data" field of this extension SHALL contain
77 * "ServerNameList" where:
78 *
79 * struct {
80 * NameType name_type;
81 * select (name_type) {
82 * case host_name: HostName;
83 * } name;
84 * } ServerName;
85 *
86 * enum {
87 * host_name(0), (255)
88 * } NameType;
89 *
90 * opaque HostName<1..2^16-1>;
91 *
92 * struct {
93 * ServerName server_name_list<1..2^16-1>
94 * } ServerNameList;
95 *
96 */
97 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
98 p += 2;
99
100 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
101 p += 2;
102
103 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
104 p += 2;
105
106 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
107
108 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
109 p += 2;
110
111 memcpy( p, ssl->hostname, hostname_len );
112
113 *olen = hostname_len + 9;
114
115 return( 0 );
116}
117#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
118
Ronald Cron3d580bf2022-02-18 17:24:56 +0100119#if defined(MBEDTLS_SSL_ALPN)
120/*
Ronald Cron71c23322022-02-18 17:29:39 +0100121 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100122 *
123 * Structure of the application_layer_protocol_negotiation extension in
124 * ClientHello:
125 *
126 * opaque ProtocolName<1..2^8-1>;
127 *
128 * struct {
129 * ProtocolName protocol_name_list<2..2^16-1>
130 * } ProtocolNameList;
131 *
132 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200133MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100134static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
135 unsigned char *buf,
136 const unsigned char *end,
137 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100138{
139 unsigned char *p = buf;
140
141 *out_len = 0;
142
143 if( ssl->conf->alpn_list == NULL )
144 return( 0 );
145
146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
147
148
149 /* Check we have enough space for the extension type (2 bytes), the
150 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
151 */
152 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
153 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
154 /* Skip writing extension and list length for now */
155 p += 6;
156
157 /*
158 * opaque ProtocolName<1..2^8-1>;
159 *
160 * struct {
161 * ProtocolName protocol_name_list<2..2^16-1>
162 * } ProtocolNameList;
163 */
164 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
165 {
166 /*
167 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
168 * protocol names is less than 255.
169 */
170 size_t protocol_name_len = strlen( *cur );
171
172 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
173 *p++ = (unsigned char)protocol_name_len;
174 memcpy( p, *cur, protocol_name_len );
175 p += protocol_name_len;
176 }
177
178 *out_len = p - buf;
179
180 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
181 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
182
183 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
184 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
185
186 return( 0 );
187}
188#endif /* MBEDTLS_SSL_ALPN */
189
Ronald Cronfbd9f992022-03-17 15:22:07 +0100190#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
191 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
192/*
193 * Function for writing a supported groups (TLS 1.3) or supported elliptic
194 * curves (TLS 1.2) extension.
195 *
196 * The "extension_data" field of a supported groups extension contains a
197 * "NamedGroupList" value (TLS 1.3 RFC8446):
198 * enum {
199 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
200 * x25519(0x001D), x448(0x001E),
201 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
202 * ffdhe6144(0x0103), ffdhe8192(0x0104),
203 * ffdhe_private_use(0x01FC..0x01FF),
204 * ecdhe_private_use(0xFE00..0xFEFF),
205 * (0xFFFF)
206 * } NamedGroup;
207 * struct {
208 * NamedGroup named_group_list<2..2^16-1>;
209 * } NamedGroupList;
210 *
211 * The "extension_data" field of a supported elliptic curves extension contains
212 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
213 * enum {
214 * deprecated(1..22),
215 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
216 * x25519(29), x448(30),
217 * reserved (0xFE00..0xFEFF),
218 * deprecated(0xFF01..0xFF02),
219 * (0xFFFF)
220 * } NamedCurve;
221 * struct {
222 * NamedCurve named_curve_list<2..2^16-1>
223 * } NamedCurveList;
224 *
225 * The TLS 1.3 supported groups extension was defined to be a compatible
226 * generalization of the TLS 1.2 supported elliptic curves extension. They both
227 * share the same extension identifier.
228 *
229 * DHE groups are not supported yet.
230 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200231MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +0100232static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
233 unsigned char *buf,
234 const unsigned char *end,
235 size_t *out_len )
236{
237 unsigned char *p = buf ;
238 unsigned char *named_group_list; /* Start of named_group_list */
239 size_t named_group_list_len; /* Length of named_group_list */
240 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
241
242 *out_len = 0;
243
244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
245
246 /* Check if we have space for header and length fields:
247 * - extension_type (2 bytes)
248 * - extension_data_length (2 bytes)
249 * - named_group_list_length (2 bytes)
250 */
251 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
252 p += 6;
253
254 named_group_list = p;
255
256 if( group_list == NULL )
257 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
258
259 for( ; *group_list != 0; group_list++ )
260 {
261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
262
263#if defined(MBEDTLS_ECP_C)
264 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
265 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
266 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
267 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
268 {
269 const mbedtls_ecp_curve_info *curve_info;
270 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
271 if( curve_info == NULL )
272 continue;
273 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
274 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
275 p += 2;
276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
277 curve_info->name, *group_list ) );
278 }
279#endif /* MBEDTLS_ECP_C */
280 /* Add DHE groups here */
281
282 }
283
284 /* Length of named_group_list */
285 named_group_list_len = p - named_group_list;
286 if( named_group_list_len == 0 )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
290 }
291
292 /* Write extension_type */
293 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
294 /* Write extension_data_length */
295 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
296 /* Write length of named_group_list */
297 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
298
299 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
300 buf + 4, named_group_list_len + 2 );
301
302 *out_len = p - buf;
303
304#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
305 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
306#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
307
308 return( 0 );
309}
310
311#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
312 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
313
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200314MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100315static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100316 mbedtls_ssl_context *ssl,
317 unsigned char *buf,
318 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100319 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100320 size_t *out_len )
321{
322 unsigned char *p = buf;
323 const int *ciphersuite_list;
324 unsigned char *cipher_suites; /* Start of the cipher_suites list */
325 size_t cipher_suites_len;
326
Ronald Crond491c2d2022-02-19 18:30:46 +0100327 *tls12_uses_ec = 0;
328 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100329
330 /*
331 * Ciphersuite list
332 *
333 * This is a list of the symmetric cipher options supported by
334 * the client, specifically the record protection algorithm
335 * ( including secret key length ) and a hash to be used with
336 * HKDF, in descending order of client preference.
337 */
338 ciphersuite_list = ssl->conf->ciphersuite_list;
339
340 /* Check there is space for the cipher suite list length (2 bytes). */
341 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
342 p += 2;
343
Ronald Cron64767262022-03-31 14:13:57 +0200344 /* Write cipher_suites
345 * CipherSuite cipher_suites<2..2^16-2>;
346 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100347 cipher_suites = p;
348 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
349 {
350 int cipher_suite = ciphersuite_list[i];
351 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
352
353 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100354
Ronald Cron757a2ab2022-03-30 13:57:40 +0200355 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400356 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400357 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100358 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100359
360#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
361 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
362 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
363 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
364#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100365
366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
367 (unsigned int) cipher_suite,
368 ciphersuite_info->name ) );
369
370 /* Check there is space for the cipher suite identifier (2 bytes). */
371 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
372 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
373 p += 2;
374 }
375
Ronald Crond491c2d2022-02-19 18:30:46 +0100376 /*
377 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
378 */
379#if defined(MBEDTLS_SSL_RENEGOTIATION)
380 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
381#endif
382 {
383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
384 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
385 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
386 p += 2;
387 }
388
Ronald Cron3d580bf2022-02-18 17:24:56 +0100389 /* Write the cipher_suites length in number of bytes */
390 cipher_suites_len = p - cipher_suites;
391 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
392 MBEDTLS_SSL_DEBUG_MSG( 3,
393 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
394 cipher_suites_len/2 ) );
395
396 /* Output the total length of cipher_suites field. */
397 *out_len = p - buf;
398
399 return( 0 );
400}
401
402/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100403 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100404 *
405 * struct {
406 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
407 * Random random;
408 * opaque legacy_session_id<0..32>;
409 * CipherSuite cipher_suites<2..2^16-2>;
410 * opaque legacy_compression_methods<1..2^8-1>;
411 * Extension extensions<8..2^16-1>;
412 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100413 *
414 * Structure of the (D)TLS 1.2 ClientHello message:
415 *
416 * struct {
417 * ProtocolVersion client_version;
418 * Random random;
419 * SessionID session_id;
420 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
421 * CipherSuite cipher_suites<2..2^16-2>;
422 * CompressionMethod compression_methods<1..2^8-1>;
423 * select (extensions_present) {
424 * case false:
425 * struct {};
426 * case true:
427 * Extension extensions<0..2^16-1>;
428 * };
429 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100430 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100432static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
433 unsigned char *buf,
434 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000435 size_t *out_len,
436 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100437{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100438 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100439 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100440 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100441 unsigned char *p_extensions_len; /* Pointer to extensions length */
442 size_t output_len; /* Length of buffer used by function */
443 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100444 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100445
Ronald Cron3d580bf2022-02-18 17:24:56 +0100446 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000447 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100448
Ronald Cron4079abc2022-02-20 10:35:26 +0100449#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200450 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400451 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200452 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400453 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100454#endif
455#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200456 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400457 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200458 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400459 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100460#endif
461
Ronald Cron3d580bf2022-02-18 17:24:56 +0100462 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100463 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100464 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100465 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100466 */
467 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400468 mbedtls_ssl_write_version( p, ssl->conf->transport,
469 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100470 p += 2;
471
Ronald Cron58b80382022-02-18 18:41:08 +0100472 /* ...
473 * Random random;
474 * ...
475 *
Ronald Cron58b80382022-02-18 18:41:08 +0100476 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100477 * the handshake->randbytes buffer and are copied here into the output
478 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100479 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100480 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100481 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100482 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
483 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
484 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
485
Ronald Cron021b1782022-02-19 17:32:53 +0100486 /* TLS 1.2:
487 * ...
488 * SessionID session_id;
489 * ...
490 * with
491 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100492 *
Ronald Cron021b1782022-02-19 17:32:53 +0100493 * TLS 1.3:
494 * ...
495 * opaque legacy_session_id<0..32>;
496 * ...
497 *
Ronald Cronda41b382022-03-30 09:57:11 +0200498 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100499 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
500 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100501 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100502 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
503 *p++ = (unsigned char)ssl->session_negotiate->id_len;
504 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
505 p += ssl->session_negotiate->id_len;
506
507 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
508 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100509
Ronald Crona874aa82022-02-19 18:11:26 +0100510 /* DTLS 1.2 ONLY
511 * ...
512 * opaque cookie<0..2^8-1>;
513 * ...
514 */
515#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
516 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
517 {
518 unsigned char cookie_len = 0;
519
Ronald Cron4079abc2022-02-20 10:35:26 +0100520 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100521 {
522 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100523 handshake->cookie,
524 handshake->verify_cookie_len );
525 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100526 }
527
528 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
529 *p++ = cookie_len;
530 if( cookie_len > 0 )
531 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100532 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100533 p += cookie_len;
534 }
535 }
536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
537
Ronald Cron3d580bf2022-02-18 17:24:56 +0100538 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100539 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
540 &tls12_uses_ec,
541 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100542 if( ret != 0 )
543 return( ret );
544 p += output_len;
545
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100546 /* Write legacy_compression_methods (TLS 1.3) or
547 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100548 *
549 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
550 * one byte set to zero, which corresponds to the 'null' compression
551 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100552 *
553 * For TLS 1.2 ClientHello, for security reasons we do not support
554 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100555 */
556 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
557 *p++ = 1;
558 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
559
560 /* Write extensions */
561
562#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
563 /* Keeping track of the included extensions */
Ronald Cron4079abc2022-02-20 10:35:26 +0100564 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100565#endif
566
567 /* First write extensions, then the total length */
568 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
569 p_extensions_len = p;
570 p += 2;
571
Ronald Crondf823bf2022-03-29 18:57:54 +0200572#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
573 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100574 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100575 if( ret != 0 )
576 return( ret );
577 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200578#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100579
580#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100581 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100582 if( ret != 0 )
583 return( ret );
584 p += output_len;
585#endif /* MBEDTLS_SSL_ALPN */
586
587#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100588 if( propose_tls13 )
589 {
590 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
591 &output_len );
592 if( ret != 0 )
593 return( ret );
594 p += output_len;
595 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200596#endif
597
Ronald Cron4079abc2022-02-20 10:35:26 +0100598#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
599 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
600 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200601#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100602 ( propose_tls13 &&
603 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
604#endif
605#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
606 ( propose_tls12 && tls12_uses_ec ) ||
607#endif
608 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100609 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100610 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100611 if( ret != 0 )
612 return( ret );
613 p += output_len;
614 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100615#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100616
Ronald Cron11e18572022-03-17 13:44:33 +0100617#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100618 if(
619#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
620 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
621#endif
622#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
623 propose_tls12 ||
624#endif
625 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100626 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000627 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100628 if( ret != 0 )
629 return( ret );
630 p += output_len;
631 }
632#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100633
Ronald Cron4079abc2022-02-20 10:35:26 +0100634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
635 if( propose_tls12 )
636 {
637 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
638 tls12_uses_ec,
639 &output_len );
640 if( ret != 0 )
641 return( ret );
642 p += output_len;
643 }
644#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100645
XiaokangQianeb69aee2022-07-05 08:21:43 +0000646#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
647 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000648 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
649 * MUST be the last extension in the ClientHello.
650 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000651 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
652 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000653 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000654 ssl, p, end, &output_len, binders_len );
655 if( ret != 0 )
656 return( ret );
657 p += output_len;
658 }
XiaokangQian008d2bf2022-07-14 07:54:01 +0000659#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000660
Ronald Cron3d580bf2022-02-18 17:24:56 +0100661 /* Write the length of the list of extensions. */
662 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100663
664 if( extensions_len == 0 )
665 p = p_extensions_len;
666 else
667 {
668 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
670 MBEDTLS_PRINTF_SIZET, extensions_len ) );
671 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
672 p_extensions_len, extensions_len );
673 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100674
675 *out_len = p - buf;
676 return( 0 );
677}
678
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200679MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100680static int ssl_generate_random( mbedtls_ssl_context *ssl )
681{
682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
683 unsigned char *randbytes = ssl->handshake->randbytes;
684 size_t gmt_unix_time_len = 0;
685
686 /*
687 * Generate the random bytes
688 *
689 * TLS 1.2 case:
690 * struct {
691 * uint32 gmt_unix_time;
692 * opaque random_bytes[28];
693 * } Random;
694 *
695 * TLS 1.3 case:
696 * opaque Random[32];
697 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400698 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100699 {
700#if defined(MBEDTLS_HAVE_TIME)
701 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
702 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
703 gmt_unix_time_len = 4;
704
705 MBEDTLS_SSL_DEBUG_MSG( 3,
706 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
707 (long long) gmt_unix_time ) );
708#endif /* MBEDTLS_HAVE_TIME */
709 }
710
711 ret = ssl->conf->f_rng( ssl->conf->p_rng,
712 randbytes + gmt_unix_time_len,
713 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
714 return( ret );
715}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000716
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200717MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100718static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100719{
720 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100721 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800722 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
723
724 if( session_negotiate == NULL )
725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100726
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800727#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
728 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
729 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800730
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800731 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800732 if( ssl->handshake->resume != 0 &&
733 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
734 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800735 {
736 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800737 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
738 if( session_negotiate->ticket_received > now ||
739 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800740 {
741 /* Without valid ticket, disable session resumption.*/
742 MBEDTLS_SSL_DEBUG_MSG(
743 3, ( "Ticket expired, disable session resumption" ) );
744 ssl->handshake->resume = 0;
745 }
746 }
747#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
748 MBEDTLS_SSL_SESSION_TICKETS &&
749 MBEDTLS_HAVE_TIME */
750
Ronald Cron3d580bf2022-02-18 17:24:56 +0100751 if( ssl->conf->f_rng == NULL )
752 {
753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
754 return( MBEDTLS_ERR_SSL_NO_RNG );
755 }
756
Ronald Cron86a477f2022-02-18 17:45:10 +0100757 /* Bet on the highest configured version if we are not in a TLS 1.2
758 * renegotiation or session resumption.
759 */
760#if defined(MBEDTLS_SSL_RENEGOTIATION)
761 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400762 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100763 else
764#endif
765 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100766 if( ssl->handshake->resume )
767 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800768 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400769 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100770 }
771 else
772 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400773 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400774 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100775 }
776 }
777
Ronald Cron58b80382022-02-18 18:41:08 +0100778 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200779 * Generate the random bytes, except when responding to a verify request
780 * where we MUST reuse the previoulsy generated random bytes
781 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100782 */
783#if defined(MBEDTLS_SSL_PROTO_DTLS)
784 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
785 ( ssl->handshake->cookie == NULL ) )
786#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100787 {
Ronald Cron58b80382022-02-18 18:41:08 +0100788 ret = ssl_generate_random( ssl );
789 if( ret != 0 )
790 {
791 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
792 return( ret );
793 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100794 }
795
Ronald Cron3d580bf2022-02-18 17:24:56 +0100796 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200797 * Prepare session identifier. At that point, the length of the session
798 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
799 * to zero, except in the case of a TLS 1.2 session renegotiation or
800 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100801 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800802 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100803
804#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400805 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100806 {
Ronald Cron021b1782022-02-19 17:32:53 +0100807 if( session_id_len < 16 || session_id_len > 32 ||
808#if defined(MBEDTLS_SSL_RENEGOTIATION)
809 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
810#endif
811 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100812 {
Ronald Cron021b1782022-02-19 17:32:53 +0100813 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100814 }
Ronald Cron021b1782022-02-19 17:32:53 +0100815
816#if defined(MBEDTLS_SSL_SESSION_TICKETS)
817 /*
818 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
819 * generate and include a Session ID in the TLS ClientHello."
820 */
821#if defined(MBEDTLS_SSL_RENEGOTIATION)
822 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
823#endif
824 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800825 if( ( session_negotiate->ticket != NULL ) &&
826 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100827 {
828 session_id_len = 32;
829 }
830 }
831#endif /* MBEDTLS_SSL_SESSION_TICKETS */
832 }
833#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
834
835#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400836 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100837 {
838 /*
839 * Create a legacy session identifier for the purpose of middlebox
840 * compatibility only if one has not been created already, which is
841 * the case if we are here for the TLS 1.3 second ClientHello.
842 *
843 * Versions of TLS before TLS 1.3 supported a "session resumption"
844 * feature which has been merged with pre-shared keys in TLS 1.3
845 * version. A client which has a cached session ID set by a pre-TLS 1.3
846 * server SHOULD set this field to that value. In compatibility mode,
847 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
848 * session MUST generate a new 32-byte value. This value need not be
849 * random but SHOULD be unpredictable to avoid implementations fixating
850 * on a specific value (also known as ossification). Otherwise, it MUST
851 * be set as a zero-length vector ( i.e., a zero-valued single byte
852 * length field ).
853 */
854 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100855 }
856#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
857
Jerry Yu22c18c12022-10-11 15:58:51 +0800858 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100859 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800860 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100861 if( session_id_len > 0 )
862 {
863 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800864 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100865 session_id_len );
866 if( ret != 0 )
867 {
868 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
869 return( ret );
870 }
871 }
872 }
873
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000874#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000875 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000876 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000877 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
878 ssl->handshake->resume )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000879 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000880 int hostname_mismatch = ssl->hostname != NULL ||
881 ssl->session_negotiate->hostname != NULL;
882 if( ssl->hostname != NULL && ssl->session_negotiate->hostname != NULL )
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000883 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000884 hostname_mismatch = strcmp(
885 ssl->hostname, ssl->session_negotiate->hostname ) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000886 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000887
888 if( hostname_mismatch )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000889 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000890 MBEDTLS_SSL_DEBUG_MSG(
891 1, ( "Hostname mismatch the session ticket, "
892 "disable session resumption." ) );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000894 }
895 }
896 else
Xiaokang Qian03409292022-10-12 02:49:52 +0000897 {
898 return mbedtls_ssl_session_set_hostname( ssl->session_negotiate,
899 ssl->hostname );
900 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000901#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000902 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000903 MBEDTLS_SSL_SERVER_NAME_INDICATION */
904
Ronald Cron3d580bf2022-02-18 17:24:56 +0100905 return( 0 );
906}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100907/*
908 * Write ClientHello handshake message.
909 * Handler for MBEDTLS_SSL_CLIENT_HELLO
910 */
911int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
912{
913 int ret = 0;
914 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000915 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100916
917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
918
Ronald Cron71c23322022-02-18 17:29:39 +0100919 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100920
921 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
922 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
923 &buf, &buf_len ) );
924
Ronald Cron71c23322022-02-18 17:29:39 +0100925 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
926 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000927 &msg_len,
928 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100929
Ronald Cron5f4e9122022-02-21 09:50:36 +0100930#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
931 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
932 {
933 ssl->out_msglen = msg_len + 4;
934 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100935
Ronald Cron8ecd9932022-03-29 12:26:54 +0200936 /*
937 * The two functions below may try to send data on the network and
938 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
939 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200940 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200941 * set the handshake state to the next state at that point to ensure
942 * that we will not write and send again a ClientHello when we
943 * eventually succeed in sending the pending data.
944 */
945 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
946
Ronald Cron5f4e9122022-02-21 09:50:36 +0100947 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
948 {
949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
950 return( ret );
951 }
952
Ronald Cron8ecd9932022-03-29 12:26:54 +0200953 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100954 {
955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
956 return( ret );
957 }
958 }
959 else
960#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
961 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000962
XiaokangQianadab9a62022-07-18 07:41:26 +0000963 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
964 msg_len );
965 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000966#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
967 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
968 if( binders_len > 0 )
969 {
970 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000971 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000972 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000973 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
974 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000975 }
976#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
977
Ronald Cron5f4e9122022-02-21 09:50:36 +0100978 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
979 buf_len,
980 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200981 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100982 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100983
Ronald Cron3d580bf2022-02-18 17:24:56 +0100984
985cleanup:
986
987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
988 return ret;
989}
990
991#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
992#endif /* MBEDTLS_SSL_CLI_C */