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 | |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "mbedtls/debug.h" |
| 30 | #include "mbedtls/error.h" |
| 31 | #include "mbedtls/platform.h" |
| 32 | |
| 33 | #include "ssl_client.h" |
| 34 | #include "ssl_misc.h" |
| 35 | #include "ecdh_misc.h" |
| 36 | #include "ssl_tls13_keys.h" |
| 37 | #include "ssl_debug_helpers.h" |
| 38 | |
| 39 | #if defined(MBEDTLS_SSL_ALPN) |
| 40 | /* |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 41 | * ssl_write_alpn_ext() |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 42 | * |
| 43 | * Structure of the application_layer_protocol_negotiation extension in |
| 44 | * ClientHello: |
| 45 | * |
| 46 | * opaque ProtocolName<1..2^8-1>; |
| 47 | * |
| 48 | * struct { |
| 49 | * ProtocolName protocol_name_list<2..2^16-1> |
| 50 | * } ProtocolNameList; |
| 51 | * |
| 52 | */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 53 | static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 54 | unsigned char *buf, |
| 55 | const unsigned char *end, |
| 56 | size_t *out_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 57 | { |
| 58 | unsigned char *p = buf; |
| 59 | |
| 60 | *out_len = 0; |
| 61 | |
| 62 | if( ssl->conf->alpn_list == NULL ) |
| 63 | return( 0 ); |
| 64 | |
| 65 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) ); |
| 66 | |
| 67 | |
| 68 | /* Check we have enough space for the extension type (2 bytes), the |
| 69 | * extension length (2 bytes) and the protocol_name_list length (2 bytes). |
| 70 | */ |
| 71 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 72 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 73 | /* Skip writing extension and list length for now */ |
| 74 | p += 6; |
| 75 | |
| 76 | /* |
| 77 | * opaque ProtocolName<1..2^8-1>; |
| 78 | * |
| 79 | * struct { |
| 80 | * ProtocolName protocol_name_list<2..2^16-1> |
| 81 | * } ProtocolNameList; |
| 82 | */ |
| 83 | for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 84 | { |
| 85 | /* |
| 86 | * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of |
| 87 | * protocol names is less than 255. |
| 88 | */ |
| 89 | size_t protocol_name_len = strlen( *cur ); |
| 90 | |
| 91 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len ); |
| 92 | *p++ = (unsigned char)protocol_name_len; |
| 93 | memcpy( p, *cur, protocol_name_len ); |
| 94 | p += protocol_name_len; |
| 95 | } |
| 96 | |
| 97 | *out_len = p - buf; |
| 98 | |
| 99 | /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ |
| 100 | MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 ); |
| 101 | |
| 102 | /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */ |
| 103 | MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 ); |
| 104 | |
| 105 | return( 0 ); |
| 106 | } |
| 107 | #endif /* MBEDTLS_SSL_ALPN */ |
| 108 | |
| 109 | /* Write cipher_suites |
| 110 | * CipherSuite cipher_suites<2..2^16-2>; |
| 111 | */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 112 | static int ssl_write_client_hello_cipher_suites( |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 113 | mbedtls_ssl_context *ssl, |
| 114 | unsigned char *buf, |
| 115 | unsigned char *end, |
| 116 | size_t *out_len ) |
| 117 | { |
| 118 | unsigned char *p = buf; |
| 119 | const int *ciphersuite_list; |
| 120 | unsigned char *cipher_suites; /* Start of the cipher_suites list */ |
| 121 | size_t cipher_suites_len; |
| 122 | |
| 123 | *out_len = 0 ; |
| 124 | |
| 125 | /* |
| 126 | * Ciphersuite list |
| 127 | * |
| 128 | * This is a list of the symmetric cipher options supported by |
| 129 | * the client, specifically the record protection algorithm |
| 130 | * ( including secret key length ) and a hash to be used with |
| 131 | * HKDF, in descending order of client preference. |
| 132 | */ |
| 133 | ciphersuite_list = ssl->conf->ciphersuite_list; |
| 134 | |
| 135 | /* Check there is space for the cipher suite list length (2 bytes). */ |
| 136 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 137 | p += 2; |
| 138 | |
| 139 | /* Write cipher_suites */ |
| 140 | cipher_suites = p; |
| 141 | for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) |
| 142 | { |
| 143 | int cipher_suite = ciphersuite_list[i]; |
| 144 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 145 | |
| 146 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); |
| 147 | if( ciphersuite_info == NULL ) |
| 148 | continue; |
| 149 | if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver && |
| 150 | MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) ) |
| 151 | continue; |
| 152 | |
| 153 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", |
| 154 | (unsigned int) cipher_suite, |
| 155 | ciphersuite_info->name ) ); |
| 156 | |
| 157 | /* Check there is space for the cipher suite identifier (2 bytes). */ |
| 158 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 159 | MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 ); |
| 160 | p += 2; |
| 161 | } |
| 162 | |
| 163 | /* Write the cipher_suites length in number of bytes */ |
| 164 | cipher_suites_len = p - cipher_suites; |
| 165 | MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); |
| 166 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 167 | ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites", |
| 168 | cipher_suites_len/2 ) ); |
| 169 | |
| 170 | /* Output the total length of cipher_suites field. */ |
| 171 | *out_len = p - buf; |
| 172 | |
| 173 | return( 0 ); |
| 174 | } |
| 175 | |
| 176 | /* |
Ronald Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame^] | 177 | * Structure of the TLS 1.3 ClientHello message: |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 178 | * |
| 179 | * struct { |
| 180 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 181 | * Random random; |
| 182 | * opaque legacy_session_id<0..32>; |
| 183 | * CipherSuite cipher_suites<2..2^16-2>; |
| 184 | * opaque legacy_compression_methods<1..2^8-1>; |
| 185 | * Extension extensions<8..2^16-1>; |
| 186 | * } ClientHello; |
Ronald Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame^] | 187 | * |
| 188 | * Structure of the (D)TLS 1.2 ClientHello message: |
| 189 | * |
| 190 | * struct { |
| 191 | * ProtocolVersion client_version; |
| 192 | * Random random; |
| 193 | * SessionID session_id; |
| 194 | * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY |
| 195 | * CipherSuite cipher_suites<2..2^16-2>; |
| 196 | * CompressionMethod compression_methods<1..2^8-1>; |
| 197 | * select (extensions_present) { |
| 198 | * case false: |
| 199 | * struct {}; |
| 200 | * case true: |
| 201 | * Extension extensions<0..2^16-1>; |
| 202 | * }; |
| 203 | * } ClientHello; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 204 | */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 205 | static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl, |
| 206 | unsigned char *buf, |
| 207 | unsigned char *end, |
| 208 | size_t *out_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 209 | { |
| 210 | |
| 211 | int ret; |
| 212 | unsigned char *p_extensions_len; /* Pointer to extensions length */ |
| 213 | size_t output_len; /* Length of buffer used by function */ |
| 214 | size_t extensions_len; /* Length of the list of extensions*/ |
| 215 | |
| 216 | /* Buffer management */ |
| 217 | unsigned char *p = buf; |
| 218 | |
| 219 | *out_len = 0; |
| 220 | |
| 221 | /* No validation needed here. It has been done by ssl_conf_check() */ |
| 222 | ssl->major_ver = ssl->conf->min_major_ver; |
| 223 | ssl->minor_ver = ssl->conf->min_minor_ver; |
| 224 | |
| 225 | /* |
| 226 | * Write legacy_version |
| 227 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 228 | * |
| 229 | * For TLS 1.3 we use the legacy version number {0x03, 0x03} |
| 230 | * instead of the true version number. |
| 231 | */ |
| 232 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 233 | MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 ); |
| 234 | p += 2; |
| 235 | |
| 236 | /* Write the random bytes ( random ).*/ |
| 237 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
| 238 | memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
| 239 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", |
| 240 | p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
| 241 | p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN; |
| 242 | |
| 243 | /* |
| 244 | * Write legacy_session_id |
| 245 | * |
| 246 | * Versions of TLS before TLS 1.3 supported a "session resumption" feature |
| 247 | * which has been merged with pre-shared keys in this version. A client |
| 248 | * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set |
| 249 | * this field to that value. In compatibility mode, this field MUST be |
| 250 | * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate |
| 251 | * a new 32-byte value. This value need not be random but SHOULD be |
| 252 | * unpredictable to avoid implementations fixating on a specific value |
| 253 | * ( also known as ossification ). Otherwise, it MUST be set as a zero-length |
| 254 | * vector ( i.e., a zero-valued single byte length field ). |
| 255 | */ |
| 256 | #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) |
| 257 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 ); |
| 258 | *p++ = (unsigned char)ssl->session_negotiate->id_len; |
| 259 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
| 260 | p += ssl->session_negotiate->id_len; |
| 261 | |
| 262 | MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id, |
| 263 | ssl->session_negotiate->id_len ); |
| 264 | #else |
| 265 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); |
| 266 | *p++ = 0; /* session id length set to zero */ |
| 267 | #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ |
| 268 | |
| 269 | /* Write cipher_suites */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 270 | ret = ssl_write_client_hello_cipher_suites( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 271 | if( ret != 0 ) |
| 272 | return( ret ); |
| 273 | p += output_len; |
| 274 | |
| 275 | /* Write legacy_compression_methods |
| 276 | * |
| 277 | * For every TLS 1.3 ClientHello, this vector MUST contain exactly |
| 278 | * one byte set to zero, which corresponds to the 'null' compression |
| 279 | * method in prior versions of TLS. |
| 280 | */ |
| 281 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 282 | *p++ = 1; |
| 283 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 284 | |
| 285 | /* Write extensions */ |
| 286 | |
| 287 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 288 | /* Keeping track of the included extensions */ |
| 289 | ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
| 290 | #endif |
| 291 | |
| 292 | /* First write extensions, then the total length */ |
| 293 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 294 | p_extensions_len = p; |
| 295 | p += 2; |
| 296 | |
| 297 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 298 | ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len ); |
| 299 | if( ret != 0 ) |
| 300 | return( ret ); |
| 301 | p += output_len; |
| 302 | #endif |
| 303 | |
| 304 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 305 | ret = ssl_write_alpn_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 306 | if( ret != 0 ) |
| 307 | return( ret ); |
| 308 | p += output_len; |
| 309 | #endif /* MBEDTLS_SSL_ALPN */ |
| 310 | |
| 311 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 312 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 313 | if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) |
| 314 | { |
| 315 | ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len ); |
| 316 | if( ret != 0 ) |
| 317 | return( ret ); |
| 318 | p += output_len; |
| 319 | } |
| 320 | |
| 321 | if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) |
| 322 | { |
| 323 | ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len ); |
| 324 | if( ret != 0 ) |
| 325 | return( ret ); |
| 326 | p += output_len; |
| 327 | } |
| 328 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 329 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 330 | |
| 331 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 332 | /* Write server name extension */ |
| 333 | ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len ); |
| 334 | if( ret != 0 ) |
| 335 | return( ret ); |
| 336 | p += output_len; |
| 337 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 338 | |
| 339 | /* Add more extensions here */ |
| 340 | |
| 341 | /* Write the length of the list of extensions. */ |
| 342 | extensions_len = p - p_extensions_len - 2; |
| 343 | MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 ); |
| 344 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , |
| 345 | extensions_len ) ); |
| 346 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len ); |
| 347 | |
| 348 | *out_len = p - buf; |
| 349 | return( 0 ); |
| 350 | } |
| 351 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 352 | static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 353 | { |
| 354 | int ret; |
| 355 | |
| 356 | if( ssl->conf->f_rng == NULL ) |
| 357 | { |
| 358 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 359 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 360 | } |
| 361 | |
| 362 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 363 | ssl->handshake->randbytes, |
| 364 | MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 ) |
| 365 | { |
| 366 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); |
| 367 | return( ret ); |
| 368 | } |
| 369 | |
| 370 | #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) |
| 371 | /* |
| 372 | * Create a session identifier for the purpose of middlebox compatibility |
| 373 | * only if one has not been created already. |
| 374 | */ |
| 375 | if( ssl->session_negotiate->id_len == 0 ) |
| 376 | { |
| 377 | /* Creating a session id with 32 byte length */ |
| 378 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 379 | ssl->session_negotiate->id, 32 ) ) != 0 ) |
| 380 | { |
| 381 | MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret ); |
| 382 | return( ret ); |
| 383 | } |
| 384 | ssl->session_negotiate->id_len = 32; |
| 385 | } |
| 386 | #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ |
| 387 | |
| 388 | return( 0 ); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Write ClientHello handshake message. |
| 393 | * Handler for MBEDTLS_SSL_CLIENT_HELLO |
| 394 | */ |
| 395 | int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
| 396 | { |
| 397 | int ret = 0; |
| 398 | unsigned char *buf; |
| 399 | size_t buf_len, msg_len; |
| 400 | |
| 401 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 402 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 403 | MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 404 | |
| 405 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( |
| 406 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 407 | &buf, &buf_len ) ); |
| 408 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 409 | MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf, |
| 410 | buf + buf_len, |
| 411 | &msg_len ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 412 | |
| 413 | mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 414 | buf, msg_len ); |
| 415 | |
| 416 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, |
| 417 | buf_len, |
| 418 | msg_len ) ); |
| 419 | |
| 420 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 421 | |
| 422 | cleanup: |
| 423 | |
| 424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 429 | #endif /* MBEDTLS_SSL_CLI_C */ |