blob: 16cef0204a5effa47d01220282bc3333ef1bb408 [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
Jerry Yu0c354a22022-08-29 15:25:36 +0800109#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800110 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SERVERNAME );
Jerry Yu0c354a22022-08-29 15:25:36 +0800111 MBEDTLS_SSL_DEBUG_MSG(
112 4, ( "sent %s extension",
113 mbedtls_tls13_get_extension_name(
114 MBEDTLS_TLS_EXT_SERVERNAME ) ) );
115#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100116 return( 0 );
117}
118#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
119
Ronald Cron3d580bf2022-02-18 17:24:56 +0100120#if defined(MBEDTLS_SSL_ALPN)
121/*
Ronald Cron71c23322022-02-18 17:29:39 +0100122 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100123 *
124 * Structure of the application_layer_protocol_negotiation extension in
125 * ClientHello:
126 *
127 * opaque ProtocolName<1..2^8-1>;
128 *
129 * struct {
130 * ProtocolName protocol_name_list<2..2^16-1>
131 * } ProtocolNameList;
132 *
133 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200134MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100135static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
136 unsigned char *buf,
137 const unsigned char *end,
138 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100139{
140 unsigned char *p = buf;
141
142 *out_len = 0;
143
144 if( ssl->conf->alpn_list == NULL )
145 return( 0 );
146
147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
148
149
150 /* Check we have enough space for the extension type (2 bytes), the
151 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
152 */
153 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
154 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
155 /* Skip writing extension and list length for now */
156 p += 6;
157
158 /*
159 * opaque ProtocolName<1..2^8-1>;
160 *
161 * struct {
162 * ProtocolName protocol_name_list<2..2^16-1>
163 * } ProtocolNameList;
164 */
165 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
166 {
167 /*
168 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
169 * protocol names is less than 255.
170 */
171 size_t protocol_name_len = strlen( *cur );
172
173 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
174 *p++ = (unsigned char)protocol_name_len;
175 memcpy( p, *cur, protocol_name_len );
176 p += protocol_name_len;
177 }
178
179 *out_len = p - buf;
180
181 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
182 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
183
184 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
185 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
186
Jerry Yu0c354a22022-08-29 15:25:36 +0800187#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800188 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
Jerry Yu0c354a22022-08-29 15:25:36 +0800189 MBEDTLS_SSL_DEBUG_MSG(
190 4, ( "sent %s extension",
191 mbedtls_tls13_get_extension_name(
192 MBEDTLS_TLS_EXT_ALPN ) ) );
193#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100194 return( 0 );
195}
196#endif /* MBEDTLS_SSL_ALPN */
197
Ronald Cronfbd9f992022-03-17 15:22:07 +0100198#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
199 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
200/*
201 * Function for writing a supported groups (TLS 1.3) or supported elliptic
202 * curves (TLS 1.2) extension.
203 *
204 * The "extension_data" field of a supported groups extension contains a
205 * "NamedGroupList" value (TLS 1.3 RFC8446):
206 * enum {
207 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
208 * x25519(0x001D), x448(0x001E),
209 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
210 * ffdhe6144(0x0103), ffdhe8192(0x0104),
211 * ffdhe_private_use(0x01FC..0x01FF),
212 * ecdhe_private_use(0xFE00..0xFEFF),
213 * (0xFFFF)
214 * } NamedGroup;
215 * struct {
216 * NamedGroup named_group_list<2..2^16-1>;
217 * } NamedGroupList;
218 *
219 * The "extension_data" field of a supported elliptic curves extension contains
220 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
221 * enum {
222 * deprecated(1..22),
223 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
224 * x25519(29), x448(30),
225 * reserved (0xFE00..0xFEFF),
226 * deprecated(0xFF01..0xFF02),
227 * (0xFFFF)
228 * } NamedCurve;
229 * struct {
230 * NamedCurve named_curve_list<2..2^16-1>
231 * } NamedCurveList;
232 *
233 * The TLS 1.3 supported groups extension was defined to be a compatible
234 * generalization of the TLS 1.2 supported elliptic curves extension. They both
235 * share the same extension identifier.
236 *
237 * DHE groups are not supported yet.
238 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200239MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +0100240static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
241 unsigned char *buf,
242 const unsigned char *end,
243 size_t *out_len )
244{
245 unsigned char *p = buf ;
246 unsigned char *named_group_list; /* Start of named_group_list */
247 size_t named_group_list_len; /* Length of named_group_list */
248 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
249
250 *out_len = 0;
251
252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
253
254 /* Check if we have space for header and length fields:
255 * - extension_type (2 bytes)
256 * - extension_data_length (2 bytes)
257 * - named_group_list_length (2 bytes)
258 */
259 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
260 p += 6;
261
262 named_group_list = p;
263
264 if( group_list == NULL )
265 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
266
267 for( ; *group_list != 0; group_list++ )
268 {
269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
270
271#if defined(MBEDTLS_ECP_C)
272 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
273 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
274 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
275 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
276 {
277 const mbedtls_ecp_curve_info *curve_info;
278 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
279 if( curve_info == NULL )
280 continue;
281 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
282 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
283 p += 2;
284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
285 curve_info->name, *group_list ) );
286 }
287#endif /* MBEDTLS_ECP_C */
288 /* Add DHE groups here */
289
290 }
291
292 /* Length of named_group_list */
293 named_group_list_len = p - named_group_list;
294 if( named_group_list_len == 0 )
295 {
296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
298 }
299
300 /* Write extension_type */
301 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
302 /* Write extension_data_length */
303 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
304 /* Write length of named_group_list */
305 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
306
307 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
308 buf + 4, named_group_list_len + 2 );
309
310 *out_len = p - buf;
311
312#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800313 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS );
Jerry Yu0c354a22022-08-29 15:25:36 +0800314 MBEDTLS_SSL_DEBUG_MSG( 4, ( "sent %s extension",
315 mbedtls_tls13_get_extension_name(
316 MBEDTLS_TLS_EXT_SUPPORTED_GROUPS ) ) );
Ronald Cronfbd9f992022-03-17 15:22:07 +0100317#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
318
319 return( 0 );
320}
321
322#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
323 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
324
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200325MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100326static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100327 mbedtls_ssl_context *ssl,
328 unsigned char *buf,
329 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100330 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100331 size_t *out_len )
332{
333 unsigned char *p = buf;
334 const int *ciphersuite_list;
335 unsigned char *cipher_suites; /* Start of the cipher_suites list */
336 size_t cipher_suites_len;
337
Ronald Crond491c2d2022-02-19 18:30:46 +0100338 *tls12_uses_ec = 0;
339 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100340
341 /*
342 * Ciphersuite list
343 *
344 * This is a list of the symmetric cipher options supported by
345 * the client, specifically the record protection algorithm
346 * ( including secret key length ) and a hash to be used with
347 * HKDF, in descending order of client preference.
348 */
349 ciphersuite_list = ssl->conf->ciphersuite_list;
350
351 /* Check there is space for the cipher suite list length (2 bytes). */
352 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
353 p += 2;
354
Ronald Cron64767262022-03-31 14:13:57 +0200355 /* Write cipher_suites
356 * CipherSuite cipher_suites<2..2^16-2>;
357 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100358 cipher_suites = p;
359 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
360 {
361 int cipher_suite = ciphersuite_list[i];
362 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
363
364 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100365
Ronald Cron757a2ab2022-03-30 13:57:40 +0200366 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400367 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400368 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100369 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100370
371#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
372 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
373 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
374 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
375#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100376
377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
378 (unsigned int) cipher_suite,
379 ciphersuite_info->name ) );
380
381 /* Check there is space for the cipher suite identifier (2 bytes). */
382 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
383 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
384 p += 2;
385 }
386
Ronald Crond491c2d2022-02-19 18:30:46 +0100387 /*
388 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
389 */
David Horstmann4a285632022-10-06 18:30:10 +0100390 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100391#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann4a285632022-10-06 18:30:10 +0100392 renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
Ronald Crond491c2d2022-02-19 18:30:46 +0100393#endif
David Horstmann4a285632022-10-06 18:30:10 +0100394 if( !renegotiating )
Ronald Crond491c2d2022-02-19 18:30:46 +0100395 {
396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
397 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
398 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
399 p += 2;
400 }
401
Ronald Cron3d580bf2022-02-18 17:24:56 +0100402 /* Write the cipher_suites length in number of bytes */
403 cipher_suites_len = p - cipher_suites;
404 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
405 MBEDTLS_SSL_DEBUG_MSG( 3,
406 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
407 cipher_suites_len/2 ) );
408
409 /* Output the total length of cipher_suites field. */
410 *out_len = p - buf;
411
412 return( 0 );
413}
414
415/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100416 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100417 *
418 * struct {
419 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
420 * Random random;
421 * opaque legacy_session_id<0..32>;
422 * CipherSuite cipher_suites<2..2^16-2>;
423 * opaque legacy_compression_methods<1..2^8-1>;
424 * Extension extensions<8..2^16-1>;
425 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100426 *
427 * Structure of the (D)TLS 1.2 ClientHello message:
428 *
429 * struct {
430 * ProtocolVersion client_version;
431 * Random random;
432 * SessionID session_id;
433 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
434 * CipherSuite cipher_suites<2..2^16-2>;
435 * CompressionMethod compression_methods<1..2^8-1>;
436 * select (extensions_present) {
437 * case false:
438 * struct {};
439 * case true:
440 * Extension extensions<0..2^16-1>;
441 * };
442 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100443 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200444MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100445static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
446 unsigned char *buf,
447 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000448 size_t *out_len,
449 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100450{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100451 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100452 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100453 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100454 unsigned char *p_extensions_len; /* Pointer to extensions length */
455 size_t output_len; /* Length of buffer used by function */
456 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100457 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100458
Ronald Cron3d580bf2022-02-18 17:24:56 +0100459 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000460 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100461
Ronald Cron4079abc2022-02-20 10:35:26 +0100462#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200463 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400464 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200465 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400466 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100467#endif
468#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200469 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400470 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200471 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400472 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100473#endif
474
Ronald Cron3d580bf2022-02-18 17:24:56 +0100475 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100476 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100477 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100478 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100479 */
480 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400481 mbedtls_ssl_write_version( p, ssl->conf->transport,
482 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100483 p += 2;
484
Ronald Cron58b80382022-02-18 18:41:08 +0100485 /* ...
486 * Random random;
487 * ...
488 *
Ronald Cron58b80382022-02-18 18:41:08 +0100489 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100490 * the handshake->randbytes buffer and are copied here into the output
491 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100492 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100493 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100494 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100495 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
496 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
497 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
498
Ronald Cron021b1782022-02-19 17:32:53 +0100499 /* TLS 1.2:
500 * ...
501 * SessionID session_id;
502 * ...
503 * with
504 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100505 *
Ronald Cron021b1782022-02-19 17:32:53 +0100506 * TLS 1.3:
507 * ...
508 * opaque legacy_session_id<0..32>;
509 * ...
510 *
Ronald Cronda41b382022-03-30 09:57:11 +0200511 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100512 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
513 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100514 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100515 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
516 *p++ = (unsigned char)ssl->session_negotiate->id_len;
517 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
518 p += ssl->session_negotiate->id_len;
519
520 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
521 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100522
Ronald Crona874aa82022-02-19 18:11:26 +0100523 /* DTLS 1.2 ONLY
524 * ...
525 * opaque cookie<0..2^8-1>;
526 * ...
527 */
528#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
529 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
530 {
531 unsigned char cookie_len = 0;
532
Ronald Cron4079abc2022-02-20 10:35:26 +0100533 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100534 {
535 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100536 handshake->cookie,
537 handshake->verify_cookie_len );
538 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100539 }
540
541 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
542 *p++ = cookie_len;
543 if( cookie_len > 0 )
544 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100545 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100546 p += cookie_len;
547 }
548 }
549#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
550
Ronald Cron3d580bf2022-02-18 17:24:56 +0100551 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100552 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
553 &tls12_uses_ec,
554 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100555 if( ret != 0 )
556 return( ret );
557 p += output_len;
558
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100559 /* Write legacy_compression_methods (TLS 1.3) or
560 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100561 *
562 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
563 * one byte set to zero, which corresponds to the 'null' compression
564 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100565 *
566 * For TLS 1.2 ClientHello, for security reasons we do not support
567 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100568 */
569 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
570 *p++ = 1;
571 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
572
573 /* Write extensions */
574
575#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
576 /* Keeping track of the included extensions */
Jerry Yu0c354a22022-08-29 15:25:36 +0800577 handshake->sent_extensions = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100578#endif
579
580 /* First write extensions, then the total length */
581 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
582 p_extensions_len = p;
583 p += 2;
584
Ronald Crondf823bf2022-03-29 18:57:54 +0200585#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
586 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100587 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100588 if( ret != 0 )
589 return( ret );
590 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200591#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100592
593#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100594 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100595 if( ret != 0 )
596 return( ret );
597 p += output_len;
598#endif /* MBEDTLS_SSL_ALPN */
599
600#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100601 if( propose_tls13 )
602 {
603 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
604 &output_len );
605 if( ret != 0 )
606 return( ret );
607 p += output_len;
608 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200609#endif
610
Ronald Cron4079abc2022-02-20 10:35:26 +0100611#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
612 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
613 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200614#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100615 ( propose_tls13 &&
616 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
617#endif
618#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
619 ( propose_tls12 && tls12_uses_ec ) ||
620#endif
621 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100622 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100623 ret = ssl_write_supported_groups_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 Cron4079abc2022-02-20 10:35:26 +0100628#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100629
Ronald Crone68ab4f2022-10-05 12:46:29 +0200630#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100631 if(
632#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
633 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
634#endif
635#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
636 propose_tls12 ||
637#endif
638 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100639 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000640 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100641 if( ret != 0 )
642 return( ret );
643 p += output_len;
644 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200645#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100646
Ronald Cron4079abc2022-02-20 10:35:26 +0100647#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
648 if( propose_tls12 )
649 {
650 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
651 tls12_uses_ec,
652 &output_len );
653 if( ret != 0 )
654 return( ret );
655 p += output_len;
656 }
657#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100658
Ronald Cron41a443a2022-10-04 16:38:25 +0200659#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000660 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
661 * MUST be the last extension in the ClientHello.
662 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000663 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
664 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000665 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000666 ssl, p, end, &output_len, binders_len );
667 if( ret != 0 )
668 return( ret );
669 p += output_len;
670 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200671#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000672
Ronald Cron3d580bf2022-02-18 17:24:56 +0100673 /* Write the length of the list of extensions. */
674 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100675
676 if( extensions_len == 0 )
677 p = p_extensions_len;
678 else
679 {
680 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
682 MBEDTLS_PRINTF_SIZET, extensions_len ) );
683 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
684 p_extensions_len, extensions_len );
685 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100686
687 *out_len = p - buf;
688 return( 0 );
689}
690
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200691MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100692static int ssl_generate_random( mbedtls_ssl_context *ssl )
693{
694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
695 unsigned char *randbytes = ssl->handshake->randbytes;
696 size_t gmt_unix_time_len = 0;
697
698 /*
699 * Generate the random bytes
700 *
701 * TLS 1.2 case:
702 * struct {
703 * uint32 gmt_unix_time;
704 * opaque random_bytes[28];
705 * } Random;
706 *
707 * TLS 1.3 case:
708 * opaque Random[32];
709 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400710 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100711 {
712#if defined(MBEDTLS_HAVE_TIME)
713 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
714 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
715 gmt_unix_time_len = 4;
716
717 MBEDTLS_SSL_DEBUG_MSG( 3,
718 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
719 (long long) gmt_unix_time ) );
720#endif /* MBEDTLS_HAVE_TIME */
721 }
722
723 ret = ssl->conf->f_rng( ssl->conf->p_rng,
724 randbytes + gmt_unix_time_len,
725 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
726 return( ret );
727}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000728
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200729MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100730static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100731{
732 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100733 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800734 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
735
736 if( session_negotiate == NULL )
737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100738
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800739#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
740 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
741 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800742
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800743 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800744 if( ssl->handshake->resume != 0 &&
745 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
746 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800747 {
748 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800749 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
750 if( session_negotiate->ticket_received > now ||
751 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800752 {
753 /* Without valid ticket, disable session resumption.*/
754 MBEDTLS_SSL_DEBUG_MSG(
755 3, ( "Ticket expired, disable session resumption" ) );
756 ssl->handshake->resume = 0;
757 }
758 }
759#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
760 MBEDTLS_SSL_SESSION_TICKETS &&
761 MBEDTLS_HAVE_TIME */
762
Ronald Cron3d580bf2022-02-18 17:24:56 +0100763 if( ssl->conf->f_rng == NULL )
764 {
765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
766 return( MBEDTLS_ERR_SSL_NO_RNG );
767 }
768
Ronald Cron86a477f2022-02-18 17:45:10 +0100769 /* Bet on the highest configured version if we are not in a TLS 1.2
770 * renegotiation or session resumption.
771 */
772#if defined(MBEDTLS_SSL_RENEGOTIATION)
773 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400774 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100775 else
776#endif
777 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100778 if( ssl->handshake->resume )
779 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800780 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400781 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100782 }
783 else
784 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400785 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400786 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100787 }
788 }
789
Ronald Cron58b80382022-02-18 18:41:08 +0100790 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200791 * Generate the random bytes, except when responding to a verify request
792 * where we MUST reuse the previoulsy generated random bytes
793 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100794 */
795#if defined(MBEDTLS_SSL_PROTO_DTLS)
796 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
797 ( ssl->handshake->cookie == NULL ) )
798#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100799 {
Ronald Cron58b80382022-02-18 18:41:08 +0100800 ret = ssl_generate_random( ssl );
801 if( ret != 0 )
802 {
803 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
804 return( ret );
805 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100806 }
807
Ronald Cron3d580bf2022-02-18 17:24:56 +0100808 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200809 * Prepare session identifier. At that point, the length of the session
810 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
811 * to zero, except in the case of a TLS 1.2 session renegotiation or
812 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100813 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800814 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100815
816#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400817 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100818 {
Ronald Cron021b1782022-02-19 17:32:53 +0100819 if( session_id_len < 16 || session_id_len > 32 ||
820#if defined(MBEDTLS_SSL_RENEGOTIATION)
821 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
822#endif
823 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100824 {
Ronald Cron021b1782022-02-19 17:32:53 +0100825 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100826 }
Ronald Cron021b1782022-02-19 17:32:53 +0100827
828#if defined(MBEDTLS_SSL_SESSION_TICKETS)
829 /*
830 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
831 * generate and include a Session ID in the TLS ClientHello."
832 */
David Horstmann4a285632022-10-06 18:30:10 +0100833 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100834#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann7aee0ec2022-10-25 10:38:25 +0100835 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
836 renegotiating = 1;
Ronald Cron021b1782022-02-19 17:32:53 +0100837#endif
David Horstmann4a285632022-10-06 18:30:10 +0100838 if( !renegotiating )
Ronald Cron021b1782022-02-19 17:32:53 +0100839 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800840 if( ( session_negotiate->ticket != NULL ) &&
841 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100842 {
843 session_id_len = 32;
844 }
845 }
846#endif /* MBEDTLS_SSL_SESSION_TICKETS */
847 }
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
849
850#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400851 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100852 {
853 /*
854 * Create a legacy session identifier for the purpose of middlebox
855 * compatibility only if one has not been created already, which is
856 * the case if we are here for the TLS 1.3 second ClientHello.
857 *
858 * Versions of TLS before TLS 1.3 supported a "session resumption"
859 * feature which has been merged with pre-shared keys in TLS 1.3
860 * version. A client which has a cached session ID set by a pre-TLS 1.3
861 * server SHOULD set this field to that value. In compatibility mode,
862 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
863 * session MUST generate a new 32-byte value. This value need not be
864 * random but SHOULD be unpredictable to avoid implementations fixating
865 * on a specific value (also known as ossification). Otherwise, it MUST
866 * be set as a zero-length vector ( i.e., a zero-valued single byte
867 * length field ).
868 */
869 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100870 }
871#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
872
Jerry Yu22c18c12022-10-11 15:58:51 +0800873 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100874 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800875 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100876 if( session_id_len > 0 )
877 {
878 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800879 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100880 session_id_len );
881 if( ret != 0 )
882 {
883 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
884 return( ret );
885 }
886 }
887 }
888
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000889#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000890 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000891 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000892 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
893 ssl->handshake->resume )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000894 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000895 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000896 session_negotiate->hostname != NULL;
897 if( ssl->hostname != NULL && session_negotiate->hostname != NULL )
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000898 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000899 hostname_mismatch = strcmp(
Xiaokang Qian307a7302022-10-12 11:14:32 +0000900 ssl->hostname, session_negotiate->hostname ) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000901 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000902
903 if( hostname_mismatch )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000904 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000905 MBEDTLS_SSL_DEBUG_MSG(
906 1, ( "Hostname mismatch the session ticket, "
907 "disable session resumption." ) );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000909 }
910 }
911 else
Xiaokang Qian03409292022-10-12 02:49:52 +0000912 {
Xiaokang Qian307a7302022-10-12 11:14:32 +0000913 return mbedtls_ssl_session_set_hostname( session_negotiate,
Xiaokang Qian03409292022-10-12 02:49:52 +0000914 ssl->hostname );
915 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000916#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000917 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000918 MBEDTLS_SSL_SERVER_NAME_INDICATION */
919
Ronald Cron3d580bf2022-02-18 17:24:56 +0100920 return( 0 );
921}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100922/*
923 * Write ClientHello handshake message.
924 * Handler for MBEDTLS_SSL_CLIENT_HELLO
925 */
926int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
927{
928 int ret = 0;
929 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000930 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100931
932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
933
Ronald Cron71c23322022-02-18 17:29:39 +0100934 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100935
936 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
937 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
938 &buf, &buf_len ) );
939
Ronald Cron71c23322022-02-18 17:29:39 +0100940 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
941 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000942 &msg_len,
943 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100944
Ronald Cron5f4e9122022-02-21 09:50:36 +0100945#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
946 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
947 {
948 ssl->out_msglen = msg_len + 4;
949 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100950
Ronald Cron8ecd9932022-03-29 12:26:54 +0200951 /*
952 * The two functions below may try to send data on the network and
953 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
954 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200955 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200956 * set the handshake state to the next state at that point to ensure
957 * that we will not write and send again a ClientHello when we
958 * eventually succeed in sending the pending data.
959 */
960 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
961
Ronald Cron5f4e9122022-02-21 09:50:36 +0100962 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
963 {
964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
965 return( ret );
966 }
967
Ronald Cron8ecd9932022-03-29 12:26:54 +0200968 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100969 {
970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
971 return( ret );
972 }
973 }
974 else
975#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
976 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000977
XiaokangQianadab9a62022-07-18 07:41:26 +0000978 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
979 msg_len );
980 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
Ronald Cron41a443a2022-10-04 16:38:25 +0200981#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000982 if( binders_len > 0 )
983 {
984 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000985 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000986 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000987 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
988 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000989 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200990#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000991
Ronald Cron5f4e9122022-02-21 09:50:36 +0100992 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
993 buf_len,
994 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200995 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100996 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100997
Ronald Cron3d580bf2022-02-18 17:24:56 +0100998
999cleanup:
1000
1001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1002 return ret;
1003}
1004
1005#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1006#endif /* MBEDTLS_SSL_CLI_C */