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 | |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 265 | /* TLS 1.2: |
| 266 | * ... |
| 267 | * SessionID session_id; |
| 268 | * ... |
| 269 | * with |
| 270 | * opaque SessionID<0..32>; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 271 | * |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 272 | * 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 Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 280 | */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 281 | 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 Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 288 | |
| 289 | /* Write cipher_suites */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 290 | ret = ssl_write_client_hello_cipher_suites( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 291 | if( ret != 0 ) |
| 292 | return( ret ); |
| 293 | p += output_len; |
| 294 | |
| 295 | /* Write legacy_compression_methods |
| 296 | * |
| 297 | * For every TLS 1.3 ClientHello, this vector MUST contain exactly |
| 298 | * one byte set to zero, which corresponds to the 'null' compression |
| 299 | * method in prior versions of TLS. |
| 300 | */ |
| 301 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 302 | *p++ = 1; |
| 303 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 304 | |
| 305 | /* Write extensions */ |
| 306 | |
| 307 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 308 | /* Keeping track of the included extensions */ |
| 309 | ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
| 310 | #endif |
| 311 | |
| 312 | /* First write extensions, then the total length */ |
| 313 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 314 | p_extensions_len = p; |
| 315 | p += 2; |
| 316 | |
| 317 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 318 | ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len ); |
| 319 | if( ret != 0 ) |
| 320 | return( ret ); |
| 321 | p += output_len; |
| 322 | #endif |
| 323 | |
| 324 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 325 | ret = ssl_write_alpn_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 326 | if( ret != 0 ) |
| 327 | return( ret ); |
| 328 | p += output_len; |
| 329 | #endif /* MBEDTLS_SSL_ALPN */ |
| 330 | |
| 331 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 332 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 333 | if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) |
| 334 | { |
| 335 | ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len ); |
| 336 | if( ret != 0 ) |
| 337 | return( ret ); |
| 338 | p += output_len; |
| 339 | } |
| 340 | |
| 341 | if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) |
| 342 | { |
| 343 | ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len ); |
| 344 | if( ret != 0 ) |
| 345 | return( ret ); |
| 346 | p += output_len; |
| 347 | } |
| 348 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 349 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 350 | |
| 351 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 352 | /* Write server name extension */ |
| 353 | ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len ); |
| 354 | if( ret != 0 ) |
| 355 | return( ret ); |
| 356 | p += output_len; |
| 357 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 358 | |
| 359 | /* Add more extensions here */ |
| 360 | |
| 361 | /* Write the length of the list of extensions. */ |
| 362 | extensions_len = p - p_extensions_len - 2; |
| 363 | MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 ); |
| 364 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , |
| 365 | extensions_len ) ); |
| 366 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len ); |
| 367 | |
| 368 | *out_len = p - buf; |
| 369 | return( 0 ); |
| 370 | } |
| 371 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 372 | static int ssl_generate_random( mbedtls_ssl_context *ssl ) |
| 373 | { |
| 374 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 375 | unsigned char *randbytes = ssl->handshake->randbytes; |
| 376 | size_t gmt_unix_time_len = 0; |
| 377 | |
| 378 | /* |
| 379 | * Generate the random bytes |
| 380 | * |
| 381 | * TLS 1.2 case: |
| 382 | * struct { |
| 383 | * uint32 gmt_unix_time; |
| 384 | * opaque random_bytes[28]; |
| 385 | * } Random; |
| 386 | * |
| 387 | * TLS 1.3 case: |
| 388 | * opaque Random[32]; |
| 389 | */ |
| 390 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 391 | { |
| 392 | #if defined(MBEDTLS_HAVE_TIME) |
| 393 | mbedtls_time_t gmt_unix_time = mbedtls_time( NULL ); |
| 394 | MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 ); |
| 395 | gmt_unix_time_len = 4; |
| 396 | |
| 397 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 398 | ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, |
| 399 | (long long) gmt_unix_time ) ); |
| 400 | #endif /* MBEDTLS_HAVE_TIME */ |
| 401 | } |
| 402 | |
| 403 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 404 | randbytes + gmt_unix_time_len, |
| 405 | MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len ); |
| 406 | return( ret ); |
| 407 | } |
| 408 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 409 | static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 410 | { |
| 411 | int ret; |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 412 | size_t session_id_len; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 413 | |
| 414 | if( ssl->conf->f_rng == NULL ) |
| 415 | { |
| 416 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 417 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 418 | } |
| 419 | |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 420 | /* Bet on the highest configured version if we are not in a TLS 1.2 |
| 421 | * renegotiation or session resumption. |
| 422 | */ |
| 423 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 424 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 425 | ssl->handshake->min_minor_ver = ssl->minor_ver; |
| 426 | else |
| 427 | #endif |
| 428 | { |
| 429 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 430 | |
| 431 | if( ssl->handshake->resume ) |
| 432 | { |
| 433 | ssl->minor_ver = ssl->session_negotiate->minor_ver; |
| 434 | ssl->handshake->min_minor_ver = ssl->minor_ver; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | ssl->minor_ver = ssl->conf->max_minor_ver; |
| 439 | ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver; |
| 440 | } |
| 441 | } |
| 442 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 443 | /* |
| 444 | * But when responding to a verify request where we MUST reuse the |
| 445 | * previoulsy generated random bytes (RFC 6347 4.2.1), generate the |
| 446 | * random bytes. |
| 447 | */ |
| 448 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 449 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 450 | ( ssl->handshake->cookie == NULL ) ) |
| 451 | #endif |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 452 | { |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 453 | ret = ssl_generate_random( ssl ); |
| 454 | if( ret != 0 ) |
| 455 | { |
| 456 | MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret ); |
| 457 | return( ret ); |
| 458 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 461 | /* |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 462 | * Prepare session identifier. But in the case of a TLS 1.2 session |
| 463 | * renegotiation or session resumption, the initial value of the session |
| 464 | * identifier length below is equal to zero. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 465 | */ |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 466 | session_id_len = ssl->session_negotiate->id_len; |
| 467 | |
| 468 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 469 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 470 | { |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 471 | if( session_id_len < 16 || session_id_len > 32 || |
| 472 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 473 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
| 474 | #endif |
| 475 | ssl->handshake->resume == 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 476 | { |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 477 | session_id_len = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 478 | } |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 479 | |
| 480 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 481 | /* |
| 482 | * RFC 5077 section 3.4: "When presenting a ticket, the client MAY |
| 483 | * generate and include a Session ID in the TLS ClientHello." |
| 484 | */ |
| 485 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 486 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 487 | #endif |
| 488 | { |
| 489 | if( ( ssl->session_negotiate->ticket != NULL ) && |
| 490 | ( ssl->session_negotiate->ticket_len != 0 ) ) |
| 491 | { |
| 492 | session_id_len = 32; |
| 493 | } |
| 494 | } |
| 495 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 496 | } |
| 497 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 498 | |
| 499 | #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) |
| 500 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 501 | { |
| 502 | /* |
| 503 | * Create a legacy session identifier for the purpose of middlebox |
| 504 | * compatibility only if one has not been created already, which is |
| 505 | * the case if we are here for the TLS 1.3 second ClientHello. |
| 506 | * |
| 507 | * Versions of TLS before TLS 1.3 supported a "session resumption" |
| 508 | * feature which has been merged with pre-shared keys in TLS 1.3 |
| 509 | * version. A client which has a cached session ID set by a pre-TLS 1.3 |
| 510 | * server SHOULD set this field to that value. In compatibility mode, |
| 511 | * this field MUST be non-empty, so a client not offering a pre-TLS 1.3 |
| 512 | * session MUST generate a new 32-byte value. This value need not be |
| 513 | * random but SHOULD be unpredictable to avoid implementations fixating |
| 514 | * on a specific value (also known as ossification). Otherwise, it MUST |
| 515 | * be set as a zero-length vector ( i.e., a zero-valued single byte |
| 516 | * length field ). |
| 517 | */ |
| 518 | session_id_len = 32; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 519 | } |
| 520 | #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ |
| 521 | |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame^] | 522 | if( session_id_len != ssl->session_negotiate->id_len ) |
| 523 | { |
| 524 | ssl->session_negotiate->id_len = session_id_len; |
| 525 | if( session_id_len > 0 ) |
| 526 | { |
| 527 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 528 | ssl->session_negotiate->id, |
| 529 | session_id_len ); |
| 530 | if( ret != 0 ) |
| 531 | { |
| 532 | MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret ); |
| 533 | return( ret ); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 538 | return( 0 ); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Write ClientHello handshake message. |
| 543 | * Handler for MBEDTLS_SSL_CLIENT_HELLO |
| 544 | */ |
| 545 | int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
| 546 | { |
| 547 | int ret = 0; |
| 548 | unsigned char *buf; |
| 549 | size_t buf_len, msg_len; |
| 550 | |
| 551 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 552 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 553 | MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 554 | |
| 555 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( |
| 556 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 557 | &buf, &buf_len ) ); |
| 558 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 559 | MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf, |
| 560 | buf + buf_len, |
| 561 | &msg_len ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 562 | |
| 563 | mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 564 | buf, msg_len ); |
| 565 | |
| 566 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, |
| 567 | buf_len, |
| 568 | msg_len ) ); |
| 569 | |
| 570 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 571 | |
| 572 | cleanup: |
| 573 | |
| 574 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 579 | #endif /* MBEDTLS_SSL_CLI_C */ |