blob: 33c02e69de49ecd090ffe63bb0bfd25c63185e3f [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
Ronald Crondf823bf2022-03-29 18:57:54 +0200414#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
415 /* Write server name extension */
416 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100417 if( ret != 0 )
418 return( ret );
419 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200420#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100421
422#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100423 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100424 if( ret != 0 )
425 return( ret );
426 p += output_len;
427#endif /* MBEDTLS_SSL_ALPN */
428
429#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Crondf823bf2022-03-29 18:57:54 +0200430 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
431 if( ret != 0 )
432 return( ret );
433 p += output_len;
434#endif
435
436#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron11e18572022-03-17 13:44:33 +0100437#if defined(MBEDTLS_ECDH_C)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100438 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
439 {
440 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
441 if( ret != 0 )
442 return( ret );
443 p += output_len;
444 }
Ronald Cron11e18572022-03-17 13:44:33 +0100445#endif /* MBEDTLS_ECDH_C */
446#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100447
Ronald Cron11e18572022-03-17 13:44:33 +0100448#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
449#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100450 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
451 {
452 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
453 if( ret != 0 )
454 return( ret );
455 p += output_len;
456 }
457#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
458#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
459
Ronald Cron3d580bf2022-02-18 17:24:56 +0100460 /* Add more extensions here */
461
462 /* Write the length of the list of extensions. */
463 extensions_len = p - p_extensions_len - 2;
464 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
466 extensions_len ) );
467 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
468
469 *out_len = p - buf;
470 return( 0 );
471}
472
Ronald Cron58b80382022-02-18 18:41:08 +0100473static int ssl_generate_random( mbedtls_ssl_context *ssl )
474{
475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
476 unsigned char *randbytes = ssl->handshake->randbytes;
477 size_t gmt_unix_time_len = 0;
478
479 /*
480 * Generate the random bytes
481 *
482 * TLS 1.2 case:
483 * struct {
484 * uint32 gmt_unix_time;
485 * opaque random_bytes[28];
486 * } Random;
487 *
488 * TLS 1.3 case:
489 * opaque Random[32];
490 */
491 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
492 {
493#if defined(MBEDTLS_HAVE_TIME)
494 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
495 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
496 gmt_unix_time_len = 4;
497
498 MBEDTLS_SSL_DEBUG_MSG( 3,
499 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
500 (long long) gmt_unix_time ) );
501#endif /* MBEDTLS_HAVE_TIME */
502 }
503
504 ret = ssl->conf->f_rng( ssl->conf->p_rng,
505 randbytes + gmt_unix_time_len,
506 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
507 return( ret );
508}
509
Ronald Cron71c23322022-02-18 17:29:39 +0100510static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100511{
512 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100513 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100514
515 if( ssl->conf->f_rng == NULL )
516 {
517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
518 return( MBEDTLS_ERR_SSL_NO_RNG );
519 }
520
Ronald Cron86a477f2022-02-18 17:45:10 +0100521 /* Bet on the highest configured version if we are not in a TLS 1.2
522 * renegotiation or session resumption.
523 */
524#if defined(MBEDTLS_SSL_RENEGOTIATION)
525 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
526 ssl->handshake->min_minor_ver = ssl->minor_ver;
527 else
528#endif
529 {
530 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
531
532 if( ssl->handshake->resume )
533 {
534 ssl->minor_ver = ssl->session_negotiate->minor_ver;
535 ssl->handshake->min_minor_ver = ssl->minor_ver;
536 }
537 else
538 {
539 ssl->minor_ver = ssl->conf->max_minor_ver;
540 ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver;
541 }
542 }
543
Ronald Cron58b80382022-02-18 18:41:08 +0100544 /*
545 * But when responding to a verify request where we MUST reuse the
546 * previoulsy generated random bytes (RFC 6347 4.2.1), generate the
547 * random bytes.
548 */
549#if defined(MBEDTLS_SSL_PROTO_DTLS)
550 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
551 ( ssl->handshake->cookie == NULL ) )
552#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100553 {
Ronald Cron58b80382022-02-18 18:41:08 +0100554 ret = ssl_generate_random( ssl );
555 if( ret != 0 )
556 {
557 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
558 return( ret );
559 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100560 }
561
Ronald Cron3d580bf2022-02-18 17:24:56 +0100562 /*
Ronald Cron021b1782022-02-19 17:32:53 +0100563 * Prepare session identifier. But in the case of a TLS 1.2 session
564 * renegotiation or session resumption, the initial value of the session
565 * identifier length below is equal to zero.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100566 */
Ronald Cron021b1782022-02-19 17:32:53 +0100567 session_id_len = ssl->session_negotiate->id_len;
568
569#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
570 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100571 {
Ronald Cron021b1782022-02-19 17:32:53 +0100572 if( session_id_len < 16 || session_id_len > 32 ||
573#if defined(MBEDTLS_SSL_RENEGOTIATION)
574 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
575#endif
576 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100577 {
Ronald Cron021b1782022-02-19 17:32:53 +0100578 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100579 }
Ronald Cron021b1782022-02-19 17:32:53 +0100580
581#if defined(MBEDTLS_SSL_SESSION_TICKETS)
582 /*
583 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
584 * generate and include a Session ID in the TLS ClientHello."
585 */
586#if defined(MBEDTLS_SSL_RENEGOTIATION)
587 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
588#endif
589 {
590 if( ( ssl->session_negotiate->ticket != NULL ) &&
591 ( ssl->session_negotiate->ticket_len != 0 ) )
592 {
593 session_id_len = 32;
594 }
595 }
596#endif /* MBEDTLS_SSL_SESSION_TICKETS */
597 }
598#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
599
600#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
601 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
602 {
603 /*
604 * Create a legacy session identifier for the purpose of middlebox
605 * compatibility only if one has not been created already, which is
606 * the case if we are here for the TLS 1.3 second ClientHello.
607 *
608 * Versions of TLS before TLS 1.3 supported a "session resumption"
609 * feature which has been merged with pre-shared keys in TLS 1.3
610 * version. A client which has a cached session ID set by a pre-TLS 1.3
611 * server SHOULD set this field to that value. In compatibility mode,
612 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
613 * session MUST generate a new 32-byte value. This value need not be
614 * random but SHOULD be unpredictable to avoid implementations fixating
615 * on a specific value (also known as ossification). Otherwise, it MUST
616 * be set as a zero-length vector ( i.e., a zero-valued single byte
617 * length field ).
618 */
619 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100620 }
621#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
622
Ronald Cron021b1782022-02-19 17:32:53 +0100623 if( session_id_len != ssl->session_negotiate->id_len )
624 {
625 ssl->session_negotiate->id_len = session_id_len;
626 if( session_id_len > 0 )
627 {
628 ret = ssl->conf->f_rng( ssl->conf->p_rng,
629 ssl->session_negotiate->id,
630 session_id_len );
631 if( ret != 0 )
632 {
633 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
634 return( ret );
635 }
636 }
637 }
638
Ronald Cron3d580bf2022-02-18 17:24:56 +0100639 return( 0 );
640}
641
642/*
643 * Write ClientHello handshake message.
644 * Handler for MBEDTLS_SSL_CLIENT_HELLO
645 */
646int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
647{
648 int ret = 0;
649 unsigned char *buf;
650 size_t buf_len, msg_len;
651
652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
653
Ronald Cron71c23322022-02-18 17:29:39 +0100654 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100655
656 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
657 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
658 &buf, &buf_len ) );
659
Ronald Cron71c23322022-02-18 17:29:39 +0100660 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
661 buf + buf_len,
662 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100663
664 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
665 buf, msg_len );
666
667 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
668 buf_len,
669 msg_len ) );
670
671 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
672
673cleanup:
674
675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
676 return ret;
677}
678
679#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
680#endif /* MBEDTLS_SSL_CLI_C */