blob: bfa1c747b1029b6a1b6525fa624fa4810afbe6a6 [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 */
David Horstmann4a285632022-10-06 18:30:10 +0100379 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100380#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann4a285632022-10-06 18:30:10 +0100381 renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
Ronald Crond491c2d2022-02-19 18:30:46 +0100382#endif
David Horstmann4a285632022-10-06 18:30:10 +0100383 if( !renegotiating )
Ronald Crond491c2d2022-02-19 18:30:46 +0100384 {
385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
386 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
387 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
388 p += 2;
389 }
390
Ronald Cron3d580bf2022-02-18 17:24:56 +0100391 /* Write the cipher_suites length in number of bytes */
392 cipher_suites_len = p - cipher_suites;
393 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
394 MBEDTLS_SSL_DEBUG_MSG( 3,
395 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
396 cipher_suites_len/2 ) );
397
398 /* Output the total length of cipher_suites field. */
399 *out_len = p - buf;
400
401 return( 0 );
402}
403
404/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100405 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100406 *
407 * struct {
408 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
409 * Random random;
410 * opaque legacy_session_id<0..32>;
411 * CipherSuite cipher_suites<2..2^16-2>;
412 * opaque legacy_compression_methods<1..2^8-1>;
413 * Extension extensions<8..2^16-1>;
414 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100415 *
416 * Structure of the (D)TLS 1.2 ClientHello message:
417 *
418 * struct {
419 * ProtocolVersion client_version;
420 * Random random;
421 * SessionID session_id;
422 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
423 * CipherSuite cipher_suites<2..2^16-2>;
424 * CompressionMethod compression_methods<1..2^8-1>;
425 * select (extensions_present) {
426 * case false:
427 * struct {};
428 * case true:
429 * Extension extensions<0..2^16-1>;
430 * };
431 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100432 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200433MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100434static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
435 unsigned char *buf,
436 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000437 size_t *out_len,
438 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100439{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100440 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100441 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100442 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100443 unsigned char *p_extensions_len; /* Pointer to extensions length */
444 size_t output_len; /* Length of buffer used by function */
445 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100446 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100447
Ronald Cron3d580bf2022-02-18 17:24:56 +0100448 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000449 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100450
Ronald Cron4079abc2022-02-20 10:35:26 +0100451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200452 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400453 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200454 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400455 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100456#endif
457#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200458 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400459 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200460 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400461 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100462#endif
463
Ronald Cron3d580bf2022-02-18 17:24:56 +0100464 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100465 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100466 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100467 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100468 */
469 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400470 mbedtls_ssl_write_version( p, ssl->conf->transport,
471 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100472 p += 2;
473
Ronald Cron58b80382022-02-18 18:41:08 +0100474 /* ...
475 * Random random;
476 * ...
477 *
Ronald Cron58b80382022-02-18 18:41:08 +0100478 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100479 * the handshake->randbytes buffer and are copied here into the output
480 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100481 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100482 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100483 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100484 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
485 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
486 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
487
Ronald Cron021b1782022-02-19 17:32:53 +0100488 /* TLS 1.2:
489 * ...
490 * SessionID session_id;
491 * ...
492 * with
493 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100494 *
Ronald Cron021b1782022-02-19 17:32:53 +0100495 * TLS 1.3:
496 * ...
497 * opaque legacy_session_id<0..32>;
498 * ...
499 *
Ronald Cronda41b382022-03-30 09:57:11 +0200500 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100501 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
502 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100503 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100504 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
505 *p++ = (unsigned char)ssl->session_negotiate->id_len;
506 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
507 p += ssl->session_negotiate->id_len;
508
509 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
510 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100511
Ronald Crona874aa82022-02-19 18:11:26 +0100512 /* DTLS 1.2 ONLY
513 * ...
514 * opaque cookie<0..2^8-1>;
515 * ...
516 */
517#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
519 {
520 unsigned char cookie_len = 0;
521
Ronald Cron4079abc2022-02-20 10:35:26 +0100522 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100523 {
524 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100525 handshake->cookie,
526 handshake->verify_cookie_len );
527 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100528 }
529
530 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
531 *p++ = cookie_len;
532 if( cookie_len > 0 )
533 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100534 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100535 p += cookie_len;
536 }
537 }
538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
539
Ronald Cron3d580bf2022-02-18 17:24:56 +0100540 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100541 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
542 &tls12_uses_ec,
543 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100544 if( ret != 0 )
545 return( ret );
546 p += output_len;
547
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100548 /* Write legacy_compression_methods (TLS 1.3) or
549 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100550 *
551 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
552 * one byte set to zero, which corresponds to the 'null' compression
553 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100554 *
555 * For TLS 1.2 ClientHello, for security reasons we do not support
556 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100557 */
558 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
559 *p++ = 1;
560 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
561
562 /* Write extensions */
563
564#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
565 /* Keeping track of the included extensions */
Ronald Cron4079abc2022-02-20 10:35:26 +0100566 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100567#endif
568
569 /* First write extensions, then the total length */
570 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
571 p_extensions_len = p;
572 p += 2;
573
Ronald Crondf823bf2022-03-29 18:57:54 +0200574#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
575 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100576 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100577 if( ret != 0 )
578 return( ret );
579 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200580#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100581
582#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100583 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100584 if( ret != 0 )
585 return( ret );
586 p += output_len;
587#endif /* MBEDTLS_SSL_ALPN */
588
589#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100590 if( propose_tls13 )
591 {
592 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
593 &output_len );
594 if( ret != 0 )
595 return( ret );
596 p += output_len;
597 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200598#endif
599
Ronald Cron4079abc2022-02-20 10:35:26 +0100600#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
601 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
602 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200603#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100604 ( propose_tls13 &&
605 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
606#endif
607#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
608 ( propose_tls12 && tls12_uses_ec ) ||
609#endif
610 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100611 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100612 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100613 if( ret != 0 )
614 return( ret );
615 p += output_len;
616 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100617#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100618
Ronald Cron11e18572022-03-17 13:44:33 +0100619#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100620 if(
621#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
622 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
623#endif
624#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
625 propose_tls12 ||
626#endif
627 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100628 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000629 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100630 if( ret != 0 )
631 return( ret );
632 p += output_len;
633 }
634#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100635
Ronald Cron4079abc2022-02-20 10:35:26 +0100636#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
637 if( propose_tls12 )
638 {
639 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
640 tls12_uses_ec,
641 &output_len );
642 if( ret != 0 )
643 return( ret );
644 p += output_len;
645 }
646#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100647
XiaokangQianeb69aee2022-07-05 08:21:43 +0000648#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
649 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000650 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
651 * MUST be the last extension in the ClientHello.
652 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000653 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
654 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000655 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000656 ssl, p, end, &output_len, binders_len );
657 if( ret != 0 )
658 return( ret );
659 p += output_len;
660 }
XiaokangQian008d2bf2022-07-14 07:54:01 +0000661#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000662
Ronald Cron3d580bf2022-02-18 17:24:56 +0100663 /* Write the length of the list of extensions. */
664 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100665
666 if( extensions_len == 0 )
667 p = p_extensions_len;
668 else
669 {
670 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
672 MBEDTLS_PRINTF_SIZET, extensions_len ) );
673 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
674 p_extensions_len, extensions_len );
675 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100676
677 *out_len = p - buf;
678 return( 0 );
679}
680
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200681MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100682static int ssl_generate_random( mbedtls_ssl_context *ssl )
683{
684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
685 unsigned char *randbytes = ssl->handshake->randbytes;
686 size_t gmt_unix_time_len = 0;
687
688 /*
689 * Generate the random bytes
690 *
691 * TLS 1.2 case:
692 * struct {
693 * uint32 gmt_unix_time;
694 * opaque random_bytes[28];
695 * } Random;
696 *
697 * TLS 1.3 case:
698 * opaque Random[32];
699 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400700 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100701 {
702#if defined(MBEDTLS_HAVE_TIME)
703 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
704 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
705 gmt_unix_time_len = 4;
706
707 MBEDTLS_SSL_DEBUG_MSG( 3,
708 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
709 (long long) gmt_unix_time ) );
710#endif /* MBEDTLS_HAVE_TIME */
711 }
712
713 ret = ssl->conf->f_rng( ssl->conf->p_rng,
714 randbytes + gmt_unix_time_len,
715 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
716 return( ret );
717}
718
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200719MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100720static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100721{
722 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100723 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100724
725 if( ssl->conf->f_rng == NULL )
726 {
727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
728 return( MBEDTLS_ERR_SSL_NO_RNG );
729 }
730
Ronald Cron86a477f2022-02-18 17:45:10 +0100731 /* Bet on the highest configured version if we are not in a TLS 1.2
732 * renegotiation or session resumption.
733 */
734#if defined(MBEDTLS_SSL_RENEGOTIATION)
735 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400736 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100737 else
738#endif
739 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100740 if( ssl->handshake->resume )
741 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400742 ssl->tls_version = ssl->session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400743 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100744 }
745 else
746 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400747 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400748 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100749 }
750 }
751
Ronald Cron58b80382022-02-18 18:41:08 +0100752 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200753 * Generate the random bytes, except when responding to a verify request
754 * where we MUST reuse the previoulsy generated random bytes
755 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100756 */
757#if defined(MBEDTLS_SSL_PROTO_DTLS)
758 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
759 ( ssl->handshake->cookie == NULL ) )
760#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100761 {
Ronald Cron58b80382022-02-18 18:41:08 +0100762 ret = ssl_generate_random( ssl );
763 if( ret != 0 )
764 {
765 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
766 return( ret );
767 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100768 }
769
Ronald Cron3d580bf2022-02-18 17:24:56 +0100770 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200771 * Prepare session identifier. At that point, the length of the session
772 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
773 * to zero, except in the case of a TLS 1.2 session renegotiation or
774 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100775 */
Ronald Cron021b1782022-02-19 17:32:53 +0100776 session_id_len = ssl->session_negotiate->id_len;
777
778#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400779 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100780 {
Ronald Cron021b1782022-02-19 17:32:53 +0100781 if( session_id_len < 16 || session_id_len > 32 ||
782#if defined(MBEDTLS_SSL_RENEGOTIATION)
783 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
784#endif
785 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100786 {
Ronald Cron021b1782022-02-19 17:32:53 +0100787 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100788 }
Ronald Cron021b1782022-02-19 17:32:53 +0100789
790#if defined(MBEDTLS_SSL_SESSION_TICKETS)
791 /*
792 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
793 * generate and include a Session ID in the TLS ClientHello."
794 */
David Horstmann4a285632022-10-06 18:30:10 +0100795 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100796#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann7aee0ec2022-10-25 10:38:25 +0100797 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
798 renegotiating = 1;
Ronald Cron021b1782022-02-19 17:32:53 +0100799#endif
David Horstmann4a285632022-10-06 18:30:10 +0100800 if( !renegotiating )
Ronald Cron021b1782022-02-19 17:32:53 +0100801 {
802 if( ( ssl->session_negotiate->ticket != NULL ) &&
803 ( ssl->session_negotiate->ticket_len != 0 ) )
804 {
805 session_id_len = 32;
806 }
807 }
808#endif /* MBEDTLS_SSL_SESSION_TICKETS */
809 }
810#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
811
812#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400813 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100814 {
815 /*
816 * Create a legacy session identifier for the purpose of middlebox
817 * compatibility only if one has not been created already, which is
818 * the case if we are here for the TLS 1.3 second ClientHello.
819 *
820 * Versions of TLS before TLS 1.3 supported a "session resumption"
821 * feature which has been merged with pre-shared keys in TLS 1.3
822 * version. A client which has a cached session ID set by a pre-TLS 1.3
823 * server SHOULD set this field to that value. In compatibility mode,
824 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
825 * session MUST generate a new 32-byte value. This value need not be
826 * random but SHOULD be unpredictable to avoid implementations fixating
827 * on a specific value (also known as ossification). Otherwise, it MUST
828 * be set as a zero-length vector ( i.e., a zero-valued single byte
829 * length field ).
830 */
831 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100832 }
833#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
834
Ronald Cron021b1782022-02-19 17:32:53 +0100835 if( session_id_len != ssl->session_negotiate->id_len )
836 {
837 ssl->session_negotiate->id_len = session_id_len;
838 if( session_id_len > 0 )
839 {
840 ret = ssl->conf->f_rng( ssl->conf->p_rng,
841 ssl->session_negotiate->id,
842 session_id_len );
843 if( ret != 0 )
844 {
845 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
846 return( ret );
847 }
848 }
849 }
850
Ronald Cron3d580bf2022-02-18 17:24:56 +0100851 return( 0 );
852}
853
854/*
855 * Write ClientHello handshake message.
856 * Handler for MBEDTLS_SSL_CLIENT_HELLO
857 */
858int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
859{
860 int ret = 0;
861 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000862 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100863
864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
865
Ronald Cron71c23322022-02-18 17:29:39 +0100866 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100867
868 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
869 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
870 &buf, &buf_len ) );
871
Ronald Cron71c23322022-02-18 17:29:39 +0100872 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
873 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000874 &msg_len,
875 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100876
Ronald Cron5f4e9122022-02-21 09:50:36 +0100877#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
878 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
879 {
880 ssl->out_msglen = msg_len + 4;
881 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100882
Ronald Cron8ecd9932022-03-29 12:26:54 +0200883 /*
884 * The two functions below may try to send data on the network and
885 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
886 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200887 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200888 * set the handshake state to the next state at that point to ensure
889 * that we will not write and send again a ClientHello when we
890 * eventually succeed in sending the pending data.
891 */
892 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
893
Ronald Cron5f4e9122022-02-21 09:50:36 +0100894 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
895 {
896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
897 return( ret );
898 }
899
Ronald Cron8ecd9932022-03-29 12:26:54 +0200900 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100901 {
902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
903 return( ret );
904 }
905 }
906 else
907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
908 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000909
XiaokangQianadab9a62022-07-18 07:41:26 +0000910 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
911 msg_len );
912 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000913#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
914 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
915 if( binders_len > 0 )
916 {
917 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000918 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000919 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000920 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
921 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000922 }
923#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
924
Ronald Cron5f4e9122022-02-21 09:50:36 +0100925 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
926 buf_len,
927 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200928 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100929 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100930
Ronald Cron3d580bf2022-02-18 17:24:56 +0100931
932cleanup:
933
934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
935 return ret;
936}
937
938#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
939#endif /* MBEDTLS_SSL_CLI_C */