blob: f6385d741b46080e28a83f525e87372ba05f23fd [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 Cron71c23322022-02-18 17:29:39 +0100122static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100123 mbedtls_ssl_context *ssl,
124 unsigned char *buf,
125 unsigned char *end,
126 size_t *out_len )
127{
128 unsigned char *p = buf;
129 const int *ciphersuite_list;
130 unsigned char *cipher_suites; /* Start of the cipher_suites list */
131 size_t cipher_suites_len;
132
133 *out_len = 0 ;
134
135 /*
136 * Ciphersuite list
137 *
138 * This is a list of the symmetric cipher options supported by
139 * the client, specifically the record protection algorithm
140 * ( including secret key length ) and a hash to be used with
141 * HKDF, in descending order of client preference.
142 */
143 ciphersuite_list = ssl->conf->ciphersuite_list;
144
145 /* Check there is space for the cipher suite list length (2 bytes). */
146 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
147 p += 2;
148
149 /* Write cipher_suites */
150 cipher_suites = p;
151 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
152 {
153 int cipher_suite = ciphersuite_list[i];
154 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
155
156 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
157 if( ciphersuite_info == NULL )
158 continue;
159 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
160 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
161 continue;
162
163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
164 (unsigned int) cipher_suite,
165 ciphersuite_info->name ) );
166
167 /* Check there is space for the cipher suite identifier (2 bytes). */
168 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
169 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
170 p += 2;
171 }
172
173 /* Write the cipher_suites length in number of bytes */
174 cipher_suites_len = p - cipher_suites;
175 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
176 MBEDTLS_SSL_DEBUG_MSG( 3,
177 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
178 cipher_suites_len/2 ) );
179
180 /* Output the total length of cipher_suites field. */
181 *out_len = p - buf;
182
183 return( 0 );
184}
185
186/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100187 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100188 *
189 * struct {
190 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
191 * Random random;
192 * opaque legacy_session_id<0..32>;
193 * CipherSuite cipher_suites<2..2^16-2>;
194 * opaque legacy_compression_methods<1..2^8-1>;
195 * Extension extensions<8..2^16-1>;
196 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100197 *
198 * Structure of the (D)TLS 1.2 ClientHello message:
199 *
200 * struct {
201 * ProtocolVersion client_version;
202 * Random random;
203 * SessionID session_id;
204 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
205 * CipherSuite cipher_suites<2..2^16-2>;
206 * CompressionMethod compression_methods<1..2^8-1>;
207 * select (extensions_present) {
208 * case false:
209 * struct {};
210 * case true:
211 * Extension extensions<0..2^16-1>;
212 * };
213 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100214 */
Ronald Cron71c23322022-02-18 17:29:39 +0100215static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
216 unsigned char *buf,
217 unsigned char *end,
218 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100219{
220
221 int ret;
222 unsigned char *p_extensions_len; /* Pointer to extensions length */
223 size_t output_len; /* Length of buffer used by function */
224 size_t extensions_len; /* Length of the list of extensions*/
225
226 /* Buffer management */
227 unsigned char *p = buf;
228
229 *out_len = 0;
230
Ronald Cron3d580bf2022-02-18 17:24:56 +0100231 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100232 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100233 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100234 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100235 */
236 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Ronald Cron1614eb62022-02-18 17:53:01 +0100237 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
238 MBEDTLS_SSL_MINOR_VERSION_3,
239 ssl->conf->transport, p );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100240 p += 2;
241
Ronald Cron58b80382022-02-18 18:41:08 +0100242 /* ...
243 * Random random;
244 * ...
245 *
246 * with for TLS 1.2
247 * struct {
248 * uint32 gmt_unix_time;
249 * opaque random_bytes[28];
250 * } Random;
251 *
252 * and for TLS 1.3
253 * opaque Random[32];
254 *
255 * The random bytes have been prepared by ssl_prepare_client_hello() into
256 * the ssl->handshake->randbytes buffer and are copied here into the
257 * output buffer.
258 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100259 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
260 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
261 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
262 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
263 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
264
Ronald Cron021b1782022-02-19 17:32:53 +0100265 /* TLS 1.2:
266 * ...
267 * SessionID session_id;
268 * ...
269 * with
270 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100271 *
Ronald Cron021b1782022-02-19 17:32:53 +0100272 * TLS 1.3:
273 * ...
274 * opaque legacy_session_id<0..32>;
275 * ...
276 *
277 * The (legacy) session identifier bytes have been by
278 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
279 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100280 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100281 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
282 *p++ = (unsigned char)ssl->session_negotiate->id_len;
283 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
284 p += ssl->session_negotiate->id_len;
285
286 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
287 ssl->session_negotiate->id_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100288
Ronald Crona874aa82022-02-19 18:11:26 +0100289 /* DTLS 1.2 ONLY
290 * ...
291 * opaque cookie<0..2^8-1>;
292 * ...
293 */
294#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
295 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
296 {
297 unsigned char cookie_len = 0;
298
299 if( ssl->handshake->cookie != NULL )
300 {
301 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
302 ssl->handshake->cookie,
303 ssl->handshake->verify_cookie_len );
304 cookie_len = ssl->handshake->verify_cookie_len;
305 }
306
307 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 );
308 *p++ = cookie_len;
309 if( cookie_len > 0 )
310 {
311 memcpy( p, ssl->handshake->cookie, cookie_len );
312 p += cookie_len;
313 }
314 }
315#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
316
Ronald Cron3d580bf2022-02-18 17:24:56 +0100317 /* Write cipher_suites */
Ronald Cron71c23322022-02-18 17:29:39 +0100318 ret = ssl_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100319 if( ret != 0 )
320 return( ret );
321 p += output_len;
322
323 /* Write legacy_compression_methods
324 *
325 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
326 * one byte set to zero, which corresponds to the 'null' compression
327 * method in prior versions of TLS.
328 */
329 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
330 *p++ = 1;
331 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
332
333 /* Write extensions */
334
335#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
336 /* Keeping track of the included extensions */
337 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
338#endif
339
340 /* First write extensions, then the total length */
341 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
342 p_extensions_len = p;
343 p += 2;
344
345#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
346 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
347 if( ret != 0 )
348 return( ret );
349 p += output_len;
350#endif
351
352#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100353 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100354 if( ret != 0 )
355 return( ret );
356 p += output_len;
357#endif /* MBEDTLS_SSL_ALPN */
358
359#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
360#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
361 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
362 {
363 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
364 if( ret != 0 )
365 return( ret );
366 p += output_len;
367 }
368
369 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
370 {
371 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
372 if( ret != 0 )
373 return( ret );
374 p += output_len;
375 }
376#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
377#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
378
379#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
380 /* Write server name extension */
381 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
382 if( ret != 0 )
383 return( ret );
384 p += output_len;
385#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
386
387 /* Add more extensions here */
388
389 /* Write the length of the list of extensions. */
390 extensions_len = p - p_extensions_len - 2;
391 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
393 extensions_len ) );
394 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
395
396 *out_len = p - buf;
397 return( 0 );
398}
399
Ronald Cron58b80382022-02-18 18:41:08 +0100400static int ssl_generate_random( mbedtls_ssl_context *ssl )
401{
402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
403 unsigned char *randbytes = ssl->handshake->randbytes;
404 size_t gmt_unix_time_len = 0;
405
406 /*
407 * Generate the random bytes
408 *
409 * TLS 1.2 case:
410 * struct {
411 * uint32 gmt_unix_time;
412 * opaque random_bytes[28];
413 * } Random;
414 *
415 * TLS 1.3 case:
416 * opaque Random[32];
417 */
418 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
419 {
420#if defined(MBEDTLS_HAVE_TIME)
421 mbedtls_time_t gmt_unix_time = mbedtls_time( NULL );
422 MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 );
423 gmt_unix_time_len = 4;
424
425 MBEDTLS_SSL_DEBUG_MSG( 3,
426 ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
427 (long long) gmt_unix_time ) );
428#endif /* MBEDTLS_HAVE_TIME */
429 }
430
431 ret = ssl->conf->f_rng( ssl->conf->p_rng,
432 randbytes + gmt_unix_time_len,
433 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len );
434 return( ret );
435}
436
Ronald Cron71c23322022-02-18 17:29:39 +0100437static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100438{
439 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100440 size_t session_id_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100441
442 if( ssl->conf->f_rng == NULL )
443 {
444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
445 return( MBEDTLS_ERR_SSL_NO_RNG );
446 }
447
Ronald Cron86a477f2022-02-18 17:45:10 +0100448 /* Bet on the highest configured version if we are not in a TLS 1.2
449 * renegotiation or session resumption.
450 */
451#if defined(MBEDTLS_SSL_RENEGOTIATION)
452 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
453 ssl->handshake->min_minor_ver = ssl->minor_ver;
454 else
455#endif
456 {
457 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
458
459 if( ssl->handshake->resume )
460 {
461 ssl->minor_ver = ssl->session_negotiate->minor_ver;
462 ssl->handshake->min_minor_ver = ssl->minor_ver;
463 }
464 else
465 {
466 ssl->minor_ver = ssl->conf->max_minor_ver;
467 ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver;
468 }
469 }
470
Ronald Cron58b80382022-02-18 18:41:08 +0100471 /*
472 * But when responding to a verify request where we MUST reuse the
473 * previoulsy generated random bytes (RFC 6347 4.2.1), generate the
474 * random bytes.
475 */
476#if defined(MBEDTLS_SSL_PROTO_DTLS)
477 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
478 ( ssl->handshake->cookie == NULL ) )
479#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100480 {
Ronald Cron58b80382022-02-18 18:41:08 +0100481 ret = ssl_generate_random( ssl );
482 if( ret != 0 )
483 {
484 MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret );
485 return( ret );
486 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100487 }
488
Ronald Cron3d580bf2022-02-18 17:24:56 +0100489 /*
Ronald Cron021b1782022-02-19 17:32:53 +0100490 * Prepare session identifier. But in the case of a TLS 1.2 session
491 * renegotiation or session resumption, the initial value of the session
492 * identifier length below is equal to zero.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100493 */
Ronald Cron021b1782022-02-19 17:32:53 +0100494 session_id_len = ssl->session_negotiate->id_len;
495
496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
497 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100498 {
Ronald Cron021b1782022-02-19 17:32:53 +0100499 if( session_id_len < 16 || session_id_len > 32 ||
500#if defined(MBEDTLS_SSL_RENEGOTIATION)
501 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
502#endif
503 ssl->handshake->resume == 0 )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100504 {
Ronald Cron021b1782022-02-19 17:32:53 +0100505 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100506 }
Ronald Cron021b1782022-02-19 17:32:53 +0100507
508#if defined(MBEDTLS_SSL_SESSION_TICKETS)
509 /*
510 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
511 * generate and include a Session ID in the TLS ClientHello."
512 */
513#if defined(MBEDTLS_SSL_RENEGOTIATION)
514 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
515#endif
516 {
517 if( ( ssl->session_negotiate->ticket != NULL ) &&
518 ( ssl->session_negotiate->ticket_len != 0 ) )
519 {
520 session_id_len = 32;
521 }
522 }
523#endif /* MBEDTLS_SSL_SESSION_TICKETS */
524 }
525#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
526
527#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
528 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
529 {
530 /*
531 * Create a legacy session identifier for the purpose of middlebox
532 * compatibility only if one has not been created already, which is
533 * the case if we are here for the TLS 1.3 second ClientHello.
534 *
535 * Versions of TLS before TLS 1.3 supported a "session resumption"
536 * feature which has been merged with pre-shared keys in TLS 1.3
537 * version. A client which has a cached session ID set by a pre-TLS 1.3
538 * server SHOULD set this field to that value. In compatibility mode,
539 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
540 * session MUST generate a new 32-byte value. This value need not be
541 * random but SHOULD be unpredictable to avoid implementations fixating
542 * on a specific value (also known as ossification). Otherwise, it MUST
543 * be set as a zero-length vector ( i.e., a zero-valued single byte
544 * length field ).
545 */
546 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100547 }
548#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
549
Ronald Cron021b1782022-02-19 17:32:53 +0100550 if( session_id_len != ssl->session_negotiate->id_len )
551 {
552 ssl->session_negotiate->id_len = session_id_len;
553 if( session_id_len > 0 )
554 {
555 ret = ssl->conf->f_rng( ssl->conf->p_rng,
556 ssl->session_negotiate->id,
557 session_id_len );
558 if( ret != 0 )
559 {
560 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
561 return( ret );
562 }
563 }
564 }
565
Ronald Cron3d580bf2022-02-18 17:24:56 +0100566 return( 0 );
567}
568
569/*
570 * Write ClientHello handshake message.
571 * Handler for MBEDTLS_SSL_CLIENT_HELLO
572 */
573int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
574{
575 int ret = 0;
576 unsigned char *buf;
577 size_t buf_len, msg_len;
578
579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
580
Ronald Cron71c23322022-02-18 17:29:39 +0100581 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100582
583 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
584 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
585 &buf, &buf_len ) );
586
Ronald Cron71c23322022-02-18 17:29:39 +0100587 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
588 buf + buf_len,
589 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100590
591 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
592 buf, msg_len );
593
594 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
595 buf_len,
596 msg_len ) );
597
598 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
599
600cleanup:
601
602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
603 return ret;
604}
605
606#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
607#endif /* MBEDTLS_SSL_CLI_C */