blob: 925d0c2018129c6a516212641c83cfc31556e6d4 [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
27#include <string.h>
28
29#include "mbedtls/debug.h"
30#include "mbedtls/error.h"
Jerry Yu141bbe72022-12-01 20:30:41 +080031#include "mbedtls/platform.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010032
33#include "ssl_client.h"
34#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010035#include "ssl_tls13_keys.h"
36#include "ssl_debug_helpers.h"
37
Ronald Cronfbd9f992022-03-17 15:22:07 +010038#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020039MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +010040static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
41 unsigned char *buf,
42 const unsigned char *end,
43 size_t *olen )
44{
45 unsigned char *p = buf;
46 size_t hostname_len;
47
48 *olen = 0;
49
50 if( ssl->hostname == NULL )
51 return( 0 );
52
53 MBEDTLS_SSL_DEBUG_MSG( 3,
54 ( "client hello, adding server name extension: %s",
55 ssl->hostname ) );
56
57 hostname_len = strlen( ssl->hostname );
58
59 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
60
61 /*
62 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
63 *
64 * In order to provide any of the server names, clients MAY include an
65 * extension of type "server_name" in the (extended) client hello. The
66 * "extension_data" field of this extension SHALL contain
67 * "ServerNameList" where:
68 *
69 * struct {
70 * NameType name_type;
71 * select (name_type) {
72 * case host_name: HostName;
73 * } name;
74 * } ServerName;
75 *
76 * enum {
77 * host_name(0), (255)
78 * } NameType;
79 *
80 * opaque HostName<1..2^16-1>;
81 *
82 * struct {
83 * ServerName server_name_list<1..2^16-1>
84 * } ServerNameList;
85 *
86 */
87 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
88 p += 2;
89
90 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
91 p += 2;
92
93 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
94 p += 2;
95
96 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
97
98 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
99 p += 2;
100
101 memcpy( p, ssl->hostname, hostname_len );
102
103 *olen = hostname_len + 9;
104
Jerry Yu0c354a22022-08-29 15:25:36 +0800105#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800106 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SERVERNAME );
Jerry Yu0c354a22022-08-29 15:25:36 +0800107#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100108 return( 0 );
109}
110#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
111
Ronald Cron3d580bf2022-02-18 17:24:56 +0100112#if defined(MBEDTLS_SSL_ALPN)
113/*
Ronald Cron71c23322022-02-18 17:29:39 +0100114 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100115 *
116 * Structure of the application_layer_protocol_negotiation extension in
117 * ClientHello:
118 *
119 * opaque ProtocolName<1..2^8-1>;
120 *
121 * struct {
122 * ProtocolName protocol_name_list<2..2^16-1>
123 * } ProtocolNameList;
124 *
125 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200126MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100127static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
128 unsigned char *buf,
129 const unsigned char *end,
130 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100131{
132 unsigned char *p = buf;
133
134 *out_len = 0;
135
136 if( ssl->conf->alpn_list == NULL )
137 return( 0 );
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
140
141
142 /* Check we have enough space for the extension type (2 bytes), the
143 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
144 */
145 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
146 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
147 /* Skip writing extension and list length for now */
148 p += 6;
149
150 /*
151 * opaque ProtocolName<1..2^8-1>;
152 *
153 * struct {
154 * ProtocolName protocol_name_list<2..2^16-1>
155 * } ProtocolNameList;
156 */
157 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
158 {
159 /*
160 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
161 * protocol names is less than 255.
162 */
163 size_t protocol_name_len = strlen( *cur );
164
165 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
166 *p++ = (unsigned char)protocol_name_len;
167 memcpy( p, *cur, protocol_name_len );
168 p += protocol_name_len;
169 }
170
171 *out_len = p - buf;
172
173 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
174 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
175
176 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
177 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
178
Jerry Yu0c354a22022-08-29 15:25:36 +0800179#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800180 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
Jerry Yu0c354a22022-08-29 15:25:36 +0800181#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100182 return( 0 );
183}
184#endif /* MBEDTLS_SSL_ALPN */
185
Ronald Cronfbd9f992022-03-17 15:22:07 +0100186#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
187 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
188/*
189 * Function for writing a supported groups (TLS 1.3) or supported elliptic
190 * curves (TLS 1.2) extension.
191 *
192 * The "extension_data" field of a supported groups extension contains a
193 * "NamedGroupList" value (TLS 1.3 RFC8446):
194 * enum {
195 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
196 * x25519(0x001D), x448(0x001E),
197 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
198 * ffdhe6144(0x0103), ffdhe8192(0x0104),
199 * ffdhe_private_use(0x01FC..0x01FF),
200 * ecdhe_private_use(0xFE00..0xFEFF),
201 * (0xFFFF)
202 * } NamedGroup;
203 * struct {
204 * NamedGroup named_group_list<2..2^16-1>;
205 * } NamedGroupList;
206 *
207 * The "extension_data" field of a supported elliptic curves extension contains
208 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
209 * enum {
210 * deprecated(1..22),
211 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
212 * x25519(29), x448(30),
213 * reserved (0xFE00..0xFEFF),
214 * deprecated(0xFF01..0xFF02),
215 * (0xFFFF)
216 * } NamedCurve;
217 * struct {
218 * NamedCurve named_curve_list<2..2^16-1>
219 * } NamedCurveList;
220 *
221 * The TLS 1.3 supported groups extension was defined to be a compatible
222 * generalization of the TLS 1.2 supported elliptic curves extension. They both
223 * share the same extension identifier.
224 *
225 * DHE groups are not supported yet.
226 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200227MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +0100228static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
229 unsigned char *buf,
230 const unsigned char *end,
231 size_t *out_len )
232{
233 unsigned char *p = buf ;
234 unsigned char *named_group_list; /* Start of named_group_list */
235 size_t named_group_list_len; /* Length of named_group_list */
236 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
237
238 *out_len = 0;
239
240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
241
242 /* Check if we have space for header and length fields:
243 * - extension_type (2 bytes)
244 * - extension_data_length (2 bytes)
245 * - named_group_list_length (2 bytes)
246 */
247 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
248 p += 6;
249
250 named_group_list = p;
251
252 if( group_list == NULL )
253 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
254
255 for( ; *group_list != 0; group_list++ )
256 {
257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
258
259#if defined(MBEDTLS_ECP_C)
260 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
261 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
262 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
263 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
264 {
Valerio Setti18c9fed2022-12-30 17:44:24 +0100265 if( mbedtls_ssl_get_ecp_group_id_from_tls_id( *group_list ) ==
266 MBEDTLS_ECP_DP_NONE )
267 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100268 continue;
Valerio Setti18c9fed2022-12-30 17:44:24 +0100269 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100270 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
271 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
272 p += 2;
273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
Valerio Setti18c9fed2022-12-30 17:44:24 +0100274 mbedtls_ssl_get_curve_name_from_tls_id( *group_list ),
275 *group_list ) );
Ronald Cronfbd9f992022-03-17 15:22:07 +0100276 }
277#endif /* MBEDTLS_ECP_C */
278 /* Add DHE groups here */
279
280 }
281
282 /* Length of named_group_list */
283 named_group_list_len = p - named_group_list;
284 if( named_group_list_len == 0 )
285 {
286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
288 }
289
290 /* Write extension_type */
291 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
292 /* Write extension_data_length */
293 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
294 /* Write length of named_group_list */
295 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
296
297 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
298 buf + 4, named_group_list_len + 2 );
299
300 *out_len = p - buf;
301
302#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800303 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
304 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS );
Ronald Cronfbd9f992022-03-17 15:22:07 +0100305#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
306
307 return( 0 );
308}
309
310#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
311 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
312
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200313MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100314static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100315 mbedtls_ssl_context *ssl,
316 unsigned char *buf,
317 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100318 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100319 size_t *out_len )
320{
321 unsigned char *p = buf;
322 const int *ciphersuite_list;
323 unsigned char *cipher_suites; /* Start of the cipher_suites list */
324 size_t cipher_suites_len;
325
Ronald Crond491c2d2022-02-19 18:30:46 +0100326 *tls12_uses_ec = 0;
327 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100328
329 /*
330 * Ciphersuite list
331 *
332 * This is a list of the symmetric cipher options supported by
333 * the client, specifically the record protection algorithm
334 * ( including secret key length ) and a hash to be used with
335 * HKDF, in descending order of client preference.
336 */
337 ciphersuite_list = ssl->conf->ciphersuite_list;
338
339 /* Check there is space for the cipher suite list length (2 bytes). */
340 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
341 p += 2;
342
Ronald Cron64767262022-03-31 14:13:57 +0200343 /* Write cipher_suites
344 * CipherSuite cipher_suites<2..2^16-2>;
345 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100346 cipher_suites = p;
347 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
348 {
349 int cipher_suite = ciphersuite_list[i];
350 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
351
352 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100353
Ronald Cron757a2ab2022-03-30 13:57:40 +0200354 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400355 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400356 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100357 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100358
359#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
360 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
361 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
362 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
363#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100364
365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
366 (unsigned int) cipher_suite,
367 ciphersuite_info->name ) );
368
369 /* Check there is space for the cipher suite identifier (2 bytes). */
370 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
371 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
372 p += 2;
373 }
374
Ronald Crond491c2d2022-02-19 18:30:46 +0100375 /*
376 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
377 */
David Horstmann4a285632022-10-06 18:30:10 +0100378 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100379#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann4a285632022-10-06 18:30:10 +0100380 renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
Ronald Crond491c2d2022-02-19 18:30:46 +0100381#endif
David Horstmann4a285632022-10-06 18:30:10 +0100382 if( !renegotiating )
Ronald Crond491c2d2022-02-19 18:30:46 +0100383 {
384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
385 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
386 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
387 p += 2;
388 }
389
Ronald Cron3d580bf2022-02-18 17:24:56 +0100390 /* Write the cipher_suites length in number of bytes */
391 cipher_suites_len = p - cipher_suites;
392 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
393 MBEDTLS_SSL_DEBUG_MSG( 3,
394 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
395 cipher_suites_len/2 ) );
396
397 /* Output the total length of cipher_suites field. */
398 *out_len = p - buf;
399
400 return( 0 );
401}
402
403/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100404 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100405 *
406 * struct {
407 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
408 * Random random;
409 * opaque legacy_session_id<0..32>;
410 * CipherSuite cipher_suites<2..2^16-2>;
411 * opaque legacy_compression_methods<1..2^8-1>;
412 * Extension extensions<8..2^16-1>;
413 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100414 *
415 * Structure of the (D)TLS 1.2 ClientHello message:
416 *
417 * struct {
418 * ProtocolVersion client_version;
419 * Random random;
420 * SessionID session_id;
421 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
422 * CipherSuite cipher_suites<2..2^16-2>;
423 * CompressionMethod compression_methods<1..2^8-1>;
424 * select (extensions_present) {
425 * case false:
426 * struct {};
427 * case true:
428 * Extension extensions<0..2^16-1>;
429 * };
430 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100431 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200432MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100433static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
434 unsigned char *buf,
435 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000436 size_t *out_len,
437 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100438{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100439 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100440 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100441 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100442 unsigned char *p_extensions_len; /* Pointer to extensions length */
443 size_t output_len; /* Length of buffer used by function */
444 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100445 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100446
Ronald Cron3d580bf2022-02-18 17:24:56 +0100447 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000448 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100449
Ronald Cron4079abc2022-02-20 10:35:26 +0100450#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200451 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400452 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200453 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400454 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100455#endif
456#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200457 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400458 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200459 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400460 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100461#endif
462
Ronald Cron3d580bf2022-02-18 17:24:56 +0100463 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100464 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100465 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100466 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100467 */
468 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400469 mbedtls_ssl_write_version( p, ssl->conf->transport,
470 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100471 p += 2;
472
Ronald Cron58b80382022-02-18 18:41:08 +0100473 /* ...
474 * Random random;
475 * ...
476 *
Ronald Cron58b80382022-02-18 18:41:08 +0100477 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100478 * the handshake->randbytes buffer and are copied here into the output
479 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100480 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100481 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100482 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100483 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
484 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
485 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
486
Ronald Cron021b1782022-02-19 17:32:53 +0100487 /* TLS 1.2:
488 * ...
489 * SessionID session_id;
490 * ...
491 * with
492 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100493 *
Ronald Cron021b1782022-02-19 17:32:53 +0100494 * TLS 1.3:
495 * ...
496 * opaque legacy_session_id<0..32>;
497 * ...
498 *
Ronald Cronda41b382022-03-30 09:57:11 +0200499 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100500 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
501 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100502 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100503 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
504 *p++ = (unsigned char)ssl->session_negotiate->id_len;
505 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
506 p += ssl->session_negotiate->id_len;
507
508 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
509 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100510
Ronald Crona874aa82022-02-19 18:11:26 +0100511 /* DTLS 1.2 ONLY
512 * ...
513 * opaque cookie<0..2^8-1>;
514 * ...
515 */
516#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
518 {
Jerry Yue01304f2022-04-07 10:51:55 +0800519#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
520 uint8_t cookie_len = 0;
521#else
522 uint16_t cookie_len = 0;
523#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100524
Ronald Cron4079abc2022-02-20 10:35:26 +0100525 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100526 {
527 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100528 handshake->cookie,
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800529 handshake->cookie_len );
530 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100531 }
532
533 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
Jerry Yue01304f2022-04-07 10:51:55 +0800534 *p++ = ( unsigned char )cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100535 if( cookie_len > 0 )
536 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100537 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100538 p += cookie_len;
539 }
540 }
541#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
542
Ronald Cron3d580bf2022-02-18 17:24:56 +0100543 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100544 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
545 &tls12_uses_ec,
546 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100547 if( ret != 0 )
548 return( ret );
549 p += output_len;
550
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100551 /* Write legacy_compression_methods (TLS 1.3) or
552 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100553 *
554 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
555 * one byte set to zero, which corresponds to the 'null' compression
556 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100557 *
558 * For TLS 1.2 ClientHello, for security reasons we do not support
559 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100560 */
561 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
562 *p++ = 1;
563 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
564
565 /* Write extensions */
566
567#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
568 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800569 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100570#endif
571
572 /* First write extensions, then the total length */
573 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
574 p_extensions_len = p;
575 p += 2;
576
Ronald Crondf823bf2022-03-29 18:57:54 +0200577#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
578 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100579 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100580 if( ret != 0 )
581 return( ret );
582 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200583#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100584
585#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100586 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100587 if( ret != 0 )
588 return( ret );
589 p += output_len;
590#endif /* MBEDTLS_SSL_ALPN */
591
592#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100593 if( propose_tls13 )
594 {
595 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
596 &output_len );
597 if( ret != 0 )
598 return( ret );
599 p += output_len;
600 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200601#endif
602
Ronald Cron4079abc2022-02-20 10:35:26 +0100603#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
604 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
605 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200606#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100607 ( propose_tls13 &&
608 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
609#endif
610#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
611 ( propose_tls12 && tls12_uses_ec ) ||
612#endif
613 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100614 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100615 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100616 if( ret != 0 )
617 return( ret );
618 p += output_len;
619 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100620#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100621
Ronald Crone68ab4f2022-10-05 12:46:29 +0200622#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100623 if(
624#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
625 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
626#endif
627#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
628 propose_tls12 ||
629#endif
630 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100631 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000632 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100633 if( ret != 0 )
634 return( ret );
635 p += output_len;
636 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200637#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100638
Ronald Cron4079abc2022-02-20 10:35:26 +0100639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
640 if( propose_tls12 )
641 {
642 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
643 tls12_uses_ec,
644 &output_len );
645 if( ret != 0 )
646 return( ret );
647 p += output_len;
648 }
649#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100650
Ronald Cron41a443a2022-10-04 16:38:25 +0200651#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000652 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
653 * MUST be the last extension in the ClientHello.
654 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000655 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
656 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000657 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000658 ssl, p, end, &output_len, binders_len );
659 if( ret != 0 )
660 return( ret );
661 p += output_len;
662 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200663#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000664
Ronald Cron3d580bf2022-02-18 17:24:56 +0100665 /* Write the length of the list of extensions. */
666 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100667
668 if( extensions_len == 0 )
669 p = p_extensions_len;
670 else
671 {
672 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
674 MBEDTLS_PRINTF_SIZET, extensions_len ) );
675 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
676 p_extensions_len, extensions_len );
677 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100678
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800679#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu7de2ff02022-11-08 21:43:46 +0800680 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu97be6a92022-11-09 22:43:31 +0800681 3, MBEDTLS_SSL_HS_CLIENT_HELLO, handshake->sent_extensions );
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800682#endif
683
Ronald Cron3d580bf2022-02-18 17:24:56 +0100684 *out_len = p - buf;
685 return( 0 );
686}
687
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200688MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100689static int ssl_generate_random( mbedtls_ssl_context *ssl )
690{
691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
692 unsigned char *randbytes = ssl->handshake->randbytes;
693 size_t gmt_unix_time_len = 0;
694
695 /*
696 * Generate the random bytes
697 *
698 * TLS 1.2 case:
699 * struct {
700 * uint32 gmt_unix_time;
701 * opaque random_bytes[28];
702 * } Random;
703 *
704 * TLS 1.3 case:
705 * opaque Random[32];
706 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400707 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100708 {
709#if defined(MBEDTLS_HAVE_TIME)
710 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
711 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
712 gmt_unix_time_len = 4;
713
714 MBEDTLS_SSL_DEBUG_MSG( 3,
715 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
716 (long long) gmt_unix_time ) );
717#endif /* MBEDTLS_HAVE_TIME */
718 }
719
720 ret = ssl->conf->f_rng( ssl->conf->p_rng,
721 randbytes + gmt_unix_time_len,
722 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
723 return( ret );
724}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000725
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200726MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100727static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100728{
729 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100730 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800731 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
732
733 if( session_negotiate == NULL )
734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100735
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800736#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
737 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
738 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800739
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800740 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800741 if( ssl->handshake->resume != 0 &&
742 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
743 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800744 {
745 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800746 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
747 if( session_negotiate->ticket_received > now ||
748 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800749 {
750 /* Without valid ticket, disable session resumption.*/
751 MBEDTLS_SSL_DEBUG_MSG(
752 3, ( "Ticket expired, disable session resumption" ) );
753 ssl->handshake->resume = 0;
754 }
755 }
756#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
757 MBEDTLS_SSL_SESSION_TICKETS &&
758 MBEDTLS_HAVE_TIME */
759
Ronald Cron3d580bf2022-02-18 17:24:56 +0100760 if( ssl->conf->f_rng == NULL )
761 {
762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
763 return( MBEDTLS_ERR_SSL_NO_RNG );
764 }
765
Ronald Cron86a477f2022-02-18 17:45:10 +0100766 /* Bet on the highest configured version if we are not in a TLS 1.2
767 * renegotiation or session resumption.
768 */
769#if defined(MBEDTLS_SSL_RENEGOTIATION)
770 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400771 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100772 else
773#endif
774 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100775 if( ssl->handshake->resume )
776 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800777 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400778 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100779 }
780 else
781 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400782 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400783 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100784 }
785 }
786
Ronald Cron58b80382022-02-18 18:41:08 +0100787 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200788 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000789 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200790 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100791 */
792#if defined(MBEDTLS_SSL_PROTO_DTLS)
793 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
794 ( ssl->handshake->cookie == NULL ) )
795#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100796 {
Ronald Cron58b80382022-02-18 18:41:08 +0100797 ret = ssl_generate_random( ssl );
798 if( ret != 0 )
799 {
800 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
801 return( ret );
802 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100803 }
804
Ronald Cron3d580bf2022-02-18 17:24:56 +0100805 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200806 * Prepare session identifier. At that point, the length of the session
807 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
808 * to zero, except in the case of a TLS 1.2 session renegotiation or
809 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100810 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800811 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100812
813#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400814 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100815 {
Ronald Cron021b1782022-02-19 17:32:53 +0100816 if( session_id_len < 16 || session_id_len > 32 ||
817#if defined(MBEDTLS_SSL_RENEGOTIATION)
818 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
819#endif
820 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100821 {
Ronald Cron021b1782022-02-19 17:32:53 +0100822 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100823 }
Ronald Cron021b1782022-02-19 17:32:53 +0100824
825#if defined(MBEDTLS_SSL_SESSION_TICKETS)
826 /*
827 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
828 * generate and include a Session ID in the TLS ClientHello."
829 */
David Horstmann4a285632022-10-06 18:30:10 +0100830 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100831#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann7aee0ec2022-10-25 10:38:25 +0100832 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
833 renegotiating = 1;
Ronald Cron021b1782022-02-19 17:32:53 +0100834#endif
David Horstmann4a285632022-10-06 18:30:10 +0100835 if( !renegotiating )
Ronald Cron021b1782022-02-19 17:32:53 +0100836 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800837 if( ( session_negotiate->ticket != NULL ) &&
838 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100839 {
840 session_id_len = 32;
841 }
842 }
843#endif /* MBEDTLS_SSL_SESSION_TICKETS */
844 }
845#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
846
847#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400848 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100849 {
850 /*
851 * Create a legacy session identifier for the purpose of middlebox
852 * compatibility only if one has not been created already, which is
853 * the case if we are here for the TLS 1.3 second ClientHello.
854 *
855 * Versions of TLS before TLS 1.3 supported a "session resumption"
856 * feature which has been merged with pre-shared keys in TLS 1.3
857 * version. A client which has a cached session ID set by a pre-TLS 1.3
858 * server SHOULD set this field to that value. In compatibility mode,
859 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
860 * session MUST generate a new 32-byte value. This value need not be
861 * random but SHOULD be unpredictable to avoid implementations fixating
862 * on a specific value (also known as ossification). Otherwise, it MUST
863 * be set as a zero-length vector ( i.e., a zero-valued single byte
864 * length field ).
865 */
866 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100867 }
868#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
869
Jerry Yu22c18c12022-10-11 15:58:51 +0800870 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100871 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800872 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100873 if( session_id_len > 0 )
874 {
875 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800876 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100877 session_id_len );
878 if( ret != 0 )
879 {
880 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
881 return( ret );
882 }
883 }
884 }
885
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000886#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000887 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000888 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000889 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
890 ssl->handshake->resume )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000891 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000892 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000893 session_negotiate->hostname != NULL;
894 if( ssl->hostname != NULL && session_negotiate->hostname != NULL )
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000895 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000896 hostname_mismatch = strcmp(
Xiaokang Qian307a7302022-10-12 11:14:32 +0000897 ssl->hostname, session_negotiate->hostname ) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000898 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000899
900 if( hostname_mismatch )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000901 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000902 MBEDTLS_SSL_DEBUG_MSG(
903 1, ( "Hostname mismatch the session ticket, "
904 "disable session resumption." ) );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000906 }
907 }
908 else
Xiaokang Qian03409292022-10-12 02:49:52 +0000909 {
Xiaokang Qian307a7302022-10-12 11:14:32 +0000910 return mbedtls_ssl_session_set_hostname( session_negotiate,
Xiaokang Qian03409292022-10-12 02:49:52 +0000911 ssl->hostname );
912 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000913#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000914 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000915 MBEDTLS_SSL_SERVER_NAME_INDICATION */
916
Ronald Cron3d580bf2022-02-18 17:24:56 +0100917 return( 0 );
918}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100919/*
920 * Write ClientHello handshake message.
921 * Handler for MBEDTLS_SSL_CLIENT_HELLO
922 */
923int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
924{
925 int ret = 0;
926 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000927 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100928
929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
930
Ronald Cron71c23322022-02-18 17:29:39 +0100931 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100932
933 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
934 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
935 &buf, &buf_len ) );
936
Ronald Cron71c23322022-02-18 17:29:39 +0100937 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
938 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000939 &msg_len,
940 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100941
Ronald Cron5f4e9122022-02-21 09:50:36 +0100942#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
944 {
945 ssl->out_msglen = msg_len + 4;
946 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100947
Ronald Cron8ecd9932022-03-29 12:26:54 +0200948 /*
949 * The two functions below may try to send data on the network and
950 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
951 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200952 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200953 * set the handshake state to the next state at that point to ensure
954 * that we will not write and send again a ClientHello when we
955 * eventually succeed in sending the pending data.
956 */
957 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
958
Ronald Cron5f4e9122022-02-21 09:50:36 +0100959 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
960 {
961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
962 return( ret );
963 }
964
Ronald Cron8ecd9932022-03-29 12:26:54 +0200965 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100966 {
967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
968 return( ret );
969 }
970 }
971 else
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
973 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000974
XiaokangQianadab9a62022-07-18 07:41:26 +0000975 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
976 msg_len );
977 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
Ronald Cron41a443a2022-10-04 16:38:25 +0200978#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000979 if( binders_len > 0 )
980 {
981 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000982 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000983 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000984 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
985 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000986 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200987#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000988
Ronald Cron5f4e9122022-02-21 09:50:36 +0100989 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
990 buf_len,
991 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200992 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100993 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100994
Ronald Cron3d580bf2022-02-18 17:24:56 +0100995
996cleanup:
997
998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
999 return ret;
1000}
1001
1002#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1003#endif /* MBEDTLS_SSL_CLI_C */