blob: b0d2dcf3ca7872cc41e5a1ddbc0035ac193e78c7 [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)
110 mbedtls_tls13_set_sent_ext_mask( ssl,
111 MBEDTLS_TLS_EXT_SERVERNAME );
112 MBEDTLS_SSL_DEBUG_MSG(
113 4, ( "sent %s extension",
114 mbedtls_tls13_get_extension_name(
115 MBEDTLS_TLS_EXT_SERVERNAME ) ) );
116#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100117 return( 0 );
118}
119#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
120
Ronald Cron3d580bf2022-02-18 17:24:56 +0100121#if defined(MBEDTLS_SSL_ALPN)
122/*
Ronald Cron71c23322022-02-18 17:29:39 +0100123 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100124 *
125 * Structure of the application_layer_protocol_negotiation extension in
126 * ClientHello:
127 *
128 * opaque ProtocolName<1..2^8-1>;
129 *
130 * struct {
131 * ProtocolName protocol_name_list<2..2^16-1>
132 * } ProtocolNameList;
133 *
134 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200135MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100136static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
137 unsigned char *buf,
138 const unsigned char *end,
139 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100140{
141 unsigned char *p = buf;
142
143 *out_len = 0;
144
145 if( ssl->conf->alpn_list == NULL )
146 return( 0 );
147
148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
149
150
151 /* Check we have enough space for the extension type (2 bytes), the
152 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
153 */
154 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
155 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
156 /* Skip writing extension and list length for now */
157 p += 6;
158
159 /*
160 * opaque ProtocolName<1..2^8-1>;
161 *
162 * struct {
163 * ProtocolName protocol_name_list<2..2^16-1>
164 * } ProtocolNameList;
165 */
166 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
167 {
168 /*
169 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
170 * protocol names is less than 255.
171 */
172 size_t protocol_name_len = strlen( *cur );
173
174 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
175 *p++ = (unsigned char)protocol_name_len;
176 memcpy( p, *cur, protocol_name_len );
177 p += protocol_name_len;
178 }
179
180 *out_len = p - buf;
181
182 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
183 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
184
185 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
186 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
187
Jerry Yu0c354a22022-08-29 15:25:36 +0800188#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
189 mbedtls_tls13_set_sent_ext_mask( ssl,
190 MBEDTLS_TLS_EXT_ALPN );
191 MBEDTLS_SSL_DEBUG_MSG(
192 4, ( "sent %s extension",
193 mbedtls_tls13_get_extension_name(
194 MBEDTLS_TLS_EXT_ALPN ) ) );
195#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100196 return( 0 );
197}
198#endif /* MBEDTLS_SSL_ALPN */
199
Ronald Cronfbd9f992022-03-17 15:22:07 +0100200#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
201 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
202/*
203 * Function for writing a supported groups (TLS 1.3) or supported elliptic
204 * curves (TLS 1.2) extension.
205 *
206 * The "extension_data" field of a supported groups extension contains a
207 * "NamedGroupList" value (TLS 1.3 RFC8446):
208 * enum {
209 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
210 * x25519(0x001D), x448(0x001E),
211 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
212 * ffdhe6144(0x0103), ffdhe8192(0x0104),
213 * ffdhe_private_use(0x01FC..0x01FF),
214 * ecdhe_private_use(0xFE00..0xFEFF),
215 * (0xFFFF)
216 * } NamedGroup;
217 * struct {
218 * NamedGroup named_group_list<2..2^16-1>;
219 * } NamedGroupList;
220 *
221 * The "extension_data" field of a supported elliptic curves extension contains
222 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
223 * enum {
224 * deprecated(1..22),
225 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
226 * x25519(29), x448(30),
227 * reserved (0xFE00..0xFEFF),
228 * deprecated(0xFF01..0xFF02),
229 * (0xFFFF)
230 * } NamedCurve;
231 * struct {
232 * NamedCurve named_curve_list<2..2^16-1>
233 * } NamedCurveList;
234 *
235 * The TLS 1.3 supported groups extension was defined to be a compatible
236 * generalization of the TLS 1.2 supported elliptic curves extension. They both
237 * share the same extension identifier.
238 *
239 * DHE groups are not supported yet.
240 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200241MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +0100242static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
243 unsigned char *buf,
244 const unsigned char *end,
245 size_t *out_len )
246{
247 unsigned char *p = buf ;
248 unsigned char *named_group_list; /* Start of named_group_list */
249 size_t named_group_list_len; /* Length of named_group_list */
250 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
251
252 *out_len = 0;
253
254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
255
256 /* Check if we have space for header and length fields:
257 * - extension_type (2 bytes)
258 * - extension_data_length (2 bytes)
259 * - named_group_list_length (2 bytes)
260 */
261 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
262 p += 6;
263
264 named_group_list = p;
265
266 if( group_list == NULL )
267 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
268
269 for( ; *group_list != 0; group_list++ )
270 {
271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
272
273#if defined(MBEDTLS_ECP_C)
274 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
275 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
276 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
277 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
278 {
279 const mbedtls_ecp_curve_info *curve_info;
280 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
281 if( curve_info == NULL )
282 continue;
283 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
284 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
285 p += 2;
286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
287 curve_info->name, *group_list ) );
288 }
289#endif /* MBEDTLS_ECP_C */
290 /* Add DHE groups here */
291
292 }
293
294 /* Length of named_group_list */
295 named_group_list_len = p - named_group_list;
296 if( named_group_list_len == 0 )
297 {
298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
300 }
301
302 /* Write extension_type */
303 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
304 /* Write extension_data_length */
305 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
306 /* Write length of named_group_list */
307 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
308
309 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
310 buf + 4, named_group_list_len + 2 );
311
312 *out_len = p - buf;
313
314#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu0c354a22022-08-29 15:25:36 +0800315 mbedtls_tls13_set_sent_ext_mask( ssl,
316 MBEDTLS_TLS_EXT_SUPPORTED_GROUPS );
317 MBEDTLS_SSL_DEBUG_MSG( 4, ( "sent %s extension",
318 mbedtls_tls13_get_extension_name(
319 MBEDTLS_TLS_EXT_SUPPORTED_GROUPS ) ) );
Ronald Cronfbd9f992022-03-17 15:22:07 +0100320#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
321
322 return( 0 );
323}
324
325#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
326 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
327
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200328MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100329static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100330 mbedtls_ssl_context *ssl,
331 unsigned char *buf,
332 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100333 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100334 size_t *out_len )
335{
336 unsigned char *p = buf;
337 const int *ciphersuite_list;
338 unsigned char *cipher_suites; /* Start of the cipher_suites list */
339 size_t cipher_suites_len;
340
Ronald Crond491c2d2022-02-19 18:30:46 +0100341 *tls12_uses_ec = 0;
342 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100343
344 /*
345 * Ciphersuite list
346 *
347 * This is a list of the symmetric cipher options supported by
348 * the client, specifically the record protection algorithm
349 * ( including secret key length ) and a hash to be used with
350 * HKDF, in descending order of client preference.
351 */
352 ciphersuite_list = ssl->conf->ciphersuite_list;
353
354 /* Check there is space for the cipher suite list length (2 bytes). */
355 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
356 p += 2;
357
Ronald Cron64767262022-03-31 14:13:57 +0200358 /* Write cipher_suites
359 * CipherSuite cipher_suites<2..2^16-2>;
360 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100361 cipher_suites = p;
362 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
363 {
364 int cipher_suite = ciphersuite_list[i];
365 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
366
367 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100368
Ronald Cron757a2ab2022-03-30 13:57:40 +0200369 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400370 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400371 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100372 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100373
374#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
375 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
376 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
377 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
378#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100379
380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
381 (unsigned int) cipher_suite,
382 ciphersuite_info->name ) );
383
384 /* Check there is space for the cipher suite identifier (2 bytes). */
385 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
386 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
387 p += 2;
388 }
389
Ronald Crond491c2d2022-02-19 18:30:46 +0100390 /*
391 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
392 */
David Horstmann4a285632022-10-06 18:30:10 +0100393 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100394#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann4a285632022-10-06 18:30:10 +0100395 renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
Ronald Crond491c2d2022-02-19 18:30:46 +0100396#endif
David Horstmann4a285632022-10-06 18:30:10 +0100397 if( !renegotiating )
Ronald Crond491c2d2022-02-19 18:30:46 +0100398 {
399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
400 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
401 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
402 p += 2;
403 }
404
Ronald Cron3d580bf2022-02-18 17:24:56 +0100405 /* Write the cipher_suites length in number of bytes */
406 cipher_suites_len = p - cipher_suites;
407 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
408 MBEDTLS_SSL_DEBUG_MSG( 3,
409 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
410 cipher_suites_len/2 ) );
411
412 /* Output the total length of cipher_suites field. */
413 *out_len = p - buf;
414
415 return( 0 );
416}
417
418/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100419 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100420 *
421 * struct {
422 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
423 * Random random;
424 * opaque legacy_session_id<0..32>;
425 * CipherSuite cipher_suites<2..2^16-2>;
426 * opaque legacy_compression_methods<1..2^8-1>;
427 * Extension extensions<8..2^16-1>;
428 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100429 *
430 * Structure of the (D)TLS 1.2 ClientHello message:
431 *
432 * struct {
433 * ProtocolVersion client_version;
434 * Random random;
435 * SessionID session_id;
436 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
437 * CipherSuite cipher_suites<2..2^16-2>;
438 * CompressionMethod compression_methods<1..2^8-1>;
439 * select (extensions_present) {
440 * case false:
441 * struct {};
442 * case true:
443 * Extension extensions<0..2^16-1>;
444 * };
445 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100446 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200447MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100448static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
449 unsigned char *buf,
450 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000451 size_t *out_len,
452 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100453{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100454 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100455 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100456 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100457 unsigned char *p_extensions_len; /* Pointer to extensions length */
458 size_t output_len; /* Length of buffer used by function */
459 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100460 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100461
Ronald Cron3d580bf2022-02-18 17:24:56 +0100462 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000463 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100464
Ronald Cron4079abc2022-02-20 10:35:26 +0100465#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200466 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400467 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200468 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400469 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100470#endif
471#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200472 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400473 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200474 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400475 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100476#endif
477
Ronald Cron3d580bf2022-02-18 17:24:56 +0100478 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100479 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100480 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100481 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100482 */
483 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400484 mbedtls_ssl_write_version( p, ssl->conf->transport,
485 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100486 p += 2;
487
Ronald Cron58b80382022-02-18 18:41:08 +0100488 /* ...
489 * Random random;
490 * ...
491 *
Ronald Cron58b80382022-02-18 18:41:08 +0100492 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100493 * the handshake->randbytes buffer and are copied here into the output
494 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100495 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100496 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100497 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100498 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
499 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
500 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
501
Ronald Cron021b1782022-02-19 17:32:53 +0100502 /* TLS 1.2:
503 * ...
504 * SessionID session_id;
505 * ...
506 * with
507 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100508 *
Ronald Cron021b1782022-02-19 17:32:53 +0100509 * TLS 1.3:
510 * ...
511 * opaque legacy_session_id<0..32>;
512 * ...
513 *
Ronald Cronda41b382022-03-30 09:57:11 +0200514 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100515 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
516 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100517 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100518 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
519 *p++ = (unsigned char)ssl->session_negotiate->id_len;
520 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
521 p += ssl->session_negotiate->id_len;
522
523 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
524 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100525
Ronald Crona874aa82022-02-19 18:11:26 +0100526 /* DTLS 1.2 ONLY
527 * ...
528 * opaque cookie<0..2^8-1>;
529 * ...
530 */
531#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
532 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
533 {
534 unsigned char cookie_len = 0;
535
Ronald Cron4079abc2022-02-20 10:35:26 +0100536 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100537 {
538 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100539 handshake->cookie,
540 handshake->verify_cookie_len );
541 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100542 }
543
544 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
545 *p++ = cookie_len;
546 if( cookie_len > 0 )
547 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100548 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100549 p += cookie_len;
550 }
551 }
552#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
553
Ronald Cron3d580bf2022-02-18 17:24:56 +0100554 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100555 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
556 &tls12_uses_ec,
557 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100558 if( ret != 0 )
559 return( ret );
560 p += output_len;
561
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100562 /* Write legacy_compression_methods (TLS 1.3) or
563 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100564 *
565 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
566 * one byte set to zero, which corresponds to the 'null' compression
567 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100568 *
569 * For TLS 1.2 ClientHello, for security reasons we do not support
570 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100571 */
572 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
573 *p++ = 1;
574 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
575
576 /* Write extensions */
577
578#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
579 /* Keeping track of the included extensions */
Jerry Yu0c354a22022-08-29 15:25:36 +0800580 handshake->sent_extensions = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100581#endif
582
583 /* First write extensions, then the total length */
584 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
585 p_extensions_len = p;
586 p += 2;
587
Ronald Crondf823bf2022-03-29 18:57:54 +0200588#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
589 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100590 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100591 if( ret != 0 )
592 return( ret );
593 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200594#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100595
596#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100597 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100598 if( ret != 0 )
599 return( ret );
600 p += output_len;
601#endif /* MBEDTLS_SSL_ALPN */
602
603#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100604 if( propose_tls13 )
605 {
606 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
607 &output_len );
608 if( ret != 0 )
609 return( ret );
610 p += output_len;
611 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200612#endif
613
Ronald Cron4079abc2022-02-20 10:35:26 +0100614#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
615 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
616 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200617#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100618 ( propose_tls13 &&
619 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
620#endif
621#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
622 ( propose_tls12 && tls12_uses_ec ) ||
623#endif
624 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100625 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100626 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100627 if( ret != 0 )
628 return( ret );
629 p += output_len;
630 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100631#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100632
Ronald Crone68ab4f2022-10-05 12:46:29 +0200633#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100634 if(
635#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
636 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
637#endif
638#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
639 propose_tls12 ||
640#endif
641 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100642 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000643 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100644 if( ret != 0 )
645 return( ret );
646 p += output_len;
647 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200648#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100649
Ronald Cron4079abc2022-02-20 10:35:26 +0100650#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
651 if( propose_tls12 )
652 {
653 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
654 tls12_uses_ec,
655 &output_len );
656 if( ret != 0 )
657 return( ret );
658 p += output_len;
659 }
660#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100661
Ronald Cron41a443a2022-10-04 16:38:25 +0200662#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000663 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
664 * MUST be the last extension in the ClientHello.
665 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000666 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
667 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000668 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000669 ssl, p, end, &output_len, binders_len );
670 if( ret != 0 )
671 return( ret );
672 p += output_len;
673 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200674#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000675
Ronald Cron3d580bf2022-02-18 17:24:56 +0100676 /* Write the length of the list of extensions. */
677 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100678
679 if( extensions_len == 0 )
680 p = p_extensions_len;
681 else
682 {
683 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
685 MBEDTLS_PRINTF_SIZET, extensions_len ) );
686 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
687 p_extensions_len, extensions_len );
688 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100689
690 *out_len = p - buf;
691 return( 0 );
692}
693
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200694MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100695static int ssl_generate_random( mbedtls_ssl_context *ssl )
696{
697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
698 unsigned char *randbytes = ssl->handshake->randbytes;
699 size_t gmt_unix_time_len = 0;
700
701 /*
702 * Generate the random bytes
703 *
704 * TLS 1.2 case:
705 * struct {
706 * uint32 gmt_unix_time;
707 * opaque random_bytes[28];
708 * } Random;
709 *
710 * TLS 1.3 case:
711 * opaque Random[32];
712 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400713 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100714 {
715#if defined(MBEDTLS_HAVE_TIME)
716 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
717 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
718 gmt_unix_time_len = 4;
719
720 MBEDTLS_SSL_DEBUG_MSG( 3,
721 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
722 (long long) gmt_unix_time ) );
723#endif /* MBEDTLS_HAVE_TIME */
724 }
725
726 ret = ssl->conf->f_rng( ssl->conf->p_rng,
727 randbytes + gmt_unix_time_len,
728 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
729 return( ret );
730}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000731
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200732MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100733static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100734{
735 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100736 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800737 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
738
739 if( session_negotiate == NULL )
740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100741
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800742#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
743 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
744 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800745
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800746 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800747 if( ssl->handshake->resume != 0 &&
748 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
749 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800750 {
751 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800752 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
753 if( session_negotiate->ticket_received > now ||
754 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800755 {
756 /* Without valid ticket, disable session resumption.*/
757 MBEDTLS_SSL_DEBUG_MSG(
758 3, ( "Ticket expired, disable session resumption" ) );
759 ssl->handshake->resume = 0;
760 }
761 }
762#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
763 MBEDTLS_SSL_SESSION_TICKETS &&
764 MBEDTLS_HAVE_TIME */
765
Ronald Cron3d580bf2022-02-18 17:24:56 +0100766 if( ssl->conf->f_rng == NULL )
767 {
768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
769 return( MBEDTLS_ERR_SSL_NO_RNG );
770 }
771
Ronald Cron86a477f2022-02-18 17:45:10 +0100772 /* Bet on the highest configured version if we are not in a TLS 1.2
773 * renegotiation or session resumption.
774 */
775#if defined(MBEDTLS_SSL_RENEGOTIATION)
776 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400777 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100778 else
779#endif
780 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100781 if( ssl->handshake->resume )
782 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800783 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400784 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100785 }
786 else
787 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400788 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400789 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100790 }
791 }
792
Ronald Cron58b80382022-02-18 18:41:08 +0100793 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200794 * Generate the random bytes, except when responding to a verify request
795 * where we MUST reuse the previoulsy generated random bytes
796 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100797 */
798#if defined(MBEDTLS_SSL_PROTO_DTLS)
799 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
800 ( ssl->handshake->cookie == NULL ) )
801#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100802 {
Ronald Cron58b80382022-02-18 18:41:08 +0100803 ret = ssl_generate_random( ssl );
804 if( ret != 0 )
805 {
806 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
807 return( ret );
808 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100809 }
810
Ronald Cron3d580bf2022-02-18 17:24:56 +0100811 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200812 * Prepare session identifier. At that point, the length of the session
813 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
814 * to zero, except in the case of a TLS 1.2 session renegotiation or
815 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100816 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800817 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100818
819#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400820 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100821 {
Ronald Cron021b1782022-02-19 17:32:53 +0100822 if( session_id_len < 16 || session_id_len > 32 ||
823#if defined(MBEDTLS_SSL_RENEGOTIATION)
824 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
825#endif
826 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100827 {
Ronald Cron021b1782022-02-19 17:32:53 +0100828 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100829 }
Ronald Cron021b1782022-02-19 17:32:53 +0100830
831#if defined(MBEDTLS_SSL_SESSION_TICKETS)
832 /*
833 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
834 * generate and include a Session ID in the TLS ClientHello."
835 */
David Horstmann4a285632022-10-06 18:30:10 +0100836 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100837#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann7aee0ec2022-10-25 10:38:25 +0100838 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
839 renegotiating = 1;
Ronald Cron021b1782022-02-19 17:32:53 +0100840#endif
David Horstmann4a285632022-10-06 18:30:10 +0100841 if( !renegotiating )
Ronald Cron021b1782022-02-19 17:32:53 +0100842 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800843 if( ( session_negotiate->ticket != NULL ) &&
844 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100845 {
846 session_id_len = 32;
847 }
848 }
849#endif /* MBEDTLS_SSL_SESSION_TICKETS */
850 }
851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
852
853#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400854 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100855 {
856 /*
857 * Create a legacy session identifier for the purpose of middlebox
858 * compatibility only if one has not been created already, which is
859 * the case if we are here for the TLS 1.3 second ClientHello.
860 *
861 * Versions of TLS before TLS 1.3 supported a "session resumption"
862 * feature which has been merged with pre-shared keys in TLS 1.3
863 * version. A client which has a cached session ID set by a pre-TLS 1.3
864 * server SHOULD set this field to that value. In compatibility mode,
865 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
866 * session MUST generate a new 32-byte value. This value need not be
867 * random but SHOULD be unpredictable to avoid implementations fixating
868 * on a specific value (also known as ossification). Otherwise, it MUST
869 * be set as a zero-length vector ( i.e., a zero-valued single byte
870 * length field ).
871 */
872 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100873 }
874#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
875
Jerry Yu22c18c12022-10-11 15:58:51 +0800876 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100877 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800878 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100879 if( session_id_len > 0 )
880 {
881 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800882 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100883 session_id_len );
884 if( ret != 0 )
885 {
886 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
887 return( ret );
888 }
889 }
890 }
891
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000892#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000893 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000894 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000895 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
896 ssl->handshake->resume )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000897 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000898 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000899 session_negotiate->hostname != NULL;
900 if( ssl->hostname != NULL && session_negotiate->hostname != NULL )
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000901 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000902 hostname_mismatch = strcmp(
Xiaokang Qian307a7302022-10-12 11:14:32 +0000903 ssl->hostname, session_negotiate->hostname ) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000904 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000905
906 if( hostname_mismatch )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000907 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000908 MBEDTLS_SSL_DEBUG_MSG(
909 1, ( "Hostname mismatch the session ticket, "
910 "disable session resumption." ) );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000912 }
913 }
914 else
Xiaokang Qian03409292022-10-12 02:49:52 +0000915 {
Xiaokang Qian307a7302022-10-12 11:14:32 +0000916 return mbedtls_ssl_session_set_hostname( session_negotiate,
Xiaokang Qian03409292022-10-12 02:49:52 +0000917 ssl->hostname );
918 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000919#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000920 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000921 MBEDTLS_SSL_SERVER_NAME_INDICATION */
922
Ronald Cron3d580bf2022-02-18 17:24:56 +0100923 return( 0 );
924}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100925/*
926 * Write ClientHello handshake message.
927 * Handler for MBEDTLS_SSL_CLIENT_HELLO
928 */
929int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
930{
931 int ret = 0;
932 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000933 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100934
935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
936
Ronald Cron71c23322022-02-18 17:29:39 +0100937 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100938
939 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
940 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
941 &buf, &buf_len ) );
942
Ronald Cron71c23322022-02-18 17:29:39 +0100943 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
944 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000945 &msg_len,
946 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100947
Ronald Cron5f4e9122022-02-21 09:50:36 +0100948#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
949 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
950 {
951 ssl->out_msglen = msg_len + 4;
952 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100953
Ronald Cron8ecd9932022-03-29 12:26:54 +0200954 /*
955 * The two functions below may try to send data on the network and
956 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
957 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200958 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200959 * set the handshake state to the next state at that point to ensure
960 * that we will not write and send again a ClientHello when we
961 * eventually succeed in sending the pending data.
962 */
963 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
964
Ronald Cron5f4e9122022-02-21 09:50:36 +0100965 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
966 {
967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
968 return( ret );
969 }
970
Ronald Cron8ecd9932022-03-29 12:26:54 +0200971 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100972 {
973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
974 return( ret );
975 }
976 }
977 else
978#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
979 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000980
XiaokangQianadab9a62022-07-18 07:41:26 +0000981 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
982 msg_len );
983 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
Ronald Cron41a443a2022-10-04 16:38:25 +0200984#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000985 if( binders_len > 0 )
986 {
987 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000988 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000989 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000990 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
991 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000992 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200993#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000994
Ronald Cron5f4e9122022-02-21 09:50:36 +0100995 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
996 buf_len,
997 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200998 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100999 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001000
Ronald Cron3d580bf2022-02-18 17:24:56 +01001001
1002cleanup:
1003
1004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1005 return ret;
1006}
1007
1008#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1009#endif /* MBEDTLS_SSL_CLI_C */