blob: 270db416831bc77cadfe0a0e925ea49eac79d1be [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
15#include "mbedtls/debug.h"
16#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;
32 size_t hostname_len;
33
34 *olen = 0;
35
Gilles Peskine449bd832023-01-11 14:50:10 +010036 if (ssl->hostname == NULL) {
37 return 0;
38 }
Ronald Cronfbd9f992022-03-17 15:22:07 +010039
Gilles Peskine449bd832023-01-11 14:50:10 +010040 MBEDTLS_SSL_DEBUG_MSG(3,
41 ("client hello, adding server name extension: %s",
42 ssl->hostname));
Ronald Cronfbd9f992022-03-17 15:22:07 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 hostname_len = strlen(ssl->hostname);
Ronald Cronfbd9f992022-03-17 15:22:07 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
Ronald Cronfbd9f992022-03-17 15:22:07 +010047
48 /*
49 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
50 *
51 * In order to provide any of the server names, clients MAY include an
52 * extension of type "server_name" in the (extended) client hello. The
53 * "extension_data" field of this extension SHALL contain
54 * "ServerNameList" where:
55 *
56 * struct {
57 * NameType name_type;
58 * select (name_type) {
59 * case host_name: HostName;
60 * } name;
61 * } ServerName;
62 *
63 * enum {
64 * host_name(0), (255)
65 * } NameType;
66 *
67 * opaque HostName<1..2^16-1>;
68 *
69 * struct {
70 * ServerName server_name_list<1..2^16-1>
71 * } ServerNameList;
72 *
73 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010075 p += 2;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010078 p += 2;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010081 p += 2;
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
Ronald Cronfbd9f992022-03-17 15:22:07 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010086 p += 2;
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 memcpy(p, ssl->hostname, hostname_len);
Ronald Cronfbd9f992022-03-17 15:22:07 +010089
90 *olen = hostname_len + 9;
91
Jerry Yu0c354a22022-08-29 15:25:36 +080092#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +010093 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
Jerry Yu0c354a22022-08-29 15:25:36 +080094#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +010096}
97#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
98
Ronald Cron3d580bf2022-02-18 17:24:56 +010099#if defined(MBEDTLS_SSL_ALPN)
100/*
Ronald Cron71c23322022-02-18 17:29:39 +0100101 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100102 *
103 * Structure of the application_layer_protocol_negotiation extension in
104 * ClientHello:
105 *
106 * opaque ProtocolName<1..2^8-1>;
107 *
108 * struct {
109 * ProtocolName protocol_name_list<2..2^16-1>
110 * } ProtocolNameList;
111 *
112 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200113MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100114static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
115 unsigned char *buf,
116 const unsigned char *end,
117 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100118{
119 unsigned char *p = buf;
120
121 *out_len = 0;
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (ssl->conf->alpn_list == NULL) {
124 return 0;
125 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100128
129
130 /* Check we have enough space for the extension type (2 bytes), the
131 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
134 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100135 /* Skip writing extension and list length for now */
136 p += 6;
137
138 /*
139 * opaque ProtocolName<1..2^8-1>;
140 *
141 * struct {
142 * ProtocolName protocol_name_list<2..2^16-1>
143 * } ProtocolNameList;
144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100146 /*
147 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
148 * protocol names is less than 255.
149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 size_t protocol_name_len = strlen(*cur);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + protocol_name_len);
153 *p++ = (unsigned char) protocol_name_len;
154 memcpy(p, *cur, protocol_name_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100155 p += protocol_name_len;
156 }
157
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000158 *out_len = (size_t) (p - buf);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100159
160 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100162
163 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100165
Jerry Yu0c354a22022-08-29 15:25:36 +0800166#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yu0c354a22022-08-29 15:25:36 +0800168#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100170}
171#endif /* MBEDTLS_SSL_ALPN */
172
Przemek Stekiel98d79332023-06-26 12:44:33 +0200173#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
174 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100175/*
176 * Function for writing a supported groups (TLS 1.3) or supported elliptic
177 * curves (TLS 1.2) extension.
178 *
179 * The "extension_data" field of a supported groups extension contains a
180 * "NamedGroupList" value (TLS 1.3 RFC8446):
181 * enum {
182 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
183 * x25519(0x001D), x448(0x001E),
184 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
185 * ffdhe6144(0x0103), ffdhe8192(0x0104),
186 * ffdhe_private_use(0x01FC..0x01FF),
187 * ecdhe_private_use(0xFE00..0xFEFF),
188 * (0xFFFF)
189 * } NamedGroup;
190 * struct {
191 * NamedGroup named_group_list<2..2^16-1>;
192 * } NamedGroupList;
193 *
194 * The "extension_data" field of a supported elliptic curves extension contains
195 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
196 * enum {
197 * deprecated(1..22),
198 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
199 * x25519(29), x448(30),
200 * reserved (0xFE00..0xFEFF),
201 * deprecated(0xFF01..0xFF02),
202 * (0xFFFF)
203 * } NamedCurve;
204 * struct {
205 * NamedCurve named_curve_list<2..2^16-1>
206 * } NamedCurveList;
207 *
208 * The TLS 1.3 supported groups extension was defined to be a compatible
209 * generalization of the TLS 1.2 supported elliptic curves extension. They both
210 * share the same extension identifier.
211 *
Ronald Cronfbd9f992022-03-17 15:22:07 +0100212 */
Ronald Cron1ffa4502023-06-30 14:56:38 +0200213#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG 1
214#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG 2
215
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200216MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100217static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
218 unsigned char *buf,
219 const unsigned char *end,
Ronald Cron1ffa4502023-06-30 14:56:38 +0200220 int flags,
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 size_t *out_len)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100222{
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 unsigned char *p = buf;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100224 unsigned char *named_group_list; /* Start of named_group_list */
225 size_t named_group_list_len; /* Length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100227
228 *out_len = 0;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100231
232 /* Check if we have space for header and length fields:
233 * - extension_type (2 bytes)
234 * - extension_data_length (2 bytes)
235 * - named_group_list_length (2 bytes)
236 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100238 p += 6;
239
240 named_group_list = p;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (group_list == NULL) {
243 return MBEDTLS_ERR_SSL_BAD_CONFIG;
244 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 for (; *group_list != 0; group_list++) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200247 int propose_group = 0;
248
mcagriaksoyd9f22802023-09-13 22:42:19 +0200249 MBEDTLS_SSL_DEBUG_MSG(3, ("got supported group(%04x)", *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100250
Ronald Cron1ffa4502023-06-30 14:56:38 +0200251#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
252 if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
253#if defined(PSA_WANT_ALG_ECDH)
254 if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list) &&
255 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
256 MBEDTLS_ECP_DP_NONE)) {
257 propose_group = 1;
Valerio Setti18c9fed2022-12-30 17:44:24 +0100258 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200259#endif
260#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiele80bbf42023-07-05 10:34:40 +0200261 if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
Ronald Cron1ffa4502023-06-30 14:56:38 +0200262 propose_group = 1;
263 }
264#endif
265 }
266#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
267
268#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
269 if ((flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG) &&
270 mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list) &&
271 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
272 MBEDTLS_ECP_DP_NONE)) {
273 propose_group = 1;
274 }
275#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC */
276
277 if (propose_group) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
279 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100280 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
Ronald Cron1ffa4502023-06-30 14:56:38 +0200282 mbedtls_ssl_named_group_to_str(*group_list),
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100284 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100285 }
286
287 /* Length of named_group_list */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000288 named_group_list_len = (size_t) (p - named_group_list);
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (named_group_list_len == 0) {
290 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
291 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100292 }
293
294 /* Write extension_type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100296 /* Write extension_data_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100298 /* Write length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
302 buf + 4, named_group_list_len + 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100303
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000304 *out_len = (size_t) (p - buf);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100305
306#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800307 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100309#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100312}
Przemek Stekiel98d79332023-06-26 12:44:33 +0200313#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
314 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100315
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200316MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100317static int ssl_write_client_hello_cipher_suites(
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 mbedtls_ssl_context *ssl,
319 unsigned char *buf,
320 unsigned char *end,
321 int *tls12_uses_ec,
322 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100323{
324 unsigned char *p = buf;
325 const int *ciphersuite_list;
326 unsigned char *cipher_suites; /* Start of the cipher_suites list */
327 size_t cipher_suites_len;
328
Ronald Crond491c2d2022-02-19 18:30:46 +0100329 *tls12_uses_ec = 0;
330 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100331
332 /*
333 * Ciphersuite list
334 *
335 * This is a list of the symmetric cipher options supported by
336 * the client, specifically the record protection algorithm
337 * ( including secret key length ) and a hash to be used with
338 * HKDF, in descending order of client preference.
339 */
340 ciphersuite_list = ssl->conf->ciphersuite_list;
341
342 /* Check there is space for the cipher suite list length (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100344 p += 2;
345
Ronald Cron64767262022-03-31 14:13:57 +0200346 /* Write cipher_suites
347 * CipherSuite cipher_suites<2..2^16-2>;
348 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100349 cipher_suites = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100351 int cipher_suite = ciphersuite_list[i];
352 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Ronald Crond491c2d2022-02-19 18:30:46 +0100355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
357 ssl->handshake->min_tls_version,
358 ssl->tls_version) != 0) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100359 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100361
362#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Valerio Setti7aeec542023-07-05 18:57:21 +0200363 (defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
Valerio Settie9646ec2023-08-02 20:02:28 +0200364 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
366 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
Ronald Crond491c2d2022-02-19 18:30:46 +0100367#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
370 (unsigned int) cipher_suite,
371 ciphersuite_info->name));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100372
373 /* Check there is space for the cipher suite identifier (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
375 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100376 p += 2;
377 }
378
Ronald Crond491c2d2022-02-19 18:30:46 +0100379 /*
380 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
381 */
David Horstmann4a285632022-10-06 18:30:10 +0100382 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100383#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
Ronald Crond491c2d2022-02-19 18:30:46 +0100385#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (!renegotiating) {
387 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
388 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
389 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
Ronald Crond491c2d2022-02-19 18:30:46 +0100390 p += 2;
391 }
392
Ronald Cron3d580bf2022-02-18 17:24:56 +0100393 /* Write the cipher_suites length in number of bytes */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000394 cipher_suites_len = (size_t) (p - cipher_suites);
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
396 MBEDTLS_SSL_DEBUG_MSG(3,
397 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
398 cipher_suites_len/2));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100399
400 /* Output the total length of cipher_suites field. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000401 *out_len = (size_t) (p - buf);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100404}
405
406/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100407 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100408 *
409 * struct {
410 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
411 * Random random;
412 * opaque legacy_session_id<0..32>;
413 * CipherSuite cipher_suites<2..2^16-2>;
414 * opaque legacy_compression_methods<1..2^8-1>;
415 * Extension extensions<8..2^16-1>;
416 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100417 *
418 * Structure of the (D)TLS 1.2 ClientHello message:
419 *
420 * struct {
421 * ProtocolVersion client_version;
422 * Random random;
423 * SessionID session_id;
424 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
425 * CipherSuite cipher_suites<2..2^16-2>;
426 * CompressionMethod compression_methods<1..2^8-1>;
427 * select (extensions_present) {
428 * case false:
429 * struct {};
430 * case true:
431 * Extension extensions<0..2^16-1>;
432 * };
433 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100434 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200435MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100436static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
437 unsigned char *buf,
438 unsigned char *end,
439 size_t *out_len,
440 size_t *binders_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100441{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100442 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100443 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100444 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100445 unsigned char *p_extensions_len; /* Pointer to extensions length */
446 size_t output_len; /* Length of buffer used by function */
447 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100448 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100449
Ronald Cron3d580bf2022-02-18 17:24:56 +0100450 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000451 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100452
Ronald Cron4079abc2022-02-20 10:35:26 +0100453#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200454 unsigned char propose_tls12 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200456 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100458#endif
459#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200460 unsigned char propose_tls13 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200462 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100464#endif
465
Ronald Cron3d580bf2022-02-18 17:24:56 +0100466 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100467 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100468 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100469 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100470 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
472 mbedtls_ssl_write_version(p, ssl->conf->transport,
473 MBEDTLS_SSL_VERSION_TLS1_2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100474 p += 2;
475
Ronald Cron58b80382022-02-18 18:41:08 +0100476 /* ...
477 * Random random;
478 * ...
479 *
Ronald Cron58b80382022-02-18 18:41:08 +0100480 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100481 * the handshake->randbytes buffer and are copied here into the output
482 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100483 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
485 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
486 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
487 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100488 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
489
Ronald Cron021b1782022-02-19 17:32:53 +0100490 /* TLS 1.2:
491 * ...
492 * SessionID session_id;
493 * ...
494 * with
495 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100496 *
Ronald Cron021b1782022-02-19 17:32:53 +0100497 * TLS 1.3:
498 * ...
499 * opaque legacy_session_id<0..32>;
500 * ...
501 *
Ronald Cronda41b382022-03-30 09:57:11 +0200502 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100503 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
504 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100505 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
507 *p++ = (unsigned char) ssl->session_negotiate->id_len;
508 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100509 p += ssl->session_negotiate->id_len;
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
512 ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100513
Ronald Crona874aa82022-02-19 18:11:26 +0100514 /* DTLS 1.2 ONLY
515 * ...
516 * opaque cookie<0..2^8-1>;
517 * ...
518 */
519#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yue01304f2022-04-07 10:51:55 +0800521#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
522 uint8_t cookie_len = 0;
523#else
524 uint16_t cookie_len = 0;
525#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (handshake->cookie != NULL) {
528 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
529 handshake->cookie,
530 handshake->cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800531 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100532 }
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
535 *p++ = (unsigned char) cookie_len;
536 if (cookie_len > 0) {
537 memcpy(p, handshake->cookie, cookie_len);
Ronald Crona874aa82022-02-19 18:11:26 +0100538 p += cookie_len;
539 }
540 }
541#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
542
Ronald Cron3d580bf2022-02-18 17:24:56 +0100543 /* Write cipher_suites */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
545 &tls12_uses_ec,
546 &output_len);
547 if (ret != 0) {
548 return ret;
549 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100550 p += output_len;
551
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100552 /* Write legacy_compression_methods (TLS 1.3) or
553 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100554 *
555 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
556 * one byte set to zero, which corresponds to the 'null' compression
557 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100558 *
559 * For TLS 1.2 ClientHello, for security reasons we do not support
560 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100561 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100563 *p++ = 1;
564 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
565
566 /* Write extensions */
567
568#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
569 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800570 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100571#endif
572
573 /* First write extensions, then the total length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100575 p_extensions_len = p;
576 p += 2;
577
Ronald Crondf823bf2022-03-29 18:57:54 +0200578#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
579 /* Write server name extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
581 if (ret != 0) {
582 return ret;
583 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100584 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200585#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100586
587#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
589 if (ret != 0) {
590 return ret;
591 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100592 p += output_len;
593#endif /* MBEDTLS_SSL_ALPN */
594
595#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (propose_tls13) {
597 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
598 &output_len);
599 if (ret != 0) {
600 return ret;
601 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100602 p += output_len;
603 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200604#endif
605
Przemek Stekiel76669452023-06-26 17:34:36 +0200606#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
607 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Ronald Cron1ffa4502023-06-30 14:56:38 +0200608 {
609 int ssl_write_supported_groups_ext_flags = 0;
610
611#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
612 if (propose_tls13 && mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
613 ssl_write_supported_groups_ext_flags |=
614 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG;
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 }
Ronald Cron1ffa4502023-06-30 14:56:38 +0200616#endif
617#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
618 if (propose_tls12 && tls12_uses_ec) {
619 ssl_write_supported_groups_ext_flags |=
620 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG;
621 }
622#endif
623 if (ssl_write_supported_groups_ext_flags != 0) {
624 ret = ssl_write_supported_groups_ext(ssl, p, end,
625 ssl_write_supported_groups_ext_flags,
626 &output_len);
627 if (ret != 0) {
628 return ret;
629 }
630 p += output_len;
631 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100632 }
Przemek Stekiel76669452023-06-26 17:34:36 +0200633#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
634 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100635
Ronald Crone68ab4f2022-10-05 12:46:29 +0200636#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100637 int write_sig_alg_ext = 0;
Ronald Cron4079abc2022-02-20 10:35:26 +0100638#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Dave Rodgmana11eac42023-09-14 16:16:04 +0100639 write_sig_alg_ext = write_sig_alg_ext ||
640 (propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl));
Ronald Cron4079abc2022-02-20 10:35:26 +0100641#endif
642#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100643 write_sig_alg_ext = write_sig_alg_ext || propose_tls12;
Ronald Cron4079abc2022-02-20 10:35:26 +0100644#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +0100645
646 if (write_sig_alg_ext) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
648 if (ret != 0) {
649 return ret;
650 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100651 p += output_len;
652 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200653#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100654
Ronald Cron4079abc2022-02-20 10:35:26 +0100655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (propose_tls12) {
657 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
658 tls12_uses_ec,
659 &output_len);
660 if (ret != 0) {
661 return ret;
662 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100663 p += output_len;
664 }
665#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100666
Ronald Cron41a443a2022-10-04 16:38:25 +0200667#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000668 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
669 * MUST be the last extension in the ClientHello.
670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000672 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 ssl, p, end, &output_len, binders_len);
674 if (ret != 0) {
675 return ret;
676 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000677 p += output_len;
678 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200679#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000680
Ronald Cron3d580bf2022-02-18 17:24:56 +0100681 /* Write the length of the list of extensions. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000682 extensions_len = (size_t) (p - p_extensions_len) - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (extensions_len == 0) {
685 p = p_extensions_len;
686 } else {
687 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
688 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
689 MBEDTLS_PRINTF_SIZET, extensions_len));
690 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
691 p_extensions_len, extensions_len);
Ronald Cron4079abc2022-02-20 10:35:26 +0100692 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100693
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000694 *out_len = (size_t) (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100696}
697
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200698MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static int ssl_generate_random(mbedtls_ssl_context *ssl)
Ronald Cron58b80382022-02-18 18:41:08 +0100700{
701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
702 unsigned char *randbytes = ssl->handshake->randbytes;
703 size_t gmt_unix_time_len = 0;
704
705 /*
706 * Generate the random bytes
707 *
708 * TLS 1.2 case:
709 * struct {
710 * uint32 gmt_unix_time;
711 * opaque random_bytes[28];
712 * } Random;
713 *
714 * TLS 1.3 case:
715 * opaque Random[32];
716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Ronald Cron58b80382022-02-18 18:41:08 +0100718#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
720 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
Ronald Cron58b80382022-02-18 18:41:08 +0100721 gmt_unix_time_len = 4;
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 MBEDTLS_SSL_DEBUG_MSG(3,
724 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
725 (long long) gmt_unix_time));
Ronald Cron58b80382022-02-18 18:41:08 +0100726#endif /* MBEDTLS_HAVE_TIME */
727 }
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 ret = ssl->conf->f_rng(ssl->conf->p_rng,
730 randbytes + gmt_unix_time_len,
731 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
732 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100733}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000734
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100736static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100737{
738 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100739 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800740 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (session_negotiate == NULL) {
743 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
744 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100745
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800746#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
747 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
748 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800749
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800750 /* Check if a tls13 ticket has been configured. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (ssl->handshake->resume != 0 &&
Jerry Yu22c18c12022-10-11 15:58:51 +0800752 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 session_negotiate->ticket != NULL) {
Jerry Yucebffc32022-12-15 18:00:05 +0800754 mbedtls_ms_time_t now = mbedtls_ms_time();
Jerry Yu342a5552023-11-10 14:23:39 +0800755 mbedtls_ms_time_t age = now - session_negotiate->ticket_reception_time;
David Horstmann47490072023-12-06 17:22:53 +0000756 if (age < 0 ||
757 age > (mbedtls_ms_time_t) session_negotiate->ticket_lifetime * 1000) {
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800758 /* Without valid ticket, disable session resumption.*/
759 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 3, ("Ticket expired, disable session resumption"));
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800761 ssl->handshake->resume = 0;
762 }
763 }
764#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
765 MBEDTLS_SSL_SESSION_TICKETS &&
766 MBEDTLS_HAVE_TIME */
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (ssl->conf->f_rng == NULL) {
769 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
770 return MBEDTLS_ERR_SSL_NO_RNG;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100771 }
772
Ronald Cron86a477f2022-02-18 17:45:10 +0100773 /* Bet on the highest configured version if we are not in a TLS 1.2
774 * renegotiation or session resumption.
775 */
776#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Glenn Strausscd78df62022-04-07 19:07:11 -0400778 ssl->handshake->min_tls_version = ssl->tls_version;
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 } else
Ronald Cron86a477f2022-02-18 17:45:10 +0100780#endif
781 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ssl->handshake->resume) {
783 ssl->tls_version = session_negotiate->tls_version;
784 ssl->handshake->min_tls_version = ssl->tls_version;
785 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100787 }
788 }
789
Ronald Cron58b80382022-02-18 18:41:08 +0100790 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200791 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000792 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200793 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100794 */
795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
797 (ssl->handshake->cookie == NULL))
Ronald Cron58b80382022-02-18 18:41:08 +0100798#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100799 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 ret = ssl_generate_random(ssl);
801 if (ret != 0) {
802 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
803 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100804 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100805 }
806
Ronald Cron3d580bf2022-02-18 17:24:56 +0100807 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200808 * Prepare session identifier. At that point, the length of the session
809 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
810 * to zero, except in the case of a TLS 1.2 session renegotiation or
811 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100812 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800813 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100814
815#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
817 if (session_id_len < 16 || session_id_len > 32 ||
Ronald Cron021b1782022-02-19 17:32:53 +0100818#if defined(MBEDTLS_SSL_RENEGOTIATION)
819 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
820#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 ssl->handshake->resume == 0) {
Ronald Cron021b1782022-02-19 17:32:53 +0100822 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100823 }
Ronald Cron021b1782022-02-19 17:32:53 +0100824
825#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 /*
827 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
828 * generate and include a Session ID in the TLS ClientHello."
829 */
David Horstmann4a285632022-10-06 18:30:10 +0100830 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100831#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann7aee0ec2022-10-25 10:38:25 +0100833 renegotiating = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 }
Ronald Cron021b1782022-02-19 17:32:53 +0100835#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (!renegotiating) {
837 if ((session_negotiate->ticket != NULL) &&
838 (session_negotiate->ticket_len != 0)) {
Ronald Cron021b1782022-02-19 17:32:53 +0100839 session_id_len = 32;
840 }
841 }
842#endif /* MBEDTLS_SSL_SESSION_TICKETS */
843 }
844#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
845
846#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron021b1782022-02-19 17:32:53 +0100848 /*
849 * Create a legacy session identifier for the purpose of middlebox
850 * compatibility only if one has not been created already, which is
851 * the case if we are here for the TLS 1.3 second ClientHello.
852 *
853 * Versions of TLS before TLS 1.3 supported a "session resumption"
854 * feature which has been merged with pre-shared keys in TLS 1.3
855 * version. A client which has a cached session ID set by a pre-TLS 1.3
856 * server SHOULD set this field to that value. In compatibility mode,
857 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
858 * session MUST generate a new 32-byte value. This value need not be
859 * random but SHOULD be unpredictable to avoid implementations fixating
860 * on a specific value (also known as ossification). Otherwise, it MUST
861 * be set as a zero-length vector ( i.e., a zero-valued single byte
862 * length field ).
863 */
864 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100865 }
866#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (session_id_len != session_negotiate->id_len) {
Jerry Yu22c18c12022-10-11 15:58:51 +0800869 session_negotiate->id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (session_id_len > 0) {
871 ret = ssl->conf->f_rng(ssl->conf->p_rng,
872 session_negotiate->id,
873 session_id_len);
874 if (ret != 0) {
875 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
876 return ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100877 }
878 }
879 }
880
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000881#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000882 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000883 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
885 ssl->handshake->resume) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000886 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000887 session_negotiate->hostname != NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (ssl->hostname != NULL && session_negotiate->hostname != NULL) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000889 hostname_mismatch = strcmp(
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 ssl->hostname, session_negotiate->hostname) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000891 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (hostname_mismatch) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000894 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 1, ("Hostname mismatch the session ticket, "
896 "disable session resumption."));
897 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000898 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 } else {
900 return mbedtls_ssl_session_set_hostname(session_negotiate,
901 ssl->hostname);
Xiaokang Qian03409292022-10-12 02:49:52 +0000902 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000903#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000904 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000905 MBEDTLS_SSL_SERVER_NAME_INDICATION */
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100908}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100909/*
910 * Write ClientHello handshake message.
911 * Handler for MBEDTLS_SSL_CLIENT_HELLO
912 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100914{
915 int ret = 0;
916 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000917 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
924 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
925 &buf, &buf_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
928 buf + buf_len,
929 &msg_len,
930 &binders_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100931
Ronald Cron5f4e9122022-02-21 09:50:36 +0100932#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Ronald Cron5f4e9122022-02-21 09:50:36 +0100934 ssl->out_msglen = msg_len + 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 mbedtls_ssl_send_flight_completed(ssl);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100936
Ronald Cron8ecd9932022-03-29 12:26:54 +0200937 /*
938 * The two functions below may try to send data on the network and
939 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
940 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200941 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200942 * set the handshake state to the next state at that point to ensure
943 * that we will not write and send again a ClientHello when we
944 * eventually succeed in sending the pending data.
945 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron8ecd9932022-03-29 12:26:54 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
949 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
950 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100951 }
952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
954 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
955 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100956 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 } else
Ronald Cron5f4e9122022-02-21 09:50:36 +0100958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
959 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000960
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100961 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
962 MBEDTLS_SSL_HS_CLIENT_HELLO,
963 msg_len);
964 if (ret != 0) {
965 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
966 return ret;
967 }
968 ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
969 if (ret != 0) {
970 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
971 return ret;
972 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200973#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (binders_len > 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000975 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000976 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 ssl, buf + msg_len - binders_len, buf + msg_len));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100978 ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100979 binders_len);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100980 if (ret != 0) {
981 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
982 return ret;
983 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000984 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200985#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
988 buf_len,
989 msg_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100990
Xiaokang Qian44051f62023-02-02 06:57:26 +0000991 /*
992 * Set next state. Note that if TLS 1.3 is proposed, this may be
Xiaokang Qian934ce6f2023-02-06 10:23:04 +0000993 * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
Xiaokang Qian44051f62023-02-02 06:57:26 +0000994 */
995 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +0000996
Xiaokang Qian44051f62023-02-02 06:57:26 +0000997#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
998 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
999 MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001000 ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
Xiaokang Qian44051f62023-02-02 06:57:26 +00001001 }
1002#endif
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001003 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001004
Jerry Yu7cca7f62023-11-07 15:00:32 +08001005#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1006 MBEDTLS_SSL_PRINT_EXTS(
1007 3, MBEDTLS_SSL_HS_CLIENT_HELLO, ssl->handshake->sent_extensions);
1008#endif
1009
Ronald Cron3d580bf2022-02-18 17:24:56 +01001010cleanup:
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001013 return ret;
1014}
1015
1016#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1017#endif /* MBEDTLS_SSL_CLI_C */