blob: 20f1aff4b83167c1470d2e3d6dd16b5236dd1379 [file] [log] [blame]
Ronald Cron3d580bf2022-02-18 17:24:56 +01001/*
2 * TLS 1.2 and 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
24#if defined(MBEDTLS_SSL_CLI_C)
25#if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
26
Ronald Cron58b80382022-02-18 18:41:08 +010027#if defined(MBEDTLS_PLATFORM_C)
28#include "mbedtls/platform.h"
29#else
30#include <stdlib.h>
31#define mbedtls_calloc calloc
32#define mbedtls_free free
33#endif
34
Ronald Cron3d580bf2022-02-18 17:24:56 +010035#include <string.h>
36
37#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Ronald Cron58b80382022-02-18 18:41:08 +010039#if defined(MBEDTLS_HAVE_TIME)
40#include "mbedtls/platform_time.h"
41#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +010042
43#include "ssl_client.h"
44#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010045#include "ssl_tls13_keys.h"
46#include "ssl_debug_helpers.h"
47
Ronald Cronfbd9f992022-03-17 15:22:07 +010048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020049MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +010050static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
51 unsigned char *buf,
52 const unsigned char *end,
53 size_t *olen )
54{
55 unsigned char *p = buf;
56 size_t hostname_len;
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 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200133MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100134static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
135 unsigned char *buf,
136 const unsigned char *end,
137 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100138{
139 unsigned char *p = buf;
140
141 *out_len = 0;
142
143 if( ssl->conf->alpn_list == NULL )
144 return( 0 );
145
146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
147
148
149 /* Check we have enough space for the extension type (2 bytes), the
150 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
151 */
152 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
153 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
154 /* Skip writing extension and list length for now */
155 p += 6;
156
157 /*
158 * opaque ProtocolName<1..2^8-1>;
159 *
160 * struct {
161 * ProtocolName protocol_name_list<2..2^16-1>
162 * } ProtocolNameList;
163 */
164 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
165 {
166 /*
167 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
168 * protocol names is less than 255.
169 */
170 size_t protocol_name_len = strlen( *cur );
171
172 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
173 *p++ = (unsigned char)protocol_name_len;
174 memcpy( p, *cur, protocol_name_len );
175 p += protocol_name_len;
176 }
177
178 *out_len = p - buf;
179
180 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
181 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
182
183 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
184 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
185
186 return( 0 );
187}
188#endif /* MBEDTLS_SSL_ALPN */
189
Ronald Cronfbd9f992022-03-17 15:22:07 +0100190#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
191 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
192/*
193 * Function for writing a supported groups (TLS 1.3) or supported elliptic
194 * curves (TLS 1.2) extension.
195 *
196 * The "extension_data" field of a supported groups extension contains a
197 * "NamedGroupList" value (TLS 1.3 RFC8446):
198 * enum {
199 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
200 * x25519(0x001D), x448(0x001E),
201 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
202 * ffdhe6144(0x0103), ffdhe8192(0x0104),
203 * ffdhe_private_use(0x01FC..0x01FF),
204 * ecdhe_private_use(0xFE00..0xFEFF),
205 * (0xFFFF)
206 * } NamedGroup;
207 * struct {
208 * NamedGroup named_group_list<2..2^16-1>;
209 * } NamedGroupList;
210 *
211 * The "extension_data" field of a supported elliptic curves extension contains
212 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
213 * enum {
214 * deprecated(1..22),
215 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
216 * x25519(29), x448(30),
217 * reserved (0xFE00..0xFEFF),
218 * deprecated(0xFF01..0xFF02),
219 * (0xFFFF)
220 * } NamedCurve;
221 * struct {
222 * NamedCurve named_curve_list<2..2^16-1>
223 * } NamedCurveList;
224 *
225 * The TLS 1.3 supported groups extension was defined to be a compatible
226 * generalization of the TLS 1.2 supported elliptic curves extension. They both
227 * share the same extension identifier.
228 *
229 * DHE groups are not supported yet.
230 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200231MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cronfbd9f992022-03-17 15:22:07 +0100232static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
233 unsigned char *buf,
234 const unsigned char *end,
235 size_t *out_len )
236{
237 unsigned char *p = buf ;
238 unsigned char *named_group_list; /* Start of named_group_list */
239 size_t named_group_list_len; /* Length of named_group_list */
240 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
241
242 *out_len = 0;
243
244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
245
246 /* Check if we have space for header and length fields:
247 * - extension_type (2 bytes)
248 * - extension_data_length (2 bytes)
249 * - named_group_list_length (2 bytes)
250 */
251 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
252 p += 6;
253
254 named_group_list = p;
255
256 if( group_list == NULL )
257 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
258
259 for( ; *group_list != 0; group_list++ )
260 {
261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
262
263#if defined(MBEDTLS_ECP_C)
264 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
265 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
266 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
267 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
268 {
269 const mbedtls_ecp_curve_info *curve_info;
270 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
271 if( curve_info == NULL )
272 continue;
273 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
274 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
275 p += 2;
276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
277 curve_info->name, *group_list ) );
278 }
279#endif /* MBEDTLS_ECP_C */
280 /* Add DHE groups here */
281
282 }
283
284 /* Length of named_group_list */
285 named_group_list_len = p - named_group_list;
286 if( named_group_list_len == 0 )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
290 }
291
292 /* Write extension_type */
293 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
294 /* Write extension_data_length */
295 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
296 /* Write length of named_group_list */
297 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
298
299 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
300 buf + 4, named_group_list_len + 2 );
301
302 *out_len = p - buf;
303
304#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
305 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
306#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
307
308 return( 0 );
309}
310
311#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
312 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
313
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200314MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100315static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100316 mbedtls_ssl_context *ssl,
317 unsigned char *buf,
318 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100319 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100320 size_t *out_len )
321{
322 unsigned char *p = buf;
323 const int *ciphersuite_list;
324 unsigned char *cipher_suites; /* Start of the cipher_suites list */
325 size_t cipher_suites_len;
326
Ronald Crond491c2d2022-02-19 18:30:46 +0100327 *tls12_uses_ec = 0;
328 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100329
330 /*
331 * Ciphersuite list
332 *
333 * This is a list of the symmetric cipher options supported by
334 * the client, specifically the record protection algorithm
335 * ( including secret key length ) and a hash to be used with
336 * HKDF, in descending order of client preference.
337 */
338 ciphersuite_list = ssl->conf->ciphersuite_list;
339
340 /* Check there is space for the cipher suite list length (2 bytes). */
341 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
342 p += 2;
343
Ronald Cron64767262022-03-31 14:13:57 +0200344 /* Write cipher_suites
345 * CipherSuite cipher_suites<2..2^16-2>;
346 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100347 cipher_suites = p;
348 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
349 {
350 int cipher_suite = ciphersuite_list[i];
351 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
352
353 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100354
Ronald Cron757a2ab2022-03-30 13:57:40 +0200355 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400356 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400357 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100358 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100359
360#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
361 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
362 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
363 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
364#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100365
366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
367 (unsigned int) cipher_suite,
368 ciphersuite_info->name ) );
369
370 /* Check there is space for the cipher suite identifier (2 bytes). */
371 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
372 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
373 p += 2;
374 }
375
Ronald Crond491c2d2022-02-19 18:30:46 +0100376 /*
377 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
378 */
379#if defined(MBEDTLS_SSL_RENEGOTIATION)
380 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
381#endif
382 {
383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
384 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
385 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
386 p += 2;
387 }
388
Ronald Cron3d580bf2022-02-18 17:24:56 +0100389 /* Write the cipher_suites length in number of bytes */
390 cipher_suites_len = p - cipher_suites;
391 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
392 MBEDTLS_SSL_DEBUG_MSG( 3,
393 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
394 cipher_suites_len/2 ) );
395
396 /* Output the total length of cipher_suites field. */
397 *out_len = p - buf;
398
399 return( 0 );
400}
401
402/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100403 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100404 *
405 * struct {
406 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
407 * Random random;
408 * opaque legacy_session_id<0..32>;
409 * CipherSuite cipher_suites<2..2^16-2>;
410 * opaque legacy_compression_methods<1..2^8-1>;
411 * Extension extensions<8..2^16-1>;
412 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100413 *
414 * Structure of the (D)TLS 1.2 ClientHello message:
415 *
416 * struct {
417 * ProtocolVersion client_version;
418 * Random random;
419 * SessionID session_id;
420 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
421 * CipherSuite cipher_suites<2..2^16-2>;
422 * CompressionMethod compression_methods<1..2^8-1>;
423 * select (extensions_present) {
424 * case false:
425 * struct {};
426 * case true:
427 * Extension extensions<0..2^16-1>;
428 * };
429 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100430 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100432static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
433 unsigned char *buf,
434 unsigned char *end,
435 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100436{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100437 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100438 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100439 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100440 unsigned char *p_extensions_len; /* Pointer to extensions length */
441 size_t output_len; /* Length of buffer used by function */
442 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100443 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100444
Ronald Cron3d580bf2022-02-18 17:24:56 +0100445 *out_len = 0;
446
Ronald Cron4079abc2022-02-20 10:35:26 +0100447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200448 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400449 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200450 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400451 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100452#endif
453#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200454 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400455 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200456 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400457 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100458#endif
459
Ronald Cron3d580bf2022-02-18 17:24:56 +0100460 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100461 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100462 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100463 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100464 */
465 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400466 mbedtls_ssl_write_version( p, ssl->conf->transport,
467 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100468 p += 2;
469
Ronald Cron58b80382022-02-18 18:41:08 +0100470 /* ...
471 * Random random;
472 * ...
473 *
Ronald Cron58b80382022-02-18 18:41:08 +0100474 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100475 * the handshake->randbytes buffer and are copied here into the output
476 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100477 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100478 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100479 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100480 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
481 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
482 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
483
Ronald Cron021b1782022-02-19 17:32:53 +0100484 /* TLS 1.2:
485 * ...
486 * SessionID session_id;
487 * ...
488 * with
489 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100490 *
Ronald Cron021b1782022-02-19 17:32:53 +0100491 * TLS 1.3:
492 * ...
493 * opaque legacy_session_id<0..32>;
494 * ...
495 *
Ronald Cronda41b382022-03-30 09:57:11 +0200496 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100497 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
498 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100499 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100500 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
501 *p++ = (unsigned char)ssl->session_negotiate->id_len;
502 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
503 p += ssl->session_negotiate->id_len;
504
505 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
506 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100507
Ronald Crona874aa82022-02-19 18:11:26 +0100508 /* DTLS 1.2 ONLY
509 * ...
510 * opaque cookie<0..2^8-1>;
511 * ...
512 */
513#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
515 {
516 unsigned char cookie_len = 0;
517
Ronald Cron4079abc2022-02-20 10:35:26 +0100518 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100519 {
520 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100521 handshake->cookie,
522 handshake->verify_cookie_len );
523 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100524 }
525
526 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
527 *p++ = cookie_len;
528 if( cookie_len > 0 )
529 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100530 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100531 p += cookie_len;
532 }
533 }
534#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
535
Ronald Cron3d580bf2022-02-18 17:24:56 +0100536 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100537 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
538 &tls12_uses_ec,
539 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100540 if( ret != 0 )
541 return( ret );
542 p += output_len;
543
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100544 /* Write legacy_compression_methods (TLS 1.3) or
545 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100546 *
547 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
548 * one byte set to zero, which corresponds to the 'null' compression
549 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100550 *
551 * For TLS 1.2 ClientHello, for security reasons we do not support
552 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100553 */
554 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
555 *p++ = 1;
556 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
557
558 /* Write extensions */
559
560#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
561 /* Keeping track of the included extensions */
Ronald Cron4079abc2022-02-20 10:35:26 +0100562 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100563#endif
564
565 /* First write extensions, then the total length */
566 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
567 p_extensions_len = p;
568 p += 2;
569
Ronald Crondf823bf2022-03-29 18:57:54 +0200570#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
571 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100572 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100573 if( ret != 0 )
574 return( ret );
575 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200576#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100577
578#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100579 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100580 if( ret != 0 )
581 return( ret );
582 p += output_len;
583#endif /* MBEDTLS_SSL_ALPN */
584
585#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100586 if( propose_tls13 )
587 {
588 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
589 &output_len );
590 if( ret != 0 )
591 return( ret );
592 p += output_len;
593 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200594#endif
595
Ronald Cron4079abc2022-02-20 10:35:26 +0100596#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
597 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
598 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200599#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100600 ( propose_tls13 &&
601 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
602#endif
603#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
604 ( propose_tls12 && tls12_uses_ec ) ||
605#endif
606 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100607 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100608 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100609 if( ret != 0 )
610 return( ret );
611 p += output_len;
612 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100613#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100614
Ronald Cron11e18572022-03-17 13:44:33 +0100615#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100616 if(
617#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
618 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
619#endif
620#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
621 propose_tls12 ||
622#endif
623 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100624 {
XiaokangQianeaf36512022-04-24 09:07:44 +0000625 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100626 if( ret != 0 )
627 return( ret );
628 p += output_len;
629 }
630#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100631
Ronald Cron4079abc2022-02-20 10:35:26 +0100632#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
633 if( propose_tls12 )
634 {
635 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
636 tls12_uses_ec,
637 &output_len );
638 if( ret != 0 )
639 return( ret );
640 p += output_len;
641 }
642#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100643
644 /* Write the length of the list of extensions. */
645 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100646
647 if( extensions_len == 0 )
648 p = p_extensions_len;
649 else
650 {
651 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
653 MBEDTLS_PRINTF_SIZET, extensions_len ) );
654 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
655 p_extensions_len, extensions_len );
656 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100657
658 *out_len = p - buf;
659 return( 0 );
660}
661
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200662MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron58b80382022-02-18 18:41:08 +0100663static int ssl_generate_random( mbedtls_ssl_context *ssl )
664{
665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
666 unsigned char *randbytes = ssl->handshake->randbytes;
667 size_t gmt_unix_time_len = 0;
668
669 /*
670 * Generate the random bytes
671 *
672 * TLS 1.2 case:
673 * struct {
674 * uint32 gmt_unix_time;
675 * opaque random_bytes[28];
676 * } Random;
677 *
678 * TLS 1.3 case:
679 * opaque Random[32];
680 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400681 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100682 {
683#if defined(MBEDTLS_HAVE_TIME)
684 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
685 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
686 gmt_unix_time_len = 4;
687
688 MBEDTLS_SSL_DEBUG_MSG( 3,
689 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
690 (long long) gmt_unix_time ) );
691#endif /* MBEDTLS_HAVE_TIME */
692 }
693
694 ret = ssl->conf->f_rng( ssl->conf->p_rng,
695 randbytes + gmt_unix_time_len,
696 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
697 return( ret );
698}
699
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200700MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100701static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100702{
703 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100704 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100705
706 if( ssl->conf->f_rng == NULL )
707 {
708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
709 return( MBEDTLS_ERR_SSL_NO_RNG );
710 }
711
Ronald Cron86a477f2022-02-18 17:45:10 +0100712 /* Bet on the highest configured version if we are not in a TLS 1.2
713 * renegotiation or session resumption.
714 */
715#if defined(MBEDTLS_SSL_RENEGOTIATION)
716 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400717 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100718 else
719#endif
720 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100721 if( ssl->handshake->resume )
722 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400723 ssl->tls_version = ssl->session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400724 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100725 }
726 else
727 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400728 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400729 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100730 }
731 }
732
Ronald Cron58b80382022-02-18 18:41:08 +0100733 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200734 * Generate the random bytes, except when responding to a verify request
735 * where we MUST reuse the previoulsy generated random bytes
736 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100737 */
738#if defined(MBEDTLS_SSL_PROTO_DTLS)
739 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
740 ( ssl->handshake->cookie == NULL ) )
741#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100742 {
Ronald Cron58b80382022-02-18 18:41:08 +0100743 ret = ssl_generate_random( ssl );
744 if( ret != 0 )
745 {
746 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
747 return( ret );
748 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100749 }
750
Ronald Cron3d580bf2022-02-18 17:24:56 +0100751 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200752 * Prepare session identifier. At that point, the length of the session
753 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
754 * to zero, except in the case of a TLS 1.2 session renegotiation or
755 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100756 */
Ronald Cron021b1782022-02-19 17:32:53 +0100757 session_id_len = ssl->session_negotiate->id_len;
758
759#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400760 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100761 {
Ronald Cron021b1782022-02-19 17:32:53 +0100762 if( session_id_len < 16 || session_id_len > 32 ||
763#if defined(MBEDTLS_SSL_RENEGOTIATION)
764 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
765#endif
766 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100767 {
Ronald Cron021b1782022-02-19 17:32:53 +0100768 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100769 }
Ronald Cron021b1782022-02-19 17:32:53 +0100770
771#if defined(MBEDTLS_SSL_SESSION_TICKETS)
772 /*
773 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
774 * generate and include a Session ID in the TLS ClientHello."
775 */
776#if defined(MBEDTLS_SSL_RENEGOTIATION)
777 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
778#endif
779 {
780 if( ( ssl->session_negotiate->ticket != NULL ) &&
781 ( ssl->session_negotiate->ticket_len != 0 ) )
782 {
783 session_id_len = 32;
784 }
785 }
786#endif /* MBEDTLS_SSL_SESSION_TICKETS */
787 }
788#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
789
790#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400791 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100792 {
793 /*
794 * Create a legacy session identifier for the purpose of middlebox
795 * compatibility only if one has not been created already, which is
796 * the case if we are here for the TLS 1.3 second ClientHello.
797 *
798 * Versions of TLS before TLS 1.3 supported a "session resumption"
799 * feature which has been merged with pre-shared keys in TLS 1.3
800 * version. A client which has a cached session ID set by a pre-TLS 1.3
801 * server SHOULD set this field to that value. In compatibility mode,
802 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
803 * session MUST generate a new 32-byte value. This value need not be
804 * random but SHOULD be unpredictable to avoid implementations fixating
805 * on a specific value (also known as ossification). Otherwise, it MUST
806 * be set as a zero-length vector ( i.e., a zero-valued single byte
807 * length field ).
808 */
809 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100810 }
811#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
812
Ronald Cron021b1782022-02-19 17:32:53 +0100813 if( session_id_len != ssl->session_negotiate->id_len )
814 {
815 ssl->session_negotiate->id_len = session_id_len;
816 if( session_id_len > 0 )
817 {
818 ret = ssl->conf->f_rng( ssl->conf->p_rng,
819 ssl->session_negotiate->id,
820 session_id_len );
821 if( ret != 0 )
822 {
823 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
824 return( ret );
825 }
826 }
827 }
828
Ronald Cron3d580bf2022-02-18 17:24:56 +0100829 return( 0 );
830}
831
832/*
833 * Write ClientHello handshake message.
834 * Handler for MBEDTLS_SSL_CLIENT_HELLO
835 */
836int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
837{
838 int ret = 0;
839 unsigned char *buf;
840 size_t buf_len, msg_len;
841
842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
843
Ronald Cron71c23322022-02-18 17:29:39 +0100844 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100845
846 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
847 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
848 &buf, &buf_len ) );
849
Ronald Cron71c23322022-02-18 17:29:39 +0100850 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
851 buf + buf_len,
852 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100853
Ronald Cron5f4e9122022-02-21 09:50:36 +0100854#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
856 {
857 ssl->out_msglen = msg_len + 4;
858 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100859
Ronald Cron8ecd9932022-03-29 12:26:54 +0200860 /*
861 * The two functions below may try to send data on the network and
862 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
863 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200864 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200865 * set the handshake state to the next state at that point to ensure
866 * that we will not write and send again a ClientHello when we
867 * eventually succeed in sending the pending data.
868 */
869 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
870
Ronald Cron5f4e9122022-02-21 09:50:36 +0100871 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
872 {
873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
874 return( ret );
875 }
876
Ronald Cron8ecd9932022-03-29 12:26:54 +0200877 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100878 {
879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
880 return( ret );
881 }
882 }
883 else
884#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
885 {
886 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
887 buf, msg_len );
888 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
889 buf_len,
890 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200891 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100892 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100893
Ronald Cron3d580bf2022-02-18 17:24:56 +0100894
895cleanup:
896
897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
898 return ret;
899}
900
901#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
902#endif /* MBEDTLS_SSL_CLI_C */