blob: bd9edf15f0fb1be9dc36a31d3804faf7417b80d5 [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#if defined(MBEDTLS_PLATFORM_C)
28#include "mbedtls/platform.h"
29#else
30#include <stdlib.h>
31#define mbedtls_calloc calloc
32#define mbedtls_free free
33#endif
34
Ronald Cron3d580bf2022-02-18 17:24:56 +010035#include <string.h>
36
37#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Ronald Cron58b80382022-02-18 18:41:08 +010039#if defined(MBEDTLS_HAVE_TIME)
40#include "mbedtls/platform_time.h"
41#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +010042
43#include "ssl_client.h"
44#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010045#include "ssl_tls13_keys.h"
46#include "ssl_debug_helpers.h"
47
Ronald Cronfbd9f992022-03-17 15:22:07 +010048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020049MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +010050static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
51 unsigned char *buf,
52 const unsigned char *end,
53 size_t *olen )
54{
55 unsigned char *p = buf;
56 size_t hostname_len;
Xiaokang Qian281fd1b2022-09-20 11:35:41 +000057 size_t cmp_hostname_len;
Ronald Cronfbd9f992022-03-17 15:22:07 +010058
59 *olen = 0;
60
61 if( ssl->hostname == NULL )
62 return( 0 );
63
64 MBEDTLS_SSL_DEBUG_MSG( 3,
65 ( "client hello, adding server name extension: %s",
66 ssl->hostname ) );
67
Xiaokang Qian281fd1b2022-09-20 11:35:41 +000068 ssl->session_negotiate->hostname_mismatch = 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +010069 hostname_len = strlen( ssl->hostname );
70
Xiaokang Qian281fd1b2022-09-20 11:35:41 +000071 cmp_hostname_len = hostname_len < ssl->session_negotiate->hostname_len ?
72 hostname_len : ssl->session_negotiate->hostname_len;
73
74 if( hostname_len != ssl->session_negotiate->hostname_len ||
75 memcmp( ssl->hostname, ssl->session_negotiate->hostname, cmp_hostname_len ) )
76 ssl->session_negotiate->hostname_mismatch = 1;
77
78 if( ssl->session_negotiate->hostname == NULL )
79 {
80 ssl->session_negotiate->hostname = mbedtls_calloc( 1, hostname_len );
81 if( ssl->session_negotiate->hostname == NULL )
82 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
83 memcpy(ssl->session_negotiate->hostname, ssl->hostname, hostname_len);
84 }
85 ssl->session_negotiate->hostname_len = hostname_len;
86
Ronald Cronfbd9f992022-03-17 15:22:07 +010087 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
88
89 /*
90 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
91 *
92 * In order to provide any of the server names, clients MAY include an
93 * extension of type "server_name" in the (extended) client hello. The
94 * "extension_data" field of this extension SHALL contain
95 * "ServerNameList" where:
96 *
97 * struct {
98 * NameType name_type;
99 * select (name_type) {
100 * case host_name: HostName;
101 * } name;
102 * } ServerName;
103 *
104 * enum {
105 * host_name(0), (255)
106 * } NameType;
107 *
108 * opaque HostName<1..2^16-1>;
109 *
110 * struct {
111 * ServerName server_name_list<1..2^16-1>
112 * } ServerNameList;
113 *
114 */
115 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
116 p += 2;
117
118 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
119 p += 2;
120
121 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
122 p += 2;
123
124 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
125
126 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
127 p += 2;
128
129 memcpy( p, ssl->hostname, hostname_len );
130
131 *olen = hostname_len + 9;
132
133 return( 0 );
134}
135#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
136
Ronald Cron3d580bf2022-02-18 17:24:56 +0100137#if defined(MBEDTLS_SSL_ALPN)
138/*
Ronald Cron71c23322022-02-18 17:29:39 +0100139 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100140 *
141 * Structure of the application_layer_protocol_negotiation extension in
142 * ClientHello:
143 *
144 * opaque ProtocolName<1..2^8-1>;
145 *
146 * struct {
147 * ProtocolName protocol_name_list<2..2^16-1>
148 * } ProtocolNameList;
149 *
150 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200151MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100152static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
153 unsigned char *buf,
154 const unsigned char *end,
155 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100156{
157 unsigned char *p = buf;
158
159 *out_len = 0;
160
161 if( ssl->conf->alpn_list == NULL )
162 return( 0 );
163
164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
165
166
167 /* Check we have enough space for the extension type (2 bytes), the
168 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
169 */
170 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
171 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
172 /* Skip writing extension and list length for now */
173 p += 6;
174
175 /*
176 * opaque ProtocolName<1..2^8-1>;
177 *
178 * struct {
179 * ProtocolName protocol_name_list<2..2^16-1>
180 * } ProtocolNameList;
181 */
182 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
183 {
184 /*
185 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
186 * protocol names is less than 255.
187 */
188 size_t protocol_name_len = strlen( *cur );
189
190 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
191 *p++ = (unsigned char)protocol_name_len;
192 memcpy( p, *cur, protocol_name_len );
193 p += protocol_name_len;
194 }
195
196 *out_len = p - buf;
197
198 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
199 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
200
201 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
202 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
203
204 return( 0 );
205}
206#endif /* MBEDTLS_SSL_ALPN */
207
Ronald Cronfbd9f992022-03-17 15:22:07 +0100208#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
209 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
210/*
211 * Function for writing a supported groups (TLS 1.3) or supported elliptic
212 * curves (TLS 1.2) extension.
213 *
214 * The "extension_data" field of a supported groups extension contains a
215 * "NamedGroupList" value (TLS 1.3 RFC8446):
216 * enum {
217 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
218 * x25519(0x001D), x448(0x001E),
219 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
220 * ffdhe6144(0x0103), ffdhe8192(0x0104),
221 * ffdhe_private_use(0x01FC..0x01FF),
222 * ecdhe_private_use(0xFE00..0xFEFF),
223 * (0xFFFF)
224 * } NamedGroup;
225 * struct {
226 * NamedGroup named_group_list<2..2^16-1>;
227 * } NamedGroupList;
228 *
229 * The "extension_data" field of a supported elliptic curves extension contains
230 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
231 * enum {
232 * deprecated(1..22),
233 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
234 * x25519(29), x448(30),
235 * reserved (0xFE00..0xFEFF),
236 * deprecated(0xFF01..0xFF02),
237 * (0xFFFF)
238 * } NamedCurve;
239 * struct {
240 * NamedCurve named_curve_list<2..2^16-1>
241 * } NamedCurveList;
242 *
243 * The TLS 1.3 supported groups extension was defined to be a compatible
244 * generalization of the TLS 1.2 supported elliptic curves extension. They both
245 * share the same extension identifier.
246 *
247 * DHE groups are not supported yet.
248 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200249MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +0100250static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
251 unsigned char *buf,
252 const unsigned char *end,
253 size_t *out_len )
254{
255 unsigned char *p = buf ;
256 unsigned char *named_group_list; /* Start of named_group_list */
257 size_t named_group_list_len; /* Length of named_group_list */
258 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
259
260 *out_len = 0;
261
262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
263
264 /* Check if we have space for header and length fields:
265 * - extension_type (2 bytes)
266 * - extension_data_length (2 bytes)
267 * - named_group_list_length (2 bytes)
268 */
269 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
270 p += 6;
271
272 named_group_list = p;
273
274 if( group_list == NULL )
275 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
276
277 for( ; *group_list != 0; group_list++ )
278 {
279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
280
281#if defined(MBEDTLS_ECP_C)
282 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
283 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
284 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
285 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
286 {
287 const mbedtls_ecp_curve_info *curve_info;
288 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
289 if( curve_info == NULL )
290 continue;
291 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
292 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
293 p += 2;
294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
295 curve_info->name, *group_list ) );
296 }
297#endif /* MBEDTLS_ECP_C */
298 /* Add DHE groups here */
299
300 }
301
302 /* Length of named_group_list */
303 named_group_list_len = p - named_group_list;
304 if( named_group_list_len == 0 )
305 {
306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 /* Write extension_type */
311 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
312 /* Write extension_data_length */
313 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
314 /* Write length of named_group_list */
315 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
316
317 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
318 buf + 4, named_group_list_len + 2 );
319
320 *out_len = p - buf;
321
322#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
323 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
324#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
325
326 return( 0 );
327}
328
329#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
330 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
331
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200332MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100333static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100334 mbedtls_ssl_context *ssl,
335 unsigned char *buf,
336 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100337 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100338 size_t *out_len )
339{
340 unsigned char *p = buf;
341 const int *ciphersuite_list;
342 unsigned char *cipher_suites; /* Start of the cipher_suites list */
343 size_t cipher_suites_len;
344
Ronald Crond491c2d2022-02-19 18:30:46 +0100345 *tls12_uses_ec = 0;
346 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100347
348 /*
349 * Ciphersuite list
350 *
351 * This is a list of the symmetric cipher options supported by
352 * the client, specifically the record protection algorithm
353 * ( including secret key length ) and a hash to be used with
354 * HKDF, in descending order of client preference.
355 */
356 ciphersuite_list = ssl->conf->ciphersuite_list;
357
358 /* Check there is space for the cipher suite list length (2 bytes). */
359 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
360 p += 2;
361
Ronald Cron64767262022-03-31 14:13:57 +0200362 /* Write cipher_suites
363 * CipherSuite cipher_suites<2..2^16-2>;
364 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100365 cipher_suites = p;
366 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
367 {
368 int cipher_suite = ciphersuite_list[i];
369 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
370
371 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100372
Ronald Cron757a2ab2022-03-30 13:57:40 +0200373 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400374 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400375 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100376 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100377
378#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
379 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
380 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
381 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
382#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100383
384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
385 (unsigned int) cipher_suite,
386 ciphersuite_info->name ) );
387
388 /* Check there is space for the cipher suite identifier (2 bytes). */
389 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
390 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
391 p += 2;
392 }
393
Ronald Crond491c2d2022-02-19 18:30:46 +0100394 /*
395 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
396 */
397#if defined(MBEDTLS_SSL_RENEGOTIATION)
398 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
399#endif
400 {
401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
402 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
403 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
404 p += 2;
405 }
406
Ronald Cron3d580bf2022-02-18 17:24:56 +0100407 /* Write the cipher_suites length in number of bytes */
408 cipher_suites_len = p - cipher_suites;
409 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
410 MBEDTLS_SSL_DEBUG_MSG( 3,
411 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
412 cipher_suites_len/2 ) );
413
414 /* Output the total length of cipher_suites field. */
415 *out_len = p - buf;
416
417 return( 0 );
418}
419
420/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100421 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100422 *
423 * struct {
424 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
425 * Random random;
426 * opaque legacy_session_id<0..32>;
427 * CipherSuite cipher_suites<2..2^16-2>;
428 * opaque legacy_compression_methods<1..2^8-1>;
429 * Extension extensions<8..2^16-1>;
430 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100431 *
432 * Structure of the (D)TLS 1.2 ClientHello message:
433 *
434 * struct {
435 * ProtocolVersion client_version;
436 * Random random;
437 * SessionID session_id;
438 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
439 * CipherSuite cipher_suites<2..2^16-2>;
440 * CompressionMethod compression_methods<1..2^8-1>;
441 * select (extensions_present) {
442 * case false:
443 * struct {};
444 * case true:
445 * Extension extensions<0..2^16-1>;
446 * };
447 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100448 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200449MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100450static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
451 unsigned char *buf,
452 unsigned char *end,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000453 size_t *out_len,
454 size_t *binders_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100455{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100456 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100457 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100458 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100459 unsigned char *p_extensions_len; /* Pointer to extensions length */
460 size_t output_len; /* Length of buffer used by function */
461 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100462 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100463
Ronald Cron3d580bf2022-02-18 17:24:56 +0100464 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000465 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100466
Ronald Cron4079abc2022-02-20 10:35:26 +0100467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200468 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400469 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200470 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400471 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100472#endif
473#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200474 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400475 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200476 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400477 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100478#endif
479
Ronald Cron3d580bf2022-02-18 17:24:56 +0100480 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100481 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100482 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100483 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100484 */
485 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400486 mbedtls_ssl_write_version( p, ssl->conf->transport,
487 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100488 p += 2;
489
Ronald Cron58b80382022-02-18 18:41:08 +0100490 /* ...
491 * Random random;
492 * ...
493 *
Ronald Cron58b80382022-02-18 18:41:08 +0100494 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100495 * the handshake->randbytes buffer and are copied here into the output
496 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100497 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100498 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100499 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100500 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
501 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
502 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
503
Ronald Cron021b1782022-02-19 17:32:53 +0100504 /* TLS 1.2:
505 * ...
506 * SessionID session_id;
507 * ...
508 * with
509 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100510 *
Ronald Cron021b1782022-02-19 17:32:53 +0100511 * TLS 1.3:
512 * ...
513 * opaque legacy_session_id<0..32>;
514 * ...
515 *
Ronald Cronda41b382022-03-30 09:57:11 +0200516 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100517 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
518 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100519 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100520 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
521 *p++ = (unsigned char)ssl->session_negotiate->id_len;
522 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
523 p += ssl->session_negotiate->id_len;
524
525 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
526 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100527
Ronald Crona874aa82022-02-19 18:11:26 +0100528 /* DTLS 1.2 ONLY
529 * ...
530 * opaque cookie<0..2^8-1>;
531 * ...
532 */
533#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
535 {
536 unsigned char cookie_len = 0;
537
Ronald Cron4079abc2022-02-20 10:35:26 +0100538 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100539 {
540 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100541 handshake->cookie,
542 handshake->verify_cookie_len );
543 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100544 }
545
546 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
547 *p++ = cookie_len;
548 if( cookie_len > 0 )
549 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100550 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100551 p += cookie_len;
552 }
553 }
554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
555
Ronald Cron3d580bf2022-02-18 17:24:56 +0100556 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100557 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
558 &tls12_uses_ec,
559 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100560 if( ret != 0 )
561 return( ret );
562 p += output_len;
563
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100564 /* Write legacy_compression_methods (TLS 1.3) or
565 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100566 *
567 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
568 * one byte set to zero, which corresponds to the 'null' compression
569 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100570 *
571 * For TLS 1.2 ClientHello, for security reasons we do not support
572 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100573 */
574 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
575 *p++ = 1;
576 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
577
578 /* Write extensions */
579
580#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
581 /* Keeping track of the included extensions */
Ronald Cron4079abc2022-02-20 10:35:26 +0100582 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100583#endif
584
585 /* First write extensions, then the total length */
586 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
587 p_extensions_len = p;
588 p += 2;
589
Ronald Crondf823bf2022-03-29 18:57:54 +0200590#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
591 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100592 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100593 if( ret != 0 )
594 return( ret );
595 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200596#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100597
598#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100599 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100600 if( ret != 0 )
601 return( ret );
602 p += output_len;
603#endif /* MBEDTLS_SSL_ALPN */
604
605#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100606 if( propose_tls13 )
607 {
608 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
609 &output_len );
610 if( ret != 0 )
611 return( ret );
612 p += output_len;
613 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200614#endif
615
Ronald Cron4079abc2022-02-20 10:35:26 +0100616#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
617 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
618 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200619#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100620 ( propose_tls13 &&
621 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
622#endif
623#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
624 ( propose_tls12 && tls12_uses_ec ) ||
625#endif
626 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100627 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100628 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100629 if( ret != 0 )
630 return( ret );
631 p += output_len;
632 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100633#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100634
Ronald Cron11e18572022-03-17 13:44:33 +0100635#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100636 if(
637#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
638 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
639#endif
640#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
641 propose_tls12 ||
642#endif
643 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100644 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000645 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100646 if( ret != 0 )
647 return( ret );
648 p += output_len;
649 }
650#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100651
Ronald Cron4079abc2022-02-20 10:35:26 +0100652#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
653 if( propose_tls12 )
654 {
655 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
656 tls12_uses_ec,
657 &output_len );
658 if( ret != 0 )
659 return( ret );
660 p += output_len;
661 }
662#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100663
XiaokangQianeb69aee2022-07-05 08:21:43 +0000664#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
665 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000666 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
667 * MUST be the last extension in the ClientHello.
668 */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000669 if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) )
670 {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000671 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000672 ssl, p, end, &output_len, binders_len );
673 if( ret != 0 )
674 return( ret );
675 p += output_len;
676 }
XiaokangQian008d2bf2022-07-14 07:54:01 +0000677#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000678
Ronald Cron3d580bf2022-02-18 17:24:56 +0100679 /* Write the length of the list of extensions. */
680 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100681
682 if( extensions_len == 0 )
683 p = p_extensions_len;
684 else
685 {
686 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
688 MBEDTLS_PRINTF_SIZET, extensions_len ) );
689 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
690 p_extensions_len, extensions_len );
691 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100692
693 *out_len = p - buf;
694 return( 0 );
695}
696
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200697MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100698static int ssl_generate_random( mbedtls_ssl_context *ssl )
699{
700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
701 unsigned char *randbytes = ssl->handshake->randbytes;
702 size_t gmt_unix_time_len = 0;
703
704 /*
705 * Generate the random bytes
706 *
707 * TLS 1.2 case:
708 * struct {
709 * uint32 gmt_unix_time;
710 * opaque random_bytes[28];
711 * } Random;
712 *
713 * TLS 1.3 case:
714 * opaque Random[32];
715 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400716 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100717 {
718#if defined(MBEDTLS_HAVE_TIME)
719 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
720 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
721 gmt_unix_time_len = 4;
722
723 MBEDTLS_SSL_DEBUG_MSG( 3,
724 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
725 (long long) gmt_unix_time ) );
726#endif /* MBEDTLS_HAVE_TIME */
727 }
728
729 ret = ssl->conf->f_rng( ssl->conf->p_rng,
730 randbytes + gmt_unix_time_len,
731 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
732 return( ret );
733}
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200734MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100735static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100736{
737 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100738 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800739 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
740
741 if( session_negotiate == NULL )
742 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100743
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800744#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
745 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
746 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800747
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800748 /* Check if a tls13 ticket has been configured. */
Jerry Yu22c18c12022-10-11 15:58:51 +0800749 if( ssl->handshake->resume != 0 &&
750 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
751 session_negotiate->ticket != NULL )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800752 {
753 mbedtls_time_t now = mbedtls_time( NULL );
Jerry Yu22c18c12022-10-11 15:58:51 +0800754 uint64_t age = (uint64_t)( now - session_negotiate->ticket_received );
755 if( session_negotiate->ticket_received > now ||
756 age > session_negotiate->ticket_lifetime )
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800757 {
758 /* Without valid ticket, disable session resumption.*/
759 MBEDTLS_SSL_DEBUG_MSG(
760 3, ( "Ticket expired, disable session resumption" ) );
761 ssl->handshake->resume = 0;
762 }
763 }
764#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
765 MBEDTLS_SSL_SESSION_TICKETS &&
766 MBEDTLS_HAVE_TIME */
767
Ronald Cron3d580bf2022-02-18 17:24:56 +0100768 if( ssl->conf->f_rng == NULL )
769 {
770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
771 return( MBEDTLS_ERR_SSL_NO_RNG );
772 }
773
Ronald Cron86a477f2022-02-18 17:45:10 +0100774 /* Bet on the highest configured version if we are not in a TLS 1.2
775 * renegotiation or session resumption.
776 */
777#if defined(MBEDTLS_SSL_RENEGOTIATION)
778 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400779 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100780 else
781#endif
782 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100783 if( ssl->handshake->resume )
784 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800785 ssl->tls_version = session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400786 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100787 }
788 else
789 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400790 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400791 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100792 }
793 }
794
Ronald Cron58b80382022-02-18 18:41:08 +0100795 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200796 * Generate the random bytes, except when responding to a verify request
797 * where we MUST reuse the previoulsy generated random bytes
798 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100799 */
800#if defined(MBEDTLS_SSL_PROTO_DTLS)
801 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
802 ( ssl->handshake->cookie == NULL ) )
803#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100804 {
Ronald Cron58b80382022-02-18 18:41:08 +0100805 ret = ssl_generate_random( ssl );
806 if( ret != 0 )
807 {
808 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
809 return( ret );
810 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100811 }
812
Ronald Cron3d580bf2022-02-18 17:24:56 +0100813 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200814 * Prepare session identifier. At that point, the length of the session
815 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
816 * to zero, except in the case of a TLS 1.2 session renegotiation or
817 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100818 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800819 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100820
821#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400822 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100823 {
Ronald Cron021b1782022-02-19 17:32:53 +0100824 if( session_id_len < 16 || session_id_len > 32 ||
825#if defined(MBEDTLS_SSL_RENEGOTIATION)
826 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
827#endif
828 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100829 {
Ronald Cron021b1782022-02-19 17:32:53 +0100830 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100831 }
Ronald Cron021b1782022-02-19 17:32:53 +0100832
833#if defined(MBEDTLS_SSL_SESSION_TICKETS)
834 /*
835 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
836 * generate and include a Session ID in the TLS ClientHello."
837 */
838#if defined(MBEDTLS_SSL_RENEGOTIATION)
839 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
840#endif
841 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800842 if( ( session_negotiate->ticket != NULL ) &&
843 ( session_negotiate->ticket_len != 0 ) )
Ronald Cron021b1782022-02-19 17:32:53 +0100844 {
845 session_id_len = 32;
846 }
847 }
848#endif /* MBEDTLS_SSL_SESSION_TICKETS */
849 }
850#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
851
852#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400853 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100854 {
855 /*
856 * Create a legacy session identifier for the purpose of middlebox
857 * compatibility only if one has not been created already, which is
858 * the case if we are here for the TLS 1.3 second ClientHello.
859 *
860 * Versions of TLS before TLS 1.3 supported a "session resumption"
861 * feature which has been merged with pre-shared keys in TLS 1.3
862 * version. A client which has a cached session ID set by a pre-TLS 1.3
863 * server SHOULD set this field to that value. In compatibility mode,
864 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
865 * session MUST generate a new 32-byte value. This value need not be
866 * random but SHOULD be unpredictable to avoid implementations fixating
867 * on a specific value (also known as ossification). Otherwise, it MUST
868 * be set as a zero-length vector ( i.e., a zero-valued single byte
869 * length field ).
870 */
871 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100872 }
873#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
874
Jerry Yu22c18c12022-10-11 15:58:51 +0800875 if( session_id_len != session_negotiate->id_len )
Ronald Cron021b1782022-02-19 17:32:53 +0100876 {
Jerry Yu22c18c12022-10-11 15:58:51 +0800877 session_negotiate->id_len = session_id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100878 if( session_id_len > 0 )
879 {
880 ret = ssl->conf->f_rng( ssl->conf->p_rng,
Jerry Yu22c18c12022-10-11 15:58:51 +0800881 session_negotiate->id,
Ronald Cron021b1782022-02-19 17:32:53 +0100882 session_id_len );
883 if( ret != 0 )
884 {
885 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
886 return( ret );
887 }
888 }
889 }
890
Ronald Cron3d580bf2022-02-18 17:24:56 +0100891 return( 0 );
892}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100893/*
894 * Write ClientHello handshake message.
895 * Handler for MBEDTLS_SSL_CLIENT_HELLO
896 */
897int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
898{
899 int ret = 0;
900 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000901 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100902
903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
904
Ronald Cron71c23322022-02-18 17:29:39 +0100905 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100906
907 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
908 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
909 &buf, &buf_len ) );
910
Ronald Cron71c23322022-02-18 17:29:39 +0100911 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
912 buf + buf_len,
XiaokangQianeb69aee2022-07-05 08:21:43 +0000913 &msg_len,
914 &binders_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100915
Ronald Cron5f4e9122022-02-21 09:50:36 +0100916#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
917 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
918 {
919 ssl->out_msglen = msg_len + 4;
920 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100921
Ronald Cron8ecd9932022-03-29 12:26:54 +0200922 /*
923 * The two functions below may try to send data on the network and
924 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
925 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200926 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200927 * set the handshake state to the next state at that point to ensure
928 * that we will not write and send again a ClientHello when we
929 * eventually succeed in sending the pending data.
930 */
931 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
932
Ronald Cron5f4e9122022-02-21 09:50:36 +0100933 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
934 {
935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
936 return( ret );
937 }
938
Ronald Cron8ecd9932022-03-29 12:26:54 +0200939 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100940 {
941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
942 return( ret );
943 }
944 }
945 else
946#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
947 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000948
XiaokangQianadab9a62022-07-18 07:41:26 +0000949 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
950 msg_len );
951 ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000952#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
953 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
954 if( binders_len > 0 )
955 {
956 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000957 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
XiaokangQianeb69aee2022-07-05 08:21:43 +0000958 ssl, buf + msg_len - binders_len, buf + msg_len ) );
XiaokangQian86981952022-07-19 09:51:50 +0000959 ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len,
960 binders_len );
XiaokangQianeb69aee2022-07-05 08:21:43 +0000961 }
962#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
963
Ronald Cron5f4e9122022-02-21 09:50:36 +0100964 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
965 buf_len,
966 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200967 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100968 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100969
Ronald Cron3d580bf2022-02-18 17:24:56 +0100970
971cleanup:
972
973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
974 return ret;
975}
976
977#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
978#endif /* MBEDTLS_SSL_CLI_C */