blob: 307da0fabb8ba07ed17a5ecc7262e1ac6230d628 [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +01009
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"
Ronald Cron3d580bf2022-02-18 17:24:56 +010020#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Ronald Cronfbd9f992022-03-17 15:22:07 +010023#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020024MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010025static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
26 unsigned char *buf,
27 const unsigned char *end,
28 size_t *olen)
Ronald Cronfbd9f992022-03-17 15:22:07 +010029{
30 unsigned char *p = buf;
31 size_t hostname_len;
32
33 *olen = 0;
34
Gilles Peskine449bd832023-01-11 14:50:10 +010035 if (ssl->hostname == NULL) {
36 return 0;
37 }
Ronald Cronfbd9f992022-03-17 15:22:07 +010038
Gilles Peskine449bd832023-01-11 14:50:10 +010039 MBEDTLS_SSL_DEBUG_MSG(3,
40 ("client hello, adding server name extension: %s",
41 ssl->hostname));
Ronald Cronfbd9f992022-03-17 15:22:07 +010042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 hostname_len = strlen(ssl->hostname);
Ronald Cronfbd9f992022-03-17 15:22:07 +010044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
Ronald Cronfbd9f992022-03-17 15:22:07 +010046
47 /*
48 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
49 *
50 * In order to provide any of the server names, clients MAY include an
51 * extension of type "server_name" in the (extended) client hello. The
52 * "extension_data" field of this extension SHALL contain
53 * "ServerNameList" where:
54 *
55 * struct {
56 * NameType name_type;
57 * select (name_type) {
58 * case host_name: HostName;
59 * } name;
60 * } ServerName;
61 *
62 * enum {
63 * host_name(0), (255)
64 * } NameType;
65 *
66 * opaque HostName<1..2^16-1>;
67 *
68 * struct {
69 * ServerName server_name_list<1..2^16-1>
70 * } ServerNameList;
71 *
72 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010074 p += 2;
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010077 p += 2;
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010080 p += 2;
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
Ronald Cronfbd9f992022-03-17 15:22:07 +010083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010085 p += 2;
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 memcpy(p, ssl->hostname, hostname_len);
Ronald Cronfbd9f992022-03-17 15:22:07 +010088
89 *olen = hostname_len + 9;
90
Jerry Yu0c354a22022-08-29 15:25:36 +080091#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
Jerry Yu0c354a22022-08-29 15:25:36 +080093#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +010095}
96#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
97
Ronald Cron3d580bf2022-02-18 17:24:56 +010098#if defined(MBEDTLS_SSL_ALPN)
99/*
Ronald Cron71c23322022-02-18 17:29:39 +0100100 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100101 *
102 * Structure of the application_layer_protocol_negotiation extension in
103 * ClientHello:
104 *
105 * opaque ProtocolName<1..2^8-1>;
106 *
107 * struct {
108 * ProtocolName protocol_name_list<2..2^16-1>
109 * } ProtocolNameList;
110 *
111 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200112MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100113static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
114 unsigned char *buf,
115 const unsigned char *end,
116 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100117{
118 unsigned char *p = buf;
119
120 *out_len = 0;
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (ssl->conf->alpn_list == NULL) {
123 return 0;
124 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100127
128
129 /* Check we have enough space for the extension type (2 bytes), the
130 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
131 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
133 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100134 /* Skip writing extension and list length for now */
135 p += 6;
136
137 /*
138 * opaque ProtocolName<1..2^8-1>;
139 *
140 * struct {
141 * ProtocolName protocol_name_list<2..2^16-1>
142 * } ProtocolNameList;
143 */
Gilles Peskinec4949d12025-05-27 19:45:29 +0200144 for (const char *const *cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100145 /*
146 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
147 * protocol names is less than 255.
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 size_t protocol_name_len = strlen(*cur);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + protocol_name_len);
152 *p++ = (unsigned char) protocol_name_len;
153 memcpy(p, *cur, protocol_name_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100154 p += protocol_name_len;
155 }
156
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000157 *out_len = (size_t) (p - buf);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100158
159 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100161
162 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100164
Jerry Yu0c354a22022-08-29 15:25:36 +0800165#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yu0c354a22022-08-29 15:25:36 +0800167#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100169}
170#endif /* MBEDTLS_SSL_ALPN */
171
Przemek Stekiel98d79332023-06-26 12:44:33 +0200172#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
173 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100174/*
175 * Function for writing a supported groups (TLS 1.3) or supported elliptic
176 * curves (TLS 1.2) extension.
177 *
178 * The "extension_data" field of a supported groups extension contains a
179 * "NamedGroupList" value (TLS 1.3 RFC8446):
180 * enum {
181 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
182 * x25519(0x001D), x448(0x001E),
183 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
184 * ffdhe6144(0x0103), ffdhe8192(0x0104),
185 * ffdhe_private_use(0x01FC..0x01FF),
186 * ecdhe_private_use(0xFE00..0xFEFF),
187 * (0xFFFF)
188 * } NamedGroup;
189 * struct {
190 * NamedGroup named_group_list<2..2^16-1>;
191 * } NamedGroupList;
192 *
193 * The "extension_data" field of a supported elliptic curves extension contains
194 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
195 * enum {
196 * deprecated(1..22),
197 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
198 * x25519(29), x448(30),
199 * reserved (0xFE00..0xFEFF),
200 * deprecated(0xFF01..0xFF02),
201 * (0xFFFF)
202 * } NamedCurve;
203 * struct {
204 * NamedCurve named_curve_list<2..2^16-1>
205 * } NamedCurveList;
206 *
207 * The TLS 1.3 supported groups extension was defined to be a compatible
208 * generalization of the TLS 1.2 supported elliptic curves extension. They both
209 * share the same extension identifier.
210 *
Ronald Cronfbd9f992022-03-17 15:22:07 +0100211 */
Ronald Cron1ffa4502023-06-30 14:56:38 +0200212#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG 1
213#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG 2
214
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
217 unsigned char *buf,
218 const unsigned char *end,
Ronald Cron1ffa4502023-06-30 14:56:38 +0200219 int flags,
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 size_t *out_len)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100221{
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 unsigned char *p = buf;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100223 unsigned char *named_group_list; /* Start of named_group_list */
224 size_t named_group_list_len; /* Length of named_group_list */
Manuel Pégourié-Gonnard6402c352025-01-14 12:23:56 +0100225 const uint16_t *group_list = ssl->conf->group_list;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100226
227 *out_len = 0;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100230
231 /* Check if we have space for header and length fields:
232 * - extension_type (2 bytes)
233 * - extension_data_length (2 bytes)
234 * - named_group_list_length (2 bytes)
235 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100237 p += 6;
238
239 named_group_list = p;
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (group_list == NULL) {
242 return MBEDTLS_ERR_SSL_BAD_CONFIG;
243 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 for (; *group_list != 0; group_list++) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200246 int propose_group = 0;
247
mcagriaksoyd9f22802023-09-13 22:42:19 +0200248 MBEDTLS_SSL_DEBUG_MSG(3, ("got supported group(%04x)", *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100249
Ronald Cron1ffa4502023-06-30 14:56:38 +0200250#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
251 if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
252#if defined(PSA_WANT_ALG_ECDH)
253 if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list) &&
254 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
255 MBEDTLS_ECP_DP_NONE)) {
256 propose_group = 1;
Valerio Setti18c9fed2022-12-30 17:44:24 +0100257 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200258#endif
259#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiele80bbf42023-07-05 10:34:40 +0200260 if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200261 propose_group = 1;
262 }
263#endif
264 }
265#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
266
267#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
268 if ((flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG) &&
269 mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list) &&
270 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
271 MBEDTLS_ECP_DP_NONE)) {
272 propose_group = 1;
273 }
274#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC */
275
276 if (propose_group) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
278 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100279 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
Ronald Cron1ffa4502023-06-30 14:56:38 +0200281 mbedtls_ssl_named_group_to_str(*group_list),
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100283 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100284 }
285
286 /* Length of named_group_list */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000287 named_group_list_len = (size_t) (p - named_group_list);
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (named_group_list_len == 0) {
289 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
290 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100291 }
292
293 /* Write extension_type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100295 /* Write extension_data_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100297 /* Write length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
301 buf + 4, named_group_list_len + 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100302
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000303 *out_len = (size_t) (p - buf);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100304
305#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800306 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100308#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100311}
Przemek Stekiel98d79332023-06-26 12:44:33 +0200312#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
313 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100314
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100316static int ssl_write_client_hello_cipher_suites(
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_ssl_context *ssl,
318 unsigned char *buf,
319 unsigned char *end,
320 int *tls12_uses_ec,
321 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100322{
323 unsigned char *p = buf;
324 const int *ciphersuite_list;
325 unsigned char *cipher_suites; /* Start of the cipher_suites list */
326 size_t cipher_suites_len;
327
Ronald Crond491c2d2022-02-19 18:30:46 +0100328 *tls12_uses_ec = 0;
329 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100330
331 /*
332 * Ciphersuite list
333 *
334 * This is a list of the symmetric cipher options supported by
335 * the client, specifically the record protection algorithm
336 * ( including secret key length ) and a hash to be used with
337 * HKDF, in descending order of client preference.
338 */
339 ciphersuite_list = ssl->conf->ciphersuite_list;
340
341 /* Check there is space for the cipher suite list length (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100343 p += 2;
344
Ronald Cron64767262022-03-31 14:13:57 +0200345 /* Write cipher_suites
346 * CipherSuite cipher_suites<2..2^16-2>;
347 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100348 cipher_suites = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100350 int cipher_suite = ciphersuite_list[i];
351 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Ronald Crond491c2d2022-02-19 18:30:46 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
356 ssl->handshake->min_tls_version,
357 ssl->tls_version) != 0) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100358 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100360
361#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Valerio Setti7aeec542023-07-05 18:57:21 +0200362 (defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200363 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
365 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
Ronald Crond491c2d2022-02-19 18:30:46 +0100366#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
369 (unsigned int) cipher_suite,
370 ciphersuite_info->name));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100371
372 /* Check there is space for the cipher suite identifier (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
374 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100375 p += 2;
376 }
377
Ronald Crond491c2d2022-02-19 18:30:46 +0100378 /*
379 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
380 */
David Horstmann4a285632022-10-06 18:30:10 +0100381 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100382#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
Ronald Crond491c2d2022-02-19 18:30:46 +0100384#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (!renegotiating) {
386 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
387 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
388 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
Ronald Crond491c2d2022-02-19 18:30:46 +0100389 p += 2;
390 }
391
Ronald Cron3d580bf2022-02-18 17:24:56 +0100392 /* Write the cipher_suites length in number of bytes */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000393 cipher_suites_len = (size_t) (p - cipher_suites);
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
395 MBEDTLS_SSL_DEBUG_MSG(3,
396 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
397 cipher_suites_len/2));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100398
399 /* Output the total length of cipher_suites field. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000400 *out_len = (size_t) (p - buf);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100403}
404
405/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100406 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100407 *
408 * struct {
409 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
410 * Random random;
411 * opaque legacy_session_id<0..32>;
412 * CipherSuite cipher_suites<2..2^16-2>;
413 * opaque legacy_compression_methods<1..2^8-1>;
414 * Extension extensions<8..2^16-1>;
415 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100416 *
417 * Structure of the (D)TLS 1.2 ClientHello message:
418 *
419 * struct {
420 * ProtocolVersion client_version;
421 * Random random;
422 * SessionID session_id;
423 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
424 * CipherSuite cipher_suites<2..2^16-2>;
425 * CompressionMethod compression_methods<1..2^8-1>;
426 * select (extensions_present) {
427 * case false:
428 * struct {};
429 * case true:
430 * Extension extensions<0..2^16-1>;
431 * };
432 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100433 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200434MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
436 unsigned char *buf,
437 unsigned char *end,
438 size_t *out_len,
439 size_t *binders_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100440{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100441 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100442 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100443 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100444 unsigned char *p_extensions_len; /* Pointer to extensions length */
445 size_t output_len; /* Length of buffer used by function */
446 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100447 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100448
Ronald Cron3d580bf2022-02-18 17:24:56 +0100449 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000450 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100451
Ronald Cron4079abc2022-02-20 10:35:26 +0100452#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200453 unsigned char propose_tls12 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200455 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100457#endif
458#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200459 unsigned char propose_tls13 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200461 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100463#endif
464
Ronald Cron3d580bf2022-02-18 17:24:56 +0100465 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100466 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100467 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100468 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100469 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
471 mbedtls_ssl_write_version(p, ssl->conf->transport,
472 MBEDTLS_SSL_VERSION_TLS1_2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100473 p += 2;
474
Ronald Cron58b80382022-02-18 18:41:08 +0100475 /* ...
476 * Random random;
477 * ...
478 *
Ronald Cron58b80382022-02-18 18:41:08 +0100479 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100480 * the handshake->randbytes buffer and are copied here into the output
481 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
484 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
485 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
486 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100487 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
488
Ronald Cron021b1782022-02-19 17:32:53 +0100489 /* TLS 1.2:
490 * ...
491 * SessionID session_id;
492 * ...
493 * with
494 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100495 *
Ronald Cron021b1782022-02-19 17:32:53 +0100496 * TLS 1.3:
497 * ...
498 * opaque legacy_session_id<0..32>;
499 * ...
500 *
Ronald Cronda41b382022-03-30 09:57:11 +0200501 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100502 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
503 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100504 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
506 *p++ = (unsigned char) ssl->session_negotiate->id_len;
507 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100508 p += ssl->session_negotiate->id_len;
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
511 ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100512
Ronald Crona874aa82022-02-19 18:11:26 +0100513 /* DTLS 1.2 ONLY
514 * ...
515 * opaque cookie<0..2^8-1>;
516 * ...
517 */
518#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yue01304f2022-04-07 10:51:55 +0800520#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
521 uint8_t cookie_len = 0;
522#else
523 uint16_t cookie_len = 0;
524#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (handshake->cookie != NULL) {
527 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
528 handshake->cookie,
529 handshake->cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800530 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100531 }
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
534 *p++ = (unsigned char) cookie_len;
535 if (cookie_len > 0) {
536 memcpy(p, handshake->cookie, cookie_len);
Ronald Crona874aa82022-02-19 18:11:26 +0100537 p += cookie_len;
538 }
539 }
540#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
541
Ronald Cron3d580bf2022-02-18 17:24:56 +0100542 /* Write cipher_suites */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
544 &tls12_uses_ec,
545 &output_len);
546 if (ret != 0) {
547 return ret;
548 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100549 p += output_len;
550
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100551 /* Write legacy_compression_methods (TLS 1.3) or
552 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100553 *
554 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
555 * one byte set to zero, which corresponds to the 'null' compression
556 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100557 *
558 * For TLS 1.2 ClientHello, for security reasons we do not support
559 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100562 *p++ = 1;
563 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
564
565 /* Write extensions */
566
567#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
568 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800569 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100570#endif
571
572 /* First write extensions, then the total length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100574 p_extensions_len = p;
575 p += 2;
576
Ronald Crondf823bf2022-03-29 18:57:54 +0200577#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
578 /* Write server name extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
580 if (ret != 0) {
581 return ret;
582 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100583 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200584#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100585
586#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
588 if (ret != 0) {
589 return ret;
590 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100591 p += output_len;
592#endif /* MBEDTLS_SSL_ALPN */
593
594#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (propose_tls13) {
596 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
597 &output_len);
598 if (ret != 0) {
599 return ret;
600 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100601 p += output_len;
602 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200603#endif
604
Przemek Stekiel76669452023-06-26 17:34:36 +0200605#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
606 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cron1ffa4502023-06-30 14:56:38 +0200607 {
608 int ssl_write_supported_groups_ext_flags = 0;
609
610#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +0800611 if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200612 ssl_write_supported_groups_ext_flags |=
613 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG;
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200615#endif
616#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
617 if (propose_tls12 && tls12_uses_ec) {
618 ssl_write_supported_groups_ext_flags |=
619 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG;
620 }
621#endif
622 if (ssl_write_supported_groups_ext_flags != 0) {
623 ret = ssl_write_supported_groups_ext(ssl, p, end,
624 ssl_write_supported_groups_ext_flags,
625 &output_len);
626 if (ret != 0) {
627 return ret;
628 }
629 p += output_len;
630 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100631 }
Przemek Stekiel76669452023-06-26 17:34:36 +0200632#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
633 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100634
Ronald Crone68ab4f2022-10-05 12:46:29 +0200635#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100636 int write_sig_alg_ext = 0;
Ronald Cron4079abc2022-02-20 10:35:26 +0100637#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Dave Rodgmana11eac42023-09-14 16:16:04 +0100638 write_sig_alg_ext = write_sig_alg_ext ||
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +0800639 (propose_tls13 && mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl));
Ronald Cron4079abc2022-02-20 10:35:26 +0100640#endif
641#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100642 write_sig_alg_ext = write_sig_alg_ext || propose_tls12;
Ronald Cron4079abc2022-02-20 10:35:26 +0100643#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100644
645 if (write_sig_alg_ext) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
647 if (ret != 0) {
648 return ret;
649 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100650 p += output_len;
651 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200652#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100653
Ronald Cron4079abc2022-02-20 10:35:26 +0100654#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (propose_tls12) {
656 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
657 tls12_uses_ec,
658 &output_len);
659 if (ret != 0) {
660 return ret;
661 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100662 p += output_len;
663 }
664#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100665
Ronald Cron41a443a2022-10-04 16:38:25 +0200666#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000667 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
668 * MUST be the last extension in the ClientHello.
669 */
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +0800670 if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl)) {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000671 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ssl, p, end, &output_len, binders_len);
673 if (ret != 0) {
674 return ret;
675 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000676 p += output_len;
677 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200678#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000679
Ronald Cron3d580bf2022-02-18 17:24:56 +0100680 /* Write the length of the list of extensions. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000681 extensions_len = (size_t) (p - p_extensions_len) - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (extensions_len == 0) {
684 p = p_extensions_len;
685 } else {
686 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
687 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
688 MBEDTLS_PRINTF_SIZET, extensions_len));
689 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
690 p_extensions_len, extensions_len);
Ronald Cron4079abc2022-02-20 10:35:26 +0100691 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100692
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000693 *out_len = (size_t) (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100695}
696
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100698static int ssl_generate_random(mbedtls_ssl_context *ssl)
Ronald Cron58b80382022-02-18 18:41:08 +0100699{
700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
701 unsigned char *randbytes = ssl->handshake->randbytes;
702 size_t gmt_unix_time_len = 0;
703
704 /*
705 * Generate the random bytes
706 *
707 * TLS 1.2 case:
708 * struct {
709 * uint32 gmt_unix_time;
710 * opaque random_bytes[28];
711 * } Random;
712 *
713 * TLS 1.3 case:
714 * opaque Random[32];
715 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Ronald Cron58b80382022-02-18 18:41:08 +0100717#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
719 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
Ronald Cron58b80382022-02-18 18:41:08 +0100720 gmt_unix_time_len = 4;
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 MBEDTLS_SSL_DEBUG_MSG(3,
723 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
724 (long long) gmt_unix_time));
Ronald Cron58b80382022-02-18 18:41:08 +0100725#endif /* MBEDTLS_HAVE_TIME */
726 }
727
Ben Taylor602b2962025-03-07 15:52:50 +0000728 ret = psa_generate_random(randbytes + gmt_unix_time_len,
Ben Taylor1cd1e012025-03-18 11:50:39 +0000729 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100731}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000732
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200733MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100734static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100735{
736 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100737 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800738 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (session_negotiate == NULL) {
741 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
742 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100743
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800744#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
745 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
746 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800747
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800748 /* Check if a tls13 ticket has been configured. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (ssl->handshake->resume != 0 &&
Jerry Yu22c18c12022-10-11 15:58:51 +0800750 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 session_negotiate->ticket != NULL) {
Jerry Yucebffc32022-12-15 18:00:05 +0800752 mbedtls_ms_time_t now = mbedtls_ms_time();
Jerry Yu342a5552023-11-10 14:23:39 +0800753 mbedtls_ms_time_t age = now - session_negotiate->ticket_reception_time;
David Horstmann47490072023-12-06 17:22:53 +0000754 if (age < 0 ||
755 age > (mbedtls_ms_time_t) session_negotiate->ticket_lifetime * 1000) {
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800756 /* Without valid ticket, disable session resumption.*/
757 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 3, ("Ticket expired, disable session resumption"));
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800759 ssl->handshake->resume = 0;
760 }
761 }
762#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
763 MBEDTLS_SSL_SESSION_TICKETS &&
764 MBEDTLS_HAVE_TIME */
765
Ronald Cron86a477f2022-02-18 17:45:10 +0100766 /* Bet on the highest configured version if we are not in a TLS 1.2
767 * renegotiation or session resumption.
768 */
769#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Glenn Strausscd78df62022-04-07 19:07:11 -0400771 ssl->handshake->min_tls_version = ssl->tls_version;
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 } else
Ronald Cron86a477f2022-02-18 17:45:10 +0100773#endif
774 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (ssl->handshake->resume) {
776 ssl->tls_version = session_negotiate->tls_version;
777 ssl->handshake->min_tls_version = ssl->tls_version;
778 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100780 }
781 }
782
Ronald Cron58b80382022-02-18 18:41:08 +0100783 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200784 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000785 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200786 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100787 */
788#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
790 (ssl->handshake->cookie == NULL))
Ronald Cron58b80382022-02-18 18:41:08 +0100791#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100792 {
BensonLiou35178fe2024-01-11 15:28:17 +0800793#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine93b305d2024-03-14 15:05:09 +0100794 if (!ssl->handshake->hello_retry_request_flag)
BensonLiou35178fe2024-01-11 15:28:17 +0800795#endif
796 {
797 ret = ssl_generate_random(ssl);
798 if (ret != 0) {
799 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
800 return ret;
801 }
Ronald Cron58b80382022-02-18 18:41:08 +0100802 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100803 }
804
Ronald Cron3d580bf2022-02-18 17:24:56 +0100805 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200806 * Prepare session identifier. At that point, the length of the session
807 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
808 * to zero, except in the case of a TLS 1.2 session renegotiation or
809 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100810 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800811 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100812
813#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
815 if (session_id_len < 16 || session_id_len > 32 ||
Ronald Cron021b1782022-02-19 17:32:53 +0100816#if defined(MBEDTLS_SSL_RENEGOTIATION)
817 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
818#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 ssl->handshake->resume == 0) {
Ronald Cron021b1782022-02-19 17:32:53 +0100820 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100821 }
Ronald Cron021b1782022-02-19 17:32:53 +0100822
823#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 /*
825 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
826 * generate and include a Session ID in the TLS ClientHello."
827 */
David Horstmann4a285632022-10-06 18:30:10 +0100828 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100829#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann7aee0ec2022-10-25 10:38:25 +0100831 renegotiating = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 }
Ronald Cron021b1782022-02-19 17:32:53 +0100833#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (!renegotiating) {
835 if ((session_negotiate->ticket != NULL) &&
836 (session_negotiate->ticket_len != 0)) {
Ronald Cron021b1782022-02-19 17:32:53 +0100837 session_id_len = 32;
838 }
839 }
840#endif /* MBEDTLS_SSL_SESSION_TICKETS */
841 }
842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
843
844#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron021b1782022-02-19 17:32:53 +0100846 /*
847 * Create a legacy session identifier for the purpose of middlebox
848 * compatibility only if one has not been created already, which is
849 * the case if we are here for the TLS 1.3 second ClientHello.
850 *
851 * Versions of TLS before TLS 1.3 supported a "session resumption"
852 * feature which has been merged with pre-shared keys in TLS 1.3
853 * version. A client which has a cached session ID set by a pre-TLS 1.3
854 * server SHOULD set this field to that value. In compatibility mode,
855 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
856 * session MUST generate a new 32-byte value. This value need not be
857 * random but SHOULD be unpredictable to avoid implementations fixating
858 * on a specific value (also known as ossification). Otherwise, it MUST
859 * be set as a zero-length vector ( i.e., a zero-valued single byte
860 * length field ).
861 */
862 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100863 }
864#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (session_id_len != session_negotiate->id_len) {
Jerry Yu22c18c12022-10-11 15:58:51 +0800867 session_negotiate->id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (session_id_len > 0) {
Ben Taylor602b2962025-03-07 15:52:50 +0000869
870 ret = psa_generate_random(session_negotiate->id,
Ben Taylor1cd1e012025-03-18 11:50:39 +0000871 session_id_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (ret != 0) {
873 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
874 return ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100875 }
876 }
877 }
878
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000879#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000880 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000881 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
883 ssl->handshake->resume) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000884 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000885 session_negotiate->hostname != NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (ssl->hostname != NULL && session_negotiate->hostname != NULL) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000887 hostname_mismatch = strcmp(
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 ssl->hostname, session_negotiate->hostname) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000889 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (hostname_mismatch) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000892 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 1, ("Hostname mismatch the session ticket, "
894 "disable session resumption."));
895 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000896 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 } else {
898 return mbedtls_ssl_session_set_hostname(session_negotiate,
899 ssl->hostname);
Xiaokang Qian03409292022-10-12 02:49:52 +0000900 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000901#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000902 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000903 MBEDTLS_SSL_SERVER_NAME_INDICATION */
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100906}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100907/*
908 * Write ClientHello handshake message.
909 * Handler for MBEDTLS_SSL_CLIENT_HELLO
910 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100912{
913 int ret = 0;
914 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000915 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
922 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
923 &buf, &buf_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
926 buf + buf_len,
927 &msg_len,
928 &binders_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100929
Ronald Cron5f4e9122022-02-21 09:50:36 +0100930#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Ronald Cron5f4e9122022-02-21 09:50:36 +0100932 ssl->out_msglen = msg_len + 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_ssl_send_flight_completed(ssl);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100934
Ronald Cron8ecd9932022-03-29 12:26:54 +0200935 /*
936 * The two functions below may try to send data on the network and
937 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
938 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200939 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200940 * set the handshake state to the next state at that point to ensure
941 * that we will not write and send again a ClientHello when we
942 * eventually succeed in sending the pending data.
943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron8ecd9932022-03-29 12:26:54 +0200945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
947 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
948 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100949 }
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
952 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
953 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100954 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 } else
Ronald Cron5f4e9122022-02-21 09:50:36 +0100956#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
957 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000958
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100959 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
960 MBEDTLS_SSL_HS_CLIENT_HELLO,
961 msg_len);
962 if (ret != 0) {
963 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
964 return ret;
965 }
966 ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
967 if (ret != 0) {
968 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
969 return ret;
970 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200971#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (binders_len > 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000973 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000974 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 ssl, buf + msg_len - binders_len, buf + msg_len));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100976 ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100977 binders_len);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100978 if (ret != 0) {
979 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
980 return ret;
981 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000982 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200983#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
986 buf_len,
987 msg_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100988
Xiaokang Qian44051f62023-02-02 06:57:26 +0000989 /*
990 * Set next state. Note that if TLS 1.3 is proposed, this may be
Xiaokang Qian934ce6f2023-02-06 10:23:04 +0000991 * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
Xiaokang Qian44051f62023-02-02 06:57:26 +0000992 */
993 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +0000994
Xiaokang Qian44051f62023-02-02 06:57:26 +0000995#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
996 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
997 MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
Xiaokang Qian934ce6f2023-02-06 10:23:04 +0000998 ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
Xiaokang Qian44051f62023-02-02 06:57:26 +0000999 }
1000#endif
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001001 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001002
Jerry Yu7cca7f62023-11-07 15:00:32 +08001003#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1004 MBEDTLS_SSL_PRINT_EXTS(
1005 3, MBEDTLS_SSL_HS_CLIENT_HELLO, ssl->handshake->sent_extensions);
1006#endif
1007
Ronald Cron3d580bf2022-02-18 17:24:56 +01001008cleanup:
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001011 return ret;
1012}
1013
1014#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1015#endif /* MBEDTLS_SSL_CLI_C */