blob: d1a66401f489debd0505d650e718b4336dc61649 [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 {
Valerio Setti6eca7b42022-12-30 17:44:24 +0100269 if( mbedtls_ssl_get_ecp_group_id_from_tls_id( *group_list ) ==
270 MBEDTLS_ECP_DP_NONE )
271 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100272 continue;
Valerio Setti6eca7b42022-12-30 17:44:24 +0100273 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100274 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
275 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
276 p += 2;
277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
Valerio Setti6eca7b42022-12-30 17:44:24 +0100278 mbedtls_ssl_get_curve_name_from_tls_id( *group_list ),
279 *group_list ) );
Ronald Cronfbd9f992022-03-17 15:22:07 +0100280 }
281#endif /* MBEDTLS_ECP_C */
282 /* Add DHE groups here */
283
284 }
285
286 /* Length of named_group_list */
287 named_group_list_len = p - named_group_list;
288 if( named_group_list_len == 0 )
289 {
290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
291 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
292 }
293
294 /* Write extension_type */
295 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
296 /* Write extension_data_length */
297 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
298 /* Write length of named_group_list */
299 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
300
301 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
302 buf + 4, named_group_list_len + 2 );
303
304 *out_len = p - buf;
305
306#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800307 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
308 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS );
Ronald Cronfbd9f992022-03-17 15:22:07 +0100309#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
310
311 return( 0 );
312}
313
314#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
315 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
316
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200317MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100318static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100319 mbedtls_ssl_context *ssl,
320 unsigned char *buf,
321 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100322 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100323 size_t *out_len )
324{
325 unsigned char *p = buf;
326 const int *ciphersuite_list;
327 unsigned char *cipher_suites; /* Start of the cipher_suites list */
328 size_t cipher_suites_len;
329
Ronald Crond491c2d2022-02-19 18:30:46 +0100330 *tls12_uses_ec = 0;
331 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100332
333 /*
334 * Ciphersuite list
335 *
336 * This is a list of the symmetric cipher options supported by
337 * the client, specifically the record protection algorithm
338 * ( including secret key length ) and a hash to be used with
339 * HKDF, in descending order of client preference.
340 */
341 ciphersuite_list = ssl->conf->ciphersuite_list;
342
343 /* Check there is space for the cipher suite list length (2 bytes). */
344 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
345 p += 2;
346
Ronald Cron64767262022-03-31 14:13:57 +0200347 /* Write cipher_suites
348 * CipherSuite cipher_suites<2..2^16-2>;
349 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100350 cipher_suites = p;
351 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
352 {
353 int cipher_suite = ciphersuite_list[i];
354 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
355
356 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100357
Ronald Cron757a2ab2022-03-30 13:57:40 +0200358 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400359 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400360 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100361 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100362
363#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
364 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
365 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
366 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
367#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100368
369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
370 (unsigned int) cipher_suite,
371 ciphersuite_info->name ) );
372
373 /* Check there is space for the cipher suite identifier (2 bytes). */
374 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
375 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
376 p += 2;
377 }
378
Ronald Crond491c2d2022-02-19 18:30:46 +0100379 /*
380 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
381 */
David Horstmann4a285632022-10-06 18:30:10 +0100382 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100383#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann4a285632022-10-06 18:30:10 +0100384 renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE );
Ronald Crond491c2d2022-02-19 18:30:46 +0100385#endif
David Horstmann4a285632022-10-06 18:30:10 +0100386 if( !renegotiating )
Ronald Crond491c2d2022-02-19 18:30:46 +0100387 {
388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
389 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
390 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
391 p += 2;
392 }
393
Ronald Cron3d580bf2022-02-18 17:24:56 +0100394 /* Write the cipher_suites length in number of bytes */
395 cipher_suites_len = p - cipher_suites;
396 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
397 MBEDTLS_SSL_DEBUG_MSG( 3,
398 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
399 cipher_suites_len/2 ) );
400
401 /* Output the total length of cipher_suites field. */
402 *out_len = p - buf;
403
404 return( 0 );
405}
406
407/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100408 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100409 *
410 * struct {
411 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
412 * Random random;
413 * opaque legacy_session_id<0..32>;
414 * CipherSuite cipher_suites<2..2^16-2>;
415 * opaque legacy_compression_methods<1..2^8-1>;
416 * Extension extensions<8..2^16-1>;
417 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100418 *
419 * Structure of the (D)TLS 1.2 ClientHello message:
420 *
421 * struct {
422 * ProtocolVersion client_version;
423 * Random random;
424 * SessionID session_id;
425 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
426 * CipherSuite cipher_suites<2..2^16-2>;
427 * CompressionMethod compression_methods<1..2^8-1>;
428 * select (extensions_present) {
429 * case false:
430 * struct {};
431 * case true:
432 * Extension extensions<0..2^16-1>;
433 * };
434 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100435 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200436MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100437static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
438 unsigned char *buf,
439 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000440 size_t *out_len,
441 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100442{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100443 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100444 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100445 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100446 unsigned char *p_extensions_len; /* Pointer to extensions length */
447 size_t output_len; /* Length of buffer used by function */
448 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100449 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100450
Ronald Cron3d580bf2022-02-18 17:24:56 +0100451 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000452 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100453
Ronald Cron4079abc2022-02-20 10:35:26 +0100454#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200455 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400456 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200457 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400458 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100459#endif
460#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200461 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400462 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200463 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400464 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100465#endif
466
Ronald Cron3d580bf2022-02-18 17:24:56 +0100467 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100468 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100469 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100470 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100471 */
472 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400473 mbedtls_ssl_write_version( p, ssl->conf->transport,
474 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100475 p += 2;
476
Ronald Cron58b80382022-02-18 18:41:08 +0100477 /* ...
478 * Random random;
479 * ...
480 *
Ronald Cron58b80382022-02-18 18:41:08 +0100481 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100482 * the handshake->randbytes buffer and are copied here into the output
483 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100484 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100485 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100486 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100487 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
488 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
489 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
490
Ronald Cron021b1782022-02-19 17:32:53 +0100491 /* TLS 1.2:
492 * ...
493 * SessionID session_id;
494 * ...
495 * with
496 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100497 *
Ronald Cron021b1782022-02-19 17:32:53 +0100498 * TLS 1.3:
499 * ...
500 * opaque legacy_session_id<0..32>;
501 * ...
502 *
Ronald Cronda41b382022-03-30 09:57:11 +0200503 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100504 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
505 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100506 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100507 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
508 *p++ = (unsigned char)ssl->session_negotiate->id_len;
509 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
510 p += ssl->session_negotiate->id_len;
511
512 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
513 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100514
Ronald Crona874aa82022-02-19 18:11:26 +0100515 /* DTLS 1.2 ONLY
516 * ...
517 * opaque cookie<0..2^8-1>;
518 * ...
519 */
520#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
521 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
522 {
Jerry Yue01304f2022-04-07 10:51:55 +0800523#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
524 uint8_t cookie_len = 0;
525#else
526 uint16_t cookie_len = 0;
527#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100528
Ronald Cron4079abc2022-02-20 10:35:26 +0100529 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100530 {
531 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100532 handshake->cookie,
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800533 handshake->cookie_len );
534 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100535 }
536
537 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
Jerry Yue01304f2022-04-07 10:51:55 +0800538 *p++ = ( unsigned char )cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100539 if( cookie_len > 0 )
540 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100541 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100542 p += cookie_len;
543 }
544 }
545#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
546
Ronald Cron3d580bf2022-02-18 17:24:56 +0100547 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100548 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
549 &tls12_uses_ec,
550 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100551 if( ret != 0 )
552 return( ret );
553 p += output_len;
554
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100555 /* Write legacy_compression_methods (TLS 1.3) or
556 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100557 *
558 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
559 * one byte set to zero, which corresponds to the 'null' compression
560 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100561 *
562 * For TLS 1.2 ClientHello, for security reasons we do not support
563 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100564 */
565 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
566 *p++ = 1;
567 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
568
569 /* Write extensions */
570
571#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
572 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800573 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100574#endif
575
576 /* First write extensions, then the total length */
577 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
578 p_extensions_len = p;
579 p += 2;
580
Ronald Crondf823bf2022-03-29 18:57:54 +0200581#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
582 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100583 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100584 if( ret != 0 )
585 return( ret );
586 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200587#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100588
589#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100590 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100591 if( ret != 0 )
592 return( ret );
593 p += output_len;
594#endif /* MBEDTLS_SSL_ALPN */
595
596#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100597 if( propose_tls13 )
598 {
599 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
600 &output_len );
601 if( ret != 0 )
602 return( ret );
603 p += output_len;
604 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200605#endif
606
Ronald Cron4079abc2022-02-20 10:35:26 +0100607#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
608 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
609 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200610#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100611 ( propose_tls13 &&
612 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
613#endif
614#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
615 ( propose_tls12 && tls12_uses_ec ) ||
616#endif
617 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100618 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100619 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100620 if( ret != 0 )
621 return( ret );
622 p += output_len;
623 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100624#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100625
Ronald Crone68ab4f2022-10-05 12:46:29 +0200626#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100627 if(
628#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
629 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
630#endif
631#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
632 propose_tls12 ||
633#endif
634 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100635 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000636 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100637 if( ret != 0 )
638 return( ret );
639 p += output_len;
640 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200641#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100642
Ronald Cron4079abc2022-02-20 10:35:26 +0100643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
644 if( propose_tls12 )
645 {
646 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
647 tls12_uses_ec,
648 &output_len );
649 if( ret != 0 )
650 return( ret );
651 p += output_len;
652 }
653#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100654
Ronald Cron41a443a2022-10-04 16:38:25 +0200655#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000656 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
657 * MUST be the last extension in the ClientHello.
658 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000659 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
660 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000661 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000662 ssl, p, end, &output_len, binders_len );
663 if( ret != 0 )
664 return( ret );
665 p += output_len;
666 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200667#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000668
Ronald Cron3d580bf2022-02-18 17:24:56 +0100669 /* Write the length of the list of extensions. */
670 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100671
672 if( extensions_len == 0 )
673 p = p_extensions_len;
674 else
675 {
676 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
678 MBEDTLS_PRINTF_SIZET, extensions_len ) );
679 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
680 p_extensions_len, extensions_len );
681 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100682
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800683#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu7de2ff02022-11-08 21:43:46 +0800684 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu97be6a92022-11-09 22:43:31 +0800685 3, MBEDTLS_SSL_HS_CLIENT_HELLO, handshake->sent_extensions );
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800686#endif
687
Ronald Cron3d580bf2022-02-18 17:24:56 +0100688 *out_len = p - buf;
689 return( 0 );
690}
691
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200692MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100693static int ssl_generate_random( mbedtls_ssl_context *ssl )
694{
695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
696 unsigned char *randbytes = ssl->handshake->randbytes;
697 size_t gmt_unix_time_len = 0;
698
699 /*
700 * Generate the random bytes
701 *
702 * TLS 1.2 case:
703 * struct {
704 * uint32 gmt_unix_time;
705 * opaque random_bytes[28];
706 * } Random;
707 *
708 * TLS 1.3 case:
709 * opaque Random[32];
710 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400711 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100712 {
713#if defined(MBEDTLS_HAVE_TIME)
714 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
715 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
716 gmt_unix_time_len = 4;
717
718 MBEDTLS_SSL_DEBUG_MSG( 3,
719 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
720 (long long) gmt_unix_time ) );
721#endif /* MBEDTLS_HAVE_TIME */
722 }
723
724 ret = ssl->conf->f_rng( ssl->conf->p_rng,
725 randbytes + gmt_unix_time_len,
726 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
727 return( ret );
728}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000729
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200730MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100731static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100732{
733 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100734 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800735 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
736
737 if( session_negotiate == NULL )
738 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100739
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800740#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
741 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
742 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800743
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800744 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800745 if( ssl->handshake->resume != 0 &&
746 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
747 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800748 {
749 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800750 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
751 if( session_negotiate->ticket_received > now ||
752 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800753 {
754 /* Without valid ticket, disable session resumption.*/
755 MBEDTLS_SSL_DEBUG_MSG(
756 3, ( "Ticket expired, disable session resumption" ) );
757 ssl->handshake->resume = 0;
758 }
759 }
760#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
761 MBEDTLS_SSL_SESSION_TICKETS &&
762 MBEDTLS_HAVE_TIME */
763
Ronald Cron3d580bf2022-02-18 17:24:56 +0100764 if( ssl->conf->f_rng == NULL )
765 {
766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
767 return( MBEDTLS_ERR_SSL_NO_RNG );
768 }
769
Ronald Cron86a477f2022-02-18 17:45:10 +0100770 /* Bet on the highest configured version if we are not in a TLS 1.2
771 * renegotiation or session resumption.
772 */
773#if defined(MBEDTLS_SSL_RENEGOTIATION)
774 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400775 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100776 else
777#endif
778 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100779 if( ssl->handshake->resume )
780 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800781 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400782 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100783 }
784 else
785 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400786 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400787 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100788 }
789 }
790
Ronald Cron58b80382022-02-18 18:41:08 +0100791 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200792 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000793 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200794 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100795 */
796#if defined(MBEDTLS_SSL_PROTO_DTLS)
797 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
798 ( ssl->handshake->cookie == NULL ) )
799#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100800 {
Ronald Cron58b80382022-02-18 18:41:08 +0100801 ret = ssl_generate_random( ssl );
802 if( ret != 0 )
803 {
804 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
805 return( ret );
806 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100807 }
808
Ronald Cron3d580bf2022-02-18 17:24:56 +0100809 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200810 * Prepare session identifier. At that point, the length of the session
811 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
812 * to zero, except in the case of a TLS 1.2 session renegotiation or
813 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100814 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800815 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100816
817#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400818 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100819 {
Ronald Cron021b1782022-02-19 17:32:53 +0100820 if( session_id_len < 16 || session_id_len > 32 ||
821#if defined(MBEDTLS_SSL_RENEGOTIATION)
822 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
823#endif
824 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100825 {
Ronald Cron021b1782022-02-19 17:32:53 +0100826 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100827 }
Ronald Cron021b1782022-02-19 17:32:53 +0100828
829#if defined(MBEDTLS_SSL_SESSION_TICKETS)
830 /*
831 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
832 * generate and include a Session ID in the TLS ClientHello."
833 */
David Horstmann4a285632022-10-06 18:30:10 +0100834 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100835#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann7aee0ec2022-10-25 10:38:25 +0100836 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
837 renegotiating = 1;
Ronald Cron021b1782022-02-19 17:32:53 +0100838#endif
David Horstmann4a285632022-10-06 18:30:10 +0100839 if( !renegotiating )
Ronald Cron021b1782022-02-19 17:32:53 +0100840 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800841 if( ( session_negotiate->ticket != NULL ) &&
842 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100843 {
844 session_id_len = 32;
845 }
846 }
847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
848 }
849#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
850
851#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400852 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100853 {
854 /*
855 * Create a legacy session identifier for the purpose of middlebox
856 * compatibility only if one has not been created already, which is
857 * the case if we are here for the TLS 1.3 second ClientHello.
858 *
859 * Versions of TLS before TLS 1.3 supported a "session resumption"
860 * feature which has been merged with pre-shared keys in TLS 1.3
861 * version. A client which has a cached session ID set by a pre-TLS 1.3
862 * server SHOULD set this field to that value. In compatibility mode,
863 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
864 * session MUST generate a new 32-byte value. This value need not be
865 * random but SHOULD be unpredictable to avoid implementations fixating
866 * on a specific value (also known as ossification). Otherwise, it MUST
867 * be set as a zero-length vector ( i.e., a zero-valued single byte
868 * length field ).
869 */
870 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100871 }
872#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
873
Jerry Yu22c18c12022-10-11 15:58:51 +0800874 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100875 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800876 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100877 if( session_id_len > 0 )
878 {
879 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800880 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100881 session_id_len );
882 if( ret != 0 )
883 {
884 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
885 return( ret );
886 }
887 }
888 }
889
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000890#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000891 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000892 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000893 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
894 ssl->handshake->resume )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000895 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000896 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000897 session_negotiate->hostname != NULL;
898 if( ssl->hostname != NULL && session_negotiate->hostname != NULL )
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000899 {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000900 hostname_mismatch = strcmp(
Xiaokang Qian307a7302022-10-12 11:14:32 +0000901 ssl->hostname, session_negotiate->hostname ) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000902 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000903
904 if( hostname_mismatch )
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000905 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000906 MBEDTLS_SSL_DEBUG_MSG(
907 1, ( "Hostname mismatch the session ticket, "
908 "disable session resumption." ) );
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000910 }
911 }
912 else
Xiaokang Qian03409292022-10-12 02:49:52 +0000913 {
Xiaokang Qian307a7302022-10-12 11:14:32 +0000914 return mbedtls_ssl_session_set_hostname( session_negotiate,
Xiaokang Qian03409292022-10-12 02:49:52 +0000915 ssl->hostname );
916 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000917#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000918 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000919 MBEDTLS_SSL_SERVER_NAME_INDICATION */
920
Ronald Cron3d580bf2022-02-18 17:24:56 +0100921 return( 0 );
922}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100923/*
924 * Write ClientHello handshake message.
925 * Handler for MBEDTLS_SSL_CLIENT_HELLO
926 */
927int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
928{
929 int ret = 0;
930 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000931 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100932
933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
934
Ronald Cron71c23322022-02-18 17:29:39 +0100935 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100936
937 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
938 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
939 &buf, &buf_len ) );
940
Ronald Cron71c23322022-02-18 17:29:39 +0100941 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
942 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000943 &msg_len,
944 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100945
Ronald Cron5f4e9122022-02-21 09:50:36 +0100946#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
947 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
948 {
949 ssl->out_msglen = msg_len + 4;
950 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100951
Ronald Cron8ecd9932022-03-29 12:26:54 +0200952 /*
953 * The two functions below may try to send data on the network and
954 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
955 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200956 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200957 * set the handshake state to the next state at that point to ensure
958 * that we will not write and send again a ClientHello when we
959 * eventually succeed in sending the pending data.
960 */
961 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
962
Ronald Cron5f4e9122022-02-21 09:50:36 +0100963 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
964 {
965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
966 return( ret );
967 }
968
Ronald Cron8ecd9932022-03-29 12:26:54 +0200969 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100970 {
971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
972 return( ret );
973 }
974 }
975 else
976#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
977 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000978
XiaokangQianadab9a62022-07-18 07:41:26 +0000979 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
980 msg_len );
981 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
Ronald Cron41a443a2022-10-04 16:38:25 +0200982#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQianeb69aee2022-07-05 08:21:43 +0000983 if( binders_len > 0 )
984 {
985 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000986 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000987 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000988 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
989 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000990 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200991#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000992
Ronald Cron5f4e9122022-02-21 09:50:36 +0100993 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
994 buf_len,
995 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200996 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100997 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100998
Ronald Cron3d580bf2022-02-18 17:24:56 +0100999
1000cleanup:
1001
1002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1003 return ret;
1004}
1005
1006#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1007#endif /* MBEDTLS_SSL_CLI_C */