blob: 2fe4aca5f7581530686387525e40afa8dfeabd97 [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
388 /* Write legacy_compression_methods
389 *
390 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
391 * one byte set to zero, which corresponds to the 'null' compression
392 * method in prior versions of TLS.
393 */
394 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
395 *p++ = 1;
396 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
397
398 /* Write extensions */
399
400#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
401 /* Keeping track of the included extensions */
402 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
403#endif
404
405 /* First write extensions, then the total length */
406 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
407 p_extensions_len = p;
408 p += 2;
409
410#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
411 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
412 if( ret != 0 )
413 return( ret );
414 p += output_len;
415#endif
416
417#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100418 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100419 if( ret != 0 )
420 return( ret );
421 p += output_len;
422#endif /* MBEDTLS_SSL_ALPN */
423
424#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
425#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
426 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
427 {
428 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
429 if( ret != 0 )
430 return( ret );
431 p += output_len;
432 }
433
434 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
435 {
436 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
437 if( ret != 0 )
438 return( ret );
439 p += output_len;
440 }
441#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
442#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
443
444#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
445 /* Write server name extension */
446 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
447 if( ret != 0 )
448 return( ret );
449 p += output_len;
450#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
451
452 /* Add more extensions here */
453
454 /* Write the length of the list of extensions. */
455 extensions_len = p - p_extensions_len - 2;
456 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
458 extensions_len ) );
459 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
460
461 *out_len = p - buf;
462 return( 0 );
463}
464
Ronald Cron58b80382022-02-18 18:41:08 +0100465static int ssl_generate_random( mbedtls_ssl_context *ssl )
466{
467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
468 unsigned char *randbytes = ssl->handshake->randbytes;
469 size_t gmt_unix_time_len = 0;
470
471 /*
472 * Generate the random bytes
473 *
474 * TLS 1.2 case:
475 * struct {
476 * uint32 gmt_unix_time;
477 * opaque random_bytes[28];
478 * } Random;
479 *
480 * TLS 1.3 case:
481 * opaque Random[32];
482 */
483 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
484 {
485#if defined(MBEDTLS_HAVE_TIME)
486 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
487 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
488 gmt_unix_time_len = 4;
489
490 MBEDTLS_SSL_DEBUG_MSG( 3,
491 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
492 (long long) gmt_unix_time ) );
493#endif /* MBEDTLS_HAVE_TIME */
494 }
495
496 ret = ssl->conf->f_rng( ssl->conf->p_rng,
497 randbytes + gmt_unix_time_len,
498 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
499 return( ret );
500}
501
Ronald Cron71c23322022-02-18 17:29:39 +0100502static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100503{
504 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100505 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100506
507 if( ssl->conf->f_rng == NULL )
508 {
509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
510 return( MBEDTLS_ERR_SSL_NO_RNG );
511 }
512
Ronald Cron86a477f2022-02-18 17:45:10 +0100513 /* Bet on the highest configured version if we are not in a TLS 1.2
514 * renegotiation or session resumption.
515 */
516#if defined(MBEDTLS_SSL_RENEGOTIATION)
517 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
518 ssl->handshake->min_minor_ver = ssl->minor_ver;
519 else
520#endif
521 {
522 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
523
524 if( ssl->handshake->resume )
525 {
526 ssl->minor_ver = ssl->session_negotiate->minor_ver;
527 ssl->handshake->min_minor_ver = ssl->minor_ver;
528 }
529 else
530 {
531 ssl->minor_ver = ssl->conf->max_minor_ver;
532 ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver;
533 }
534 }
535
Ronald Cron58b80382022-02-18 18:41:08 +0100536 /*
537 * But when responding to a verify request where we MUST reuse the
538 * previoulsy generated random bytes (RFC 6347 4.2.1), generate the
539 * random bytes.
540 */
541#if defined(MBEDTLS_SSL_PROTO_DTLS)
542 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
543 ( ssl->handshake->cookie == NULL ) )
544#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100545 {
Ronald Cron58b80382022-02-18 18:41:08 +0100546 ret = ssl_generate_random( ssl );
547 if( ret != 0 )
548 {
549 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
550 return( ret );
551 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100552 }
553
Ronald Cron3d580bf2022-02-18 17:24:56 +0100554 /*
Ronald Cron021b1782022-02-19 17:32:53 +0100555 * Prepare session identifier. But in the case of a TLS 1.2 session
556 * renegotiation or session resumption, the initial value of the session
557 * identifier length below is equal to zero.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100558 */
Ronald Cron021b1782022-02-19 17:32:53 +0100559 session_id_len = ssl->session_negotiate->id_len;
560
561#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
562 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100563 {
Ronald Cron021b1782022-02-19 17:32:53 +0100564 if( session_id_len < 16 || session_id_len > 32 ||
565#if defined(MBEDTLS_SSL_RENEGOTIATION)
566 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
567#endif
568 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100569 {
Ronald Cron021b1782022-02-19 17:32:53 +0100570 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100571 }
Ronald Cron021b1782022-02-19 17:32:53 +0100572
573#if defined(MBEDTLS_SSL_SESSION_TICKETS)
574 /*
575 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
576 * generate and include a Session ID in the TLS ClientHello."
577 */
578#if defined(MBEDTLS_SSL_RENEGOTIATION)
579 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
580#endif
581 {
582 if( ( ssl->session_negotiate->ticket != NULL ) &&
583 ( ssl->session_negotiate->ticket_len != 0 ) )
584 {
585 session_id_len = 32;
586 }
587 }
588#endif /* MBEDTLS_SSL_SESSION_TICKETS */
589 }
590#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
591
592#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
593 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
594 {
595 /*
596 * Create a legacy session identifier for the purpose of middlebox
597 * compatibility only if one has not been created already, which is
598 * the case if we are here for the TLS 1.3 second ClientHello.
599 *
600 * Versions of TLS before TLS 1.3 supported a "session resumption"
601 * feature which has been merged with pre-shared keys in TLS 1.3
602 * version. A client which has a cached session ID set by a pre-TLS 1.3
603 * server SHOULD set this field to that value. In compatibility mode,
604 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
605 * session MUST generate a new 32-byte value. This value need not be
606 * random but SHOULD be unpredictable to avoid implementations fixating
607 * on a specific value (also known as ossification). Otherwise, it MUST
608 * be set as a zero-length vector ( i.e., a zero-valued single byte
609 * length field ).
610 */
611 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100612 }
613#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
614
Ronald Cron021b1782022-02-19 17:32:53 +0100615 if( session_id_len != ssl->session_negotiate->id_len )
616 {
617 ssl->session_negotiate->id_len = session_id_len;
618 if( session_id_len > 0 )
619 {
620 ret = ssl->conf->f_rng( ssl->conf->p_rng,
621 ssl->session_negotiate->id,
622 session_id_len );
623 if( ret != 0 )
624 {
625 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
626 return( ret );
627 }
628 }
629 }
630
Ronald Cron3d580bf2022-02-18 17:24:56 +0100631 return( 0 );
632}
633
634/*
635 * Write ClientHello handshake message.
636 * Handler for MBEDTLS_SSL_CLIENT_HELLO
637 */
638int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
639{
640 int ret = 0;
641 unsigned char *buf;
642 size_t buf_len, msg_len;
643
644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
645
Ronald Cron71c23322022-02-18 17:29:39 +0100646 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100647
648 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
649 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
650 &buf, &buf_len ) );
651
Ronald Cron71c23322022-02-18 17:29:39 +0100652 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
653 buf + buf_len,
654 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100655
656 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
657 buf, msg_len );
658
659 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
660 buf_len,
661 msg_len ) );
662
663 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
664
665cleanup:
666
667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
668 return ret;
669}
670
671#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
672#endif /* MBEDTLS_SSL_CLI_C */