blob: fd9686550e612347f6719d6fb6707aff1054f888 [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
49#if defined(MBEDTLS_SSL_ALPN)
50/*
Ronald Cron71c23322022-02-18 17:29:39 +010051 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +010052 *
53 * Structure of the application_layer_protocol_negotiation extension in
54 * ClientHello:
55 *
56 * opaque ProtocolName<1..2^8-1>;
57 *
58 * struct {
59 * ProtocolName protocol_name_list<2..2^16-1>
60 * } ProtocolNameList;
61 *
62 */
Ronald Cron71c23322022-02-18 17:29:39 +010063static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
64 unsigned char *buf,
65 const unsigned char *end,
66 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +010067{
68 unsigned char *p = buf;
69
70 *out_len = 0;
71
72 if( ssl->conf->alpn_list == NULL )
73 return( 0 );
74
75 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
76
77
78 /* Check we have enough space for the extension type (2 bytes), the
79 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
80 */
81 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
82 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
83 /* Skip writing extension and list length for now */
84 p += 6;
85
86 /*
87 * opaque ProtocolName<1..2^8-1>;
88 *
89 * struct {
90 * ProtocolName protocol_name_list<2..2^16-1>
91 * } ProtocolNameList;
92 */
93 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
94 {
95 /*
96 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
97 * protocol names is less than 255.
98 */
99 size_t protocol_name_len = strlen( *cur );
100
101 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
102 *p++ = (unsigned char)protocol_name_len;
103 memcpy( p, *cur, protocol_name_len );
104 p += protocol_name_len;
105 }
106
107 *out_len = p - buf;
108
109 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
110 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
111
112 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
113 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
114
115 return( 0 );
116}
117#endif /* MBEDTLS_SSL_ALPN */
118
119/* Write cipher_suites
120 * CipherSuite cipher_suites<2..2^16-2>;
121 */
Ronald Crond491c2d2022-02-19 18:30:46 +0100122/**
123 * \brief Validate cipher suite against config in SSL context.
124 *
125 * \param ssl SSL context
126 * \param suite_info Cipher suite to validate
127 *
128 * \return 0 if valid, else 1
129 */
130static int ssl_validate_ciphersuite(
131 const mbedtls_ssl_context *ssl,
132 const mbedtls_ssl_ciphersuite_t *suite_info )
133{
134 if( suite_info == NULL )
135 return( 1 );
136
137 if( ( suite_info->min_minor_ver > ssl->conf->max_minor_ver ) ||
138 ( suite_info->max_minor_ver < ssl->conf->min_minor_ver ) )
139 return( 1 );
140
141#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
142#if defined(MBEDTLS_SSL_PROTO_DTLS)
143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
144 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
145 return( 1 );
146#endif
147
148#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
149 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
150 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
151 return( 1 );
152#endif
153
154 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
155#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
156 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
157 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
158 {
159 return( 1 );
160 }
161#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
162#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
163
164 return( 0 );
165}
166
Ronald Cron71c23322022-02-18 17:29:39 +0100167static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100168 mbedtls_ssl_context *ssl,
169 unsigned char *buf,
170 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100171 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100172 size_t *out_len )
173{
174 unsigned char *p = buf;
175 const int *ciphersuite_list;
176 unsigned char *cipher_suites; /* Start of the cipher_suites list */
177 size_t cipher_suites_len;
178
Ronald Crond491c2d2022-02-19 18:30:46 +0100179 *tls12_uses_ec = 0;
180 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100181
182 /*
183 * Ciphersuite list
184 *
185 * This is a list of the symmetric cipher options supported by
186 * the client, specifically the record protection algorithm
187 * ( including secret key length ) and a hash to be used with
188 * HKDF, in descending order of client preference.
189 */
190 ciphersuite_list = ssl->conf->ciphersuite_list;
191
192 /* Check there is space for the cipher suite list length (2 bytes). */
193 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
194 p += 2;
195
196 /* Write cipher_suites */
197 cipher_suites = p;
198 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
199 {
200 int cipher_suite = ciphersuite_list[i];
201 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
202
203 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100204
205 if( ssl_validate_ciphersuite( ssl, ciphersuite_info ) )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100206 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100207
208#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
209 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
210 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
211 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
212#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100213
214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
215 (unsigned int) cipher_suite,
216 ciphersuite_info->name ) );
217
218 /* Check there is space for the cipher suite identifier (2 bytes). */
219 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
220 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
221 p += 2;
222 }
223
Ronald Crond491c2d2022-02-19 18:30:46 +0100224 /*
225 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
226 */
227#if defined(MBEDTLS_SSL_RENEGOTIATION)
228 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
229#endif
230 {
231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
232 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
233 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
234 p += 2;
235 }
236
Ronald Cron3d580bf2022-02-18 17:24:56 +0100237 /* Write the cipher_suites length in number of bytes */
238 cipher_suites_len = p - cipher_suites;
239 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
240 MBEDTLS_SSL_DEBUG_MSG( 3,
241 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
242 cipher_suites_len/2 ) );
243
244 /* Output the total length of cipher_suites field. */
245 *out_len = p - buf;
246
247 return( 0 );
248}
249
250/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100251 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100252 *
253 * struct {
254 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
255 * Random random;
256 * opaque legacy_session_id<0..32>;
257 * CipherSuite cipher_suites<2..2^16-2>;
258 * opaque legacy_compression_methods<1..2^8-1>;
259 * Extension extensions<8..2^16-1>;
260 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100261 *
262 * Structure of the (D)TLS 1.2 ClientHello message:
263 *
264 * struct {
265 * ProtocolVersion client_version;
266 * Random random;
267 * SessionID session_id;
268 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
269 * CipherSuite cipher_suites<2..2^16-2>;
270 * CompressionMethod compression_methods<1..2^8-1>;
271 * select (extensions_present) {
272 * case false:
273 * struct {};
274 * case true:
275 * Extension extensions<0..2^16-1>;
276 * };
277 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100278 */
Ronald Cron71c23322022-02-18 17:29:39 +0100279static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
280 unsigned char *buf,
281 unsigned char *end,
282 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100283{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100284 int ret;
Ronald Crond491c2d2022-02-19 18:30:46 +0100285 unsigned char *p = buf;
286 int tls12_uses_ec = 0;
287
Ronald Cron3d580bf2022-02-18 17:24:56 +0100288 unsigned char *p_extensions_len; /* Pointer to extensions length */
289 size_t output_len; /* Length of buffer used by function */
290 size_t extensions_len; /* Length of the list of extensions*/
291
Ronald Cron3d580bf2022-02-18 17:24:56 +0100292 *out_len = 0;
293
Ronald Cron3d580bf2022-02-18 17:24:56 +0100294 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100295 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100296 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100297 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100298 */
299 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Ronald Cron1614eb62022-02-18 17:53:01 +0100300 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
301 MBEDTLS_SSL_MINOR_VERSION_3,
302 ssl->conf->transport, p );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100303 p += 2;
304
Ronald Cron58b80382022-02-18 18:41:08 +0100305 /* ...
306 * Random random;
307 * ...
308 *
309 * with for TLS 1.2
310 * struct {
311 * uint32 gmt_unix_time;
312 * opaque random_bytes[28];
313 * } Random;
314 *
315 * and for TLS 1.3
316 * opaque Random[32];
317 *
318 * The random bytes have been prepared by ssl_prepare_client_hello() into
319 * the ssl->handshake->randbytes buffer and are copied here into the
320 * output buffer.
321 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100322 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
323 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
324 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
325 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
326 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
327
Ronald Cron021b1782022-02-19 17:32:53 +0100328 /* TLS 1.2:
329 * ...
330 * SessionID session_id;
331 * ...
332 * with
333 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100334 *
Ronald Cron021b1782022-02-19 17:32:53 +0100335 * TLS 1.3:
336 * ...
337 * opaque legacy_session_id<0..32>;
338 * ...
339 *
340 * The (legacy) session identifier bytes have been by
341 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
342 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100343 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100344 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
345 *p++ = (unsigned char)ssl->session_negotiate->id_len;
346 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
347 p += ssl->session_negotiate->id_len;
348
349 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
350 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100351
Ronald Crona874aa82022-02-19 18:11:26 +0100352 /* DTLS 1.2 ONLY
353 * ...
354 * opaque cookie<0..2^8-1>;
355 * ...
356 */
357#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
359 {
360 unsigned char cookie_len = 0;
361
362 if( ssl->handshake->cookie != NULL )
363 {
364 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
365 ssl->handshake->cookie,
366 ssl->handshake->verify_cookie_len );
367 cookie_len = ssl->handshake->verify_cookie_len;
368 }
369
370 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
371 *p++ = cookie_len;
372 if( cookie_len > 0 )
373 {
374 memcpy( p, ssl->handshake->cookie, cookie_len );
375 p += cookie_len;
376 }
377 }
378#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
379
Ronald Cron3d580bf2022-02-18 17:24:56 +0100380 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100381 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
382 &tls12_uses_ec,
383 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100384 if( ret != 0 )
385 return( ret );
386 p += output_len;
387
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100388 /* Write legacy_compression_methods (TLS 1.3) or
389 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100390 *
391 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
392 * one byte set to zero, which corresponds to the 'null' compression
393 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100394 *
395 * For TLS 1.2 ClientHello, for security reasons we do not support
396 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100397 */
398 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
399 *p++ = 1;
400 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
401
402 /* Write extensions */
403
404#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
405 /* Keeping track of the included extensions */
406 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
407#endif
408
409 /* First write extensions, then the total length */
410 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
411 p_extensions_len = p;
412 p += 2;
413
414#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
415 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
416 if( ret != 0 )
417 return( ret );
418 p += output_len;
419#endif
420
421#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100422 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100423 if( ret != 0 )
424 return( ret );
425 p += output_len;
426#endif /* MBEDTLS_SSL_ALPN */
427
428#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
429#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
430 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
431 {
432 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
433 if( ret != 0 )
434 return( ret );
435 p += output_len;
436 }
437
438 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
439 {
440 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
441 if( ret != 0 )
442 return( ret );
443 p += output_len;
444 }
445#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
446#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
447
448#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
449 /* Write server name extension */
450 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
451 if( ret != 0 )
452 return( ret );
453 p += output_len;
454#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
455
456 /* Add more extensions here */
457
458 /* Write the length of the list of extensions. */
459 extensions_len = p - p_extensions_len - 2;
460 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
461 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
462 extensions_len ) );
463 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
464
465 *out_len = p - buf;
466 return( 0 );
467}
468
Ronald Cron58b80382022-02-18 18:41:08 +0100469static int ssl_generate_random( mbedtls_ssl_context *ssl )
470{
471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
472 unsigned char *randbytes = ssl->handshake->randbytes;
473 size_t gmt_unix_time_len = 0;
474
475 /*
476 * Generate the random bytes
477 *
478 * TLS 1.2 case:
479 * struct {
480 * uint32 gmt_unix_time;
481 * opaque random_bytes[28];
482 * } Random;
483 *
484 * TLS 1.3 case:
485 * opaque Random[32];
486 */
487 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
488 {
489#if defined(MBEDTLS_HAVE_TIME)
490 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
491 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
492 gmt_unix_time_len = 4;
493
494 MBEDTLS_SSL_DEBUG_MSG( 3,
495 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
496 (long long) gmt_unix_time ) );
497#endif /* MBEDTLS_HAVE_TIME */
498 }
499
500 ret = ssl->conf->f_rng( ssl->conf->p_rng,
501 randbytes + gmt_unix_time_len,
502 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
503 return( ret );
504}
505
Ronald Cron71c23322022-02-18 17:29:39 +0100506static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100507{
508 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100509 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100510
511 if( ssl->conf->f_rng == NULL )
512 {
513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
514 return( MBEDTLS_ERR_SSL_NO_RNG );
515 }
516
Ronald Cron86a477f2022-02-18 17:45:10 +0100517 /* Bet on the highest configured version if we are not in a TLS 1.2
518 * renegotiation or session resumption.
519 */
520#if defined(MBEDTLS_SSL_RENEGOTIATION)
521 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
522 ssl->handshake->min_minor_ver = ssl->minor_ver;
523 else
524#endif
525 {
526 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
527
528 if( ssl->handshake->resume )
529 {
530 ssl->minor_ver = ssl->session_negotiate->minor_ver;
531 ssl->handshake->min_minor_ver = ssl->minor_ver;
532 }
533 else
534 {
535 ssl->minor_ver = ssl->conf->max_minor_ver;
536 ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver;
537 }
538 }
539
Ronald Cron58b80382022-02-18 18:41:08 +0100540 /*
541 * But when responding to a verify request where we MUST reuse the
542 * previoulsy generated random bytes (RFC 6347 4.2.1), generate the
543 * random bytes.
544 */
545#if defined(MBEDTLS_SSL_PROTO_DTLS)
546 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
547 ( ssl->handshake->cookie == NULL ) )
548#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100549 {
Ronald Cron58b80382022-02-18 18:41:08 +0100550 ret = ssl_generate_random( ssl );
551 if( ret != 0 )
552 {
553 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
554 return( ret );
555 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100556 }
557
Ronald Cron3d580bf2022-02-18 17:24:56 +0100558 /*
Ronald Cron021b1782022-02-19 17:32:53 +0100559 * Prepare session identifier. But in the case of a TLS 1.2 session
560 * renegotiation or session resumption, the initial value of the session
561 * identifier length below is equal to zero.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100562 */
Ronald Cron021b1782022-02-19 17:32:53 +0100563 session_id_len = ssl->session_negotiate->id_len;
564
565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
566 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100567 {
Ronald Cron021b1782022-02-19 17:32:53 +0100568 if( session_id_len < 16 || session_id_len > 32 ||
569#if defined(MBEDTLS_SSL_RENEGOTIATION)
570 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
571#endif
572 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100573 {
Ronald Cron021b1782022-02-19 17:32:53 +0100574 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100575 }
Ronald Cron021b1782022-02-19 17:32:53 +0100576
577#if defined(MBEDTLS_SSL_SESSION_TICKETS)
578 /*
579 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
580 * generate and include a Session ID in the TLS ClientHello."
581 */
582#if defined(MBEDTLS_SSL_RENEGOTIATION)
583 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
584#endif
585 {
586 if( ( ssl->session_negotiate->ticket != NULL ) &&
587 ( ssl->session_negotiate->ticket_len != 0 ) )
588 {
589 session_id_len = 32;
590 }
591 }
592#endif /* MBEDTLS_SSL_SESSION_TICKETS */
593 }
594#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
595
596#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
597 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
598 {
599 /*
600 * Create a legacy session identifier for the purpose of middlebox
601 * compatibility only if one has not been created already, which is
602 * the case if we are here for the TLS 1.3 second ClientHello.
603 *
604 * Versions of TLS before TLS 1.3 supported a "session resumption"
605 * feature which has been merged with pre-shared keys in TLS 1.3
606 * version. A client which has a cached session ID set by a pre-TLS 1.3
607 * server SHOULD set this field to that value. In compatibility mode,
608 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
609 * session MUST generate a new 32-byte value. This value need not be
610 * random but SHOULD be unpredictable to avoid implementations fixating
611 * on a specific value (also known as ossification). Otherwise, it MUST
612 * be set as a zero-length vector ( i.e., a zero-valued single byte
613 * length field ).
614 */
615 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100616 }
617#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
618
Ronald Cron021b1782022-02-19 17:32:53 +0100619 if( session_id_len != ssl->session_negotiate->id_len )
620 {
621 ssl->session_negotiate->id_len = session_id_len;
622 if( session_id_len > 0 )
623 {
624 ret = ssl->conf->f_rng( ssl->conf->p_rng,
625 ssl->session_negotiate->id,
626 session_id_len );
627 if( ret != 0 )
628 {
629 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
630 return( ret );
631 }
632 }
633 }
634
Ronald Cron3d580bf2022-02-18 17:24:56 +0100635 return( 0 );
636}
637
638/*
639 * Write ClientHello handshake message.
640 * Handler for MBEDTLS_SSL_CLIENT_HELLO
641 */
642int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
643{
644 int ret = 0;
645 unsigned char *buf;
646 size_t buf_len, msg_len;
647
648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
649
Ronald Cron71c23322022-02-18 17:29:39 +0100650 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100651
652 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
653 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
654 &buf, &buf_len ) );
655
Ronald Cron71c23322022-02-18 17:29:39 +0100656 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
657 buf + buf_len,
658 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100659
660 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
661 buf, msg_len );
662
663 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
664 buf_len,
665 msg_len ) );
666
667 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
668
669cleanup:
670
671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
672 return ret;
673}
674
675#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
676#endif /* MBEDTLS_SSL_CLI_C */