blob: 0bd00cd91a337e7b9850a80efe33b2b31133a193 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cron3d580bf2022-02-18 17:24:56 +01006 */
7
8#include "common.h"
9
10#if defined(MBEDTLS_SSL_CLI_C)
11#if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
12
13#include <string.h>
14
Valerio Settib4f50762024-01-17 10:24:52 +010015#include "debug_internal.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010016#include "mbedtls/error.h"
Jerry Yu141bbe72022-12-01 20:30:41 +080017#include "mbedtls/platform.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010018
19#include "ssl_client.h"
20#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010021#include "ssl_tls13_keys.h"
22#include "ssl_debug_helpers.h"
23
Ronald Cronfbd9f992022-03-17 15:22:07 +010024#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020025MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010026static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
27 unsigned char *buf,
28 const unsigned char *end,
29 size_t *olen)
Ronald Cronfbd9f992022-03-17 15:22:07 +010030{
31 unsigned char *p = buf;
Gilles Peskinee61852e2025-02-12 23:28:48 +010032 const char *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
Ronald Cronfbd9f992022-03-17 15:22:07 +010033 size_t hostname_len;
34
35 *olen = 0;
36
Gilles Peskinee61852e2025-02-12 23:28:48 +010037 if (hostname == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +010038 return 0;
39 }
Ronald Cronfbd9f992022-03-17 15:22:07 +010040
Gilles Peskine449bd832023-01-11 14:50:10 +010041 MBEDTLS_SSL_DEBUG_MSG(3,
42 ("client hello, adding server name extension: %s",
Gilles Peskinee61852e2025-02-12 23:28:48 +010043 hostname));
Ronald Cronfbd9f992022-03-17 15:22:07 +010044
Gilles Peskinee61852e2025-02-12 23:28:48 +010045 hostname_len = strlen(hostname);
Ronald Cronfbd9f992022-03-17 15:22:07 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
Ronald Cronfbd9f992022-03-17 15:22:07 +010048
49 /*
50 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
51 *
52 * In order to provide any of the server names, clients MAY include an
53 * extension of type "server_name" in the (extended) client hello. The
54 * "extension_data" field of this extension SHALL contain
55 * "ServerNameList" where:
56 *
57 * struct {
58 * NameType name_type;
59 * select (name_type) {
60 * case host_name: HostName;
61 * } name;
62 * } ServerName;
63 *
64 * enum {
65 * host_name(0), (255)
66 * } NameType;
67 *
68 * opaque HostName<1..2^16-1>;
69 *
70 * struct {
71 * ServerName server_name_list<1..2^16-1>
72 * } ServerNameList;
73 *
74 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010076 p += 2;
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010079 p += 2;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010082 p += 2;
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084 *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
Ronald Cronfbd9f992022-03-17 15:22:07 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010087 p += 2;
88
Gilles Peskinee61852e2025-02-12 23:28:48 +010089 memcpy(p, hostname, hostname_len);
Ronald Cronfbd9f992022-03-17 15:22:07 +010090
91 *olen = hostname_len + 9;
92
Jerry Yu0c354a22022-08-29 15:25:36 +080093#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
Jerry Yu0c354a22022-08-29 15:25:36 +080095#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +010097}
98#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
99
Ronald Cron3d580bf2022-02-18 17:24:56 +0100100#if defined(MBEDTLS_SSL_ALPN)
101/*
Ronald Cron71c23322022-02-18 17:29:39 +0100102 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100103 *
104 * Structure of the application_layer_protocol_negotiation extension in
105 * ClientHello:
106 *
107 * opaque ProtocolName<1..2^8-1>;
108 *
109 * struct {
110 * ProtocolName protocol_name_list<2..2^16-1>
111 * } ProtocolNameList;
112 *
113 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200114MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100115static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
116 unsigned char *buf,
117 const unsigned char *end,
118 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100119{
120 unsigned char *p = buf;
121
122 *out_len = 0;
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (ssl->conf->alpn_list == NULL) {
125 return 0;
126 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100129
130
131 /* Check we have enough space for the extension type (2 bytes), the
132 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
135 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100136 /* Skip writing extension and list length for now */
137 p += 6;
138
139 /*
140 * opaque ProtocolName<1..2^8-1>;
141 *
142 * struct {
143 * ProtocolName protocol_name_list<2..2^16-1>
144 * } ProtocolNameList;
145 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100147 /*
148 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
149 * protocol names is less than 255.
150 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 size_t protocol_name_len = strlen(*cur);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + protocol_name_len);
154 *p++ = (unsigned char) protocol_name_len;
155 memcpy(p, *cur, protocol_name_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100156 p += protocol_name_len;
157 }
158
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000159 *out_len = (size_t) (p - buf);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100160
161 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100163
164 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100166
Jerry Yu0c354a22022-08-29 15:25:36 +0800167#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yu0c354a22022-08-29 15:25:36 +0800169#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100171}
172#endif /* MBEDTLS_SSL_ALPN */
173
Przemek Stekiel98d79332023-06-26 12:44:33 +0200174#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
175 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100176/*
177 * Function for writing a supported groups (TLS 1.3) or supported elliptic
178 * curves (TLS 1.2) extension.
179 *
180 * The "extension_data" field of a supported groups extension contains a
181 * "NamedGroupList" value (TLS 1.3 RFC8446):
182 * enum {
183 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
184 * x25519(0x001D), x448(0x001E),
185 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
186 * ffdhe6144(0x0103), ffdhe8192(0x0104),
187 * ffdhe_private_use(0x01FC..0x01FF),
188 * ecdhe_private_use(0xFE00..0xFEFF),
189 * (0xFFFF)
190 * } NamedGroup;
191 * struct {
192 * NamedGroup named_group_list<2..2^16-1>;
193 * } NamedGroupList;
194 *
195 * The "extension_data" field of a supported elliptic curves extension contains
196 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
197 * enum {
198 * deprecated(1..22),
199 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
200 * x25519(29), x448(30),
201 * reserved (0xFE00..0xFEFF),
202 * deprecated(0xFF01..0xFF02),
203 * (0xFFFF)
204 * } NamedCurve;
205 * struct {
206 * NamedCurve named_curve_list<2..2^16-1>
207 * } NamedCurveList;
208 *
209 * The TLS 1.3 supported groups extension was defined to be a compatible
210 * generalization of the TLS 1.2 supported elliptic curves extension. They both
211 * share the same extension identifier.
212 *
Ronald Cronfbd9f992022-03-17 15:22:07 +0100213 */
Ronald Cron1ffa4502023-06-30 14:56:38 +0200214#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG 1
215#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG 2
216
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200217MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100218static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
219 unsigned char *buf,
220 const unsigned char *end,
Ronald Cron1ffa4502023-06-30 14:56:38 +0200221 int flags,
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 size_t *out_len)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100223{
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 unsigned char *p = buf;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100225 unsigned char *named_group_list; /* Start of named_group_list */
226 size_t named_group_list_len; /* Length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100228
229 *out_len = 0;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100232
233 /* Check if we have space for header and length fields:
234 * - extension_type (2 bytes)
235 * - extension_data_length (2 bytes)
236 * - named_group_list_length (2 bytes)
237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100239 p += 6;
240
241 named_group_list = p;
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (group_list == NULL) {
244 return MBEDTLS_ERR_SSL_BAD_CONFIG;
245 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 for (; *group_list != 0; group_list++) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200248 int propose_group = 0;
249
mcagriaksoyd9f22802023-09-13 22:42:19 +0200250 MBEDTLS_SSL_DEBUG_MSG(3, ("got supported group(%04x)", *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100251
Ronald Cron1ffa4502023-06-30 14:56:38 +0200252#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
253 if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
254#if defined(PSA_WANT_ALG_ECDH)
255 if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list) &&
256 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
257 MBEDTLS_ECP_DP_NONE)) {
258 propose_group = 1;
Valerio Setti18c9fed2022-12-30 17:44:24 +0100259 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200260#endif
261#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiele80bbf42023-07-05 10:34:40 +0200262 if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200263 propose_group = 1;
264 }
265#endif
266 }
267#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
268
269#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
270 if ((flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG) &&
271 mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list) &&
272 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
273 MBEDTLS_ECP_DP_NONE)) {
274 propose_group = 1;
275 }
276#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC */
277
278 if (propose_group) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
280 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100281 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
Ronald Cron1ffa4502023-06-30 14:56:38 +0200283 mbedtls_ssl_named_group_to_str(*group_list),
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100285 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100286 }
287
288 /* Length of named_group_list */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000289 named_group_list_len = (size_t) (p - named_group_list);
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (named_group_list_len == 0) {
291 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
292 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100293 }
294
295 /* Write extension_type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100297 /* Write extension_data_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100299 /* Write length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
303 buf + 4, named_group_list_len + 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100304
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000305 *out_len = (size_t) (p - buf);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100306
307#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800308 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100310#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100313}
Przemek Stekiel98d79332023-06-26 12:44:33 +0200314#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
315 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100316
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200317MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100318static int ssl_write_client_hello_cipher_suites(
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 mbedtls_ssl_context *ssl,
320 unsigned char *buf,
321 unsigned char *end,
322 int *tls12_uses_ec,
323 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100324{
325 unsigned char *p = buf;
326 const int *ciphersuite_list;
327 unsigned char *cipher_suites; /* Start of the cipher_suites list */
328 size_t cipher_suites_len;
329
Ronald Crond491c2d2022-02-19 18:30:46 +0100330 *tls12_uses_ec = 0;
331 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100332
333 /*
334 * Ciphersuite list
335 *
336 * This is a list of the symmetric cipher options supported by
337 * the client, specifically the record protection algorithm
338 * ( including secret key length ) and a hash to be used with
339 * HKDF, in descending order of client preference.
340 */
341 ciphersuite_list = ssl->conf->ciphersuite_list;
342
343 /* Check there is space for the cipher suite list length (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100345 p += 2;
346
Ronald Cron64767262022-03-31 14:13:57 +0200347 /* Write cipher_suites
348 * CipherSuite cipher_suites<2..2^16-2>;
349 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100350 cipher_suites = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100352 int cipher_suite = ciphersuite_list[i];
353 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Ronald Crond491c2d2022-02-19 18:30:46 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
358 ssl->handshake->min_tls_version,
359 ssl->tls_version) != 0) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100360 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100362
363#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Valerio Setti7aeec542023-07-05 18:57:21 +0200364 (defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200365 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
367 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
Ronald Crond491c2d2022-02-19 18:30:46 +0100368#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
371 (unsigned int) cipher_suite,
372 ciphersuite_info->name));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100373
374 /* Check there is space for the cipher suite identifier (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
376 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100377 p += 2;
378 }
379
Ronald Crond491c2d2022-02-19 18:30:46 +0100380 /*
381 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
382 */
David Horstmann4a285632022-10-06 18:30:10 +0100383 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100384#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
Ronald Crond491c2d2022-02-19 18:30:46 +0100386#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (!renegotiating) {
388 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
389 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
390 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
Ronald Crond491c2d2022-02-19 18:30:46 +0100391 p += 2;
392 }
393
Ronald Cron3d580bf2022-02-18 17:24:56 +0100394 /* Write the cipher_suites length in number of bytes */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000395 cipher_suites_len = (size_t) (p - cipher_suites);
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
397 MBEDTLS_SSL_DEBUG_MSG(3,
398 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
399 cipher_suites_len/2));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100400
401 /* Output the total length of cipher_suites field. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000402 *out_len = (size_t) (p - buf);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100405}
406
407/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100408 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100409 *
410 * struct {
411 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
412 * Random random;
413 * opaque legacy_session_id<0..32>;
414 * CipherSuite cipher_suites<2..2^16-2>;
415 * opaque legacy_compression_methods<1..2^8-1>;
416 * Extension extensions<8..2^16-1>;
417 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100418 *
419 * Structure of the (D)TLS 1.2 ClientHello message:
420 *
421 * struct {
422 * ProtocolVersion client_version;
423 * Random random;
424 * SessionID session_id;
425 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
426 * CipherSuite cipher_suites<2..2^16-2>;
427 * CompressionMethod compression_methods<1..2^8-1>;
428 * select (extensions_present) {
429 * case false:
430 * struct {};
431 * case true:
432 * Extension extensions<0..2^16-1>;
433 * };
434 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100435 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200436MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100437static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
438 unsigned char *buf,
439 unsigned char *end,
440 size_t *out_len,
441 size_t *binders_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100442{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100443 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100444 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100445 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100446 unsigned char *p_extensions_len; /* Pointer to extensions length */
447 size_t output_len; /* Length of buffer used by function */
448 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100449 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100450
Ronald Cron3d580bf2022-02-18 17:24:56 +0100451 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000452 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100453
Ronald Cron4079abc2022-02-20 10:35:26 +0100454#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200455 unsigned char propose_tls12 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200457 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100459#endif
460#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200461 unsigned char propose_tls13 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200463 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100465#endif
466
Ronald Cron3d580bf2022-02-18 17:24:56 +0100467 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100468 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100469 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100470 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
473 mbedtls_ssl_write_version(p, ssl->conf->transport,
474 MBEDTLS_SSL_VERSION_TLS1_2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100475 p += 2;
476
Ronald Cron58b80382022-02-18 18:41:08 +0100477 /* ...
478 * Random random;
479 * ...
480 *
Ronald Cron58b80382022-02-18 18:41:08 +0100481 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100482 * the handshake->randbytes buffer and are copied here into the output
483 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100484 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
486 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
487 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
488 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100489 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
490
Ronald Cron021b1782022-02-19 17:32:53 +0100491 /* TLS 1.2:
492 * ...
493 * SessionID session_id;
494 * ...
495 * with
496 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100497 *
Ronald Cron021b1782022-02-19 17:32:53 +0100498 * TLS 1.3:
499 * ...
500 * opaque legacy_session_id<0..32>;
501 * ...
502 *
Ronald Cronda41b382022-03-30 09:57:11 +0200503 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100504 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
505 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100506 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
508 *p++ = (unsigned char) ssl->session_negotiate->id_len;
509 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100510 p += ssl->session_negotiate->id_len;
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
513 ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100514
Ronald Crona874aa82022-02-19 18:11:26 +0100515 /* DTLS 1.2 ONLY
516 * ...
517 * opaque cookie<0..2^8-1>;
518 * ...
519 */
520#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yue01304f2022-04-07 10:51:55 +0800522#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
523 uint8_t cookie_len = 0;
524#else
525 uint16_t cookie_len = 0;
526#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (handshake->cookie != NULL) {
529 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
530 handshake->cookie,
531 handshake->cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800532 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100533 }
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
536 *p++ = (unsigned char) cookie_len;
537 if (cookie_len > 0) {
538 memcpy(p, handshake->cookie, cookie_len);
Ronald Crona874aa82022-02-19 18:11:26 +0100539 p += cookie_len;
540 }
541 }
542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
543
Ronald Cron3d580bf2022-02-18 17:24:56 +0100544 /* Write cipher_suites */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
546 &tls12_uses_ec,
547 &output_len);
548 if (ret != 0) {
549 return ret;
550 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100551 p += output_len;
552
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100553 /* Write legacy_compression_methods (TLS 1.3) or
554 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100555 *
556 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
557 * one byte set to zero, which corresponds to the 'null' compression
558 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100559 *
560 * For TLS 1.2 ClientHello, for security reasons we do not support
561 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100562 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100564 *p++ = 1;
565 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
566
567 /* Write extensions */
568
569#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
570 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800571 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100572#endif
573
574 /* First write extensions, then the total length */
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_extensions_len = p;
577 p += 2;
578
Ronald Crondf823bf2022-03-29 18:57:54 +0200579#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
580 /* Write server name extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
582 if (ret != 0) {
583 return ret;
584 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100585 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200586#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100587
588#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
590 if (ret != 0) {
591 return ret;
592 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100593 p += output_len;
594#endif /* MBEDTLS_SSL_ALPN */
595
596#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (propose_tls13) {
598 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
599 &output_len);
600 if (ret != 0) {
601 return ret;
602 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100603 p += output_len;
604 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200605#endif
606
Przemek Stekiel76669452023-06-26 17:34:36 +0200607#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
608 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cron1ffa4502023-06-30 14:56:38 +0200609 {
610 int ssl_write_supported_groups_ext_flags = 0;
611
612#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +0800613 if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200614 ssl_write_supported_groups_ext_flags |=
615 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG;
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200617#endif
618#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
619 if (propose_tls12 && tls12_uses_ec) {
620 ssl_write_supported_groups_ext_flags |=
621 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG;
622 }
623#endif
624 if (ssl_write_supported_groups_ext_flags != 0) {
625 ret = ssl_write_supported_groups_ext(ssl, p, end,
626 ssl_write_supported_groups_ext_flags,
627 &output_len);
628 if (ret != 0) {
629 return ret;
630 }
631 p += output_len;
632 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100633 }
Przemek Stekiel76669452023-06-26 17:34:36 +0200634#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
635 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100636
Ronald Crone68ab4f2022-10-05 12:46:29 +0200637#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100638 int write_sig_alg_ext = 0;
Ronald Cron4079abc2022-02-20 10:35:26 +0100639#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Dave Rodgmana11eac42023-09-14 16:16:04 +0100640 write_sig_alg_ext = write_sig_alg_ext ||
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +0800641 (propose_tls13 && mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl));
Ronald Cron4079abc2022-02-20 10:35:26 +0100642#endif
643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100644 write_sig_alg_ext = write_sig_alg_ext || propose_tls12;
Ronald Cron4079abc2022-02-20 10:35:26 +0100645#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100646
647 if (write_sig_alg_ext) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
649 if (ret != 0) {
650 return ret;
651 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100652 p += output_len;
653 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200654#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100655
Ronald Cron4079abc2022-02-20 10:35:26 +0100656#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (propose_tls12) {
658 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
659 tls12_uses_ec,
660 &output_len);
661 if (ret != 0) {
662 return ret;
663 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100664 p += output_len;
665 }
666#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100667
Ronald Cron41a443a2022-10-04 16:38:25 +0200668#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000669 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
670 * MUST be the last extension in the ClientHello.
671 */
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +0800672 if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl)) {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000673 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 ssl, p, end, &output_len, binders_len);
675 if (ret != 0) {
676 return ret;
677 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000678 p += output_len;
679 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200680#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000681
Ronald Cron3d580bf2022-02-18 17:24:56 +0100682 /* Write the length of the list of extensions. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000683 extensions_len = (size_t) (p - p_extensions_len) - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (extensions_len == 0) {
686 p = p_extensions_len;
687 } else {
688 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
689 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
690 MBEDTLS_PRINTF_SIZET, extensions_len));
691 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
692 p_extensions_len, extensions_len);
Ronald Cron4079abc2022-02-20 10:35:26 +0100693 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100694
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000695 *out_len = (size_t) (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100697}
698
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200699MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100700static int ssl_generate_random(mbedtls_ssl_context *ssl)
Ronald Cron58b80382022-02-18 18:41:08 +0100701{
702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
703 unsigned char *randbytes = ssl->handshake->randbytes;
704 size_t gmt_unix_time_len = 0;
705
706 /*
707 * Generate the random bytes
708 *
709 * TLS 1.2 case:
710 * struct {
711 * uint32 gmt_unix_time;
712 * opaque random_bytes[28];
713 * } Random;
714 *
715 * TLS 1.3 case:
716 * opaque Random[32];
717 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Ronald Cron58b80382022-02-18 18:41:08 +0100719#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
721 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
Ronald Cron58b80382022-02-18 18:41:08 +0100722 gmt_unix_time_len = 4;
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 MBEDTLS_SSL_DEBUG_MSG(3,
725 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
726 (long long) gmt_unix_time));
Ronald Cron58b80382022-02-18 18:41:08 +0100727#endif /* MBEDTLS_HAVE_TIME */
728 }
729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 ret = ssl->conf->f_rng(ssl->conf->p_rng,
731 randbytes + gmt_unix_time_len,
732 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
733 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100734}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000735
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200736MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100737static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100738{
739 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100740 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800741 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (session_negotiate == NULL) {
744 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
745 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100746
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800747#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
748 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
749 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800750
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800751 /* Check if a tls13 ticket has been configured. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (ssl->handshake->resume != 0 &&
Jerry Yu22c18c12022-10-11 15:58:51 +0800753 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 session_negotiate->ticket != NULL) {
Jerry Yucebffc32022-12-15 18:00:05 +0800755 mbedtls_ms_time_t now = mbedtls_ms_time();
Jerry Yu342a5552023-11-10 14:23:39 +0800756 mbedtls_ms_time_t age = now - session_negotiate->ticket_reception_time;
David Horstmann47490072023-12-06 17:22:53 +0000757 if (age < 0 ||
758 age > (mbedtls_ms_time_t) session_negotiate->ticket_lifetime * 1000) {
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800759 /* Without valid ticket, disable session resumption.*/
760 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 3, ("Ticket expired, disable session resumption"));
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800762 ssl->handshake->resume = 0;
763 }
764 }
765#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
766 MBEDTLS_SSL_SESSION_TICKETS &&
767 MBEDTLS_HAVE_TIME */
768
Ronald Cron86a477f2022-02-18 17:45:10 +0100769 /* Bet on the highest configured version if we are not in a TLS 1.2
770 * renegotiation or session resumption.
771 */
772#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Glenn Strausscd78df62022-04-07 19:07:11 -0400774 ssl->handshake->min_tls_version = ssl->tls_version;
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 } else
Ronald Cron86a477f2022-02-18 17:45:10 +0100776#endif
777 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (ssl->handshake->resume) {
779 ssl->tls_version = session_negotiate->tls_version;
780 ssl->handshake->min_tls_version = ssl->tls_version;
781 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100783 }
784 }
785
Ronald Cron58b80382022-02-18 18:41:08 +0100786 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200787 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000788 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200789 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100790 */
791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
793 (ssl->handshake->cookie == NULL))
Ronald Cron58b80382022-02-18 18:41:08 +0100794#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100795 {
BensonLiou35178fe2024-01-11 15:28:17 +0800796#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine93b305d2024-03-14 15:05:09 +0100797 if (!ssl->handshake->hello_retry_request_flag)
BensonLiou35178fe2024-01-11 15:28:17 +0800798#endif
799 {
800 ret = ssl_generate_random(ssl);
801 if (ret != 0) {
802 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
803 return ret;
804 }
Ronald Cron58b80382022-02-18 18:41:08 +0100805 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100806 }
807
Ronald Cron3d580bf2022-02-18 17:24:56 +0100808 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200809 * Prepare session identifier. At that point, the length of the session
810 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
811 * to zero, except in the case of a TLS 1.2 session renegotiation or
812 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100813 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800814 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100815
816#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
818 if (session_id_len < 16 || session_id_len > 32 ||
Ronald Cron021b1782022-02-19 17:32:53 +0100819#if defined(MBEDTLS_SSL_RENEGOTIATION)
820 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
821#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 ssl->handshake->resume == 0) {
Ronald Cron021b1782022-02-19 17:32:53 +0100823 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100824 }
Ronald Cron021b1782022-02-19 17:32:53 +0100825
826#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 /*
828 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
829 * generate and include a Session ID in the TLS ClientHello."
830 */
David Horstmann4a285632022-10-06 18:30:10 +0100831 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100832#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann7aee0ec2022-10-25 10:38:25 +0100834 renegotiating = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 }
Ronald Cron021b1782022-02-19 17:32:53 +0100836#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (!renegotiating) {
838 if ((session_negotiate->ticket != NULL) &&
839 (session_negotiate->ticket_len != 0)) {
Ronald Cron021b1782022-02-19 17:32:53 +0100840 session_id_len = 32;
841 }
842 }
843#endif /* MBEDTLS_SSL_SESSION_TICKETS */
844 }
845#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
846
847#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron021b1782022-02-19 17:32:53 +0100849 /*
850 * Create a legacy session identifier for the purpose of middlebox
851 * compatibility only if one has not been created already, which is
852 * the case if we are here for the TLS 1.3 second ClientHello.
853 *
854 * Versions of TLS before TLS 1.3 supported a "session resumption"
855 * feature which has been merged with pre-shared keys in TLS 1.3
856 * version. A client which has a cached session ID set by a pre-TLS 1.3
857 * server SHOULD set this field to that value. In compatibility mode,
858 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
859 * session MUST generate a new 32-byte value. This value need not be
860 * random but SHOULD be unpredictable to avoid implementations fixating
861 * on a specific value (also known as ossification). Otherwise, it MUST
862 * be set as a zero-length vector ( i.e., a zero-valued single byte
863 * length field ).
864 */
865 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100866 }
867#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (session_id_len != session_negotiate->id_len) {
Jerry Yu22c18c12022-10-11 15:58:51 +0800870 session_negotiate->id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (session_id_len > 0) {
872 ret = ssl->conf->f_rng(ssl->conf->p_rng,
873 session_negotiate->id,
874 session_id_len);
875 if (ret != 0) {
876 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
877 return ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100878 }
879 }
880 }
881
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000882#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000883 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000884 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskinee61852e2025-02-12 23:28:48 +0100885 const char *context_hostname = mbedtls_ssl_get_hostname_pointer(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
887 ssl->handshake->resume) {
Gilles Peskinee61852e2025-02-12 23:28:48 +0100888 int hostname_mismatch = context_hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000889 session_negotiate->hostname != NULL;
Gilles Peskinee61852e2025-02-12 23:28:48 +0100890 if (context_hostname != NULL && session_negotiate->hostname != NULL) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000891 hostname_mismatch = strcmp(
Gilles Peskinee61852e2025-02-12 23:28:48 +0100892 context_hostname, session_negotiate->hostname) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000893 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (hostname_mismatch) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000896 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 1, ("Hostname mismatch the session ticket, "
898 "disable session resumption."));
899 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000900 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 } else {
902 return mbedtls_ssl_session_set_hostname(session_negotiate,
Gilles Peskinee61852e2025-02-12 23:28:48 +0100903 context_hostname);
Xiaokang Qian03409292022-10-12 02:49:52 +0000904 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000905#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000906 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000907 MBEDTLS_SSL_SERVER_NAME_INDICATION */
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100910}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100911/*
912 * Write ClientHello handshake message.
913 * Handler for MBEDTLS_SSL_CLIENT_HELLO
914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100916{
917 int ret = 0;
918 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000919 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
926 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
927 &buf, &buf_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
930 buf + buf_len,
931 &msg_len,
932 &binders_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100933
Ronald Cron5f4e9122022-02-21 09:50:36 +0100934#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Ronald Cron5f4e9122022-02-21 09:50:36 +0100936 ssl->out_msglen = msg_len + 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 mbedtls_ssl_send_flight_completed(ssl);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100938
Ronald Cron8ecd9932022-03-29 12:26:54 +0200939 /*
940 * The two functions below may try to send data on the network and
941 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
942 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200943 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200944 * set the handshake state to the next state at that point to ensure
945 * that we will not write and send again a ClientHello when we
946 * eventually succeed in sending the pending data.
947 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron8ecd9932022-03-29 12:26:54 +0200949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
951 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
952 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100953 }
954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
956 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
957 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100958 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 } else
Ronald Cron5f4e9122022-02-21 09:50:36 +0100960#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
961 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000962
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100963 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
964 MBEDTLS_SSL_HS_CLIENT_HELLO,
965 msg_len);
966 if (ret != 0) {
967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
968 return ret;
969 }
970 ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
971 if (ret != 0) {
972 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
973 return ret;
974 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200975#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if (binders_len > 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000977 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000978 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 ssl, buf + msg_len - binders_len, buf + msg_len));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100980 ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100981 binders_len);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100982 if (ret != 0) {
983 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
984 return ret;
985 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000986 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200987#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
990 buf_len,
991 msg_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100992
Xiaokang Qian44051f62023-02-02 06:57:26 +0000993 /*
994 * Set next state. Note that if TLS 1.3 is proposed, this may be
Xiaokang Qian934ce6f2023-02-06 10:23:04 +0000995 * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
Xiaokang Qian44051f62023-02-02 06:57:26 +0000996 */
997 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +0000998
Xiaokang Qian44051f62023-02-02 06:57:26 +0000999#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1000 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
1001 MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001002 ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
Xiaokang Qian44051f62023-02-02 06:57:26 +00001003 }
1004#endif
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001005 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001006
Jerry Yu7cca7f62023-11-07 15:00:32 +08001007#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1008 MBEDTLS_SSL_PRINT_EXTS(
1009 3, MBEDTLS_SSL_HS_CLIENT_HELLO, ssl->handshake->sent_extensions);
1010#endif
1011
Ronald Cron3d580bf2022-02-18 17:24:56 +01001012cleanup:
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001015 return ret;
1016}
1017
1018#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1019#endif /* MBEDTLS_SSL_CLI_C */