blob: 0c32f07bf07db4c6e7fa243e20c5eee7d7045bab [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)
49static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
50 unsigned char *buf,
51 const unsigned char *end,
52 size_t *olen )
53{
54 unsigned char *p = buf;
55 size_t hostname_len;
56
57 *olen = 0;
58
59 if( ssl->hostname == NULL )
60 return( 0 );
61
62 MBEDTLS_SSL_DEBUG_MSG( 3,
63 ( "client hello, adding server name extension: %s",
64 ssl->hostname ) );
65
66 hostname_len = strlen( ssl->hostname );
67
68 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
69
70 /*
71 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
72 *
73 * In order to provide any of the server names, clients MAY include an
74 * extension of type "server_name" in the (extended) client hello. The
75 * "extension_data" field of this extension SHALL contain
76 * "ServerNameList" where:
77 *
78 * struct {
79 * NameType name_type;
80 * select (name_type) {
81 * case host_name: HostName;
82 * } name;
83 * } ServerName;
84 *
85 * enum {
86 * host_name(0), (255)
87 * } NameType;
88 *
89 * opaque HostName<1..2^16-1>;
90 *
91 * struct {
92 * ServerName server_name_list<1..2^16-1>
93 * } ServerNameList;
94 *
95 */
96 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
97 p += 2;
98
99 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
100 p += 2;
101
102 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
103 p += 2;
104
105 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
106
107 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
108 p += 2;
109
110 memcpy( p, ssl->hostname, hostname_len );
111
112 *olen = hostname_len + 9;
113
114 return( 0 );
115}
116#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
117
Ronald Cron3d580bf2022-02-18 17:24:56 +0100118#if defined(MBEDTLS_SSL_ALPN)
119/*
Ronald Cron71c23322022-02-18 17:29:39 +0100120 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100121 *
122 * Structure of the application_layer_protocol_negotiation extension in
123 * ClientHello:
124 *
125 * opaque ProtocolName<1..2^8-1>;
126 *
127 * struct {
128 * ProtocolName protocol_name_list<2..2^16-1>
129 * } ProtocolNameList;
130 *
131 */
Ronald Cron71c23322022-02-18 17:29:39 +0100132static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
133 unsigned char *buf,
134 const unsigned char *end,
135 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100136{
137 unsigned char *p = buf;
138
139 *out_len = 0;
140
141 if( ssl->conf->alpn_list == NULL )
142 return( 0 );
143
144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
145
146
147 /* Check we have enough space for the extension type (2 bytes), the
148 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
149 */
150 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
151 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
152 /* Skip writing extension and list length for now */
153 p += 6;
154
155 /*
156 * opaque ProtocolName<1..2^8-1>;
157 *
158 * struct {
159 * ProtocolName protocol_name_list<2..2^16-1>
160 * } ProtocolNameList;
161 */
162 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
163 {
164 /*
165 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
166 * protocol names is less than 255.
167 */
168 size_t protocol_name_len = strlen( *cur );
169
170 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
171 *p++ = (unsigned char)protocol_name_len;
172 memcpy( p, *cur, protocol_name_len );
173 p += protocol_name_len;
174 }
175
176 *out_len = p - buf;
177
178 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
179 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
180
181 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
182 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
183
184 return( 0 );
185}
186#endif /* MBEDTLS_SSL_ALPN */
187
Ronald Cronfbd9f992022-03-17 15:22:07 +0100188#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
189 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
190/*
191 * Function for writing a supported groups (TLS 1.3) or supported elliptic
192 * curves (TLS 1.2) extension.
193 *
194 * The "extension_data" field of a supported groups extension contains a
195 * "NamedGroupList" value (TLS 1.3 RFC8446):
196 * enum {
197 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
198 * x25519(0x001D), x448(0x001E),
199 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
200 * ffdhe6144(0x0103), ffdhe8192(0x0104),
201 * ffdhe_private_use(0x01FC..0x01FF),
202 * ecdhe_private_use(0xFE00..0xFEFF),
203 * (0xFFFF)
204 * } NamedGroup;
205 * struct {
206 * NamedGroup named_group_list<2..2^16-1>;
207 * } NamedGroupList;
208 *
209 * The "extension_data" field of a supported elliptic curves extension contains
210 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
211 * enum {
212 * deprecated(1..22),
213 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
214 * x25519(29), x448(30),
215 * reserved (0xFE00..0xFEFF),
216 * deprecated(0xFF01..0xFF02),
217 * (0xFFFF)
218 * } NamedCurve;
219 * struct {
220 * NamedCurve named_curve_list<2..2^16-1>
221 * } NamedCurveList;
222 *
223 * The TLS 1.3 supported groups extension was defined to be a compatible
224 * generalization of the TLS 1.2 supported elliptic curves extension. They both
225 * share the same extension identifier.
226 *
227 * DHE groups are not supported yet.
228 */
229static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
230 unsigned char *buf,
231 const unsigned char *end,
232 size_t *out_len )
233{
234 unsigned char *p = buf ;
235 unsigned char *named_group_list; /* Start of named_group_list */
236 size_t named_group_list_len; /* Length of named_group_list */
237 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
238
239 *out_len = 0;
240
241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
242
243 /* Check if we have space for header and length fields:
244 * - extension_type (2 bytes)
245 * - extension_data_length (2 bytes)
246 * - named_group_list_length (2 bytes)
247 */
248 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
249 p += 6;
250
251 named_group_list = p;
252
253 if( group_list == NULL )
254 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
255
256 for( ; *group_list != 0; group_list++ )
257 {
258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
259
260#if defined(MBEDTLS_ECP_C)
261 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
262 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
263 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
264 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
265 {
266 const mbedtls_ecp_curve_info *curve_info;
267 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
268 if( curve_info == NULL )
269 continue;
270 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
271 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
272 p += 2;
273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
274 curve_info->name, *group_list ) );
275 }
276#endif /* MBEDTLS_ECP_C */
277 /* Add DHE groups here */
278
279 }
280
281 /* Length of named_group_list */
282 named_group_list_len = p - named_group_list;
283 if( named_group_list_len == 0 )
284 {
285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
287 }
288
289 /* Write extension_type */
290 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
291 /* Write extension_data_length */
292 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
293 /* Write length of named_group_list */
294 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
295
296 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
297 buf + 4, named_group_list_len + 2 );
298
299 *out_len = p - buf;
300
301#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
302 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
304
305 return( 0 );
306}
307
308#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
309 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
310
311#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
312/*
313 * Function for writing a signature algorithm extension.
314 *
315 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
316 * value (TLS 1.3 RFC8446):
317 * enum {
318 * ....
319 * ecdsa_secp256r1_sha256( 0x0403 ),
320 * ecdsa_secp384r1_sha384( 0x0503 ),
321 * ecdsa_secp521r1_sha512( 0x0603 ),
322 * ....
323 * } SignatureScheme;
324 *
325 * struct {
326 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
327 * } SignatureSchemeList;
328 *
329 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
330 * value (TLS 1.2 RFC5246):
331 * enum {
332 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
333 * sha512(6), (255)
334 * } HashAlgorithm;
335 *
336 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
337 * SignatureAlgorithm;
338 *
339 * struct {
340 * HashAlgorithm hash;
341 * SignatureAlgorithm signature;
342 * } SignatureAndHashAlgorithm;
343 *
344 * SignatureAndHashAlgorithm
345 * supported_signature_algorithms<2..2^16-2>;
346 *
347 * The TLS 1.3 signature algorithm extension was defined to be a compatible
348 * generalization of the TLS 1.2 signature algorithm extension.
349 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
350 * `SignatureScheme` field of TLS 1.3
351 *
352 */
353static int ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
354 const unsigned char *end, size_t *out_len )
355{
356 unsigned char *p = buf;
357 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
358 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
359
360 *out_len = 0;
361
362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
363
364 /* Check if we have space for header and length field:
365 * - extension_type (2 bytes)
366 * - extension_data_length (2 bytes)
367 * - supported_signature_algorithms_length (2 bytes)
368 */
369 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
370 p += 6;
371
372 /*
373 * Write supported_signature_algorithms
374 */
375 supported_sig_alg = p;
376 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
377 if( sig_alg == NULL )
378 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
379
380 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
381 {
382 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
383 continue;
384 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
385 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
386 p += 2;
387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
388 }
389
390 /* Length of supported_signature_algorithms */
391 supported_sig_alg_len = p - supported_sig_alg;
392 if( supported_sig_alg_len == 0 )
393 {
394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
396 }
397
398 /* Write extension_type */
399 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
400 /* Write extension_data_length */
401 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
402 /* Write length of supported_signature_algorithms */
403 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
404
405 /* Output the total length of signature algorithms extension. */
406 *out_len = p - buf;
407
408#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
409 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
410#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
411 return( 0 );
412}
413#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
414
Ronald Cron71c23322022-02-18 17:29:39 +0100415static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100416 mbedtls_ssl_context *ssl,
417 unsigned char *buf,
418 unsigned char *end,
Ronald Crond491c2d2022-02-19 18:30:46 +0100419 int *tls12_uses_ec,
Ronald Cron3d580bf2022-02-18 17:24:56 +0100420 size_t *out_len )
421{
422 unsigned char *p = buf;
423 const int *ciphersuite_list;
424 unsigned char *cipher_suites; /* Start of the cipher_suites list */
425 size_t cipher_suites_len;
426
Ronald Crond491c2d2022-02-19 18:30:46 +0100427 *tls12_uses_ec = 0;
428 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100429
430 /*
431 * Ciphersuite list
432 *
433 * This is a list of the symmetric cipher options supported by
434 * the client, specifically the record protection algorithm
435 * ( including secret key length ) and a hash to be used with
436 * HKDF, in descending order of client preference.
437 */
438 ciphersuite_list = ssl->conf->ciphersuite_list;
439
440 /* Check there is space for the cipher suite list length (2 bytes). */
441 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
442 p += 2;
443
Ronald Cron64767262022-03-31 14:13:57 +0200444 /* Write cipher_suites
445 * CipherSuite cipher_suites<2..2^16-2>;
446 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100447 cipher_suites = p;
448 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
449 {
450 int cipher_suite = ciphersuite_list[i];
451 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
452
453 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Ronald Crond491c2d2022-02-19 18:30:46 +0100454
Ronald Cron757a2ab2022-03-30 13:57:40 +0200455 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
Glenn Strausscd78df62022-04-07 19:07:11 -0400456 ssl->handshake->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -0400457 ssl->tls_version ) != 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100458 continue;
Ronald Crond491c2d2022-02-19 18:30:46 +0100459
460#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
461 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
462 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) )
463 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
464#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100465
466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
467 (unsigned int) cipher_suite,
468 ciphersuite_info->name ) );
469
470 /* Check there is space for the cipher suite identifier (2 bytes). */
471 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
472 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
473 p += 2;
474 }
475
Ronald Crond491c2d2022-02-19 18:30:46 +0100476 /*
477 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
478 */
479#if defined(MBEDTLS_SSL_RENEGOTIATION)
480 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
481#endif
482 {
483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
484 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
485 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
486 p += 2;
487 }
488
Ronald Cron3d580bf2022-02-18 17:24:56 +0100489 /* Write the cipher_suites length in number of bytes */
490 cipher_suites_len = p - cipher_suites;
491 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
492 MBEDTLS_SSL_DEBUG_MSG( 3,
493 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
494 cipher_suites_len/2 ) );
495
496 /* Output the total length of cipher_suites field. */
497 *out_len = p - buf;
498
499 return( 0 );
500}
501
502/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100503 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100504 *
505 * struct {
506 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
507 * Random random;
508 * opaque legacy_session_id<0..32>;
509 * CipherSuite cipher_suites<2..2^16-2>;
510 * opaque legacy_compression_methods<1..2^8-1>;
511 * Extension extensions<8..2^16-1>;
512 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100513 *
514 * Structure of the (D)TLS 1.2 ClientHello message:
515 *
516 * struct {
517 * ProtocolVersion client_version;
518 * Random random;
519 * SessionID session_id;
520 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
521 * CipherSuite cipher_suites<2..2^16-2>;
522 * CompressionMethod compression_methods<1..2^8-1>;
523 * select (extensions_present) {
524 * case false:
525 * struct {};
526 * case true:
527 * Extension extensions<0..2^16-1>;
528 * };
529 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100530 */
Ronald Cron71c23322022-02-18 17:29:39 +0100531static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
532 unsigned char *buf,
533 unsigned char *end,
534 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100535{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100536 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100537 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100538 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100539 unsigned char *p_extensions_len; /* Pointer to extensions length */
540 size_t output_len; /* Length of buffer used by function */
541 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100542 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100543
Ronald Cron3d580bf2022-02-18 17:24:56 +0100544 *out_len = 0;
545
Ronald Cron4079abc2022-02-20 10:35:26 +0100546#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200547 unsigned char propose_tls12 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400548 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron150d5792022-03-30 20:24:51 +0200549 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400550 ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100551#endif
552#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200553 unsigned char propose_tls13 =
Glenn Strausscd78df62022-04-07 19:07:11 -0400554 ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron150d5792022-03-30 20:24:51 +0200555 &&
Glenn Strauss60bfe602022-03-14 19:04:24 -0400556 ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version );
Ronald Cron4079abc2022-02-20 10:35:26 +0100557#endif
558
Ronald Cron3d580bf2022-02-18 17:24:56 +0100559 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100560 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100561 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100562 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100563 */
564 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Glenn Strausse3af4cb2022-03-15 03:23:42 -0400565 mbedtls_ssl_write_version( p, ssl->conf->transport,
566 MBEDTLS_SSL_VERSION_TLS1_2 );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100567 p += 2;
568
Ronald Cron58b80382022-02-18 18:41:08 +0100569 /* ...
570 * Random random;
571 * ...
572 *
Ronald Cron58b80382022-02-18 18:41:08 +0100573 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100574 * the handshake->randbytes buffer and are copied here into the output
575 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100576 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100577 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron4079abc2022-02-20 10:35:26 +0100578 memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100579 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
580 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
581 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
582
Ronald Cron021b1782022-02-19 17:32:53 +0100583 /* TLS 1.2:
584 * ...
585 * SessionID session_id;
586 * ...
587 * with
588 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100589 *
Ronald Cron021b1782022-02-19 17:32:53 +0100590 * TLS 1.3:
591 * ...
592 * opaque legacy_session_id<0..32>;
593 * ...
594 *
Ronald Cronda41b382022-03-30 09:57:11 +0200595 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100596 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
597 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100598 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100599 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
600 *p++ = (unsigned char)ssl->session_negotiate->id_len;
601 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
602 p += ssl->session_negotiate->id_len;
603
604 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
605 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100606
Ronald Crona874aa82022-02-19 18:11:26 +0100607 /* DTLS 1.2 ONLY
608 * ...
609 * opaque cookie<0..2^8-1>;
610 * ...
611 */
612#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
614 {
615 unsigned char cookie_len = 0;
616
Ronald Cron4079abc2022-02-20 10:35:26 +0100617 if( handshake->cookie != NULL )
Ronald Crona874aa82022-02-19 18:11:26 +0100618 {
619 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Ronald Cron4079abc2022-02-20 10:35:26 +0100620 handshake->cookie,
621 handshake->verify_cookie_len );
622 cookie_len = handshake->verify_cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100623 }
624
625 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
626 *p++ = cookie_len;
627 if( cookie_len > 0 )
628 {
Ronald Cron4079abc2022-02-20 10:35:26 +0100629 memcpy( p, handshake->cookie, cookie_len );
Ronald Crona874aa82022-02-19 18:11:26 +0100630 p += cookie_len;
631 }
632 }
633#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
634
Ronald Cron3d580bf2022-02-18 17:24:56 +0100635 /* Write cipher_suites */
Ronald Crond491c2d2022-02-19 18:30:46 +0100636 ret = ssl_write_client_hello_cipher_suites( ssl, p, end,
637 &tls12_uses_ec,
638 &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100639 if( ret != 0 )
640 return( ret );
641 p += output_len;
642
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100643 /* Write legacy_compression_methods (TLS 1.3) or
644 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100645 *
646 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
647 * one byte set to zero, which corresponds to the 'null' compression
648 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100649 *
650 * For TLS 1.2 ClientHello, for security reasons we do not support
651 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100652 */
653 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
654 *p++ = 1;
655 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
656
657 /* Write extensions */
658
659#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
660 /* Keeping track of the included extensions */
Ronald Cron4079abc2022-02-20 10:35:26 +0100661 handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100662#endif
663
664 /* First write extensions, then the total length */
665 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
666 p_extensions_len = p;
667 p += 2;
668
Ronald Crondf823bf2022-03-29 18:57:54 +0200669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
670 /* Write server name extension */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100671 ret = ssl_write_hostname_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100672 if( ret != 0 )
673 return( ret );
674 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200675#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100676
677#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100678 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100679 if( ret != 0 )
680 return( ret );
681 p += output_len;
682#endif /* MBEDTLS_SSL_ALPN */
683
684#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100685 if( propose_tls13 )
686 {
687 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end,
688 &output_len );
689 if( ret != 0 )
690 return( ret );
691 p += output_len;
692 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200693#endif
694
Ronald Cron4079abc2022-02-20 10:35:26 +0100695#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
696 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
697 if(
Ronald Crondf823bf2022-03-29 18:57:54 +0200698#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron4079abc2022-02-20 10:35:26 +0100699 ( propose_tls13 &&
700 mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) ||
701#endif
702#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
703 ( propose_tls12 && tls12_uses_ec ) ||
704#endif
705 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100706 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100707 ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100708 if( ret != 0 )
709 return( ret );
710 p += output_len;
711 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100712#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100713
Ronald Cron11e18572022-03-17 13:44:33 +0100714#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron4079abc2022-02-20 10:35:26 +0100715 if(
716#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
717 ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) ||
718#endif
719#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
720 propose_tls12 ||
721#endif
722 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100723 {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100724 ret = ssl_write_sig_alg_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100725 if( ret != 0 )
726 return( ret );
727 p += output_len;
728 }
729#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100730
Ronald Cron4079abc2022-02-20 10:35:26 +0100731#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
732 if( propose_tls12 )
733 {
734 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end,
735 tls12_uses_ec,
736 &output_len );
737 if( ret != 0 )
738 return( ret );
739 p += output_len;
740 }
741#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100742
743 /* Write the length of the list of extensions. */
744 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100745
746 if( extensions_len == 0 )
747 p = p_extensions_len;
748 else
749 {
750 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \
752 MBEDTLS_PRINTF_SIZET, extensions_len ) );
753 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions",
754 p_extensions_len, extensions_len );
755 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100756
757 *out_len = p - buf;
758 return( 0 );
759}
760
Ronald Cron58b80382022-02-18 18:41:08 +0100761static int ssl_generate_random( mbedtls_ssl_context *ssl )
762{
763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
764 unsigned char *randbytes = ssl->handshake->randbytes;
765 size_t gmt_unix_time_len = 0;
766
767 /*
768 * Generate the random bytes
769 *
770 * TLS 1.2 case:
771 * struct {
772 * uint32 gmt_unix_time;
773 * opaque random_bytes[28];
774 * } Random;
775 *
776 * TLS 1.3 case:
777 * opaque Random[32];
778 */
Glenn Strauss60bfe602022-03-14 19:04:24 -0400779 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron58b80382022-02-18 18:41:08 +0100780 {
781#if defined(MBEDTLS_HAVE_TIME)
782 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
783 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
784 gmt_unix_time_len = 4;
785
786 MBEDTLS_SSL_DEBUG_MSG( 3,
787 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
788 (long long) gmt_unix_time ) );
789#endif /* MBEDTLS_HAVE_TIME */
790 }
791
792 ret = ssl->conf->f_rng( ssl->conf->p_rng,
793 randbytes + gmt_unix_time_len,
794 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
795 return( ret );
796}
797
Ronald Cron71c23322022-02-18 17:29:39 +0100798static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100799{
800 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100801 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100802
803 if( ssl->conf->f_rng == NULL )
804 {
805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
806 return( MBEDTLS_ERR_SSL_NO_RNG );
807 }
808
Ronald Cron86a477f2022-02-18 17:45:10 +0100809 /* Bet on the highest configured version if we are not in a TLS 1.2
810 * renegotiation or session resumption.
811 */
812#if defined(MBEDTLS_SSL_RENEGOTIATION)
813 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Glenn Strausscd78df62022-04-07 19:07:11 -0400814 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100815 else
816#endif
817 {
Ronald Cron86a477f2022-02-18 17:45:10 +0100818 if( ssl->handshake->resume )
819 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400820 ssl->tls_version = ssl->session_negotiate->tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400821 ssl->handshake->min_tls_version = ssl->tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100822 }
823 else
824 {
Glenn Strauss60bfe602022-03-14 19:04:24 -0400825 ssl->tls_version = ssl->conf->max_tls_version;
Glenn Strausscd78df62022-04-07 19:07:11 -0400826 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100827 }
828 }
829
Ronald Cron58b80382022-02-18 18:41:08 +0100830 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200831 * Generate the random bytes, except when responding to a verify request
832 * where we MUST reuse the previoulsy generated random bytes
833 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100834 */
835#if defined(MBEDTLS_SSL_PROTO_DTLS)
836 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
837 ( ssl->handshake->cookie == NULL ) )
838#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100839 {
Ronald Cron58b80382022-02-18 18:41:08 +0100840 ret = ssl_generate_random( ssl );
841 if( ret != 0 )
842 {
843 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
844 return( ret );
845 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100846 }
847
Ronald Cron3d580bf2022-02-18 17:24:56 +0100848 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200849 * Prepare session identifier. At that point, the length of the session
850 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
851 * to zero, except in the case of a TLS 1.2 session renegotiation or
852 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100853 */
Ronald Cron021b1782022-02-19 17:32:53 +0100854 session_id_len = ssl->session_negotiate->id_len;
855
856#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400857 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100858 {
Ronald Cron021b1782022-02-19 17:32:53 +0100859 if( session_id_len < 16 || session_id_len > 32 ||
860#if defined(MBEDTLS_SSL_RENEGOTIATION)
861 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
862#endif
863 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100864 {
Ronald Cron021b1782022-02-19 17:32:53 +0100865 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100866 }
Ronald Cron021b1782022-02-19 17:32:53 +0100867
868#if defined(MBEDTLS_SSL_SESSION_TICKETS)
869 /*
870 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
871 * generate and include a Session ID in the TLS ClientHello."
872 */
873#if defined(MBEDTLS_SSL_RENEGOTIATION)
874 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
875#endif
876 {
877 if( ( ssl->session_negotiate->ticket != NULL ) &&
878 ( ssl->session_negotiate->ticket_len != 0 ) )
879 {
880 session_id_len = 32;
881 }
882 }
883#endif /* MBEDTLS_SSL_SESSION_TICKETS */
884 }
885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
886
887#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Glenn Strauss60bfe602022-03-14 19:04:24 -0400888 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron021b1782022-02-19 17:32:53 +0100889 {
890 /*
891 * Create a legacy session identifier for the purpose of middlebox
892 * compatibility only if one has not been created already, which is
893 * the case if we are here for the TLS 1.3 second ClientHello.
894 *
895 * Versions of TLS before TLS 1.3 supported a "session resumption"
896 * feature which has been merged with pre-shared keys in TLS 1.3
897 * version. A client which has a cached session ID set by a pre-TLS 1.3
898 * server SHOULD set this field to that value. In compatibility mode,
899 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
900 * session MUST generate a new 32-byte value. This value need not be
901 * random but SHOULD be unpredictable to avoid implementations fixating
902 * on a specific value (also known as ossification). Otherwise, it MUST
903 * be set as a zero-length vector ( i.e., a zero-valued single byte
904 * length field ).
905 */
906 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100907 }
908#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
909
Ronald Cron021b1782022-02-19 17:32:53 +0100910 if( session_id_len != ssl->session_negotiate->id_len )
911 {
912 ssl->session_negotiate->id_len = session_id_len;
913 if( session_id_len > 0 )
914 {
915 ret = ssl->conf->f_rng( ssl->conf->p_rng,
916 ssl->session_negotiate->id,
917 session_id_len );
918 if( ret != 0 )
919 {
920 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
921 return( ret );
922 }
923 }
924 }
925
Ronald Cron3d580bf2022-02-18 17:24:56 +0100926 return( 0 );
927}
928
929/*
930 * Write ClientHello handshake message.
931 * Handler for MBEDTLS_SSL_CLIENT_HELLO
932 */
933int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
934{
935 int ret = 0;
936 unsigned char *buf;
937 size_t buf_len, msg_len;
938
939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
940
Ronald Cron71c23322022-02-18 17:29:39 +0100941 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100942
943 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
944 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
945 &buf, &buf_len ) );
946
Ronald Cron71c23322022-02-18 17:29:39 +0100947 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
948 buf + buf_len,
949 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100950
Ronald Cron5f4e9122022-02-21 09:50:36 +0100951#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
953 {
954 ssl->out_msglen = msg_len + 4;
955 mbedtls_ssl_send_flight_completed( ssl );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100956
Ronald Cron8ecd9932022-03-29 12:26:54 +0200957 /*
958 * The two functions below may try to send data on the network and
959 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
960 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200961 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200962 * set the handshake state to the next state at that point to ensure
963 * that we will not write and send again a ClientHello when we
964 * eventually succeed in sending the pending data.
965 */
966 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
967
Ronald Cron5f4e9122022-02-21 09:50:36 +0100968 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
969 {
970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
971 return( ret );
972 }
973
Ronald Cron8ecd9932022-03-29 12:26:54 +0200974 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Ronald Cron5f4e9122022-02-21 09:50:36 +0100975 {
976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
977 return( ret );
978 }
979 }
980 else
981#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
982 {
983 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
984 buf, msg_len );
985 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
986 buf_len,
987 msg_len ) );
Ronald Cron8ecd9932022-03-29 12:26:54 +0200988 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
Ronald Cron5f4e9122022-02-21 09:50:36 +0100989 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100990
Ronald Cron3d580bf2022-02-18 17:24:56 +0100991
992cleanup:
993
994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
995 return ret;
996}
997
998#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
999#endif /* MBEDTLS_SSL_CLI_C */