Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 1 | /* |
| 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 Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame^] | 27 | #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 Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
| 37 | #include "mbedtls/debug.h" |
| 38 | #include "mbedtls/error.h" |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame^] | 39 | #if defined(MBEDTLS_HAVE_TIME) |
| 40 | #include "mbedtls/platform_time.h" |
| 41 | #endif |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 42 | |
| 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 Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 51 | * ssl_write_alpn_ext() |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 52 | * |
| 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 Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 63 | static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 64 | unsigned char *buf, |
| 65 | const unsigned char *end, |
| 66 | size_t *out_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 67 | { |
| 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 Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 122 | static int ssl_write_client_hello_cipher_suites( |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 123 | 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 Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame] | 187 | * Structure of the TLS 1.3 ClientHello message: |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 188 | * |
| 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 Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame] | 197 | * |
| 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 Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 214 | */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 215 | static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl, |
| 216 | unsigned char *buf, |
| 217 | unsigned char *end, |
| 218 | size_t *out_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 219 | { |
| 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 Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 231 | /* |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 232 | * Write client_version (TLS 1.2) or legacy_version (TLS 1.3) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 233 | * |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 234 | * In all cases this is the TLS 1.2 version. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 235 | */ |
| 236 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 237 | mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3, |
| 238 | MBEDTLS_SSL_MINOR_VERSION_3, |
| 239 | ssl->conf->transport, p ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 240 | p += 2; |
| 241 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame^] | 242 | /* ... |
| 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 Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 259 | 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 | |
| 265 | /* |
| 266 | * Write legacy_session_id |
| 267 | * |
| 268 | * Versions of TLS before TLS 1.3 supported a "session resumption" feature |
| 269 | * which has been merged with pre-shared keys in this version. A client |
| 270 | * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set |
| 271 | * this field to that value. In compatibility mode, this field MUST be |
| 272 | * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate |
| 273 | * a new 32-byte value. This value need not be random but SHOULD be |
| 274 | * unpredictable to avoid implementations fixating on a specific value |
| 275 | * ( also known as ossification ). Otherwise, it MUST be set as a zero-length |
| 276 | * vector ( i.e., a zero-valued single byte length field ). |
| 277 | */ |
| 278 | #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) |
| 279 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 ); |
| 280 | *p++ = (unsigned char)ssl->session_negotiate->id_len; |
| 281 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
| 282 | p += ssl->session_negotiate->id_len; |
| 283 | |
| 284 | MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id, |
| 285 | ssl->session_negotiate->id_len ); |
| 286 | #else |
| 287 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); |
| 288 | *p++ = 0; /* session id length set to zero */ |
| 289 | #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ |
| 290 | |
| 291 | /* Write cipher_suites */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 292 | ret = ssl_write_client_hello_cipher_suites( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 293 | if( ret != 0 ) |
| 294 | return( ret ); |
| 295 | p += output_len; |
| 296 | |
| 297 | /* Write legacy_compression_methods |
| 298 | * |
| 299 | * For every TLS 1.3 ClientHello, this vector MUST contain exactly |
| 300 | * one byte set to zero, which corresponds to the 'null' compression |
| 301 | * method in prior versions of TLS. |
| 302 | */ |
| 303 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 304 | *p++ = 1; |
| 305 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 306 | |
| 307 | /* Write extensions */ |
| 308 | |
| 309 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 310 | /* Keeping track of the included extensions */ |
| 311 | ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
| 312 | #endif |
| 313 | |
| 314 | /* First write extensions, then the total length */ |
| 315 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 316 | p_extensions_len = p; |
| 317 | p += 2; |
| 318 | |
| 319 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 320 | ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len ); |
| 321 | if( ret != 0 ) |
| 322 | return( ret ); |
| 323 | p += output_len; |
| 324 | #endif |
| 325 | |
| 326 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 327 | ret = ssl_write_alpn_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 328 | if( ret != 0 ) |
| 329 | return( ret ); |
| 330 | p += output_len; |
| 331 | #endif /* MBEDTLS_SSL_ALPN */ |
| 332 | |
| 333 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 334 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 335 | if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) |
| 336 | { |
| 337 | ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len ); |
| 338 | if( ret != 0 ) |
| 339 | return( ret ); |
| 340 | p += output_len; |
| 341 | } |
| 342 | |
| 343 | if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) |
| 344 | { |
| 345 | ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len ); |
| 346 | if( ret != 0 ) |
| 347 | return( ret ); |
| 348 | p += output_len; |
| 349 | } |
| 350 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 351 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 352 | |
| 353 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 354 | /* Write server name extension */ |
| 355 | ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len ); |
| 356 | if( ret != 0 ) |
| 357 | return( ret ); |
| 358 | p += output_len; |
| 359 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 360 | |
| 361 | /* Add more extensions here */ |
| 362 | |
| 363 | /* Write the length of the list of extensions. */ |
| 364 | extensions_len = p - p_extensions_len - 2; |
| 365 | MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 ); |
| 366 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , |
| 367 | extensions_len ) ); |
| 368 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len ); |
| 369 | |
| 370 | *out_len = p - buf; |
| 371 | return( 0 ); |
| 372 | } |
| 373 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame^] | 374 | static int ssl_generate_random( mbedtls_ssl_context *ssl ) |
| 375 | { |
| 376 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 377 | unsigned char *randbytes = ssl->handshake->randbytes; |
| 378 | size_t gmt_unix_time_len = 0; |
| 379 | |
| 380 | /* |
| 381 | * Generate the random bytes |
| 382 | * |
| 383 | * TLS 1.2 case: |
| 384 | * struct { |
| 385 | * uint32 gmt_unix_time; |
| 386 | * opaque random_bytes[28]; |
| 387 | * } Random; |
| 388 | * |
| 389 | * TLS 1.3 case: |
| 390 | * opaque Random[32]; |
| 391 | */ |
| 392 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 393 | { |
| 394 | #if defined(MBEDTLS_HAVE_TIME) |
| 395 | mbedtls_time_t gmt_unix_time = mbedtls_time( NULL ); |
| 396 | MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 ); |
| 397 | gmt_unix_time_len = 4; |
| 398 | |
| 399 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 400 | ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, |
| 401 | (long long) gmt_unix_time ) ); |
| 402 | #endif /* MBEDTLS_HAVE_TIME */ |
| 403 | } |
| 404 | |
| 405 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 406 | randbytes + gmt_unix_time_len, |
| 407 | MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len ); |
| 408 | return( ret ); |
| 409 | } |
| 410 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 411 | static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 412 | { |
| 413 | int ret; |
| 414 | |
| 415 | if( ssl->conf->f_rng == NULL ) |
| 416 | { |
| 417 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 418 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 419 | } |
| 420 | |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 421 | /* Bet on the highest configured version if we are not in a TLS 1.2 |
| 422 | * renegotiation or session resumption. |
| 423 | */ |
| 424 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 425 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 426 | ssl->handshake->min_minor_ver = ssl->minor_ver; |
| 427 | else |
| 428 | #endif |
| 429 | { |
| 430 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 431 | |
| 432 | if( ssl->handshake->resume ) |
| 433 | { |
| 434 | ssl->minor_ver = ssl->session_negotiate->minor_ver; |
| 435 | ssl->handshake->min_minor_ver = ssl->minor_ver; |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | ssl->minor_ver = ssl->conf->max_minor_ver; |
| 440 | ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver; |
| 441 | } |
| 442 | } |
| 443 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame^] | 444 | /* |
| 445 | * But when responding to a verify request where we MUST reuse the |
| 446 | * previoulsy generated random bytes (RFC 6347 4.2.1), generate the |
| 447 | * random bytes. |
| 448 | */ |
| 449 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 450 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 451 | ( ssl->handshake->cookie == NULL ) ) |
| 452 | #endif |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 453 | { |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame^] | 454 | ret = ssl_generate_random( ssl ); |
| 455 | if( ret != 0 ) |
| 456 | { |
| 457 | MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret ); |
| 458 | return( ret ); |
| 459 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) |
| 463 | /* |
| 464 | * Create a session identifier for the purpose of middlebox compatibility |
| 465 | * only if one has not been created already. |
| 466 | */ |
| 467 | if( ssl->session_negotiate->id_len == 0 ) |
| 468 | { |
| 469 | /* Creating a session id with 32 byte length */ |
| 470 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 471 | ssl->session_negotiate->id, 32 ) ) != 0 ) |
| 472 | { |
| 473 | MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret ); |
| 474 | return( ret ); |
| 475 | } |
| 476 | ssl->session_negotiate->id_len = 32; |
| 477 | } |
| 478 | #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ |
| 479 | |
| 480 | return( 0 ); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Write ClientHello handshake message. |
| 485 | * Handler for MBEDTLS_SSL_CLIENT_HELLO |
| 486 | */ |
| 487 | int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
| 488 | { |
| 489 | int ret = 0; |
| 490 | unsigned char *buf; |
| 491 | size_t buf_len, msg_len; |
| 492 | |
| 493 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 494 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 495 | MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 496 | |
| 497 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( |
| 498 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 499 | &buf, &buf_len ) ); |
| 500 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 501 | MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf, |
| 502 | buf + buf_len, |
| 503 | &msg_len ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 504 | |
| 505 | mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 506 | buf, msg_len ); |
| 507 | |
| 508 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, |
| 509 | buf_len, |
| 510 | msg_len ) ); |
| 511 | |
| 512 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 513 | |
| 514 | cleanup: |
| 515 | |
| 516 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 521 | #endif /* MBEDTLS_SSL_CLI_C */ |