blob: d65731751a585d7cb3e99670b29583ea9b40691a [file] [log] [blame]
Ronald Cron3d580bf2022-02-18 17:24:56 +01001/*
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 Yu141bbe72022-12-01 20:30:41 +080031#include "mbedtls/platform.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010032
33#include "ssl_client.h"
34#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010035#include "ssl_tls13_keys.h"
36#include "ssl_debug_helpers.h"
37
Ronald Cronfbd9f992022-03-17 15:22:07 +010038#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020039MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010040static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
41 unsigned char *buf,
42 const unsigned char *end,
43 size_t *olen)
Ronald Cronfbd9f992022-03-17 15:22:07 +010044{
45 unsigned char *p = buf;
46 size_t hostname_len;
47
48 *olen = 0;
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if (ssl->hostname == NULL) {
51 return 0;
52 }
Ronald Cronfbd9f992022-03-17 15:22:07 +010053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 MBEDTLS_SSL_DEBUG_MSG(3,
55 ("client hello, adding server name extension: %s",
56 ssl->hostname));
Ronald Cronfbd9f992022-03-17 15:22:07 +010057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 hostname_len = strlen(ssl->hostname);
Ronald Cronfbd9f992022-03-17 15:22:07 +010059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
Ronald Cronfbd9f992022-03-17 15:22:07 +010061
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 Peskine449bd832023-01-11 14:50:10 +010088 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010089 p += 2;
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091 MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010092 p += 2;
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010095 p += 2;
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
Ronald Cronfbd9f992022-03-17 15:22:07 +010098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100100 p += 2;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 memcpy(p, ssl->hostname, hostname_len);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100103
104 *olen = hostname_len + 9;
105
Jerry Yu0c354a22022-08-29 15:25:36 +0800106#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
Jerry Yu0c354a22022-08-29 15:25:36 +0800108#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100110}
111#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
112
Ronald Cron3d580bf2022-02-18 17:24:56 +0100113#if defined(MBEDTLS_SSL_ALPN)
114/*
Ronald Cron71c23322022-02-18 17:29:39 +0100115 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100116 *
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é-Gonnarda3115dc2022-06-17 10:52:54 +0200127MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
129 unsigned char *buf,
130 const unsigned char *end,
131 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100132{
133 unsigned char *p = buf;
134
135 *out_len = 0;
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (ssl->conf->alpn_list == NULL) {
138 return 0;
139 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100142
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 Peskine449bd832023-01-11 14:50:10 +0100147 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
148 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100149 /* 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 Peskine449bd832023-01-11 14:50:10 +0100159 for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100160 /*
161 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
162 * protocol names is less than 255.
163 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 size_t protocol_name_len = strlen(*cur);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 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 Cron3d580bf2022-02-18 17:24:56 +0100169 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 Peskine449bd832023-01-11 14:50:10 +0100175 MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100176
177 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100179
Jerry Yu0c354a22022-08-29 15:25:36 +0800180#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yu0c354a22022-08-29 15:25:36 +0800182#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100184}
185#endif /* MBEDTLS_SSL_ALPN */
186
Przemek Stekiel98d79332023-06-26 12:44:33 +0200187#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
188 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100189/*
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 *
Ronald Cronfbd9f992022-03-17 15:22:07 +0100226 */
Ronald Cron1ffa4502023-06-30 14:56:38 +0200227#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG 1
228#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG 2
229
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200230MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100231static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
232 unsigned char *buf,
233 const unsigned char *end,
Ronald Cron1ffa4502023-06-30 14:56:38 +0200234 int flags,
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 size_t *out_len)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100236{
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 unsigned char *p = buf;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100238 unsigned char *named_group_list; /* Start of named_group_list */
239 size_t named_group_list_len; /* Length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100241
242 *out_len = 0;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100245
246 /* Check if we have space for header and length fields:
247 * - extension_type (2 bytes)
248 * - extension_data_length (2 bytes)
249 * - named_group_list_length (2 bytes)
250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100252 p += 6;
253
254 named_group_list = p;
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (group_list == NULL) {
257 return MBEDTLS_ERR_SSL_BAD_CONFIG;
258 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 for (; *group_list != 0; group_list++) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200261 int propose_group = 0;
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(1, ("got supported group(%04x)", *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100264
Ronald Cron1ffa4502023-06-30 14:56:38 +0200265#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
266 if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
267#if defined(PSA_WANT_ALG_ECDH)
268 if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list) &&
269 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
270 MBEDTLS_ECP_DP_NONE)) {
271 propose_group = 1;
Valerio Setti18c9fed2022-12-30 17:44:24 +0100272 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200273#endif
274#if defined(PSA_WANT_ALG_FFDH)
275 if (mbedtls_ssl_tls13_named_group_is_dhe(*group_list)) {
276 propose_group = 1;
277 }
278#endif
279 }
280#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
281
282#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
283 if ((flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG) &&
284 mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list) &&
285 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
286 MBEDTLS_ECP_DP_NONE)) {
287 propose_group = 1;
288 }
289#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC */
290
291 if (propose_group) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
293 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100294 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
Ronald Cron1ffa4502023-06-30 14:56:38 +0200296 mbedtls_ssl_named_group_to_str(*group_list),
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100298 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100299 }
300
301 /* Length of named_group_list */
302 named_group_list_len = p - named_group_list;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (named_group_list_len == 0) {
304 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
305 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100306 }
307
308 /* Write extension_type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100310 /* Write extension_data_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100312 /* Write length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
316 buf + 4, named_group_list_len + 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100317
318 *out_len = p - buf;
319
320#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800321 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100323#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100326}
Przemek Stekiel98d79332023-06-26 12:44:33 +0200327#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
328 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100329
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200330MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100331static int ssl_write_client_hello_cipher_suites(
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 mbedtls_ssl_context *ssl,
333 unsigned char *buf,
334 unsigned char *end,
335 int *tls12_uses_ec,
336 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100337{
338 unsigned char *p = buf;
339 const int *ciphersuite_list;
340 unsigned char *cipher_suites; /* Start of the cipher_suites list */
341 size_t cipher_suites_len;
342
Ronald Crond491c2d2022-02-19 18:30:46 +0100343 *tls12_uses_ec = 0;
344 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100345
346 /*
347 * Ciphersuite list
348 *
349 * This is a list of the symmetric cipher options supported by
350 * the client, specifically the record protection algorithm
351 * ( including secret key length ) and a hash to be used with
352 * HKDF, in descending order of client preference.
353 */
354 ciphersuite_list = ssl->conf->ciphersuite_list;
355
356 /* Check there is space for the cipher suite list length (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100358 p += 2;
359
Ronald Cron64767262022-03-31 14:13:57 +0200360 /* Write cipher_suites
361 * CipherSuite cipher_suites<2..2^16-2>;
362 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100363 cipher_suites = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100365 int cipher_suite = ciphersuite_list[i];
366 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Ronald Crond491c2d2022-02-19 18:30:46 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
371 ssl->handshake->min_tls_version,
372 ssl->tls_version) != 0) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100373 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100375
376#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 (defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
378 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
379 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
Ronald Crond491c2d2022-02-19 18:30:46 +0100380#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
383 (unsigned int) cipher_suite,
384 ciphersuite_info->name));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100385
386 /* Check there is space for the cipher suite identifier (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
388 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100389 p += 2;
390 }
391
Ronald Crond491c2d2022-02-19 18:30:46 +0100392 /*
393 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
394 */
David Horstmann4a285632022-10-06 18:30:10 +0100395 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100396#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
Ronald Crond491c2d2022-02-19 18:30:46 +0100398#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (!renegotiating) {
400 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
401 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
402 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
Ronald Crond491c2d2022-02-19 18:30:46 +0100403 p += 2;
404 }
405
Ronald Cron3d580bf2022-02-18 17:24:56 +0100406 /* Write the cipher_suites length in number of bytes */
407 cipher_suites_len = p - cipher_suites;
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
409 MBEDTLS_SSL_DEBUG_MSG(3,
410 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
411 cipher_suites_len/2));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100412
413 /* Output the total length of cipher_suites field. */
414 *out_len = p - buf;
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100417}
418
419/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100420 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100421 *
422 * struct {
423 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
424 * Random random;
425 * opaque legacy_session_id<0..32>;
426 * CipherSuite cipher_suites<2..2^16-2>;
427 * opaque legacy_compression_methods<1..2^8-1>;
428 * Extension extensions<8..2^16-1>;
429 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100430 *
431 * Structure of the (D)TLS 1.2 ClientHello message:
432 *
433 * struct {
434 * ProtocolVersion client_version;
435 * Random random;
436 * SessionID session_id;
437 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
438 * CipherSuite cipher_suites<2..2^16-2>;
439 * CompressionMethod compression_methods<1..2^8-1>;
440 * select (extensions_present) {
441 * case false:
442 * struct {};
443 * case true:
444 * Extension extensions<0..2^16-1>;
445 * };
446 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100447 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200448MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100449static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
450 unsigned char *buf,
451 unsigned char *end,
452 size_t *out_len,
453 size_t *binders_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100454{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100455 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100456 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100457 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100458 unsigned char *p_extensions_len; /* Pointer to extensions length */
459 size_t output_len; /* Length of buffer used by function */
460 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100461 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100462
Ronald Cron3d580bf2022-02-18 17:24:56 +0100463 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000464 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100465
Ronald Cron4079abc2022-02-20 10:35:26 +0100466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200467 unsigned char propose_tls12 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200469 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100471#endif
472#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200473 unsigned char propose_tls13 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200475 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100477#endif
478
Ronald Cron3d580bf2022-02-18 17:24:56 +0100479 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100480 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100481 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100482 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100483 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
485 mbedtls_ssl_write_version(p, ssl->conf->transport,
486 MBEDTLS_SSL_VERSION_TLS1_2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100487 p += 2;
488
Ronald Cron58b80382022-02-18 18:41:08 +0100489 /* ...
490 * Random random;
491 * ...
492 *
Ronald Cron58b80382022-02-18 18:41:08 +0100493 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100494 * the handshake->randbytes buffer and are copied here into the output
495 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
498 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
499 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
500 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100501 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
502
Ronald Cron021b1782022-02-19 17:32:53 +0100503 /* TLS 1.2:
504 * ...
505 * SessionID session_id;
506 * ...
507 * with
508 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100509 *
Ronald Cron021b1782022-02-19 17:32:53 +0100510 * TLS 1.3:
511 * ...
512 * opaque legacy_session_id<0..32>;
513 * ...
514 *
Ronald Cronda41b382022-03-30 09:57:11 +0200515 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100516 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
517 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100518 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
520 *p++ = (unsigned char) ssl->session_negotiate->id_len;
521 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100522 p += ssl->session_negotiate->id_len;
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
525 ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100526
Ronald Crona874aa82022-02-19 18:11:26 +0100527 /* DTLS 1.2 ONLY
528 * ...
529 * opaque cookie<0..2^8-1>;
530 * ...
531 */
532#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yue01304f2022-04-07 10:51:55 +0800534#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
535 uint8_t cookie_len = 0;
536#else
537 uint16_t cookie_len = 0;
538#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (handshake->cookie != NULL) {
541 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
542 handshake->cookie,
543 handshake->cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800544 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100545 }
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
548 *p++ = (unsigned char) cookie_len;
549 if (cookie_len > 0) {
550 memcpy(p, handshake->cookie, cookie_len);
Ronald Crona874aa82022-02-19 18:11:26 +0100551 p += cookie_len;
552 }
553 }
554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
555
Ronald Cron3d580bf2022-02-18 17:24:56 +0100556 /* Write cipher_suites */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
558 &tls12_uses_ec,
559 &output_len);
560 if (ret != 0) {
561 return ret;
562 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100563 p += output_len;
564
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100565 /* Write legacy_compression_methods (TLS 1.3) or
566 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100567 *
568 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
569 * one byte set to zero, which corresponds to the 'null' compression
570 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100571 *
572 * For TLS 1.2 ClientHello, for security reasons we do not support
573 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100574 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100576 *p++ = 1;
577 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
578
579 /* Write extensions */
580
581#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
582 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800583 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100584#endif
585
586 /* First write extensions, then the total length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100588 p_extensions_len = p;
589 p += 2;
590
Ronald Crondf823bf2022-03-29 18:57:54 +0200591#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
592 /* Write server name extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
594 if (ret != 0) {
595 return ret;
596 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100597 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200598#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100599
600#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
602 if (ret != 0) {
603 return ret;
604 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100605 p += output_len;
606#endif /* MBEDTLS_SSL_ALPN */
607
608#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (propose_tls13) {
610 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
611 &output_len);
612 if (ret != 0) {
613 return ret;
614 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100615 p += output_len;
616 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200617#endif
618
Przemek Stekiel76669452023-06-26 17:34:36 +0200619#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
620 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cron1ffa4502023-06-30 14:56:38 +0200621 {
622 int ssl_write_supported_groups_ext_flags = 0;
623
624#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
625 if (propose_tls13 && mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
626 ssl_write_supported_groups_ext_flags |=
627 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200629#endif
630#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
631 if (propose_tls12 && tls12_uses_ec) {
632 ssl_write_supported_groups_ext_flags |=
633 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG;
634 }
635#endif
636 if (ssl_write_supported_groups_ext_flags != 0) {
637 ret = ssl_write_supported_groups_ext(ssl, p, end,
638 ssl_write_supported_groups_ext_flags,
639 &output_len);
640 if (ret != 0) {
641 return ret;
642 }
643 p += output_len;
644 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100645 }
Przemek Stekiel76669452023-06-26 17:34:36 +0200646#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
647 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100648
Ronald Crone68ab4f2022-10-05 12:46:29 +0200649#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (
Ronald Cron4079abc2022-02-20 10:35:26 +0100651#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 (propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl)) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100653#endif
654#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
655 propose_tls12 ||
656#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 0) {
658 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
659 if (ret != 0) {
660 return ret;
661 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100662 p += output_len;
663 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200664#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100665
Ronald Cron4079abc2022-02-20 10:35:26 +0100666#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (propose_tls12) {
668 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
669 tls12_uses_ec,
670 &output_len);
671 if (ret != 0) {
672 return ret;
673 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100674 p += output_len;
675 }
676#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100677
Ronald Cron41a443a2022-10-04 16:38:25 +0200678#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000679 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
680 * MUST be the last extension in the ClientHello.
681 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000683 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 ssl, p, end, &output_len, binders_len);
685 if (ret != 0) {
686 return ret;
687 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000688 p += output_len;
689 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200690#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000691
Ronald Cron3d580bf2022-02-18 17:24:56 +0100692 /* Write the length of the list of extensions. */
693 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (extensions_len == 0) {
696 p = p_extensions_len;
697 } else {
698 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
699 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
700 MBEDTLS_PRINTF_SIZET, extensions_len));
701 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
702 p_extensions_len, extensions_len);
Ronald Cron4079abc2022-02-20 10:35:26 +0100703 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100704
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800705#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu7de2ff02022-11-08 21:43:46 +0800706 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 3, MBEDTLS_SSL_HS_CLIENT_HELLO, handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800708#endif
709
Ronald Cron3d580bf2022-02-18 17:24:56 +0100710 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100712}
713
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200714MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100715static int ssl_generate_random(mbedtls_ssl_context *ssl)
Ronald Cron58b80382022-02-18 18:41:08 +0100716{
717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
718 unsigned char *randbytes = ssl->handshake->randbytes;
719 size_t gmt_unix_time_len = 0;
720
721 /*
722 * Generate the random bytes
723 *
724 * TLS 1.2 case:
725 * struct {
726 * uint32 gmt_unix_time;
727 * opaque random_bytes[28];
728 * } Random;
729 *
730 * TLS 1.3 case:
731 * opaque Random[32];
732 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Ronald Cron58b80382022-02-18 18:41:08 +0100734#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
736 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
Ronald Cron58b80382022-02-18 18:41:08 +0100737 gmt_unix_time_len = 4;
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 MBEDTLS_SSL_DEBUG_MSG(3,
740 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
741 (long long) gmt_unix_time));
Ronald Cron58b80382022-02-18 18:41:08 +0100742#endif /* MBEDTLS_HAVE_TIME */
743 }
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 ret = ssl->conf->f_rng(ssl->conf->p_rng,
746 randbytes + gmt_unix_time_len,
747 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
748 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100749}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000750
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200751MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100752static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100753{
754 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100755 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800756 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (session_negotiate == NULL) {
759 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
760 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100761
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800762#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
763 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
764 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800765
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800766 /* Check if a tls13 ticket has been configured. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (ssl->handshake->resume != 0 &&
Jerry Yu22c18c12022-10-11 15:58:51 +0800768 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 session_negotiate->ticket != NULL) {
770 mbedtls_time_t now = mbedtls_time(NULL);
771 uint64_t age = (uint64_t) (now - session_negotiate->ticket_received);
772 if (session_negotiate->ticket_received > now ||
773 age > session_negotiate->ticket_lifetime) {
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800774 /* Without valid ticket, disable session resumption.*/
775 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 3, ("Ticket expired, disable session resumption"));
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800777 ssl->handshake->resume = 0;
778 }
779 }
780#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
781 MBEDTLS_SSL_SESSION_TICKETS &&
782 MBEDTLS_HAVE_TIME */
783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (ssl->conf->f_rng == NULL) {
785 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
786 return MBEDTLS_ERR_SSL_NO_RNG;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100787 }
788
Ronald Cron86a477f2022-02-18 17:45:10 +0100789 /* Bet on the highest configured version if we are not in a TLS 1.2
790 * renegotiation or session resumption.
791 */
792#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Glenn Strausscd78df62022-04-07 19:07:11 -0400794 ssl->handshake->min_tls_version = ssl->tls_version;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 } else
Ronald Cron86a477f2022-02-18 17:45:10 +0100796#endif
797 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (ssl->handshake->resume) {
799 ssl->tls_version = session_negotiate->tls_version;
800 ssl->handshake->min_tls_version = ssl->tls_version;
801 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100803 }
804 }
805
Ronald Cron58b80382022-02-18 18:41:08 +0100806 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200807 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000808 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200809 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100810 */
811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
813 (ssl->handshake->cookie == NULL))
Ronald Cron58b80382022-02-18 18:41:08 +0100814#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100815 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 ret = ssl_generate_random(ssl);
817 if (ret != 0) {
818 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
819 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100820 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100821 }
822
Ronald Cron3d580bf2022-02-18 17:24:56 +0100823 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200824 * Prepare session identifier. At that point, the length of the session
825 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
826 * to zero, except in the case of a TLS 1.2 session renegotiation or
827 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100828 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800829 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100830
831#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
833 if (session_id_len < 16 || session_id_len > 32 ||
Ronald Cron021b1782022-02-19 17:32:53 +0100834#if defined(MBEDTLS_SSL_RENEGOTIATION)
835 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
836#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 ssl->handshake->resume == 0) {
Ronald Cron021b1782022-02-19 17:32:53 +0100838 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100839 }
Ronald Cron021b1782022-02-19 17:32:53 +0100840
841#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 /*
843 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
844 * generate and include a Session ID in the TLS ClientHello."
845 */
David Horstmann4a285632022-10-06 18:30:10 +0100846 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100847#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann7aee0ec2022-10-25 10:38:25 +0100849 renegotiating = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 }
Ronald Cron021b1782022-02-19 17:32:53 +0100851#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (!renegotiating) {
853 if ((session_negotiate->ticket != NULL) &&
854 (session_negotiate->ticket_len != 0)) {
Ronald Cron021b1782022-02-19 17:32:53 +0100855 session_id_len = 32;
856 }
857 }
858#endif /* MBEDTLS_SSL_SESSION_TICKETS */
859 }
860#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
861
862#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron021b1782022-02-19 17:32:53 +0100864 /*
865 * Create a legacy session identifier for the purpose of middlebox
866 * compatibility only if one has not been created already, which is
867 * the case if we are here for the TLS 1.3 second ClientHello.
868 *
869 * Versions of TLS before TLS 1.3 supported a "session resumption"
870 * feature which has been merged with pre-shared keys in TLS 1.3
871 * version. A client which has a cached session ID set by a pre-TLS 1.3
872 * server SHOULD set this field to that value. In compatibility mode,
873 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
874 * session MUST generate a new 32-byte value. This value need not be
875 * random but SHOULD be unpredictable to avoid implementations fixating
876 * on a specific value (also known as ossification). Otherwise, it MUST
877 * be set as a zero-length vector ( i.e., a zero-valued single byte
878 * length field ).
879 */
880 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100881 }
882#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (session_id_len != session_negotiate->id_len) {
Jerry Yu22c18c12022-10-11 15:58:51 +0800885 session_negotiate->id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (session_id_len > 0) {
887 ret = ssl->conf->f_rng(ssl->conf->p_rng,
888 session_negotiate->id,
889 session_id_len);
890 if (ret != 0) {
891 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
892 return ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100893 }
894 }
895 }
896
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000897#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000898 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000899 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
901 ssl->handshake->resume) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000902 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000903 session_negotiate->hostname != NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (ssl->hostname != NULL && session_negotiate->hostname != NULL) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000905 hostname_mismatch = strcmp(
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 ssl->hostname, session_negotiate->hostname) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000907 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (hostname_mismatch) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000910 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 1, ("Hostname mismatch the session ticket, "
912 "disable session resumption."));
913 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000914 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 } else {
916 return mbedtls_ssl_session_set_hostname(session_negotiate,
917 ssl->hostname);
Xiaokang Qian03409292022-10-12 02:49:52 +0000918 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000919#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000920 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000921 MBEDTLS_SSL_SERVER_NAME_INDICATION */
922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100924}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100925/*
926 * Write ClientHello handshake message.
927 * Handler for MBEDTLS_SSL_CLIENT_HELLO
928 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100930{
931 int ret = 0;
932 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000933 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
940 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
941 &buf, &buf_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
944 buf + buf_len,
945 &msg_len,
946 &binders_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100947
Ronald Cron5f4e9122022-02-21 09:50:36 +0100948#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Ronald Cron5f4e9122022-02-21 09:50:36 +0100950 ssl->out_msglen = msg_len + 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 mbedtls_ssl_send_flight_completed(ssl);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100952
Ronald Cron8ecd9932022-03-29 12:26:54 +0200953 /*
954 * The two functions below may try to send data on the network and
955 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
956 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200957 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200958 * set the handshake state to the next state at that point to ensure
959 * that we will not write and send again a ClientHello when we
960 * eventually succeed in sending the pending data.
961 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron8ecd9932022-03-29 12:26:54 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
965 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
966 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
970 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
971 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100972 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 } else
Ronald Cron5f4e9122022-02-21 09:50:36 +0100974#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
975 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000976
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100977 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
978 MBEDTLS_SSL_HS_CLIENT_HELLO,
979 msg_len);
980 if (ret != 0) {
981 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
982 return ret;
983 }
984 ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
985 if (ret != 0) {
986 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
987 return ret;
988 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200989#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (binders_len > 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000991 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000992 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 ssl, buf + msg_len - binders_len, buf + msg_len));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100994 ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100995 binders_len);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100996 if (ret != 0) {
997 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
998 return ret;
999 }
XiaokangQianeb69aee2022-07-05 08:21:43 +00001000 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001001#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +00001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
1004 buf_len,
1005 msg_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001006
Xiaokang Qian44051f62023-02-02 06:57:26 +00001007 /*
1008 * Set next state. Note that if TLS 1.3 is proposed, this may be
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001009 * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
Xiaokang Qian44051f62023-02-02 06:57:26 +00001010 */
1011 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001012
Xiaokang Qian44051f62023-02-02 06:57:26 +00001013#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1014 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
1015 MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001016 ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
Xiaokang Qian44051f62023-02-02 06:57:26 +00001017 }
1018#endif
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001019 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001020
1021cleanup:
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001024 return ret;
1025}
1026
1027#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1028#endif /* MBEDTLS_SSL_CLI_C */