blob: e838845940b070cff46a93ccc1ae3ef3f85c109b [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#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100112 return( 0 );
113}
114#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
115
Ronald Cron3d580bf2022-02-18 17:24:56 +0100116#if defined(MBEDTLS_SSL_ALPN)
117/*
Ronald Cron71c23322022-02-18 17:29:39 +0100118 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100119 *
120 * Structure of the application_layer_protocol_negotiation extension in
121 * ClientHello:
122 *
123 * opaque ProtocolName<1..2^8-1>;
124 *
125 * struct {
126 * ProtocolName protocol_name_list<2..2^16-1>
127 * } ProtocolNameList;
128 *
129 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200130MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100131static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
132 unsigned char *buf,
133 const unsigned char *end,
134 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100135{
136 unsigned char *p = buf;
137
138 *out_len = 0;
139
140 if( ssl->conf->alpn_list == NULL )
141 return( 0 );
142
143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
144
145
146 /* Check we have enough space for the extension type (2 bytes), the
147 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
148 */
149 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
150 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
151 /* Skip writing extension and list length for now */
152 p += 6;
153
154 /*
155 * opaque ProtocolName<1..2^8-1>;
156 *
157 * struct {
158 * ProtocolName protocol_name_list<2..2^16-1>
159 * } ProtocolNameList;
160 */
161 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
162 {
163 /*
164 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
165 * protocol names is less than 255.
166 */
167 size_t protocol_name_len = strlen( *cur );
168
169 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
170 *p++ = (unsigned char)protocol_name_len;
171 memcpy( p, *cur, protocol_name_len );
172 p += protocol_name_len;
173 }
174
175 *out_len = p - buf;
176
177 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
178 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
179
180 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
181 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
182
Jerry Yu0c354a22022-08-29 15:25:36 +0800183#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800184 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
Jerry Yu0c354a22022-08-29 15:25:36 +0800185#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100186 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)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800305 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
306 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS );
Ronald Cronfbd9f992022-03-17 15:22:07 +0100307#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
308
309 return( 0 );
310}
311
312#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
313 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
314
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100316static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100317 mbedtls_ssl_context *ssl,
318 unsigned char *buf,
319 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100320 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100321 size_t *out_len )
322{
323 unsigned char *p = buf;
324 const int *ciphersuite_list;
325 unsigned char *cipher_suites; /* Start of the cipher_suites list */
326 size_t cipher_suites_len;
327
Ronald Crond491c2d2022-02-19 18:30:46 +0100328 *tls12_uses_ec = 0;
329 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100330
331 /*
332 * Ciphersuite list
333 *
334 * This is a list of the symmetric cipher options supported by
335 * the client, specifically the record protection algorithm
336 * ( including secret key length ) and a hash to be used with
337 * HKDF, in descending order of client preference.
338 */
339 ciphersuite_list = ssl->conf->ciphersuite_list;
340
341 /* Check there is space for the cipher suite list length (2 bytes). */
342 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
343 p += 2;
344
Ronald Cron64767262022-03-31 14:13:57 +0200345 /* Write cipher_suites
346 * CipherSuite cipher_suites<2..2^16-2>;
347 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100348 cipher_suites = p;
349 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
350 {
351 int cipher_suite = ciphersuite_list[i];
352 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
353
354 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100355
Ronald Cron757a2ab2022-03-30 13:57:40 +0200356 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400357 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400358 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100359 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100360
361#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
362 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
363 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
364 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
365#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100366
367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
368 (unsigned int) cipher_suite,
369 ciphersuite_info->name ) );
370
371 /* Check there is space for the cipher suite identifier (2 bytes). */
372 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
373 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
374 p += 2;
375 }
376
Ronald Crond491c2d2022-02-19 18:30:46 +0100377 /*
378 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
379 */
David Horstmann4a285632022-10-06 18:30:10 +0100380 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100381#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann4a285632022-10-06 18:30:10 +0100382 renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
Ronald Crond491c2d2022-02-19 18:30:46 +0100383#endif
David Horstmann4a285632022-10-06 18:30:10 +0100384 if( !renegotiating )
Ronald Crond491c2d2022-02-19 18:30:46 +0100385 {
386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
387 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
388 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
389 p += 2;
390 }
391
Ronald Cron3d580bf2022-02-18 17:24:56 +0100392 /* Write the cipher_suites length in number of bytes */
393 cipher_suites_len = p - cipher_suites;
394 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
395 MBEDTLS_SSL_DEBUG_MSG( 3,
396 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
397 cipher_suites_len/2 ) );
398
399 /* Output the total length of cipher_suites field. */
400 *out_len = p - buf;
401
402 return( 0 );
403}
404
405/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100406 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100407 *
408 * struct {
409 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
410 * Random random;
411 * opaque legacy_session_id<0..32>;
412 * CipherSuite cipher_suites<2..2^16-2>;
413 * opaque legacy_compression_methods<1..2^8-1>;
414 * Extension extensions<8..2^16-1>;
415 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100416 *
417 * Structure of the (D)TLS 1.2 ClientHello message:
418 *
419 * struct {
420 * ProtocolVersion client_version;
421 * Random random;
422 * SessionID session_id;
423 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
424 * CipherSuite cipher_suites<2..2^16-2>;
425 * CompressionMethod compression_methods<1..2^8-1>;
426 * select (extensions_present) {
427 * case false:
428 * struct {};
429 * case true:
430 * Extension extensions<0..2^16-1>;
431 * };
432 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100433 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200434MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100435static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
436 unsigned char *buf,
437 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000438 size_t *out_len,
439 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100440{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100441 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100442 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100443 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100444 unsigned char *p_extensions_len; /* Pointer to extensions length */
445 size_t output_len; /* Length of buffer used by function */
446 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100447 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100448
Ronald Cron3d580bf2022-02-18 17:24:56 +0100449 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000450 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100451
Ronald Cron4079abc2022-02-20 10:35:26 +0100452#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200453 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400454 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200455 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400456 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100457#endif
458#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200459 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400460 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200461 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400462 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100463#endif
464
Ronald Cron3d580bf2022-02-18 17:24:56 +0100465 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100466 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100467 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100468 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100469 */
470 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400471 mbedtls_ssl_write_version( p, ssl->conf->transport,
472 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100473 p += 2;
474
Ronald Cron58b80382022-02-18 18:41:08 +0100475 /* ...
476 * Random random;
477 * ...
478 *
Ronald Cron58b80382022-02-18 18:41:08 +0100479 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100480 * the handshake->randbytes buffer and are copied here into the output
481 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100482 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100483 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100484 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100485 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
486 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
487 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
488
Ronald Cron021b1782022-02-19 17:32:53 +0100489 /* TLS 1.2:
490 * ...
491 * SessionID session_id;
492 * ...
493 * with
494 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100495 *
Ronald Cron021b1782022-02-19 17:32:53 +0100496 * TLS 1.3:
497 * ...
498 * opaque legacy_session_id<0..32>;
499 * ...
500 *
Ronald Cronda41b382022-03-30 09:57:11 +0200501 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100502 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
503 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100504 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100505 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
506 *p++ = (unsigned char)ssl->session_negotiate->id_len;
507 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
508 p += ssl->session_negotiate->id_len;
509
510 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
511 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100512
Ronald Crona874aa82022-02-19 18:11:26 +0100513 /* DTLS 1.2 ONLY
514 * ...
515 * opaque cookie<0..2^8-1>;
516 * ...
517 */
518#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
520 {
Jerry Yue01304f2022-04-07 10:51:55 +0800521#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
522 uint8_t cookie_len = 0;
523#else
524 uint16_t cookie_len = 0;
525#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100526
Ronald Cron4079abc2022-02-20 10:35:26 +0100527 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100528 {
529 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100530 handshake->cookie,
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800531 handshake->cookie_len );
532 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100533 }
534
535 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
Jerry Yue01304f2022-04-07 10:51:55 +0800536 *p++ = ( unsigned char )cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100537 if( cookie_len > 0 )
538 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100539 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100540 p += cookie_len;
541 }
542 }
543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
544
Ronald Cron3d580bf2022-02-18 17:24:56 +0100545 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100546 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
547 &tls12_uses_ec,
548 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100549 if( ret != 0 )
550 return( ret );
551 p += output_len;
552
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100553 /* Write legacy_compression_methods (TLS 1.3) or
554 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100555 *
556 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
557 * one byte set to zero, which corresponds to the 'null' compression
558 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100559 *
560 * For TLS 1.2 ClientHello, for security reasons we do not support
561 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100562 */
563 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
564 *p++ = 1;
565 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
566
567 /* Write extensions */
568
569#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
570 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800571 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100572#endif
573
574 /* First write extensions, then the total length */
575 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
576 p_extensions_len = p;
577 p += 2;
578
Ronald Crondf823bf2022-03-29 18:57:54 +0200579#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
580 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100581 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100582 if( ret != 0 )
583 return( ret );
584 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200585#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100586
587#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100588 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100589 if( ret != 0 )
590 return( ret );
591 p += output_len;
592#endif /* MBEDTLS_SSL_ALPN */
593
594#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100595 if( propose_tls13 )
596 {
597 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
598 &output_len );
599 if( ret != 0 )
600 return( ret );
601 p += output_len;
602 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200603#endif
604
Ronald Cron4079abc2022-02-20 10:35:26 +0100605#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
606 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
607 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200608#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100609 ( propose_tls13 &&
610 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
611#endif
612#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
613 ( propose_tls12 && tls12_uses_ec ) ||
614#endif
615 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100616 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100617 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100618 if( ret != 0 )
619 return( ret );
620 p += output_len;
621 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100622#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100623
Ronald Crone68ab4f2022-10-05 12:46:29 +0200624#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100625 if(
626#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
627 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
628#endif
629#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
630 propose_tls12 ||
631#endif
632 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100633 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000634 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100635 if( ret != 0 )
636 return( ret );
637 p += output_len;
638 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200639#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100640
Ronald Cron4079abc2022-02-20 10:35:26 +0100641#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
642 if( propose_tls12 )
643 {
644 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
645 tls12_uses_ec,
646 &output_len );
647 if( ret != 0 )
648 return( ret );
649 p += output_len;
650 }
651#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100652
Ronald Cron41a443a2022-10-04 16:38:25 +0200653#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000654 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
655 * MUST be the last extension in the ClientHello.
656 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000657 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
658 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000659 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000660 ssl, p, end, &output_len, binders_len );
661 if( ret != 0 )
662 return( ret );
663 p += output_len;
664 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200665#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000666
Ronald Cron3d580bf2022-02-18 17:24:56 +0100667 /* Write the length of the list of extensions. */
668 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100669
670 if( extensions_len == 0 )
671 p = p_extensions_len;
672 else
673 {
674 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
676 MBEDTLS_PRINTF_SIZET, extensions_len ) );
677 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
678 p_extensions_len, extensions_len );
679 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100680
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800681#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu7de2ff02022-11-08 21:43:46 +0800682 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu97be6a92022-11-09 22:43:31 +0800683 3, MBEDTLS_SSL_HS_CLIENT_HELLO, handshake->sent_extensions );
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800684#endif
685
Ronald Cron3d580bf2022-02-18 17:24:56 +0100686 *out_len = p - buf;
687 return( 0 );
688}
689
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200690MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100691static int ssl_generate_random( mbedtls_ssl_context *ssl )
692{
693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
694 unsigned char *randbytes = ssl->handshake->randbytes;
695 size_t gmt_unix_time_len = 0;
696
697 /*
698 * Generate the random bytes
699 *
700 * TLS 1.2 case:
701 * struct {
702 * uint32 gmt_unix_time;
703 * opaque random_bytes[28];
704 * } Random;
705 *
706 * TLS 1.3 case:
707 * opaque Random[32];
708 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400709 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100710 {
711#if defined(MBEDTLS_HAVE_TIME)
712 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
713 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
714 gmt_unix_time_len = 4;
715
716 MBEDTLS_SSL_DEBUG_MSG( 3,
717 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
718 (long long) gmt_unix_time ) );
719#endif /* MBEDTLS_HAVE_TIME */
720 }
721
722 ret = ssl->conf->f_rng( ssl->conf->p_rng,
723 randbytes + gmt_unix_time_len,
724 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
725 return( ret );
726}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000727
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200728MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100729static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100730{
731 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100732 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800733 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
734
735 if( session_negotiate == NULL )
736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100737
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
739 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
740 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800741
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800742 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800743 if( ssl->handshake->resume != 0 &&
744 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
745 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800746 {
747 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800748 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
749 if( session_negotiate->ticket_received > now ||
750 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800751 {
752 /* Without valid ticket, disable session resumption.*/
753 MBEDTLS_SSL_DEBUG_MSG(
754 3, ( "Ticket expired, disable session resumption" ) );
755 ssl->handshake->resume = 0;
756 }
757 }
758#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
759 MBEDTLS_SSL_SESSION_TICKETS &&
760 MBEDTLS_HAVE_TIME */
761
Ronald Cron3d580bf2022-02-18 17:24:56 +0100762 if( ssl->conf->f_rng == NULL )
763 {
764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
765 return( MBEDTLS_ERR_SSL_NO_RNG );
766 }
767
Ronald Cron86a477f2022-02-18 17:45:10 +0100768 /* Bet on the highest configured version if we are not in a TLS 1.2
769 * renegotiation or session resumption.
770 */
771#if defined(MBEDTLS_SSL_RENEGOTIATION)
772 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400773 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100774 else
775#endif
776 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100777 if( ssl->handshake->resume )
778 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800779 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400780 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100781 }
782 else
783 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400784 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400785 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100786 }
787 }
788
Ronald Cron58b80382022-02-18 18:41:08 +0100789 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200790 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000791 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200792 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100793 */
794#if defined(MBEDTLS_SSL_PROTO_DTLS)
795 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
796 ( ssl->handshake->cookie == NULL ) )
797#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100798 {
Ronald Cron58b80382022-02-18 18:41:08 +0100799 ret = ssl_generate_random( ssl );
800 if( ret != 0 )
801 {
802 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
803 return( ret );
804 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100805 }
806
Ronald Cron3d580bf2022-02-18 17:24:56 +0100807 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200808 * Prepare session identifier. At that point, the length of the session
809 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
810 * to zero, except in the case of a TLS 1.2 session renegotiation or
811 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100812 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800813 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100814
815#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400816 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100817 {
Ronald Cron021b1782022-02-19 17:32:53 +0100818 if( session_id_len < 16 || session_id_len > 32 ||
819#if defined(MBEDTLS_SSL_RENEGOTIATION)
820 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
821#endif
822 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100823 {
Ronald Cron021b1782022-02-19 17:32:53 +0100824 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100825 }
Ronald Cron021b1782022-02-19 17:32:53 +0100826
827#if defined(MBEDTLS_SSL_SESSION_TICKETS)
828 /*
829 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
830 * generate and include a Session ID in the TLS ClientHello."
831 */
David Horstmann4a285632022-10-06 18:30:10 +0100832 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100833#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann7aee0ec2022-10-25 10:38:25 +0100834 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
835 renegotiating = 1;
Ronald Cron021b1782022-02-19 17:32:53 +0100836#endif
David Horstmann4a285632022-10-06 18:30:10 +0100837 if( !renegotiating )
Ronald Cron021b1782022-02-19 17:32:53 +0100838 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800839 if( ( session_negotiate->ticket != NULL ) &&
840 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100841 {
842 session_id_len = 32;
843 }
844 }
845#endif /* MBEDTLS_SSL_SESSION_TICKETS */
846 }
847#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
848
849#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400850 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100851 {
852 /*
853 * Create a legacy session identifier for the purpose of middlebox
854 * compatibility only if one has not been created already, which is
855 * the case if we are here for the TLS 1.3 second ClientHello.
856 *
857 * Versions of TLS before TLS 1.3 supported a "session resumption"
858 * feature which has been merged with pre-shared keys in TLS 1.3
859 * version. A client which has a cached session ID set by a pre-TLS 1.3
860 * server SHOULD set this field to that value. In compatibility mode,
861 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
862 * session MUST generate a new 32-byte value. This value need not be
863 * random but SHOULD be unpredictable to avoid implementations fixating
864 * on a specific value (also known as ossification). Otherwise, it MUST
865 * be set as a zero-length vector ( i.e., a zero-valued single byte
866 * length field ).
867 */
868 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100869 }
870#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
871
Jerry Yu22c18c12022-10-11 15:58:51 +0800872 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100873 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800874 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100875 if( session_id_len > 0 )
876 {
877 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800878 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100879 session_id_len );
880 if( ret != 0 )
881 {
882 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
883 return( ret );
884 }
885 }
886 }
887
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000888#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000889 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000890 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000891 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
892 ssl->handshake->resume )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000893 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000894 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000895 session_negotiate->hostname != NULL;
896 if( ssl->hostname != NULL && session_negotiate->hostname != NULL )
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000897 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000898 hostname_mismatch = strcmp(
Xiaokang Qian307a7302022-10-12 11:14:32 +0000899 ssl->hostname, session_negotiate->hostname ) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000900 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000901
902 if( hostname_mismatch )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000903 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000904 MBEDTLS_SSL_DEBUG_MSG(
905 1, ( "Hostname mismatch the session ticket, "
906 "disable session resumption." ) );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000908 }
909 }
910 else
Xiaokang Qian03409292022-10-12 02:49:52 +0000911 {
Xiaokang Qian307a7302022-10-12 11:14:32 +0000912 return mbedtls_ssl_session_set_hostname( session_negotiate,
Xiaokang Qian03409292022-10-12 02:49:52 +0000913 ssl->hostname );
914 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000915#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000916 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000917 MBEDTLS_SSL_SERVER_NAME_INDICATION */
918
Ronald Cron3d580bf2022-02-18 17:24:56 +0100919 return( 0 );
920}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100921/*
922 * Write ClientHello handshake message.
923 * Handler for MBEDTLS_SSL_CLIENT_HELLO
924 */
925int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
926{
927 int ret = 0;
928 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000929 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100930
931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
932
Ronald Cron71c23322022-02-18 17:29:39 +0100933 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100934
935 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
936 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
937 &buf, &buf_len ) );
938
Ronald Cron71c23322022-02-18 17:29:39 +0100939 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
940 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000941 &msg_len,
942 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100943
Ronald Cron5f4e9122022-02-21 09:50:36 +0100944#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
945 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
946 {
947 ssl->out_msglen = msg_len + 4;
948 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100949
Ronald Cron8ecd9932022-03-29 12:26:54 +0200950 /*
951 * The two functions below may try to send data on the network and
952 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
953 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200954 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200955 * set the handshake state to the next state at that point to ensure
956 * that we will not write and send again a ClientHello when we
957 * eventually succeed in sending the pending data.
958 */
959 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
960
Ronald Cron5f4e9122022-02-21 09:50:36 +0100961 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
962 {
963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
964 return( ret );
965 }
966
Ronald Cron8ecd9932022-03-29 12:26:54 +0200967 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100968 {
969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
970 return( ret );
971 }
972 }
973 else
974#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
975 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000976
XiaokangQianadab9a62022-07-18 07:41:26 +0000977 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
978 msg_len );
979 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
Ronald Cron41a443a2022-10-04 16:38:25 +0200980#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000981 if( binders_len > 0 )
982 {
983 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000984 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000985 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000986 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
987 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000988 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200989#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000990
Ronald Cron5f4e9122022-02-21 09:50:36 +0100991 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
992 buf_len,
993 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200994 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100995 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100996
Ronald Cron3d580bf2022-02-18 17:24:56 +0100997
998cleanup:
999
1000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1001 return ret;
1002}
1003
1004#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1005#endif /* MBEDTLS_SSL_CLI_C */