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 | |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 49 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 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 | */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 133 | static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 134 | unsigned char *buf, |
| 135 | const unsigned char *end, |
| 136 | size_t *out_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 137 | { |
| 138 | unsigned char *p = buf; |
| 139 | |
| 140 | *out_len = 0; |
| 141 | |
| 142 | if( ssl->conf->alpn_list == NULL ) |
| 143 | return( 0 ); |
| 144 | |
| 145 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) ); |
| 146 | |
| 147 | |
| 148 | /* Check we have enough space for the extension type (2 bytes), the |
| 149 | * extension length (2 bytes) and the protocol_name_list length (2 bytes). |
| 150 | */ |
| 151 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 152 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 153 | /* Skip writing extension and list length for now */ |
| 154 | p += 6; |
| 155 | |
| 156 | /* |
| 157 | * opaque ProtocolName<1..2^8-1>; |
| 158 | * |
| 159 | * struct { |
| 160 | * ProtocolName protocol_name_list<2..2^16-1> |
| 161 | * } ProtocolNameList; |
| 162 | */ |
| 163 | for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 164 | { |
| 165 | /* |
| 166 | * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of |
| 167 | * protocol names is less than 255. |
| 168 | */ |
| 169 | size_t protocol_name_len = strlen( *cur ); |
| 170 | |
| 171 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len ); |
| 172 | *p++ = (unsigned char)protocol_name_len; |
| 173 | memcpy( p, *cur, protocol_name_len ); |
| 174 | p += protocol_name_len; |
| 175 | } |
| 176 | |
| 177 | *out_len = p - buf; |
| 178 | |
| 179 | /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ |
| 180 | MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 ); |
| 181 | |
| 182 | /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */ |
| 183 | MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 ); |
| 184 | |
| 185 | return( 0 ); |
| 186 | } |
| 187 | #endif /* MBEDTLS_SSL_ALPN */ |
| 188 | |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 189 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 190 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 191 | /* |
| 192 | * Function for writing a supported groups (TLS 1.3) or supported elliptic |
| 193 | * curves (TLS 1.2) extension. |
| 194 | * |
| 195 | * The "extension_data" field of a supported groups extension contains a |
| 196 | * "NamedGroupList" value (TLS 1.3 RFC8446): |
| 197 | * enum { |
| 198 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 199 | * x25519(0x001D), x448(0x001E), |
| 200 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 201 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 202 | * ffdhe_private_use(0x01FC..0x01FF), |
| 203 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 204 | * (0xFFFF) |
| 205 | * } NamedGroup; |
| 206 | * struct { |
| 207 | * NamedGroup named_group_list<2..2^16-1>; |
| 208 | * } NamedGroupList; |
| 209 | * |
| 210 | * The "extension_data" field of a supported elliptic curves extension contains |
| 211 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): |
| 212 | * enum { |
| 213 | * deprecated(1..22), |
| 214 | * secp256r1 (23), secp384r1 (24), secp521r1 (25), |
| 215 | * x25519(29), x448(30), |
| 216 | * reserved (0xFE00..0xFEFF), |
| 217 | * deprecated(0xFF01..0xFF02), |
| 218 | * (0xFFFF) |
| 219 | * } NamedCurve; |
| 220 | * struct { |
| 221 | * NamedCurve named_curve_list<2..2^16-1> |
| 222 | * } NamedCurveList; |
| 223 | * |
| 224 | * The TLS 1.3 supported groups extension was defined to be a compatible |
| 225 | * generalization of the TLS 1.2 supported elliptic curves extension. They both |
| 226 | * share the same extension identifier. |
| 227 | * |
| 228 | * DHE groups are not supported yet. |
| 229 | */ |
| 230 | static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 231 | unsigned char *buf, |
| 232 | const unsigned char *end, |
| 233 | size_t *out_len ) |
| 234 | { |
| 235 | unsigned char *p = buf ; |
| 236 | unsigned char *named_group_list; /* Start of named_group_list */ |
| 237 | size_t named_group_list_len; /* Length of named_group_list */ |
| 238 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
| 239 | |
| 240 | *out_len = 0; |
| 241 | |
| 242 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 243 | |
| 244 | /* Check if we have space for header and length fields: |
| 245 | * - extension_type (2 bytes) |
| 246 | * - extension_data_length (2 bytes) |
| 247 | * - named_group_list_length (2 bytes) |
| 248 | */ |
| 249 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 250 | p += 6; |
| 251 | |
| 252 | named_group_list = p; |
| 253 | |
| 254 | if( group_list == NULL ) |
| 255 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 256 | |
| 257 | for( ; *group_list != 0; group_list++ ) |
| 258 | { |
| 259 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); |
| 260 | |
| 261 | #if defined(MBEDTLS_ECP_C) |
| 262 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && |
| 263 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || |
| 264 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && |
| 265 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) |
| 266 | { |
| 267 | const mbedtls_ecp_curve_info *curve_info; |
| 268 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); |
| 269 | if( curve_info == NULL ) |
| 270 | continue; |
| 271 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 272 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); |
| 273 | p += 2; |
| 274 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 275 | curve_info->name, *group_list ) ); |
| 276 | } |
| 277 | #endif /* MBEDTLS_ECP_C */ |
| 278 | /* Add DHE groups here */ |
| 279 | |
| 280 | } |
| 281 | |
| 282 | /* Length of named_group_list */ |
| 283 | named_group_list_len = p - named_group_list; |
| 284 | if( named_group_list_len == 0 ) |
| 285 | { |
| 286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); |
| 287 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 288 | } |
| 289 | |
| 290 | /* Write extension_type */ |
| 291 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 292 | /* Write extension_data_length */ |
| 293 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); |
| 294 | /* Write length of named_group_list */ |
| 295 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); |
| 296 | |
| 297 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", |
| 298 | buf + 4, named_group_list_len + 2 ); |
| 299 | |
| 300 | *out_len = p - buf; |
| 301 | |
| 302 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 303 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 304 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 305 | |
| 306 | return( 0 ); |
| 307 | } |
| 308 | |
| 309 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 310 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 311 | |
| 312 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 313 | /* |
| 314 | * Function for writing a signature algorithm extension. |
| 315 | * |
| 316 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 317 | * value (TLS 1.3 RFC8446): |
| 318 | * enum { |
| 319 | * .... |
| 320 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 321 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 322 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 323 | * .... |
| 324 | * } SignatureScheme; |
| 325 | * |
| 326 | * struct { |
| 327 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 328 | * } SignatureSchemeList; |
| 329 | * |
| 330 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 331 | * value (TLS 1.2 RFC5246): |
| 332 | * enum { |
| 333 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 334 | * sha512(6), (255) |
| 335 | * } HashAlgorithm; |
| 336 | * |
| 337 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 338 | * SignatureAlgorithm; |
| 339 | * |
| 340 | * struct { |
| 341 | * HashAlgorithm hash; |
| 342 | * SignatureAlgorithm signature; |
| 343 | * } SignatureAndHashAlgorithm; |
| 344 | * |
| 345 | * SignatureAndHashAlgorithm |
| 346 | * supported_signature_algorithms<2..2^16-2>; |
| 347 | * |
| 348 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 349 | * generalization of the TLS 1.2 signature algorithm extension. |
| 350 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 351 | * `SignatureScheme` field of TLS 1.3 |
| 352 | * |
| 353 | */ |
| 354 | static int ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 355 | const unsigned char *end, size_t *out_len ) |
| 356 | { |
| 357 | unsigned char *p = buf; |
| 358 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 359 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 360 | |
| 361 | *out_len = 0; |
| 362 | |
| 363 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 364 | |
| 365 | /* Check if we have space for header and length field: |
| 366 | * - extension_type (2 bytes) |
| 367 | * - extension_data_length (2 bytes) |
| 368 | * - supported_signature_algorithms_length (2 bytes) |
| 369 | */ |
| 370 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 371 | p += 6; |
| 372 | |
| 373 | /* |
| 374 | * Write supported_signature_algorithms |
| 375 | */ |
| 376 | supported_sig_alg = p; |
| 377 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 378 | if( sig_alg == NULL ) |
| 379 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 380 | |
| 381 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 382 | { |
| 383 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 384 | continue; |
| 385 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 386 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 387 | p += 2; |
| 388 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 389 | } |
| 390 | |
| 391 | /* Length of supported_signature_algorithms */ |
| 392 | supported_sig_alg_len = p - supported_sig_alg; |
| 393 | if( supported_sig_alg_len == 0 ) |
| 394 | { |
| 395 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 396 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 397 | } |
| 398 | |
| 399 | /* Write extension_type */ |
| 400 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
| 401 | /* Write extension_data_length */ |
| 402 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
| 403 | /* Write length of supported_signature_algorithms */ |
| 404 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 405 | |
| 406 | /* Output the total length of signature algorithms extension. */ |
| 407 | *out_len = p - buf; |
| 408 | |
| 409 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 410 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 411 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 412 | return( 0 ); |
| 413 | } |
| 414 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 415 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 416 | /* Write cipher_suites |
| 417 | * CipherSuite cipher_suites<2..2^16-2>; |
| 418 | */ |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 419 | /** |
| 420 | * \brief Validate cipher suite against config in SSL context. |
| 421 | * |
| 422 | * \param ssl SSL context |
| 423 | * \param suite_info Cipher suite to validate |
| 424 | * |
| 425 | * \return 0 if valid, else 1 |
| 426 | */ |
| 427 | static int ssl_validate_ciphersuite( |
| 428 | const mbedtls_ssl_context *ssl, |
| 429 | const mbedtls_ssl_ciphersuite_t *suite_info ) |
| 430 | { |
| 431 | if( suite_info == NULL ) |
| 432 | return( 1 ); |
| 433 | |
| 434 | if( ( suite_info->min_minor_ver > ssl->conf->max_minor_ver ) || |
| 435 | ( suite_info->max_minor_ver < ssl->conf->min_minor_ver ) ) |
| 436 | return( 1 ); |
| 437 | |
| 438 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 439 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 440 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 441 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) |
| 442 | return( 1 ); |
| 443 | #endif |
| 444 | |
| 445 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 446 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 447 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 448 | return( 1 ); |
| 449 | #endif |
| 450 | |
| 451 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 452 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 453 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 454 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 455 | { |
| 456 | return( 1 ); |
| 457 | } |
| 458 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 459 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 460 | |
| 461 | return( 0 ); |
| 462 | } |
| 463 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 464 | static int ssl_write_client_hello_cipher_suites( |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 465 | mbedtls_ssl_context *ssl, |
| 466 | unsigned char *buf, |
| 467 | unsigned char *end, |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 468 | int *tls12_uses_ec, |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 469 | size_t *out_len ) |
| 470 | { |
| 471 | unsigned char *p = buf; |
| 472 | const int *ciphersuite_list; |
| 473 | unsigned char *cipher_suites; /* Start of the cipher_suites list */ |
| 474 | size_t cipher_suites_len; |
| 475 | |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 476 | *tls12_uses_ec = 0; |
| 477 | *out_len = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Ciphersuite list |
| 481 | * |
| 482 | * This is a list of the symmetric cipher options supported by |
| 483 | * the client, specifically the record protection algorithm |
| 484 | * ( including secret key length ) and a hash to be used with |
| 485 | * HKDF, in descending order of client preference. |
| 486 | */ |
| 487 | ciphersuite_list = ssl->conf->ciphersuite_list; |
| 488 | |
| 489 | /* Check there is space for the cipher suite list length (2 bytes). */ |
| 490 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 491 | p += 2; |
| 492 | |
| 493 | /* Write cipher_suites */ |
| 494 | cipher_suites = p; |
| 495 | for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) |
| 496 | { |
| 497 | int cipher_suite = ciphersuite_list[i]; |
| 498 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 499 | |
| 500 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 501 | |
| 502 | if( ssl_validate_ciphersuite( ssl, ciphersuite_info ) ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 503 | continue; |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 504 | |
| 505 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 506 | ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 507 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) ) |
| 508 | *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info ); |
| 509 | #endif |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 510 | |
| 511 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", |
| 512 | (unsigned int) cipher_suite, |
| 513 | ciphersuite_info->name ) ); |
| 514 | |
| 515 | /* Check there is space for the cipher suite identifier (2 bytes). */ |
| 516 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 517 | MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 ); |
| 518 | p += 2; |
| 519 | } |
| 520 | |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 521 | /* |
| 522 | * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 523 | */ |
| 524 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 525 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 526 | #endif |
| 527 | { |
| 528 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); |
| 529 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 530 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 ); |
| 531 | p += 2; |
| 532 | } |
| 533 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 534 | /* Write the cipher_suites length in number of bytes */ |
| 535 | cipher_suites_len = p - cipher_suites; |
| 536 | MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); |
| 537 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 538 | ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites", |
| 539 | cipher_suites_len/2 ) ); |
| 540 | |
| 541 | /* Output the total length of cipher_suites field. */ |
| 542 | *out_len = p - buf; |
| 543 | |
| 544 | return( 0 ); |
| 545 | } |
| 546 | |
| 547 | /* |
Ronald Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame] | 548 | * Structure of the TLS 1.3 ClientHello message: |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 549 | * |
| 550 | * struct { |
| 551 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 552 | * Random random; |
| 553 | * opaque legacy_session_id<0..32>; |
| 554 | * CipherSuite cipher_suites<2..2^16-2>; |
| 555 | * opaque legacy_compression_methods<1..2^8-1>; |
| 556 | * Extension extensions<8..2^16-1>; |
| 557 | * } ClientHello; |
Ronald Cron | 5456a7f | 2022-02-18 17:38:42 +0100 | [diff] [blame] | 558 | * |
| 559 | * Structure of the (D)TLS 1.2 ClientHello message: |
| 560 | * |
| 561 | * struct { |
| 562 | * ProtocolVersion client_version; |
| 563 | * Random random; |
| 564 | * SessionID session_id; |
| 565 | * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY |
| 566 | * CipherSuite cipher_suites<2..2^16-2>; |
| 567 | * CompressionMethod compression_methods<1..2^8-1>; |
| 568 | * select (extensions_present) { |
| 569 | * case false: |
| 570 | * struct {}; |
| 571 | * case true: |
| 572 | * Extension extensions<0..2^16-1>; |
| 573 | * }; |
| 574 | * } ClientHello; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 575 | */ |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 576 | static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl, |
| 577 | unsigned char *buf, |
| 578 | unsigned char *end, |
| 579 | size_t *out_len ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 580 | { |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 581 | int ret; |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 582 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 583 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 584 | unsigned char propose_tls12 = 0; |
| 585 | #endif |
| 586 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 587 | unsigned char propose_tls13 = 0; |
| 588 | #endif |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 589 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 590 | unsigned char *p = buf; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 591 | unsigned char *p_extensions_len; /* Pointer to extensions length */ |
| 592 | size_t output_len; /* Length of buffer used by function */ |
| 593 | size_t extensions_len; /* Length of the list of extensions*/ |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 594 | int tls12_uses_ec = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 595 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 596 | *out_len = 0; |
| 597 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 598 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 599 | propose_tls12 = ( handshake->min_minor_ver <= MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 600 | && |
| 601 | ( MBEDTLS_SSL_MINOR_VERSION_3 <= ssl->minor_ver ); |
| 602 | #endif |
| 603 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 604 | propose_tls13 = ( handshake->min_minor_ver <= MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 605 | && |
| 606 | ( MBEDTLS_SSL_MINOR_VERSION_4 <= ssl->minor_ver ); |
| 607 | #endif |
| 608 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 609 | /* |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 610 | * Write client_version (TLS 1.2) or legacy_version (TLS 1.3) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 611 | * |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 612 | * In all cases this is the TLS 1.2 version. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 613 | */ |
| 614 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
Ronald Cron | 1614eb6 | 2022-02-18 17:53:01 +0100 | [diff] [blame] | 615 | mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3, |
| 616 | MBEDTLS_SSL_MINOR_VERSION_3, |
| 617 | ssl->conf->transport, p ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 618 | p += 2; |
| 619 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 620 | /* ... |
| 621 | * Random random; |
| 622 | * ... |
| 623 | * |
| 624 | * with for TLS 1.2 |
| 625 | * struct { |
| 626 | * uint32 gmt_unix_time; |
| 627 | * opaque random_bytes[28]; |
| 628 | * } Random; |
| 629 | * |
| 630 | * and for TLS 1.3 |
| 631 | * opaque Random[32]; |
| 632 | * |
| 633 | * The random bytes have been prepared by ssl_prepare_client_hello() into |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 634 | * the handshake->randbytes buffer and are copied here into the output |
| 635 | * buffer. |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 636 | */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 637 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 638 | memcpy( p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 639 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", |
| 640 | p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN ); |
| 641 | p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN; |
| 642 | |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 643 | /* TLS 1.2: |
| 644 | * ... |
| 645 | * SessionID session_id; |
| 646 | * ... |
| 647 | * with |
| 648 | * opaque SessionID<0..32>; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 649 | * |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 650 | * TLS 1.3: |
| 651 | * ... |
| 652 | * opaque legacy_session_id<0..32>; |
| 653 | * ... |
| 654 | * |
| 655 | * The (legacy) session identifier bytes have been by |
| 656 | * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer |
| 657 | * and are copied here into the output buffer. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 658 | */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 659 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 ); |
| 660 | *p++ = (unsigned char)ssl->session_negotiate->id_len; |
| 661 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
| 662 | p += ssl->session_negotiate->id_len; |
| 663 | |
| 664 | MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id, |
| 665 | ssl->session_negotiate->id_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 666 | |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 667 | /* DTLS 1.2 ONLY |
| 668 | * ... |
| 669 | * opaque cookie<0..2^8-1>; |
| 670 | * ... |
| 671 | */ |
| 672 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS) |
| 673 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 674 | { |
| 675 | unsigned char cookie_len = 0; |
| 676 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 677 | if( handshake->cookie != NULL ) |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 678 | { |
| 679 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 680 | handshake->cookie, |
| 681 | handshake->verify_cookie_len ); |
| 682 | cookie_len = handshake->verify_cookie_len; |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, cookie_len + 1 ); |
| 686 | *p++ = cookie_len; |
| 687 | if( cookie_len > 0 ) |
| 688 | { |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 689 | memcpy( p, handshake->cookie, cookie_len ); |
Ronald Cron | a874aa8 | 2022-02-19 18:11:26 +0100 | [diff] [blame] | 690 | p += cookie_len; |
| 691 | } |
| 692 | } |
| 693 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */ |
| 694 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 695 | /* Write cipher_suites */ |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 696 | ret = ssl_write_client_hello_cipher_suites( ssl, p, end, |
| 697 | &tls12_uses_ec, |
| 698 | &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 699 | if( ret != 0 ) |
| 700 | return( ret ); |
| 701 | p += output_len; |
| 702 | |
Ronald Cron | 42c1cbf | 2022-02-20 10:24:39 +0100 | [diff] [blame] | 703 | /* Write legacy_compression_methods (TLS 1.3) or |
| 704 | * compression_methods (TLS 1.2) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 705 | * |
| 706 | * For every TLS 1.3 ClientHello, this vector MUST contain exactly |
| 707 | * one byte set to zero, which corresponds to the 'null' compression |
| 708 | * method in prior versions of TLS. |
Ronald Cron | 42c1cbf | 2022-02-20 10:24:39 +0100 | [diff] [blame] | 709 | * |
| 710 | * For TLS 1.2 ClientHello, for security reasons we do not support |
| 711 | * compression anymore, thus also just the 'null' compression method. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 712 | */ |
| 713 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 714 | *p++ = 1; |
| 715 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 716 | |
| 717 | /* Write extensions */ |
| 718 | |
| 719 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 720 | /* Keeping track of the included extensions */ |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 721 | handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 722 | #endif |
| 723 | |
| 724 | /* First write extensions, then the total length */ |
| 725 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 726 | p_extensions_len = p; |
| 727 | p += 2; |
| 728 | |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 729 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 730 | /* Write server name extension */ |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 731 | ret = ssl_write_hostname_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 732 | if( ret != 0 ) |
| 733 | return( ret ); |
| 734 | p += output_len; |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 735 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 736 | |
| 737 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 738 | ret = ssl_write_alpn_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 739 | if( ret != 0 ) |
| 740 | return( ret ); |
| 741 | p += output_len; |
| 742 | #endif /* MBEDTLS_SSL_ALPN */ |
| 743 | |
| 744 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 745 | if( propose_tls13 ) |
| 746 | { |
| 747 | ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, |
| 748 | &output_len ); |
| 749 | if( ret != 0 ) |
| 750 | return( ret ); |
| 751 | p += output_len; |
| 752 | } |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 753 | #endif |
| 754 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 755 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 756 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 757 | if( |
Ronald Cron | df823bf | 2022-03-29 18:57:54 +0200 | [diff] [blame] | 758 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 759 | ( propose_tls13 && |
| 760 | mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) || |
| 761 | #endif |
| 762 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 763 | ( propose_tls12 && tls12_uses_ec ) || |
| 764 | #endif |
| 765 | 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 766 | { |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 767 | ret = ssl_write_supported_groups_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 768 | if( ret != 0 ) |
| 769 | return( ret ); |
| 770 | p += output_len; |
| 771 | } |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 772 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 773 | |
Ronald Cron | 11e1857 | 2022-03-17 13:44:33 +0100 | [diff] [blame] | 774 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 775 | if( |
| 776 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 777 | ( propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) || |
| 778 | #endif |
| 779 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 780 | propose_tls12 || |
| 781 | #endif |
| 782 | 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 783 | { |
Ronald Cron | fbd9f99 | 2022-03-17 15:22:07 +0100 | [diff] [blame] | 784 | ret = ssl_write_sig_alg_ext( ssl, p, end, &output_len ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 785 | if( ret != 0 ) |
| 786 | return( ret ); |
| 787 | p += output_len; |
| 788 | } |
| 789 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 790 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 791 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 792 | if( propose_tls12 ) |
| 793 | { |
| 794 | ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p, end, |
| 795 | tls12_uses_ec, |
| 796 | &output_len ); |
| 797 | if( ret != 0 ) |
| 798 | return( ret ); |
| 799 | p += output_len; |
| 800 | } |
| 801 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 802 | |
| 803 | /* Write the length of the list of extensions. */ |
| 804 | extensions_len = p - p_extensions_len - 2; |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 805 | |
| 806 | if( extensions_len == 0 ) |
| 807 | p = p_extensions_len; |
| 808 | else |
| 809 | { |
| 810 | MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 ); |
| 811 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" \ |
| 812 | MBEDTLS_PRINTF_SIZET, extensions_len ) ); |
| 813 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", |
| 814 | p_extensions_len, extensions_len ); |
| 815 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 816 | |
| 817 | *out_len = p - buf; |
| 818 | return( 0 ); |
| 819 | } |
| 820 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 821 | static int ssl_generate_random( mbedtls_ssl_context *ssl ) |
| 822 | { |
| 823 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 824 | unsigned char *randbytes = ssl->handshake->randbytes; |
| 825 | size_t gmt_unix_time_len = 0; |
| 826 | |
| 827 | /* |
| 828 | * Generate the random bytes |
| 829 | * |
| 830 | * TLS 1.2 case: |
| 831 | * struct { |
| 832 | * uint32 gmt_unix_time; |
| 833 | * opaque random_bytes[28]; |
| 834 | * } Random; |
| 835 | * |
| 836 | * TLS 1.3 case: |
| 837 | * opaque Random[32]; |
| 838 | */ |
| 839 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 840 | { |
| 841 | #if defined(MBEDTLS_HAVE_TIME) |
| 842 | mbedtls_time_t gmt_unix_time = mbedtls_time( NULL ); |
| 843 | MBEDTLS_PUT_UINT32_BE( gmt_unix_time, randbytes, 0 ); |
| 844 | gmt_unix_time_len = 4; |
| 845 | |
| 846 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 847 | ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, |
| 848 | (long long) gmt_unix_time ) ); |
| 849 | #endif /* MBEDTLS_HAVE_TIME */ |
| 850 | } |
| 851 | |
| 852 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 853 | randbytes + gmt_unix_time_len, |
| 854 | MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len ); |
| 855 | return( ret ); |
| 856 | } |
| 857 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 858 | static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 859 | { |
| 860 | int ret; |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 861 | size_t session_id_len; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 862 | |
| 863 | if( ssl->conf->f_rng == NULL ) |
| 864 | { |
| 865 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 866 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 867 | } |
| 868 | |
Ronald Cron | 86a477f | 2022-02-18 17:45:10 +0100 | [diff] [blame] | 869 | /* Bet on the highest configured version if we are not in a TLS 1.2 |
| 870 | * renegotiation or session resumption. |
| 871 | */ |
| 872 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 873 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 874 | ssl->handshake->min_minor_ver = ssl->minor_ver; |
| 875 | else |
| 876 | #endif |
| 877 | { |
| 878 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 879 | |
| 880 | if( ssl->handshake->resume ) |
| 881 | { |
| 882 | ssl->minor_ver = ssl->session_negotiate->minor_ver; |
| 883 | ssl->handshake->min_minor_ver = ssl->minor_ver; |
| 884 | } |
| 885 | else |
| 886 | { |
| 887 | ssl->minor_ver = ssl->conf->max_minor_ver; |
| 888 | ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver; |
| 889 | } |
| 890 | } |
| 891 | |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 892 | /* |
| 893 | * But when responding to a verify request where we MUST reuse the |
| 894 | * previoulsy generated random bytes (RFC 6347 4.2.1), generate the |
| 895 | * random bytes. |
| 896 | */ |
| 897 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 898 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 899 | ( ssl->handshake->cookie == NULL ) ) |
| 900 | #endif |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 901 | { |
Ronald Cron | 58b8038 | 2022-02-18 18:41:08 +0100 | [diff] [blame] | 902 | ret = ssl_generate_random( ssl ); |
| 903 | if( ret != 0 ) |
| 904 | { |
| 905 | MBEDTLS_SSL_DEBUG_RET( 1, "Random bytes generation failed", ret ); |
| 906 | return( ret ); |
| 907 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 908 | } |
| 909 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 910 | /* |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 911 | * Prepare session identifier. But in the case of a TLS 1.2 session |
| 912 | * renegotiation or session resumption, the initial value of the session |
| 913 | * identifier length below is equal to zero. |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 914 | */ |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 915 | session_id_len = ssl->session_negotiate->id_len; |
| 916 | |
| 917 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 918 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 919 | { |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 920 | if( session_id_len < 16 || session_id_len > 32 || |
| 921 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 922 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
| 923 | #endif |
| 924 | ssl->handshake->resume == 0 ) |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 925 | { |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 926 | session_id_len = 0; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 927 | } |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 928 | |
| 929 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 930 | /* |
| 931 | * RFC 5077 section 3.4: "When presenting a ticket, the client MAY |
| 932 | * generate and include a Session ID in the TLS ClientHello." |
| 933 | */ |
| 934 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 935 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
| 936 | #endif |
| 937 | { |
| 938 | if( ( ssl->session_negotiate->ticket != NULL ) && |
| 939 | ( ssl->session_negotiate->ticket_len != 0 ) ) |
| 940 | { |
| 941 | session_id_len = 32; |
| 942 | } |
| 943 | } |
| 944 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 945 | } |
| 946 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 947 | |
| 948 | #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) |
| 949 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 950 | { |
| 951 | /* |
| 952 | * Create a legacy session identifier for the purpose of middlebox |
| 953 | * compatibility only if one has not been created already, which is |
| 954 | * the case if we are here for the TLS 1.3 second ClientHello. |
| 955 | * |
| 956 | * Versions of TLS before TLS 1.3 supported a "session resumption" |
| 957 | * feature which has been merged with pre-shared keys in TLS 1.3 |
| 958 | * version. A client which has a cached session ID set by a pre-TLS 1.3 |
| 959 | * server SHOULD set this field to that value. In compatibility mode, |
| 960 | * this field MUST be non-empty, so a client not offering a pre-TLS 1.3 |
| 961 | * session MUST generate a new 32-byte value. This value need not be |
| 962 | * random but SHOULD be unpredictable to avoid implementations fixating |
| 963 | * on a specific value (also known as ossification). Otherwise, it MUST |
| 964 | * be set as a zero-length vector ( i.e., a zero-valued single byte |
| 965 | * length field ). |
| 966 | */ |
| 967 | session_id_len = 32; |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 968 | } |
| 969 | #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ |
| 970 | |
Ronald Cron | 021b178 | 2022-02-19 17:32:53 +0100 | [diff] [blame] | 971 | if( session_id_len != ssl->session_negotiate->id_len ) |
| 972 | { |
| 973 | ssl->session_negotiate->id_len = session_id_len; |
| 974 | if( session_id_len > 0 ) |
| 975 | { |
| 976 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 977 | ssl->session_negotiate->id, |
| 978 | session_id_len ); |
| 979 | if( ret != 0 ) |
| 980 | { |
| 981 | MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret ); |
| 982 | return( ret ); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 987 | return( 0 ); |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Write ClientHello handshake message. |
| 992 | * Handler for MBEDTLS_SSL_CLIENT_HELLO |
| 993 | */ |
| 994 | int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
| 995 | { |
| 996 | int ret = 0; |
| 997 | unsigned char *buf; |
| 998 | size_t buf_len, msg_len; |
| 999 | |
| 1000 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 1001 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 1002 | MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 1003 | |
| 1004 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( |
| 1005 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 1006 | &buf, &buf_len ) ); |
| 1007 | |
Ronald Cron | 71c2332 | 2022-02-18 17:29:39 +0100 | [diff] [blame] | 1008 | MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf, |
| 1009 | buf + buf_len, |
| 1010 | &msg_len ) ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 1011 | |
Ronald Cron | 5f4e912 | 2022-02-21 09:50:36 +0100 | [diff] [blame] | 1012 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1013 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1014 | { |
| 1015 | ssl->out_msglen = msg_len + 4; |
| 1016 | mbedtls_ssl_send_flight_completed( ssl ); |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 1017 | |
Ronald Cron | 5f4e912 | 2022-02-21 09:50:36 +0100 | [diff] [blame] | 1018 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 1019 | { |
| 1020 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 1021 | return( ret ); |
| 1022 | } |
| 1023 | |
| 1024 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 1025 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 1026 | { |
| 1027 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 1028 | return( ret ); |
| 1029 | } |
| 1030 | } |
| 1031 | else |
| 1032 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */ |
| 1033 | { |
| 1034 | mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 1035 | buf, msg_len ); |
| 1036 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, |
| 1037 | buf_len, |
| 1038 | msg_len ) ); |
| 1039 | } |
Ronald Cron | 3d580bf | 2022-02-18 17:24:56 +0100 | [diff] [blame] | 1040 | |
| 1041 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 1042 | |
| 1043 | cleanup: |
| 1044 | |
| 1045 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1050 | #endif /* MBEDTLS_SSL_CLI_C */ |