blob: 731b44f6e9ec7764f7b5783ada263b2acef250af [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"
45#include "ecdh_misc.h"
46#include "ssl_tls13_keys.h"
47#include "ssl_debug_helpers.h"
48
Ronald Cronfbd9f992022-03-17 15:22:07 +010049#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
50static 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;
57
58 *olen = 0;
59
60 if( ssl->hostname == NULL )
61 return( 0 );
62
63 MBEDTLS_SSL_DEBUG_MSG( 3,
64 ( "client hello, adding server name extension: %s",
65 ssl->hostname ) );
66
67 hostname_len = strlen( ssl->hostname );
68
69 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
70
71 /*
72 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
73 *
74 * In order to provide any of the server names, clients MAY include an
75 * extension of type "server_name" in the (extended) client hello. The
76 * "extension_data" field of this extension SHALL contain
77 * "ServerNameList" where:
78 *
79 * struct {
80 * NameType name_type;
81 * select (name_type) {
82 * case host_name: HostName;
83 * } name;
84 * } ServerName;
85 *
86 * enum {
87 * host_name(0), (255)
88 * } NameType;
89 *
90 * opaque HostName<1..2^16-1>;
91 *
92 * struct {
93 * ServerName server_name_list<1..2^16-1>
94 * } ServerNameList;
95 *
96 */
97 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
98 p += 2;
99
100 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
101 p += 2;
102
103 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
104 p += 2;
105
106 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
107
108 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
109 p += 2;
110
111 memcpy( p, ssl->hostname, hostname_len );
112
113 *olen = hostname_len + 9;
114
115 return( 0 );
116}
117#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
118
Ronald Cron3d580bf2022-02-18 17:24:56 +0100119#if defined(MBEDTLS_SSL_ALPN)
120/*
Ronald Cron71c23322022-02-18 17:29:39 +0100121 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100122 *
123 * Structure of the application_layer_protocol_negotiation extension in
124 * ClientHello:
125 *
126 * opaque ProtocolName<1..2^8-1>;
127 *
128 * struct {
129 * ProtocolName protocol_name_list<2..2^16-1>
130 * } ProtocolNameList;
131 *
132 */
Ronald Cron71c23322022-02-18 17:29:39 +0100133static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
134 unsigned char *buf,
135 const unsigned char *end,
136 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100137{
138 unsigned char *p = buf;
139
140 *out_len = 0;
141
142 if( ssl->conf->alpn_list == NULL )
143 return( 0 );
144
145 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
146
147
148 /* Check we have enough space for the extension type (2 bytes), the
149 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
150 */
151 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
152 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
153 /* Skip writing extension and list length for now */
154 p += 6;
155
156 /*
157 * opaque ProtocolName<1..2^8-1>;
158 *
159 * struct {
160 * ProtocolName protocol_name_list<2..2^16-1>
161 * } ProtocolNameList;
162 */
163 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
164 {
165 /*
166 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
167 * protocol names is less than 255.
168 */
169 size_t protocol_name_len = strlen( *cur );
170
171 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
172 *p++ = (unsigned char)protocol_name_len;
173 memcpy( p, *cur, protocol_name_len );
174 p += protocol_name_len;
175 }
176
177 *out_len = p - buf;
178
179 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
180 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
181
182 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
183 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
184
185 return( 0 );
186}
187#endif /* MBEDTLS_SSL_ALPN */
188
Ronald Cronfbd9f992022-03-17 15:22:07 +0100189#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
190 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
191/*
192 * Function for writing a supported groups (TLS 1.3) or supported elliptic
193 * curves (TLS 1.2) extension.
194 *
195 * The "extension_data" field of a supported groups extension contains a
196 * "NamedGroupList" value (TLS 1.3 RFC8446):
197 * enum {
198 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
199 * x25519(0x001D), x448(0x001E),
200 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
201 * ffdhe6144(0x0103), ffdhe8192(0x0104),
202 * ffdhe_private_use(0x01FC..0x01FF),
203 * ecdhe_private_use(0xFE00..0xFEFF),
204 * (0xFFFF)
205 * } NamedGroup;
206 * struct {
207 * NamedGroup named_group_list<2..2^16-1>;
208 * } NamedGroupList;
209 *
210 * The "extension_data" field of a supported elliptic curves extension contains
211 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
212 * enum {
213 * deprecated(1..22),
214 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
215 * x25519(29), x448(30),
216 * reserved (0xFE00..0xFEFF),
217 * deprecated(0xFF01..0xFF02),
218 * (0xFFFF)
219 * } NamedCurve;
220 * struct {
221 * NamedCurve named_curve_list<2..2^16-1>
222 * } NamedCurveList;
223 *
224 * The TLS 1.3 supported groups extension was defined to be a compatible
225 * generalization of the TLS 1.2 supported elliptic curves extension. They both
226 * share the same extension identifier.
227 *
228 * DHE groups are not supported yet.
229 */
230static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
231 unsigned char *buf,
232 const unsigned char *end,
233 size_t *out_len )
234{
235 unsigned char *p = buf ;
236 unsigned char *named_group_list; /* Start of named_group_list */
237 size_t named_group_list_len; /* Length of named_group_list */
238 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
239
240 *out_len = 0;
241
242 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
243
244 /* Check if we have space for header and length fields:
245 * - extension_type (2 bytes)
246 * - extension_data_length (2 bytes)
247 * - named_group_list_length (2 bytes)
248 */
249 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
250 p += 6;
251
252 named_group_list = p;
253
254 if( group_list == NULL )
255 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
256
257 for( ; *group_list != 0; group_list++ )
258 {
259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
260
261#if defined(MBEDTLS_ECP_C)
262 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
263 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
264 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
265 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
266 {
267 const mbedtls_ecp_curve_info *curve_info;
268 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
269 if( curve_info == NULL )
270 continue;
271 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
272 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
273 p += 2;
274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
275 curve_info->name, *group_list ) );
276 }
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)
303 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
304#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
305
306 return( 0 );
307}
308
309#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
310 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
311
312#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
313/*
314 * Function for writing a signature algorithm extension.
315 *
316 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
317 * value (TLS 1.3 RFC8446):
318 * enum {
319 * ....
320 * ecdsa_secp256r1_sha256( 0x0403 ),
321 * ecdsa_secp384r1_sha384( 0x0503 ),
322 * ecdsa_secp521r1_sha512( 0x0603 ),
323 * ....
324 * } SignatureScheme;
325 *
326 * struct {
327 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
328 * } SignatureSchemeList;
329 *
330 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
331 * value (TLS 1.2 RFC5246):
332 * enum {
333 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
334 * sha512(6), (255)
335 * } HashAlgorithm;
336 *
337 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
338 * SignatureAlgorithm;
339 *
340 * struct {
341 * HashAlgorithm hash;
342 * SignatureAlgorithm signature;
343 * } SignatureAndHashAlgorithm;
344 *
345 * SignatureAndHashAlgorithm
346 * supported_signature_algorithms<2..2^16-2>;
347 *
348 * The TLS 1.3 signature algorithm extension was defined to be a compatible
349 * generalization of the TLS 1.2 signature algorithm extension.
350 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
351 * `SignatureScheme` field of TLS 1.3
352 *
353 */
354static int ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
355 const unsigned char *end, size_t *out_len )
356{
357 unsigned char *p = buf;
358 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
359 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
360
361 *out_len = 0;
362
363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
364
365 /* Check if we have space for header and length field:
366 * - extension_type (2 bytes)
367 * - extension_data_length (2 bytes)
368 * - supported_signature_algorithms_length (2 bytes)
369 */
370 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
371 p += 6;
372
373 /*
374 * Write supported_signature_algorithms
375 */
376 supported_sig_alg = p;
377 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
378 if( sig_alg == NULL )
379 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
380
381 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
382 {
383 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
384 continue;
385 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
386 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
387 p += 2;
388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
389 }
390
391 /* Length of supported_signature_algorithms */
392 supported_sig_alg_len = p - supported_sig_alg;
393 if( supported_sig_alg_len == 0 )
394 {
395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
397 }
398
399 /* Write extension_type */
400 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
401 /* Write extension_data_length */
402 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
403 /* Write length of supported_signature_algorithms */
404 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
405
406 /* Output the total length of signature algorithms extension. */
407 *out_len = p - buf;
408
409#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
410 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
411#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
412 return( 0 );
413}
414#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
415
Ronald Cron757a2ab2022-03-30 13:57:40 +0200416int mbedtls_ssl_validate_ciphersuite(
Ronald Crond491c2d2022-02-19 18:30:46 +0100417 const mbedtls_ssl_context *ssl,
Ronald Cron757a2ab2022-03-30 13:57:40 +0200418 const mbedtls_ssl_ciphersuite_t *suite_info,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400419 mbedtls_ssl_protocol_version min_tls_version,
420 mbedtls_ssl_protocol_version max_tls_version )
Ronald Crond491c2d2022-02-19 18:30:46 +0100421{
Ronald Cron11218dd2022-03-31 16:23:49 +0200422 (void) ssl;
423
Ronald Crond491c2d2022-02-19 18:30:46 +0100424 if( suite_info == NULL )
Ronald Cron757a2ab2022-03-30 13:57:40 +0200425 return( -1 );
Ronald Crond491c2d2022-02-19 18:30:46 +0100426
Glenn Strauss60bfe602022-03-14 19:04:24 -0400427 if( ( suite_info->min_tls_version > max_tls_version ) ||
428 ( suite_info->max_tls_version < min_tls_version ) )
Ronald Cronf735cf12022-03-30 19:51:43 +0200429 {
Ronald Cron757a2ab2022-03-30 13:57:40 +0200430 return( -1 );
Ronald Cronf735cf12022-03-30 19:51:43 +0200431 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100432
433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Crond491c2d2022-02-19 18:30:46 +0100434#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
435 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Ronald Cron757a2ab2022-03-30 13:57:40 +0200436 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
437 {
438 return( -1 );
439 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100440#endif
441
442 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
443#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
444 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
445 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
446 {
Ronald Cron757a2ab2022-03-30 13:57:40 +0200447 return( -1 );
Ronald Crond491c2d2022-02-19 18:30:46 +0100448 }
449#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
450#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
451
452 return( 0 );
453}
454
Ronald Cron71c23322022-02-18 17:29:39 +0100455static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100456 mbedtls_ssl_context *ssl,
457 unsigned char *buf,
458 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100459 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100460 size_t *out_len )
461{
462 unsigned char *p = buf;
463 const int *ciphersuite_list;
464 unsigned char *cipher_suites; /* Start of the cipher_suites list */
465 size_t cipher_suites_len;
466
Ronald Crond491c2d2022-02-19 18:30:46 +0100467 *tls12_uses_ec = 0;
468 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100469
470 /*
471 * Ciphersuite list
472 *
473 * This is a list of the symmetric cipher options supported by
474 * the client, specifically the record protection algorithm
475 * ( including secret key length ) and a hash to be used with
476 * HKDF, in descending order of client preference.
477 */
478 ciphersuite_list = ssl->conf->ciphersuite_list;
479
480 /* Check there is space for the cipher suite list length (2 bytes). */
481 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
482 p += 2;
483
Ronald Cron64767262022-03-31 14:13:57 +0200484 /* Write cipher_suites
485 * CipherSuite cipher_suites<2..2^16-2>;
486 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100487 cipher_suites = p;
488 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
489 {
490 int cipher_suite = ciphersuite_list[i];
491 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
492
493 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100494
Ronald Cron757a2ab2022-03-30 13:57:40 +0200495 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400496 0x0300 | ssl->handshake->min_minor_ver,
497 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100498 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100499
500#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
501 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
502 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
503 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
504#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100505
506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
507 (unsigned int) cipher_suite,
508 ciphersuite_info->name ) );
509
510 /* Check there is space for the cipher suite identifier (2 bytes). */
511 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
512 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
513 p += 2;
514 }
515
Ronald Crond491c2d2022-02-19 18:30:46 +0100516 /*
517 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
518 */
519#if defined(MBEDTLS_SSL_RENEGOTIATION)
520 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
521#endif
522 {
523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
524 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
525 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
526 p += 2;
527 }
528
Ronald Cron3d580bf2022-02-18 17:24:56 +0100529 /* Write the cipher_suites length in number of bytes */
530 cipher_suites_len = p - cipher_suites;
531 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
532 MBEDTLS_SSL_DEBUG_MSG( 3,
533 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
534 cipher_suites_len/2 ) );
535
536 /* Output the total length of cipher_suites field. */
537 *out_len = p - buf;
538
539 return( 0 );
540}
541
542/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100543 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100544 *
545 * struct {
546 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
547 * Random random;
548 * opaque legacy_session_id<0..32>;
549 * CipherSuite cipher_suites<2..2^16-2>;
550 * opaque legacy_compression_methods<1..2^8-1>;
551 * Extension extensions<8..2^16-1>;
552 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100553 *
554 * Structure of the (D)TLS 1.2 ClientHello message:
555 *
556 * struct {
557 * ProtocolVersion client_version;
558 * Random random;
559 * SessionID session_id;
560 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
561 * CipherSuite cipher_suites<2..2^16-2>;
562 * CompressionMethod compression_methods<1..2^8-1>;
563 * select (extensions_present) {
564 * case false:
565 * struct {};
566 * case true:
567 * Extension extensions<0..2^16-1>;
568 * };
569 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100570 */
Ronald Cron71c23322022-02-18 17:29:39 +0100571static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
572 unsigned char *buf,
573 unsigned char *end,
574 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100575{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100576 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100577 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100578 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100579 unsigned char *p_extensions_len; /* Pointer to extensions length */
580 size_t output_len; /* Length of buffer used by function */
581 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100582 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100583
Ronald Cron3d580bf2022-02-18 17:24:56 +0100584 *out_len = 0;
585
Ronald Cron4079abc2022-02-20 10:35:26 +0100586#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200587 unsigned char propose_tls12 =
588 ( handshake->min_minor_ver <= MBEDTLS_SSL_MINOR_VERSION_3 )
589 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400590 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100591#endif
592#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200593 unsigned char propose_tls13 =
594 ( handshake->min_minor_ver <= MBEDTLS_SSL_MINOR_VERSION_4 )
595 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400596 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100597#endif
598
Ronald Cron3d580bf2022-02-18 17:24:56 +0100599 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100600 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100601 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100602 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100603 */
604 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400605 mbedtls_ssl_write_version( p, ssl->conf->transport,
606 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100607 p += 2;
608
Ronald Cron58b80382022-02-18 18:41:08 +0100609 /* ...
610 * Random random;
611 * ...
612 *
Ronald Cron58b80382022-02-18 18:41:08 +0100613 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100614 * the handshake->randbytes buffer and are copied here into the output
615 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100616 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100617 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100618 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100619 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
620 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
621 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
622
Ronald Cron021b1782022-02-19 17:32:53 +0100623 /* TLS 1.2:
624 * ...
625 * SessionID session_id;
626 * ...
627 * with
628 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100629 *
Ronald Cron021b1782022-02-19 17:32:53 +0100630 * TLS 1.3:
631 * ...
632 * opaque legacy_session_id<0..32>;
633 * ...
634 *
Ronald Cronda41b382022-03-30 09:57:11 +0200635 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100636 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
637 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100638 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100639 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
640 *p++ = (unsigned char)ssl->session_negotiate->id_len;
641 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
642 p += ssl->session_negotiate->id_len;
643
644 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
645 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100646
Ronald Crona874aa82022-02-19 18:11:26 +0100647 /* DTLS 1.2 ONLY
648 * ...
649 * opaque cookie<0..2^8-1>;
650 * ...
651 */
652#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
653 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
654 {
655 unsigned char cookie_len = 0;
656
Ronald Cron4079abc2022-02-20 10:35:26 +0100657 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100658 {
659 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100660 handshake->cookie,
661 handshake->verify_cookie_len );
662 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100663 }
664
665 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
666 *p++ = cookie_len;
667 if( cookie_len > 0 )
668 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100669 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100670 p += cookie_len;
671 }
672 }
673#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
674
Ronald Cron3d580bf2022-02-18 17:24:56 +0100675 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100676 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
677 &tls12_uses_ec,
678 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100679 if( ret != 0 )
680 return( ret );
681 p += output_len;
682
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100683 /* Write legacy_compression_methods (TLS 1.3) or
684 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100685 *
686 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
687 * one byte set to zero, which corresponds to the 'null' compression
688 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100689 *
690 * For TLS 1.2 ClientHello, for security reasons we do not support
691 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100692 */
693 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
694 *p++ = 1;
695 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
696
697 /* Write extensions */
698
699#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
700 /* Keeping track of the included extensions */
Ronald Cron4079abc2022-02-20 10:35:26 +0100701 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100702#endif
703
704 /* First write extensions, then the total length */
705 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
706 p_extensions_len = p;
707 p += 2;
708
Ronald Crondf823bf2022-03-29 18:57:54 +0200709#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
710 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100711 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100712 if( ret != 0 )
713 return( ret );
714 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200715#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100716
717#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100718 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100719 if( ret != 0 )
720 return( ret );
721 p += output_len;
722#endif /* MBEDTLS_SSL_ALPN */
723
724#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100725 if( propose_tls13 )
726 {
727 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
728 &output_len );
729 if( ret != 0 )
730 return( ret );
731 p += output_len;
732 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200733#endif
734
Ronald Cron4079abc2022-02-20 10:35:26 +0100735#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
736 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
737 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200738#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100739 ( propose_tls13 &&
740 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
741#endif
742#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
743 ( propose_tls12 && tls12_uses_ec ) ||
744#endif
745 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100746 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100747 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100748 if( ret != 0 )
749 return( ret );
750 p += output_len;
751 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100752#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100753
Ronald Cron11e18572022-03-17 13:44:33 +0100754#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100755 if(
756#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
757 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
758#endif
759#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
760 propose_tls12 ||
761#endif
762 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100763 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100764 ret = ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100765 if( ret != 0 )
766 return( ret );
767 p += output_len;
768 }
769#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100770
Ronald Cron4079abc2022-02-20 10:35:26 +0100771#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
772 if( propose_tls12 )
773 {
774 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
775 tls12_uses_ec,
776 &output_len );
777 if( ret != 0 )
778 return( ret );
779 p += output_len;
780 }
781#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100782
783 /* Write the length of the list of extensions. */
784 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100785
786 if( extensions_len == 0 )
787 p = p_extensions_len;
788 else
789 {
790 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
792 MBEDTLS_PRINTF_SIZET, extensions_len ) );
793 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
794 p_extensions_len, extensions_len );
795 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100796
797 *out_len = p - buf;
798 return( 0 );
799}
800
Ronald Cron58b80382022-02-18 18:41:08 +0100801static int ssl_generate_random( mbedtls_ssl_context *ssl )
802{
803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
804 unsigned char *randbytes = ssl->handshake->randbytes;
805 size_t gmt_unix_time_len = 0;
806
807 /*
808 * Generate the random bytes
809 *
810 * TLS 1.2 case:
811 * struct {
812 * uint32 gmt_unix_time;
813 * opaque random_bytes[28];
814 * } Random;
815 *
816 * TLS 1.3 case:
817 * opaque Random[32];
818 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400819 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100820 {
821#if defined(MBEDTLS_HAVE_TIME)
822 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
823 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
824 gmt_unix_time_len = 4;
825
826 MBEDTLS_SSL_DEBUG_MSG( 3,
827 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
828 (long long) gmt_unix_time ) );
829#endif /* MBEDTLS_HAVE_TIME */
830 }
831
832 ret = ssl->conf->f_rng( ssl->conf->p_rng,
833 randbytes + gmt_unix_time_len,
834 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
835 return( ret );
836}
837
Ronald Cron71c23322022-02-18 17:29:39 +0100838static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100839{
840 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100841 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100842
843 if( ssl->conf->f_rng == NULL )
844 {
845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
846 return( MBEDTLS_ERR_SSL_NO_RNG );
847 }
848
Ronald Cron86a477f2022-02-18 17:45:10 +0100849 /* Bet on the highest configured version if we are not in a TLS 1.2
850 * renegotiation or session resumption.
851 */
852#if defined(MBEDTLS_SSL_RENEGOTIATION)
853 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strauss60bfe602022-03-14 19:04:24 -0400854 ssl->handshake->min_minor_ver = ssl->tls_version & 0xFF;
Ronald Cron86a477f2022-02-18 17:45:10 +0100855 else
856#endif
857 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100858 if( ssl->handshake->resume )
859 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400860 ssl->tls_version = ssl->session_negotiate->tls_version;
861 ssl->handshake->min_minor_ver = ssl->tls_version & 0xFF;
Ronald Cron86a477f2022-02-18 17:45:10 +0100862 }
863 else
864 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400865 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strauss2dfcea22022-03-14 17:26:42 -0400866 ssl->handshake->min_minor_ver = ssl->conf->min_tls_version & 0xFF;
Ronald Cron86a477f2022-02-18 17:45:10 +0100867 }
868 }
869
Ronald Cron58b80382022-02-18 18:41:08 +0100870 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200871 * Generate the random bytes, except when responding to a verify request
872 * where we MUST reuse the previoulsy generated random bytes
873 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100874 */
875#if defined(MBEDTLS_SSL_PROTO_DTLS)
876 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
877 ( ssl->handshake->cookie == NULL ) )
878#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100879 {
Ronald Cron58b80382022-02-18 18:41:08 +0100880 ret = ssl_generate_random( ssl );
881 if( ret != 0 )
882 {
883 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
884 return( ret );
885 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100886 }
887
Ronald Cron3d580bf2022-02-18 17:24:56 +0100888 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200889 * Prepare session identifier. At that point, the length of the session
890 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
891 * to zero, except in the case of a TLS 1.2 session renegotiation or
892 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100893 */
Ronald Cron021b1782022-02-19 17:32:53 +0100894 session_id_len = ssl->session_negotiate->id_len;
895
896#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400897 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100898 {
Ronald Cron021b1782022-02-19 17:32:53 +0100899 if( session_id_len < 16 || session_id_len > 32 ||
900#if defined(MBEDTLS_SSL_RENEGOTIATION)
901 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
902#endif
903 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100904 {
Ronald Cron021b1782022-02-19 17:32:53 +0100905 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100906 }
Ronald Cron021b1782022-02-19 17:32:53 +0100907
908#if defined(MBEDTLS_SSL_SESSION_TICKETS)
909 /*
910 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
911 * generate and include a Session ID in the TLS ClientHello."
912 */
913#if defined(MBEDTLS_SSL_RENEGOTIATION)
914 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
915#endif
916 {
917 if( ( ssl->session_negotiate->ticket != NULL ) &&
918 ( ssl->session_negotiate->ticket_len != 0 ) )
919 {
920 session_id_len = 32;
921 }
922 }
923#endif /* MBEDTLS_SSL_SESSION_TICKETS */
924 }
925#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
926
927#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400928 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100929 {
930 /*
931 * Create a legacy session identifier for the purpose of middlebox
932 * compatibility only if one has not been created already, which is
933 * the case if we are here for the TLS 1.3 second ClientHello.
934 *
935 * Versions of TLS before TLS 1.3 supported a "session resumption"
936 * feature which has been merged with pre-shared keys in TLS 1.3
937 * version. A client which has a cached session ID set by a pre-TLS 1.3
938 * server SHOULD set this field to that value. In compatibility mode,
939 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
940 * session MUST generate a new 32-byte value. This value need not be
941 * random but SHOULD be unpredictable to avoid implementations fixating
942 * on a specific value (also known as ossification). Otherwise, it MUST
943 * be set as a zero-length vector ( i.e., a zero-valued single byte
944 * length field ).
945 */
946 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100947 }
948#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
949
Ronald Cron021b1782022-02-19 17:32:53 +0100950 if( session_id_len != ssl->session_negotiate->id_len )
951 {
952 ssl->session_negotiate->id_len = session_id_len;
953 if( session_id_len > 0 )
954 {
955 ret = ssl->conf->f_rng( ssl->conf->p_rng,
956 ssl->session_negotiate->id,
957 session_id_len );
958 if( ret != 0 )
959 {
960 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
961 return( ret );
962 }
963 }
964 }
965
Ronald Cron3d580bf2022-02-18 17:24:56 +0100966 return( 0 );
967}
968
969/*
970 * Write ClientHello handshake message.
971 * Handler for MBEDTLS_SSL_CLIENT_HELLO
972 */
973int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
974{
975 int ret = 0;
976 unsigned char *buf;
977 size_t buf_len, msg_len;
978
979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
980
Ronald Cron71c23322022-02-18 17:29:39 +0100981 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100982
983 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
984 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
985 &buf, &buf_len ) );
986
Ronald Cron71c23322022-02-18 17:29:39 +0100987 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
988 buf + buf_len,
989 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100990
Ronald Cron5f4e9122022-02-21 09:50:36 +0100991#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
992 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
993 {
994 ssl->out_msglen = msg_len + 4;
995 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100996
Ronald Cron8ecd9932022-03-29 12:26:54 +0200997 /*
998 * The two functions below may try to send data on the network and
999 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
1000 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +02001001 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +02001002 * set the handshake state to the next state at that point to ensure
1003 * that we will not write and send again a ClientHello when we
1004 * eventually succeed in sending the pending data.
1005 */
1006 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
1007
Ronald Cron5f4e9122022-02-21 09:50:36 +01001008 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
1009 {
1010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
1011 return( ret );
1012 }
1013
Ronald Cron8ecd9932022-03-29 12:26:54 +02001014 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +01001015 {
1016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
1017 return( ret );
1018 }
1019 }
1020 else
1021#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
1022 {
1023 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1024 buf, msg_len );
1025 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
1026 buf_len,
1027 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +02001028 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +01001029 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001030
Ronald Cron3d580bf2022-02-18 17:24:56 +01001031
1032cleanup:
1033
1034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1035 return ret;
1036}
1037
1038#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1039#endif /* MBEDTLS_SSL_CLI_C */