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