blob: 257a696b9f1a023764aa5b532dfa708d53dd017f [file] [log] [blame]
Ronald Cron3d580bf2022-02-18 17:24:56 +01001/*
2 * TLS 1.2 and 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
24#if defined(MBEDTLS_SSL_CLI_C)
25#if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
26
27#include <string.h>
28
29#include "mbedtls/debug.h"
30#include "mbedtls/error.h"
Jerry Yu141bbe72022-12-01 20:30:41 +080031#include "mbedtls/platform.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010032
33#include "ssl_client.h"
34#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010035#include "ssl_tls13_keys.h"
36#include "ssl_debug_helpers.h"
37
Ronald Cronfbd9f992022-03-17 15:22:07 +010038#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020039MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010040static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
41 unsigned char *buf,
42 const unsigned char *end,
43 size_t *olen)
Ronald Cronfbd9f992022-03-17 15:22:07 +010044{
45 unsigned char *p = buf;
46 size_t hostname_len;
47
48 *olen = 0;
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if (ssl->hostname == NULL) {
51 return 0;
52 }
Ronald Cronfbd9f992022-03-17 15:22:07 +010053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 MBEDTLS_SSL_DEBUG_MSG(3,
55 ("client hello, adding server name extension: %s",
56 ssl->hostname));
Ronald Cronfbd9f992022-03-17 15:22:07 +010057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 hostname_len = strlen(ssl->hostname);
Ronald Cronfbd9f992022-03-17 15:22:07 +010059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
Ronald Cronfbd9f992022-03-17 15:22:07 +010061
62 /*
63 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
64 *
65 * In order to provide any of the server names, clients MAY include an
66 * extension of type "server_name" in the (extended) client hello. The
67 * "extension_data" field of this extension SHALL contain
68 * "ServerNameList" where:
69 *
70 * struct {
71 * NameType name_type;
72 * select (name_type) {
73 * case host_name: HostName;
74 * } name;
75 * } ServerName;
76 *
77 * enum {
78 * host_name(0), (255)
79 * } NameType;
80 *
81 * opaque HostName<1..2^16-1>;
82 *
83 * struct {
84 * ServerName server_name_list<1..2^16-1>
85 * } ServerNameList;
86 *
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010089 p += 2;
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091 MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010092 p += 2;
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010095 p += 2;
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
Ronald Cronfbd9f992022-03-17 15:22:07 +010098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100100 p += 2;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 memcpy(p, ssl->hostname, hostname_len);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100103
104 *olen = hostname_len + 9;
105
Jerry Yu0c354a22022-08-29 15:25:36 +0800106#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
Jerry Yu0c354a22022-08-29 15:25:36 +0800108#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100110}
111#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
112
Ronald Cron3d580bf2022-02-18 17:24:56 +0100113#if defined(MBEDTLS_SSL_ALPN)
114/*
Ronald Cron71c23322022-02-18 17:29:39 +0100115 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100116 *
117 * Structure of the application_layer_protocol_negotiation extension in
118 * ClientHello:
119 *
120 * opaque ProtocolName<1..2^8-1>;
121 *
122 * struct {
123 * ProtocolName protocol_name_list<2..2^16-1>
124 * } ProtocolNameList;
125 *
126 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200127MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
129 unsigned char *buf,
130 const unsigned char *end,
131 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100132{
133 unsigned char *p = buf;
134
135 *out_len = 0;
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (ssl->conf->alpn_list == NULL) {
138 return 0;
139 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100142
143
144 /* Check we have enough space for the extension type (2 bytes), the
145 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
148 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100149 /* Skip writing extension and list length for now */
150 p += 6;
151
152 /*
153 * opaque ProtocolName<1..2^8-1>;
154 *
155 * struct {
156 * ProtocolName protocol_name_list<2..2^16-1>
157 * } ProtocolNameList;
158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100160 /*
161 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
162 * protocol names is less than 255.
163 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 size_t protocol_name_len = strlen(*cur);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + protocol_name_len);
167 *p++ = (unsigned char) protocol_name_len;
168 memcpy(p, *cur, protocol_name_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100169 p += protocol_name_len;
170 }
171
172 *out_len = p - buf;
173
174 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100176
177 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100179
Jerry Yu0c354a22022-08-29 15:25:36 +0800180#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yu0c354a22022-08-29 15:25:36 +0800182#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100184}
185#endif /* MBEDTLS_SSL_ALPN */
186
Ronald Cronfbd9f992022-03-17 15:22:07 +0100187#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Przemek Stekiel316c19e2023-05-31 15:25:11 +0200188 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || defined(PSA_WANT_ALG_FFDH)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100189/*
190 * Function for writing a supported groups (TLS 1.3) or supported elliptic
191 * curves (TLS 1.2) extension.
192 *
193 * The "extension_data" field of a supported groups extension contains a
194 * "NamedGroupList" value (TLS 1.3 RFC8446):
195 * enum {
196 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
197 * x25519(0x001D), x448(0x001E),
198 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
199 * ffdhe6144(0x0103), ffdhe8192(0x0104),
200 * ffdhe_private_use(0x01FC..0x01FF),
201 * ecdhe_private_use(0xFE00..0xFEFF),
202 * (0xFFFF)
203 * } NamedGroup;
204 * struct {
205 * NamedGroup named_group_list<2..2^16-1>;
206 * } NamedGroupList;
207 *
208 * The "extension_data" field of a supported elliptic curves extension contains
209 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
210 * enum {
211 * deprecated(1..22),
212 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
213 * x25519(29), x448(30),
214 * reserved (0xFE00..0xFEFF),
215 * deprecated(0xFF01..0xFF02),
216 * (0xFFFF)
217 * } NamedCurve;
218 * struct {
219 * NamedCurve named_curve_list<2..2^16-1>
220 * } NamedCurveList;
221 *
222 * The TLS 1.3 supported groups extension was defined to be a compatible
223 * generalization of the TLS 1.2 supported elliptic curves extension. They both
224 * share the same extension identifier.
225 *
226 * DHE groups are not supported yet.
227 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200228MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100229static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
230 unsigned char *buf,
231 const unsigned char *end,
232 size_t *out_len)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100233{
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 unsigned char *p = buf;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100235 unsigned char *named_group_list; /* Start of named_group_list */
236 size_t named_group_list_len; /* Length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100238
239 *out_len = 0;
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100242
243 /* Check if we have space for header and length fields:
244 * - extension_type (2 bytes)
245 * - extension_data_length (2 bytes)
246 * - named_group_list_length (2 bytes)
247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100249 p += 6;
250
251 named_group_list = p;
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (group_list == NULL) {
254 return MBEDTLS_ERR_SSL_BAD_CONFIG;
255 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 for (; *group_list != 0; group_list++) {
258 MBEDTLS_SSL_DEBUG_MSG(1, ("got supported group(%04x)", *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100259
Valerio Settid4a5d462023-04-05 18:19:01 +0200260#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if ((mbedtls_ssl_conf_is_tls13_enabled(ssl->conf) &&
262 mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) ||
263 (mbedtls_ssl_conf_is_tls12_enabled(ssl->conf) &&
264 mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list))) {
265 if (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) ==
266 MBEDTLS_ECP_DP_NONE) {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100267 continue;
Valerio Setti18c9fed2022-12-30 17:44:24 +0100268 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
270 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100271 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
273 mbedtls_ssl_get_curve_name_from_tls_id(*group_list),
274 *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100275 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200276#endif /* MBEDTLS_ECP_LIGHT */
Przemek Stekiel316c19e2023-05-31 15:25:11 +0200277#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel060012c2023-05-18 14:10:02 +0200278 if ((mbedtls_ssl_conf_is_tls13_enabled(ssl->conf) &&
279 mbedtls_ssl_tls13_named_group_is_dhe(*group_list))) {
280 const char *ffdh_group = NULL;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100281
Przemek Stekiel060012c2023-05-18 14:10:02 +0200282 switch (*group_list) {
283 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
284 ffdh_group = "ffdhe2048";
285 break;
286 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
287 ffdh_group = "ffdhe3072";
288 break;
289 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
290 ffdh_group = "ffdhe4096";
291 break;
292 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
293 ffdh_group = "ffdhe6144";
294 break;
295 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
296 ffdh_group = "ffdhe8192";
297 break;
298 default:
299 break;
300 }
301
302 if (ffdh_group == NULL) {
303 continue;
304 }
305
306 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
307 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
308 p += 2;
309 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
310 ffdh_group, *group_list));
Przemek Stekielcceb9332023-05-18 14:31:10 +0200311 }
Przemek Stekiel316c19e2023-05-31 15:25:11 +0200312#endif /* PSA_WANT_ALG_FFDH */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100313 }
314
315 /* Length of named_group_list */
316 named_group_list_len = p - named_group_list;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (named_group_list_len == 0) {
318 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
319 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100320 }
321
322 /* Write extension_type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100324 /* Write extension_data_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100326 /* Write length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
330 buf + 4, named_group_list_len + 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100331
332 *out_len = p - buf;
333
334#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800335 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100337#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100340}
Ronald Cronfbd9f992022-03-17 15:22:07 +0100341#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Przemek Stekiel316c19e2023-05-31 15:25:11 +0200342 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED || PSA_WANT_ALG_FFDH */
Ronald Cronfbd9f992022-03-17 15:22:07 +0100343
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200344MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100345static int ssl_write_client_hello_cipher_suites(
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 mbedtls_ssl_context *ssl,
347 unsigned char *buf,
348 unsigned char *end,
349 int *tls12_uses_ec,
350 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100351{
352 unsigned char *p = buf;
353 const int *ciphersuite_list;
354 unsigned char *cipher_suites; /* Start of the cipher_suites list */
355 size_t cipher_suites_len;
356
Ronald Crond491c2d2022-02-19 18:30:46 +0100357 *tls12_uses_ec = 0;
358 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100359
360 /*
361 * Ciphersuite list
362 *
363 * This is a list of the symmetric cipher options supported by
364 * the client, specifically the record protection algorithm
365 * ( including secret key length ) and a hash to be used with
366 * HKDF, in descending order of client preference.
367 */
368 ciphersuite_list = ssl->conf->ciphersuite_list;
369
370 /* Check there is space for the cipher suite list length (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100372 p += 2;
373
Ronald Cron64767262022-03-31 14:13:57 +0200374 /* Write cipher_suites
375 * CipherSuite cipher_suites<2..2^16-2>;
376 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100377 cipher_suites = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100379 int cipher_suite = ciphersuite_list[i];
380 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Ronald Crond491c2d2022-02-19 18:30:46 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
385 ssl->handshake->min_tls_version,
386 ssl->tls_version) != 0) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100387 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100389
390#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 (defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
392 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
393 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
Ronald Crond491c2d2022-02-19 18:30:46 +0100394#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
397 (unsigned int) cipher_suite,
398 ciphersuite_info->name));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100399
400 /* Check there is space for the cipher suite identifier (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
402 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100403 p += 2;
404 }
405
Ronald Crond491c2d2022-02-19 18:30:46 +0100406 /*
407 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
408 */
David Horstmann4a285632022-10-06 18:30:10 +0100409 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100410#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
Ronald Crond491c2d2022-02-19 18:30:46 +0100412#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (!renegotiating) {
414 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
415 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
416 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
Ronald Crond491c2d2022-02-19 18:30:46 +0100417 p += 2;
418 }
419
Ronald Cron3d580bf2022-02-18 17:24:56 +0100420 /* Write the cipher_suites length in number of bytes */
421 cipher_suites_len = p - cipher_suites;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
423 MBEDTLS_SSL_DEBUG_MSG(3,
424 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
425 cipher_suites_len/2));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100426
427 /* Output the total length of cipher_suites field. */
428 *out_len = p - buf;
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100431}
432
433/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100434 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100435 *
436 * struct {
437 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
438 * Random random;
439 * opaque legacy_session_id<0..32>;
440 * CipherSuite cipher_suites<2..2^16-2>;
441 * opaque legacy_compression_methods<1..2^8-1>;
442 * Extension extensions<8..2^16-1>;
443 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100444 *
445 * Structure of the (D)TLS 1.2 ClientHello message:
446 *
447 * struct {
448 * ProtocolVersion client_version;
449 * Random random;
450 * SessionID session_id;
451 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
452 * CipherSuite cipher_suites<2..2^16-2>;
453 * CompressionMethod compression_methods<1..2^8-1>;
454 * select (extensions_present) {
455 * case false:
456 * struct {};
457 * case true:
458 * Extension extensions<0..2^16-1>;
459 * };
460 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100461 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100463static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
464 unsigned char *buf,
465 unsigned char *end,
466 size_t *out_len,
467 size_t *binders_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100468{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100469 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100470 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100471 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100472 unsigned char *p_extensions_len; /* Pointer to extensions length */
473 size_t output_len; /* Length of buffer used by function */
474 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100475 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100476
Ronald Cron3d580bf2022-02-18 17:24:56 +0100477 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000478 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100479
Ronald Cron4079abc2022-02-20 10:35:26 +0100480#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200481 unsigned char propose_tls12 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200483 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100485#endif
486#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200487 unsigned char propose_tls13 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200489 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100491#endif
492
Ronald Cron3d580bf2022-02-18 17:24:56 +0100493 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100494 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100495 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100496 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
499 mbedtls_ssl_write_version(p, ssl->conf->transport,
500 MBEDTLS_SSL_VERSION_TLS1_2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100501 p += 2;
502
Ronald Cron58b80382022-02-18 18:41:08 +0100503 /* ...
504 * Random random;
505 * ...
506 *
Ronald Cron58b80382022-02-18 18:41:08 +0100507 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100508 * the handshake->randbytes buffer and are copied here into the output
509 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
512 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
513 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
514 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100515 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
516
Ronald Cron021b1782022-02-19 17:32:53 +0100517 /* TLS 1.2:
518 * ...
519 * SessionID session_id;
520 * ...
521 * with
522 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100523 *
Ronald Cron021b1782022-02-19 17:32:53 +0100524 * TLS 1.3:
525 * ...
526 * opaque legacy_session_id<0..32>;
527 * ...
528 *
Ronald Cronda41b382022-03-30 09:57:11 +0200529 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100530 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
531 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100532 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
534 *p++ = (unsigned char) ssl->session_negotiate->id_len;
535 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100536 p += ssl->session_negotiate->id_len;
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
539 ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100540
Ronald Crona874aa82022-02-19 18:11:26 +0100541 /* DTLS 1.2 ONLY
542 * ...
543 * opaque cookie<0..2^8-1>;
544 * ...
545 */
546#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yue01304f2022-04-07 10:51:55 +0800548#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
549 uint8_t cookie_len = 0;
550#else
551 uint16_t cookie_len = 0;
552#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (handshake->cookie != NULL) {
555 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
556 handshake->cookie,
557 handshake->cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800558 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100559 }
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
562 *p++ = (unsigned char) cookie_len;
563 if (cookie_len > 0) {
564 memcpy(p, handshake->cookie, cookie_len);
Ronald Crona874aa82022-02-19 18:11:26 +0100565 p += cookie_len;
566 }
567 }
568#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
569
Ronald Cron3d580bf2022-02-18 17:24:56 +0100570 /* Write cipher_suites */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
572 &tls12_uses_ec,
573 &output_len);
574 if (ret != 0) {
575 return ret;
576 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100577 p += output_len;
578
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100579 /* Write legacy_compression_methods (TLS 1.3) or
580 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100581 *
582 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
583 * one byte set to zero, which corresponds to the 'null' compression
584 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100585 *
586 * For TLS 1.2 ClientHello, for security reasons we do not support
587 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100588 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100590 *p++ = 1;
591 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
592
593 /* Write extensions */
594
595#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
596 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800597 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100598#endif
599
600 /* First write extensions, then the total length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100602 p_extensions_len = p;
603 p += 2;
604
Ronald Crondf823bf2022-03-29 18:57:54 +0200605#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
606 /* Write server name extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
608 if (ret != 0) {
609 return ret;
610 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100611 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200612#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100613
614#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
616 if (ret != 0) {
617 return ret;
618 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100619 p += output_len;
620#endif /* MBEDTLS_SSL_ALPN */
621
622#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (propose_tls13) {
624 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
625 &output_len);
626 if (ret != 0) {
627 return ret;
628 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100629 p += output_len;
630 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200631#endif
632
Ronald Cron4079abc2022-02-20 10:35:26 +0100633#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Przemek Stekiel316c19e2023-05-31 15:25:11 +0200634 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || defined(PSA_WANT_ALG_FFDH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (
Ronald Crondf823bf2022-03-29 18:57:54 +0200636#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 (propose_tls13 &&
638 mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100639#endif
640#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 (propose_tls12 && tls12_uses_ec) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100642#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 0) {
644 ret = ssl_write_supported_groups_ext(ssl, p, end, &output_len);
645 if (ret != 0) {
646 return ret;
647 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100648 p += output_len;
649 }
Przemek Stekiel316c19e2023-05-31 15:25:11 +0200650#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
651 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED || PSA_WANT_ALG_FFDH */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100652
Ronald Crone68ab4f2022-10-05 12:46:29 +0200653#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (
Ronald Cron4079abc2022-02-20 10:35:26 +0100655#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 (propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl)) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100657#endif
658#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
659 propose_tls12 ||
660#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 0) {
662 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
663 if (ret != 0) {
664 return ret;
665 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100666 p += output_len;
667 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200668#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100669
Ronald Cron4079abc2022-02-20 10:35:26 +0100670#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (propose_tls12) {
672 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
673 tls12_uses_ec,
674 &output_len);
675 if (ret != 0) {
676 return ret;
677 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100678 p += output_len;
679 }
680#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100681
Ronald Cron41a443a2022-10-04 16:38:25 +0200682#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000683 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
684 * MUST be the last extension in the ClientHello.
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000687 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 ssl, p, end, &output_len, binders_len);
689 if (ret != 0) {
690 return ret;
691 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000692 p += output_len;
693 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200694#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000695
Ronald Cron3d580bf2022-02-18 17:24:56 +0100696 /* Write the length of the list of extensions. */
697 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (extensions_len == 0) {
700 p = p_extensions_len;
701 } else {
702 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
703 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
704 MBEDTLS_PRINTF_SIZET, extensions_len));
705 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
706 p_extensions_len, extensions_len);
Ronald Cron4079abc2022-02-20 10:35:26 +0100707 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100708
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800709#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu7de2ff02022-11-08 21:43:46 +0800710 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 3, MBEDTLS_SSL_HS_CLIENT_HELLO, handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800712#endif
713
Ronald Cron3d580bf2022-02-18 17:24:56 +0100714 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100716}
717
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200718MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100719static int ssl_generate_random(mbedtls_ssl_context *ssl)
Ronald Cron58b80382022-02-18 18:41:08 +0100720{
721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
722 unsigned char *randbytes = ssl->handshake->randbytes;
723 size_t gmt_unix_time_len = 0;
724
725 /*
726 * Generate the random bytes
727 *
728 * TLS 1.2 case:
729 * struct {
730 * uint32 gmt_unix_time;
731 * opaque random_bytes[28];
732 * } Random;
733 *
734 * TLS 1.3 case:
735 * opaque Random[32];
736 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Ronald Cron58b80382022-02-18 18:41:08 +0100738#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
740 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
Ronald Cron58b80382022-02-18 18:41:08 +0100741 gmt_unix_time_len = 4;
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 MBEDTLS_SSL_DEBUG_MSG(3,
744 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
745 (long long) gmt_unix_time));
Ronald Cron58b80382022-02-18 18:41:08 +0100746#endif /* MBEDTLS_HAVE_TIME */
747 }
748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 ret = ssl->conf->f_rng(ssl->conf->p_rng,
750 randbytes + gmt_unix_time_len,
751 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
752 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100753}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000754
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200755MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100756static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100757{
758 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100759 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800760 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (session_negotiate == NULL) {
763 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
764 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100765
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800766#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
767 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
768 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800769
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800770 /* Check if a tls13 ticket has been configured. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (ssl->handshake->resume != 0 &&
Jerry Yu22c18c12022-10-11 15:58:51 +0800772 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 session_negotiate->ticket != NULL) {
774 mbedtls_time_t now = mbedtls_time(NULL);
775 uint64_t age = (uint64_t) (now - session_negotiate->ticket_received);
776 if (session_negotiate->ticket_received > now ||
777 age > session_negotiate->ticket_lifetime) {
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800778 /* Without valid ticket, disable session resumption.*/
779 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 3, ("Ticket expired, disable session resumption"));
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800781 ssl->handshake->resume = 0;
782 }
783 }
784#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
785 MBEDTLS_SSL_SESSION_TICKETS &&
786 MBEDTLS_HAVE_TIME */
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (ssl->conf->f_rng == NULL) {
789 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
790 return MBEDTLS_ERR_SSL_NO_RNG;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100791 }
792
Ronald Cron86a477f2022-02-18 17:45:10 +0100793 /* Bet on the highest configured version if we are not in a TLS 1.2
794 * renegotiation or session resumption.
795 */
796#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Glenn Strausscd78df62022-04-07 19:07:11 -0400798 ssl->handshake->min_tls_version = ssl->tls_version;
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 } else
Ronald Cron86a477f2022-02-18 17:45:10 +0100800#endif
801 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (ssl->handshake->resume) {
803 ssl->tls_version = session_negotiate->tls_version;
804 ssl->handshake->min_tls_version = ssl->tls_version;
805 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100807 }
808 }
809
Ronald Cron58b80382022-02-18 18:41:08 +0100810 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200811 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000812 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200813 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100814 */
815#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
817 (ssl->handshake->cookie == NULL))
Ronald Cron58b80382022-02-18 18:41:08 +0100818#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100819 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 ret = ssl_generate_random(ssl);
821 if (ret != 0) {
822 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
823 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100824 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100825 }
826
Ronald Cron3d580bf2022-02-18 17:24:56 +0100827 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200828 * Prepare session identifier. At that point, the length of the session
829 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
830 * to zero, except in the case of a TLS 1.2 session renegotiation or
831 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100832 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800833 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100834
835#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
837 if (session_id_len < 16 || session_id_len > 32 ||
Ronald Cron021b1782022-02-19 17:32:53 +0100838#if defined(MBEDTLS_SSL_RENEGOTIATION)
839 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
840#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 ssl->handshake->resume == 0) {
Ronald Cron021b1782022-02-19 17:32:53 +0100842 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100843 }
Ronald Cron021b1782022-02-19 17:32:53 +0100844
845#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 /*
847 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
848 * generate and include a Session ID in the TLS ClientHello."
849 */
David Horstmann4a285632022-10-06 18:30:10 +0100850 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100851#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann7aee0ec2022-10-25 10:38:25 +0100853 renegotiating = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 }
Ronald Cron021b1782022-02-19 17:32:53 +0100855#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (!renegotiating) {
857 if ((session_negotiate->ticket != NULL) &&
858 (session_negotiate->ticket_len != 0)) {
Ronald Cron021b1782022-02-19 17:32:53 +0100859 session_id_len = 32;
860 }
861 }
862#endif /* MBEDTLS_SSL_SESSION_TICKETS */
863 }
864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
865
866#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron021b1782022-02-19 17:32:53 +0100868 /*
869 * Create a legacy session identifier for the purpose of middlebox
870 * compatibility only if one has not been created already, which is
871 * the case if we are here for the TLS 1.3 second ClientHello.
872 *
873 * Versions of TLS before TLS 1.3 supported a "session resumption"
874 * feature which has been merged with pre-shared keys in TLS 1.3
875 * version. A client which has a cached session ID set by a pre-TLS 1.3
876 * server SHOULD set this field to that value. In compatibility mode,
877 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
878 * session MUST generate a new 32-byte value. This value need not be
879 * random but SHOULD be unpredictable to avoid implementations fixating
880 * on a specific value (also known as ossification). Otherwise, it MUST
881 * be set as a zero-length vector ( i.e., a zero-valued single byte
882 * length field ).
883 */
884 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100885 }
886#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (session_id_len != session_negotiate->id_len) {
Jerry Yu22c18c12022-10-11 15:58:51 +0800889 session_negotiate->id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (session_id_len > 0) {
891 ret = ssl->conf->f_rng(ssl->conf->p_rng,
892 session_negotiate->id,
893 session_id_len);
894 if (ret != 0) {
895 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
896 return ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100897 }
898 }
899 }
900
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000901#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000902 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000903 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
905 ssl->handshake->resume) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000906 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000907 session_negotiate->hostname != NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (ssl->hostname != NULL && session_negotiate->hostname != NULL) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000909 hostname_mismatch = strcmp(
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 ssl->hostname, session_negotiate->hostname) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000911 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (hostname_mismatch) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000914 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 1, ("Hostname mismatch the session ticket, "
916 "disable session resumption."));
917 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000918 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 } else {
920 return mbedtls_ssl_session_set_hostname(session_negotiate,
921 ssl->hostname);
Xiaokang Qian03409292022-10-12 02:49:52 +0000922 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000923#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000924 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000925 MBEDTLS_SSL_SERVER_NAME_INDICATION */
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100928}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100929/*
930 * Write ClientHello handshake message.
931 * Handler for MBEDTLS_SSL_CLIENT_HELLO
932 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100934{
935 int ret = 0;
936 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000937 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
944 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
945 &buf, &buf_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
948 buf + buf_len,
949 &msg_len,
950 &binders_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100951
Ronald Cron5f4e9122022-02-21 09:50:36 +0100952#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Ronald Cron5f4e9122022-02-21 09:50:36 +0100954 ssl->out_msglen = msg_len + 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 mbedtls_ssl_send_flight_completed(ssl);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100956
Ronald Cron8ecd9932022-03-29 12:26:54 +0200957 /*
958 * The two functions below may try to send data on the network and
959 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
960 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200961 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200962 * set the handshake state to the next state at that point to ensure
963 * that we will not write and send again a ClientHello when we
964 * eventually succeed in sending the pending data.
965 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron8ecd9932022-03-29 12:26:54 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
969 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
970 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100971 }
972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
974 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
975 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100976 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 } else
Ronald Cron5f4e9122022-02-21 09:50:36 +0100978#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
979 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000980
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100981 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
982 MBEDTLS_SSL_HS_CLIENT_HELLO,
983 msg_len);
984 if (ret != 0) {
985 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
986 return ret;
987 }
988 ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
989 if (ret != 0) {
990 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
991 return ret;
992 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (binders_len > 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000995 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000996 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 ssl, buf + msg_len - binders_len, buf + msg_len));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100998 ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100999 binders_len);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001000 if (ret != 0) {
1001 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1002 return ret;
1003 }
XiaokangQianeb69aee2022-07-05 08:21:43 +00001004 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001005#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +00001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
1008 buf_len,
1009 msg_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001010
Xiaokang Qian44051f62023-02-02 06:57:26 +00001011 /*
1012 * Set next state. Note that if TLS 1.3 is proposed, this may be
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001013 * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
Xiaokang Qian44051f62023-02-02 06:57:26 +00001014 */
1015 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001016
Xiaokang Qian44051f62023-02-02 06:57:26 +00001017#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1018 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
1019 MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001020 ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
Xiaokang Qian44051f62023-02-02 06:57:26 +00001021 }
1022#endif
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001023 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001024
1025cleanup:
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001028 return ret;
1029}
1030
1031#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1032#endif /* MBEDTLS_SSL_CLI_C */