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" |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 45 | #include "ssl_tls13_keys.h" |
| 46 | #include "ssl_debug_helpers.h" |
| 47 | |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 48 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 49 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 50 | static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl, |
| 51 | unsigned char *buf, |
| 52 | const unsigned char *end, |
| 53 | size_t *olen ) |
| 54 | { |
| 55 | unsigned char *p = buf; |
| 56 | size_t hostname_len; |
| 57 | |
| 58 | *olen = 0; |
| 59 | |
| 60 | if( ssl->hostname == NULL ) |
| 61 | return( 0 ); |
| 62 | |
| 63 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 64 | ( "client hello, adding server name extension: %s", |
| 65 | ssl->hostname ) ); |
| 66 | |
| 67 | hostname_len = strlen( ssl->hostname ); |
| 68 | |
| 69 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); |
| 70 | |
| 71 | /* |
| 72 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) |
| 73 | * |
| 74 | * In order to provide any of the server names, clients MAY include an |
| 75 | * extension of type "server_name" in the (extended) client hello. The |
| 76 | * "extension_data" field of this extension SHALL contain |
| 77 | * "ServerNameList" where: |
| 78 | * |
| 79 | * struct { |
| 80 | * NameType name_type; |
| 81 | * select (name_type) { |
| 82 | * case host_name: HostName; |
| 83 | * } name; |
| 84 | * } ServerName; |
| 85 | * |
| 86 | * enum { |
| 87 | * host_name(0), (255) |
| 88 | * } NameType; |
| 89 | * |
| 90 | * opaque HostName<1..2^16-1>; |
| 91 | * |
| 92 | * struct { |
| 93 | * ServerName server_name_list<1..2^16-1> |
| 94 | * } ServerNameList; |
| 95 | * |
| 96 | */ |
| 97 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); |
| 98 | p += 2; |
| 99 | |
| 100 | MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); |
| 101 | p += 2; |
| 102 | |
| 103 | MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); |
| 104 | p += 2; |
| 105 | |
| 106 | *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); |
| 107 | |
| 108 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 109 | p += 2; |
| 110 | |
| 111 | memcpy( p, ssl->hostname, hostname_len ); |
| 112 | |
| 113 | *olen = hostname_len + 9; |
| 114 | |
| 115 | return( 0 ); |
| 116 | } |
| 117 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 118 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 119 | #if defined(MBEDTLS_SSL_ALPN) |
| 120 | /* |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 121 | * ssl_write_alpn_ext() |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 122 | * |
| 123 | * Structure of the application_layer_protocol_negotiation extension in |
| 124 | * ClientHello: |
| 125 | * |
| 126 | * opaque ProtocolName<1..2^8-1>; |
| 127 | * |
| 128 | * struct { |
| 129 | * ProtocolName protocol_name_list<2..2^16-1> |
| 130 | * } ProtocolNameList; |
| 131 | * |
| 132 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 133 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 134 | static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 135 | unsigned char *buf, |
| 136 | const unsigned char *end, |
| 137 | size_t *out_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 138 | { |
| 139 | unsigned char *p = buf; |
| 140 | |
| 141 | *out_len = 0; |
| 142 | |
| 143 | if( ssl->conf->alpn_list == NULL ) |
| 144 | return( 0 ); |
| 145 | |
| 146 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) ); |
| 147 | |
| 148 | |
| 149 | /* Check we have enough space for the extension type (2 bytes), the |
| 150 | * extension length (2 bytes) and the protocol_name_list length (2 bytes). |
| 151 | */ |
| 152 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 153 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 154 | /* Skip writing extension and list length for now */ |
| 155 | p += 6; |
| 156 | |
| 157 | /* |
| 158 | * opaque ProtocolName<1..2^8-1>; |
| 159 | * |
| 160 | * struct { |
| 161 | * ProtocolName protocol_name_list<2..2^16-1> |
| 162 | * } ProtocolNameList; |
| 163 | */ |
| 164 | for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 165 | { |
| 166 | /* |
| 167 | * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of |
| 168 | * protocol names is less than 255. |
| 169 | */ |
| 170 | size_t protocol_name_len = strlen( *cur ); |
| 171 | |
| 172 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len ); |
| 173 | *p++ = (unsigned char)protocol_name_len; |
| 174 | memcpy( p, *cur, protocol_name_len ); |
| 175 | p += protocol_name_len; |
| 176 | } |
| 177 | |
| 178 | *out_len = p - buf; |
| 179 | |
| 180 | /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ |
| 181 | MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 ); |
| 182 | |
| 183 | /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */ |
| 184 | MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 ); |
| 185 | |
| 186 | return( 0 ); |
| 187 | } |
| 188 | #endif /* MBEDTLS_SSL_ALPN */ |
| 189 | |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 190 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 191 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 192 | /* |
| 193 | * Function for writing a supported groups (TLS 1.3) or supported elliptic |
| 194 | * curves (TLS 1.2) extension. |
| 195 | * |
| 196 | * The "extension_data" field of a supported groups extension contains a |
| 197 | * "NamedGroupList" value (TLS 1.3 RFC8446): |
| 198 | * enum { |
| 199 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 200 | * x25519(0x001D), x448(0x001E), |
| 201 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 202 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 203 | * ffdhe_private_use(0x01FC..0x01FF), |
| 204 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 205 | * (0xFFFF) |
| 206 | * } NamedGroup; |
| 207 | * struct { |
| 208 | * NamedGroup named_group_list<2..2^16-1>; |
| 209 | * } NamedGroupList; |
| 210 | * |
| 211 | * The "extension_data" field of a supported elliptic curves extension contains |
| 212 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): |
| 213 | * enum { |
| 214 | * deprecated(1..22), |
| 215 | * secp256r1 (23), secp384r1 (24), secp521r1 (25), |
| 216 | * x25519(29), x448(30), |
| 217 | * reserved (0xFE00..0xFEFF), |
| 218 | * deprecated(0xFF01..0xFF02), |
| 219 | * (0xFFFF) |
| 220 | * } NamedCurve; |
| 221 | * struct { |
| 222 | * NamedCurve named_curve_list<2..2^16-1> |
| 223 | * } NamedCurveList; |
| 224 | * |
| 225 | * The TLS 1.3 supported groups extension was defined to be a compatible |
| 226 | * generalization of the TLS 1.2 supported elliptic curves extension. They both |
| 227 | * share the same extension identifier. |
| 228 | * |
| 229 | * DHE groups are not supported yet. |
| 230 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 231 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 232 | static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 233 | unsigned char *buf, |
| 234 | const unsigned char *end, |
| 235 | size_t *out_len ) |
| 236 | { |
| 237 | unsigned char *p = buf ; |
| 238 | unsigned char *named_group_list; /* Start of named_group_list */ |
| 239 | size_t named_group_list_len; /* Length of named_group_list */ |
| 240 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
| 241 | |
| 242 | *out_len = 0; |
| 243 | |
| 244 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 245 | |
| 246 | /* Check if we have space for header and length fields: |
| 247 | * - extension_type (2 bytes) |
| 248 | * - extension_data_length (2 bytes) |
| 249 | * - named_group_list_length (2 bytes) |
| 250 | */ |
| 251 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 252 | p += 6; |
| 253 | |
| 254 | named_group_list = p; |
| 255 | |
| 256 | if( group_list == NULL ) |
| 257 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 258 | |
| 259 | for( ; *group_list != 0; group_list++ ) |
| 260 | { |
| 261 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); |
| 262 | |
| 263 | #if defined(MBEDTLS_ECP_C) |
| 264 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && |
| 265 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || |
| 266 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && |
| 267 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) |
| 268 | { |
| 269 | const mbedtls_ecp_curve_info *curve_info; |
| 270 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); |
| 271 | if( curve_info == NULL ) |
| 272 | continue; |
| 273 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 274 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); |
| 275 | p += 2; |
| 276 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 277 | curve_info->name, *group_list ) ); |
| 278 | } |
| 279 | #endif /* MBEDTLS_ECP_C */ |
| 280 | /* Add DHE groups here */ |
| 281 | |
| 282 | } |
| 283 | |
| 284 | /* Length of named_group_list */ |
| 285 | named_group_list_len = p - named_group_list; |
| 286 | if( named_group_list_len == 0 ) |
| 287 | { |
| 288 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); |
| 289 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 290 | } |
| 291 | |
| 292 | /* Write extension_type */ |
| 293 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 294 | /* Write extension_data_length */ |
| 295 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); |
| 296 | /* Write length of named_group_list */ |
| 297 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); |
| 298 | |
| 299 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", |
| 300 | buf + 4, named_group_list_len + 2 ); |
| 301 | |
| 302 | *out_len = p - buf; |
| 303 | |
| 304 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 305 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 306 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 307 | |
| 308 | return( 0 ); |
| 309 | } |
| 310 | |
| 311 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 312 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 313 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 314 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 315 | static int ssl_write_client_hello_cipher_suites( |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 316 | mbedtls_ssl_context *ssl, |
| 317 | unsigned char *buf, |
| 318 | unsigned char *end, |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 319 | int *tls12_uses_ec, |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 320 | size_t *out_len ) |
| 321 | { |
| 322 | unsigned char *p = buf; |
| 323 | const int *ciphersuite_list; |
| 324 | unsigned char *cipher_suites; /* Start of the cipher_suites list */ |
| 325 | size_t cipher_suites_len; |
| 326 | |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 327 | *tls12_uses_ec = 0; |
| 328 | *out_len = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Ciphersuite list |
| 332 | * |
| 333 | * This is a list of the symmetric cipher options supported by |
| 334 | * the client, specifically the record protection algorithm |
| 335 | * ( including secret key length ) and a hash to be used with |
| 336 | * HKDF, in descending order of client preference. |
| 337 | */ |
| 338 | ciphersuite_list = ssl->conf->ciphersuite_list; |
| 339 | |
| 340 | /* Check there is space for the cipher suite list length (2 bytes). */ |
| 341 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 342 | p += 2; |
| 343 | |
Ronald Cron | 6476726 | 2022-03-31 14:13:57 +0200 | [diff] [blame] | 344 | /* Write cipher_suites |
| 345 | * CipherSuite cipher_suites<2..2^16-2>; |
| 346 | */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 347 | cipher_suites = p; |
| 348 | for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) |
| 349 | { |
| 350 | int cipher_suite = ciphersuite_list[i]; |
| 351 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 352 | |
| 353 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 354 | |
Ronald Cron | 757a2ab | 2022-03-30 13:57:40 +0200 | [diff] [blame] | 355 | if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info, |
Glenn Strauss | cd78df6 | 2022-04-07 19:07:11 -0400 | [diff] [blame] | 356 | ssl->handshake->min_tls_version, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 357 | ssl->tls_version ) != 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 358 | continue; |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 359 | |
| 360 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 361 | ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 362 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) ) |
| 363 | *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info ); |
| 364 | #endif |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 365 | |
| 366 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", |
| 367 | (unsigned int) cipher_suite, |
| 368 | ciphersuite_info->name ) ); |
| 369 | |
| 370 | /* Check there is space for the cipher suite identifier (2 bytes). */ |
| 371 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 372 | MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 ); |
| 373 | p += 2; |
| 374 | } |
| 375 | |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 376 | /* |
| 377 | * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 378 | */ |
David Horstmann | 4a28563 | 2022-10-06 18:30:10 +0100 | [diff] [blame^] | 379 | int renegotiating = 0; |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 380 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
David Horstmann | 4a28563 | 2022-10-06 18:30:10 +0100 | [diff] [blame^] | 381 | renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 382 | #endif |
David Horstmann | 4a28563 | 2022-10-06 18:30:10 +0100 | [diff] [blame^] | 383 | if( !renegotiating ) |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 384 | { |
| 385 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); |
| 386 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 387 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 ); |
| 388 | p += 2; |
| 389 | } |
| 390 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 391 | /* Write the cipher_suites length in number of bytes */ |
| 392 | cipher_suites_len = p - cipher_suites; |
| 393 | MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); |
| 394 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 395 | ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites", |
| 396 | cipher_suites_len/2 ) ); |
| 397 | |
| 398 | /* Output the total length of cipher_suites field. */ |
| 399 | *out_len = p - buf; |
| 400 | |
| 401 | return( 0 ); |
| 402 | } |
| 403 | |
| 404 | /* |
Ronald Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame] | 405 | * Structure of the TLS 1.3 ClientHello message: |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 406 | * |
| 407 | * struct { |
| 408 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 409 | * Random random; |
| 410 | * opaque legacy_session_id<0..32>; |
| 411 | * CipherSuite cipher_suites<2..2^16-2>; |
| 412 | * opaque legacy_compression_methods<1..2^8-1>; |
| 413 | * Extension extensions<8..2^16-1>; |
| 414 | * } ClientHello; |
Ronald Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame] | 415 | * |
| 416 | * Structure of the (D)TLS 1.2 ClientHello message: |
| 417 | * |
| 418 | * struct { |
| 419 | * ProtocolVersion client_version; |
| 420 | * Random random; |
| 421 | * SessionID session_id; |
| 422 | * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY |
| 423 | * CipherSuite cipher_suites<2..2^16-2>; |
| 424 | * CompressionMethod compression_methods<1..2^8-1>; |
| 425 | * select (extensions_present) { |
| 426 | * case false: |
| 427 | * struct {}; |
| 428 | * case true: |
| 429 | * Extension extensions<0..2^16-1>; |
| 430 | * }; |
| 431 | * } ClientHello; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 432 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 433 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 434 | static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl, |
| 435 | unsigned char *buf, |
| 436 | unsigned char *end, |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 437 | size_t *out_len, |
| 438 | size_t *binders_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 439 | { |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 440 | int ret; |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 441 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 442 | unsigned char *p = buf; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 443 | unsigned char *p_extensions_len; /* Pointer to extensions length */ |
| 444 | size_t output_len; /* Length of buffer used by function */ |
| 445 | size_t extensions_len; /* Length of the list of extensions*/ |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 446 | int tls12_uses_ec = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 447 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 448 | *out_len = 0; |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 449 | *binders_len = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 450 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 451 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 150d579 | 2022-03-30 20:24:51 +0200 | [diff] [blame] | 452 | unsigned char propose_tls12 = |
Glenn Strauss | cd78df6 | 2022-04-07 19:07:11 -0400 | [diff] [blame] | 453 | ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 ) |
Ronald Cron | 150d579 | 2022-03-30 20:24:51 +0200 | [diff] [blame] | 454 | && |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 455 | ( MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version ); |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 456 | #endif |
| 457 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 150d579 | 2022-03-30 20:24:51 +0200 | [diff] [blame] | 458 | unsigned char propose_tls13 = |
Glenn Strauss | cd78df6 | 2022-04-07 19:07:11 -0400 | [diff] [blame] | 459 | ( handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 150d579 | 2022-03-30 20:24:51 +0200 | [diff] [blame] | 460 | && |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 461 | ( MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version ); |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 462 | #endif |
| 463 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 464 | /* |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 465 | * Write client_version (TLS 1.2) or legacy_version (TLS 1.3) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 466 | * |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 467 | * In all cases this is the TLS 1.2 version. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 468 | */ |
| 469 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 470 | mbedtls_ssl_write_version( p, ssl->conf->transport, |
| 471 | MBEDTLS_SSL_VERSION_TLS1_2 ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 472 | p += 2; |
| 473 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 474 | /* ... |
| 475 | * Random random; |
| 476 | * ... |
| 477 | * |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 478 | * The random bytes have been prepared by ssl_prepare_client_hello() into |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 479 | * the handshake->randbytes buffer and are copied here into the output |
| 480 | * buffer. |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 481 | */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 482 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 483 | memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 484 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", |
| 485 | p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
| 486 | p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN; |
| 487 | |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 488 | /* TLS 1.2: |
| 489 | * ... |
| 490 | * SessionID session_id; |
| 491 | * ... |
| 492 | * with |
| 493 | * opaque SessionID<0..32>; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 494 | * |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 495 | * TLS 1.3: |
| 496 | * ... |
| 497 | * opaque legacy_session_id<0..32>; |
| 498 | * ... |
| 499 | * |
Ronald Cron | da41b38 | 2022-03-30 09:57:11 +0200 | [diff] [blame] | 500 | * The (legacy) session identifier bytes have been prepared by |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 501 | * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer |
| 502 | * and are copied here into the output buffer. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 503 | */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 504 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 ); |
| 505 | *p++ = (unsigned char)ssl->session_negotiate->id_len; |
| 506 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
| 507 | p += ssl->session_negotiate->id_len; |
| 508 | |
| 509 | MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id, |
| 510 | ssl->session_negotiate->id_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 511 | |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 512 | /* DTLS 1.2 ONLY |
| 513 | * ... |
| 514 | * opaque cookie<0..2^8-1>; |
| 515 | * ... |
| 516 | */ |
| 517 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS) |
| 518 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 519 | { |
| 520 | unsigned char cookie_len = 0; |
| 521 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 522 | if( handshake->cookie != NULL ) |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 523 | { |
| 524 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 525 | handshake->cookie, |
| 526 | handshake->verify_cookie_len ); |
| 527 | cookie_len = handshake->verify_cookie_len; |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 ); |
| 531 | *p++ = cookie_len; |
| 532 | if( cookie_len > 0 ) |
| 533 | { |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 534 | memcpy( p, handshake->cookie, cookie_len ); |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 535 | p += cookie_len; |
| 536 | } |
| 537 | } |
| 538 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */ |
| 539 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 540 | /* Write cipher_suites */ |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 541 | ret = ssl_write_client_hello_cipher_suites( ssl, p, end, |
| 542 | &tls12_uses_ec, |
| 543 | &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 544 | if( ret != 0 ) |
| 545 | return( ret ); |
| 546 | p += output_len; |
| 547 | |
Ronald Cron | 42c1cbf | 2022-02-20 10:24:39 +0100 | [diff] [blame] | 548 | /* Write legacy_compression_methods (TLS 1.3) or |
| 549 | * compression_methods (TLS 1.2) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 550 | * |
| 551 | * For every TLS 1.3 ClientHello, this vector MUST contain exactly |
| 552 | * one byte set to zero, which corresponds to the 'null' compression |
| 553 | * method in prior versions of TLS. |
Ronald Cron | 42c1cbf | 2022-02-20 10:24:39 +0100 | [diff] [blame] | 554 | * |
| 555 | * For TLS 1.2 ClientHello, for security reasons we do not support |
| 556 | * compression anymore, thus also just the 'null' compression method. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 557 | */ |
| 558 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 559 | *p++ = 1; |
| 560 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 561 | |
| 562 | /* Write extensions */ |
| 563 | |
| 564 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 565 | /* Keeping track of the included extensions */ |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 566 | handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 567 | #endif |
| 568 | |
| 569 | /* First write extensions, then the total length */ |
| 570 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 571 | p_extensions_len = p; |
| 572 | p += 2; |
| 573 | |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 574 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 575 | /* Write server name extension */ |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 576 | ret = ssl_write_hostname_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 577 | if( ret != 0 ) |
| 578 | return( ret ); |
| 579 | p += output_len; |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 580 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 581 | |
| 582 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 583 | ret = ssl_write_alpn_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 584 | if( ret != 0 ) |
| 585 | return( ret ); |
| 586 | p += output_len; |
| 587 | #endif /* MBEDTLS_SSL_ALPN */ |
| 588 | |
| 589 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 590 | if( propose_tls13 ) |
| 591 | { |
| 592 | ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, |
| 593 | &output_len ); |
| 594 | if( ret != 0 ) |
| 595 | return( ret ); |
| 596 | p += output_len; |
| 597 | } |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 598 | #endif |
| 599 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 600 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 601 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 602 | if( |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 603 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 604 | ( propose_tls13 && |
| 605 | mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) || |
| 606 | #endif |
| 607 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 608 | ( propose_tls12 && tls12_uses_ec ) || |
| 609 | #endif |
| 610 | 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 611 | { |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 612 | ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 613 | if( ret != 0 ) |
| 614 | return( ret ); |
| 615 | p += output_len; |
| 616 | } |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 617 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 618 | |
Ronald Cron | 11e1857 | 2022-03-17 13:44:33 +0100 | [diff] [blame] | 619 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 620 | if( |
| 621 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 622 | ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) || |
| 623 | #endif |
| 624 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 625 | propose_tls12 || |
| 626 | #endif |
| 627 | 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 628 | { |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 629 | ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 630 | if( ret != 0 ) |
| 631 | return( ret ); |
| 632 | p += output_len; |
| 633 | } |
| 634 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 635 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 636 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 637 | if( propose_tls12 ) |
| 638 | { |
| 639 | ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end, |
| 640 | tls12_uses_ec, |
| 641 | &output_len ); |
| 642 | if( ret != 0 ) |
| 643 | return( ret ); |
| 644 | p += output_len; |
| 645 | } |
| 646 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 647 | |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 648 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 649 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
XiaokangQian | 8698195 | 2022-07-19 09:51:50 +0000 | [diff] [blame] | 650 | /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11) |
| 651 | * MUST be the last extension in the ClientHello. |
| 652 | */ |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 653 | if( propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled( ssl ) ) |
| 654 | { |
XiaokangQian | 3ad67bf | 2022-07-21 02:26:21 +0000 | [diff] [blame] | 655 | ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext( |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 656 | ssl, p, end, &output_len, binders_len ); |
| 657 | if( ret != 0 ) |
| 658 | return( ret ); |
| 659 | p += output_len; |
| 660 | } |
XiaokangQian | 008d2bf | 2022-07-14 07:54:01 +0000 | [diff] [blame] | 661 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 662 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 663 | /* Write the length of the list of extensions. */ |
| 664 | extensions_len = p - p_extensions_len - 2; |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 665 | |
| 666 | if( extensions_len == 0 ) |
| 667 | p = p_extensions_len; |
| 668 | else |
| 669 | { |
| 670 | MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 ); |
| 671 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \ |
| 672 | MBEDTLS_PRINTF_SIZET, extensions_len ) ); |
| 673 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", |
| 674 | p_extensions_len, extensions_len ); |
| 675 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 676 | |
| 677 | *out_len = p - buf; |
| 678 | return( 0 ); |
| 679 | } |
| 680 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 681 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 682 | static int ssl_generate_random( mbedtls_ssl_context *ssl ) |
| 683 | { |
| 684 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 685 | unsigned char *randbytes = ssl->handshake->randbytes; |
| 686 | size_t gmt_unix_time_len = 0; |
| 687 | |
| 688 | /* |
| 689 | * Generate the random bytes |
| 690 | * |
| 691 | * TLS 1.2 case: |
| 692 | * struct { |
| 693 | * uint32 gmt_unix_time; |
| 694 | * opaque random_bytes[28]; |
| 695 | * } Random; |
| 696 | * |
| 697 | * TLS 1.3 case: |
| 698 | * opaque Random[32]; |
| 699 | */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 700 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 701 | { |
| 702 | #if defined(MBEDTLS_HAVE_TIME) |
| 703 | mbedtls_time_t gmt_unix_time = mbedtls_time( NULL ); |
| 704 | MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 ); |
| 705 | gmt_unix_time_len = 4; |
| 706 | |
| 707 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 708 | ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, |
| 709 | (long long) gmt_unix_time ) ); |
| 710 | #endif /* MBEDTLS_HAVE_TIME */ |
| 711 | } |
| 712 | |
| 713 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 714 | randbytes + gmt_unix_time_len, |
| 715 | MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len ); |
| 716 | return( ret ); |
| 717 | } |
| 718 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 719 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 720 | static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 721 | { |
| 722 | int ret; |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 723 | size_t session_id_len; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 724 | |
| 725 | if( ssl->conf->f_rng == NULL ) |
| 726 | { |
| 727 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 728 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 729 | } |
| 730 | |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 731 | /* Bet on the highest configured version if we are not in a TLS 1.2 |
| 732 | * renegotiation or session resumption. |
| 733 | */ |
| 734 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 735 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Glenn Strauss | cd78df6 | 2022-04-07 19:07:11 -0400 | [diff] [blame] | 736 | ssl->handshake->min_tls_version = ssl->tls_version; |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 737 | else |
| 738 | #endif |
| 739 | { |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 740 | if( ssl->handshake->resume ) |
| 741 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 742 | ssl->tls_version = ssl->session_negotiate->tls_version; |
Glenn Strauss | cd78df6 | 2022-04-07 19:07:11 -0400 | [diff] [blame] | 743 | ssl->handshake->min_tls_version = ssl->tls_version; |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 744 | } |
| 745 | else |
| 746 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 747 | ssl->tls_version = ssl->conf->max_tls_version; |
Glenn Strauss | cd78df6 | 2022-04-07 19:07:11 -0400 | [diff] [blame] | 748 | ssl->handshake->min_tls_version = ssl->conf->min_tls_version; |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 752 | /* |
Ronald Cron | da41b38 | 2022-03-30 09:57:11 +0200 | [diff] [blame] | 753 | * Generate the random bytes, except when responding to a verify request |
| 754 | * where we MUST reuse the previoulsy generated random bytes |
| 755 | * (RFC 6347 4.2.1). |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 756 | */ |
| 757 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 758 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 759 | ( ssl->handshake->cookie == NULL ) ) |
| 760 | #endif |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 761 | { |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 762 | ret = ssl_generate_random( ssl ); |
| 763 | if( ret != 0 ) |
| 764 | { |
| 765 | MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret ); |
| 766 | return( ret ); |
| 767 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 770 | /* |
Ronald Cron | da41b38 | 2022-03-30 09:57:11 +0200 | [diff] [blame] | 771 | * Prepare session identifier. At that point, the length of the session |
| 772 | * identifier in the SSL context `ssl->session_negotiate->id_len` is equal |
| 773 | * to zero, except in the case of a TLS 1.2 session renegotiation or |
| 774 | * session resumption. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 775 | */ |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 776 | session_id_len = ssl->session_negotiate->id_len; |
| 777 | |
| 778 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 779 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 780 | { |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 781 | if( session_id_len < 16 || session_id_len > 32 || |
| 782 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 783 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
| 784 | #endif |
| 785 | ssl->handshake->resume == 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 786 | { |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 787 | session_id_len = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 788 | } |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 789 | |
| 790 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 791 | /* |
| 792 | * RFC 5077 section 3.4: "When presenting a ticket, the client MAY |
| 793 | * generate and include a Session ID in the TLS ClientHello." |
| 794 | */ |
David Horstmann | 4a28563 | 2022-10-06 18:30:10 +0100 | [diff] [blame^] | 795 | int renegotiating = 0; |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 796 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
David Horstmann | 4a28563 | 2022-10-06 18:30:10 +0100 | [diff] [blame^] | 797 | renegotiating = ( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 798 | #endif |
David Horstmann | 4a28563 | 2022-10-06 18:30:10 +0100 | [diff] [blame^] | 799 | if( !renegotiating ) |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 800 | { |
| 801 | if( ( ssl->session_negotiate->ticket != NULL ) && |
| 802 | ( ssl->session_negotiate->ticket_len != 0 ) ) |
| 803 | { |
| 804 | session_id_len = 32; |
| 805 | } |
| 806 | } |
| 807 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 808 | } |
| 809 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 810 | |
| 811 | #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 812 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 813 | { |
| 814 | /* |
| 815 | * Create a legacy session identifier for the purpose of middlebox |
| 816 | * compatibility only if one has not been created already, which is |
| 817 | * the case if we are here for the TLS 1.3 second ClientHello. |
| 818 | * |
| 819 | * Versions of TLS before TLS 1.3 supported a "session resumption" |
| 820 | * feature which has been merged with pre-shared keys in TLS 1.3 |
| 821 | * version. A client which has a cached session ID set by a pre-TLS 1.3 |
| 822 | * server SHOULD set this field to that value. In compatibility mode, |
| 823 | * this field MUST be non-empty, so a client not offering a pre-TLS 1.3 |
| 824 | * session MUST generate a new 32-byte value. This value need not be |
| 825 | * random but SHOULD be unpredictable to avoid implementations fixating |
| 826 | * on a specific value (also known as ossification). Otherwise, it MUST |
| 827 | * be set as a zero-length vector ( i.e., a zero-valued single byte |
| 828 | * length field ). |
| 829 | */ |
| 830 | session_id_len = 32; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 831 | } |
| 832 | #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ |
| 833 | |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 834 | if( session_id_len != ssl->session_negotiate->id_len ) |
| 835 | { |
| 836 | ssl->session_negotiate->id_len = session_id_len; |
| 837 | if( session_id_len > 0 ) |
| 838 | { |
| 839 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 840 | ssl->session_negotiate->id, |
| 841 | session_id_len ); |
| 842 | if( ret != 0 ) |
| 843 | { |
| 844 | MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret ); |
| 845 | return( ret ); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 850 | return( 0 ); |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Write ClientHello handshake message. |
| 855 | * Handler for MBEDTLS_SSL_CLIENT_HELLO |
| 856 | */ |
| 857 | int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
| 858 | { |
| 859 | int ret = 0; |
| 860 | unsigned char *buf; |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 861 | size_t buf_len, msg_len, binders_len; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 862 | |
| 863 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 864 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 865 | MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 866 | |
| 867 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( |
| 868 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 869 | &buf, &buf_len ) ); |
| 870 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 871 | MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf, |
| 872 | buf + buf_len, |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 873 | &msg_len, |
| 874 | &binders_len ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 875 | |
Ronald Cron | 5f4e912 | 2022-02-21 09:50:36 +0100 | [diff] [blame] | 876 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS) |
| 877 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 878 | { |
| 879 | ssl->out_msglen = msg_len + 4; |
| 880 | mbedtls_ssl_send_flight_completed( ssl ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 881 | |
Ronald Cron | 8ecd993 | 2022-03-29 12:26:54 +0200 | [diff] [blame] | 882 | /* |
| 883 | * The two functions below may try to send data on the network and |
| 884 | * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they |
| 885 | * fail to do so and the transmission has to be retried later. In that |
Ronald Cron | da41b38 | 2022-03-30 09:57:11 +0200 | [diff] [blame] | 886 | * case as in fatal error cases, we return immediately. But we must have |
Ronald Cron | 8ecd993 | 2022-03-29 12:26:54 +0200 | [diff] [blame] | 887 | * set the handshake state to the next state at that point to ensure |
| 888 | * that we will not write and send again a ClientHello when we |
| 889 | * eventually succeed in sending the pending data. |
| 890 | */ |
| 891 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 892 | |
Ronald Cron | 5f4e912 | 2022-02-21 09:50:36 +0100 | [diff] [blame] | 893 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 894 | { |
| 895 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 896 | return( ret ); |
| 897 | } |
| 898 | |
Ronald Cron | 8ecd993 | 2022-03-29 12:26:54 +0200 | [diff] [blame] | 899 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Ronald Cron | 5f4e912 | 2022-02-21 09:50:36 +0100 | [diff] [blame] | 900 | { |
| 901 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 902 | return( ret ); |
| 903 | } |
| 904 | } |
| 905 | else |
| 906 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */ |
| 907 | { |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 908 | |
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 909 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 910 | msg_len ); |
| 911 | ssl->handshake->update_checksum( ssl, buf, msg_len - binders_len ); |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 912 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 913 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 914 | if( binders_len > 0 ) |
| 915 | { |
| 916 | MBEDTLS_SSL_PROC_CHK( |
XiaokangQian | 8698195 | 2022-07-19 09:51:50 +0000 | [diff] [blame] | 917 | mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext( |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 918 | ssl, buf + msg_len - binders_len, buf + msg_len ) ); |
XiaokangQian | 8698195 | 2022-07-19 09:51:50 +0000 | [diff] [blame] | 919 | ssl->handshake->update_checksum( ssl, buf + msg_len - binders_len, |
| 920 | binders_len ); |
XiaokangQian | eb69aee | 2022-07-05 08:21:43 +0000 | [diff] [blame] | 921 | } |
| 922 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 923 | |
Ronald Cron | 5f4e912 | 2022-02-21 09:50:36 +0100 | [diff] [blame] | 924 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, |
| 925 | buf_len, |
| 926 | msg_len ) ); |
Ronald Cron | 8ecd993 | 2022-03-29 12:26:54 +0200 | [diff] [blame] | 927 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
Ronald Cron | 5f4e912 | 2022-02-21 09:50:36 +0100 | [diff] [blame] | 928 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 929 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 930 | |
| 931 | cleanup: |
| 932 | |
| 933 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 934 | return ret; |
| 935 | } |
| 936 | |
| 937 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 938 | #endif /* MBEDTLS_SSL_CLI_C */ |