blob: 863ce211062703c56cbff6d5e385e8d35db0e9dd [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
Ronald Cron58b80382022-02-18 18:41:08 +010027#include "mbedtls/platform.h"
Ronald Cron58b80382022-02-18 18:41:08 +010028
Ronald Cron3d580bf2022-02-18 17:24:56 +010029#include <string.h>
30
31#include "mbedtls/debug.h"
32#include "mbedtls/error.h"
Ronald Cron58b80382022-02-18 18:41:08 +010033#if defined(MBEDTLS_HAVE_TIME)
34#include "mbedtls/platform_time.h"
35#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +010036
37#include "ssl_client.h"
38#include "ssl_misc.h"
Ronald Cron3d580bf2022-02-18 17:24:56 +010039#include "ssl_tls13_keys.h"
40#include "ssl_debug_helpers.h"
41
Ronald Cronfbd9f992022-03-17 15:22:07 +010042#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020043MBEDTLS_CHECK_RETURN_CRITICAL
David Horstmann71159f42023-01-03 12:51:59 +000044static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
45 unsigned char *buf,
46 const unsigned char *end,
47 size_t *olen)
Ronald Cronfbd9f992022-03-17 15:22:07 +010048{
49 unsigned char *p = buf;
50 size_t hostname_len;
51
52 *olen = 0;
53
David Horstmann71159f42023-01-03 12:51:59 +000054 if (ssl->hostname == NULL) {
55 return 0;
56 }
Ronald Cronfbd9f992022-03-17 15:22:07 +010057
David Horstmann71159f42023-01-03 12:51:59 +000058 MBEDTLS_SSL_DEBUG_MSG(3,
59 ("client hello, adding server name extension: %s",
60 ssl->hostname));
Ronald Cronfbd9f992022-03-17 15:22:07 +010061
David Horstmann71159f42023-01-03 12:51:59 +000062 hostname_len = strlen(ssl->hostname);
Ronald Cronfbd9f992022-03-17 15:22:07 +010063
David Horstmann71159f42023-01-03 12:51:59 +000064 MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
Ronald Cronfbd9f992022-03-17 15:22:07 +010065
66 /*
67 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
68 *
69 * In order to provide any of the server names, clients MAY include an
70 * extension of type "server_name" in the (extended) client hello. The
71 * "extension_data" field of this extension SHALL contain
72 * "ServerNameList" where:
73 *
74 * struct {
75 * NameType name_type;
76 * select (name_type) {
77 * case host_name: HostName;
78 * } name;
79 * } ServerName;
80 *
81 * enum {
82 * host_name(0), (255)
83 * } NameType;
84 *
85 * opaque HostName<1..2^16-1>;
86 *
87 * struct {
88 * ServerName server_name_list<1..2^16-1>
89 * } ServerNameList;
90 *
91 */
David Horstmann71159f42023-01-03 12:51:59 +000092 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010093 p += 2;
94
David Horstmann71159f42023-01-03 12:51:59 +000095 MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010096 p += 2;
97
David Horstmann71159f42023-01-03 12:51:59 +000098 MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +010099 p += 2;
100
David Horstmann71159f42023-01-03 12:51:59 +0000101 *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100102
David Horstmann71159f42023-01-03 12:51:59 +0000103 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100104 p += 2;
105
David Horstmann71159f42023-01-03 12:51:59 +0000106 memcpy(p, ssl->hostname, hostname_len);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100107
108 *olen = hostname_len + 9;
109
Jerry Yu0c354a22022-08-29 15:25:36 +0800110#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
David Horstmann71159f42023-01-03 12:51:59 +0000111 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
Jerry Yu0c354a22022-08-29 15:25:36 +0800112#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
David Horstmann71159f42023-01-03 12:51:59 +0000113 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100114}
115#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
116
Ronald Cron3d580bf2022-02-18 17:24:56 +0100117#if defined(MBEDTLS_SSL_ALPN)
118/*
Ronald Cron71c23322022-02-18 17:29:39 +0100119 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +0100120 *
121 * Structure of the application_layer_protocol_negotiation extension in
122 * ClientHello:
123 *
124 * opaque ProtocolName<1..2^8-1>;
125 *
126 * struct {
127 * ProtocolName protocol_name_list<2..2^16-1>
128 * } ProtocolNameList;
129 *
130 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200131MBEDTLS_CHECK_RETURN_CRITICAL
David Horstmann71159f42023-01-03 12:51:59 +0000132static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
133 unsigned char *buf,
134 const unsigned char *end,
135 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100136{
137 unsigned char *p = buf;
138
139 *out_len = 0;
140
David Horstmann71159f42023-01-03 12:51:59 +0000141 if (ssl->conf->alpn_list == NULL) {
142 return 0;
143 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100144
David Horstmann71159f42023-01-03 12:51:59 +0000145 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100146
147
148 /* Check we have enough space for the extension type (2 bytes), the
149 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
150 */
David Horstmann71159f42023-01-03 12:51:59 +0000151 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
152 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100153 /* Skip writing extension and list length for now */
154 p += 6;
155
156 /*
157 * opaque ProtocolName<1..2^8-1>;
158 *
159 * struct {
160 * ProtocolName protocol_name_list<2..2^16-1>
161 * } ProtocolNameList;
162 */
David Horstmann71159f42023-01-03 12:51:59 +0000163 for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100164 /*
165 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
166 * protocol names is less than 255.
167 */
David Horstmann71159f42023-01-03 12:51:59 +0000168 size_t protocol_name_len = strlen(*cur);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100169
David Horstmann71159f42023-01-03 12:51:59 +0000170 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + protocol_name_len);
171 *p++ = (unsigned char) protocol_name_len;
172 memcpy(p, *cur, protocol_name_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100173 p += protocol_name_len;
174 }
175
176 *out_len = p - buf;
177
178 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
David Horstmann71159f42023-01-03 12:51:59 +0000179 MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100180
181 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
David Horstmann71159f42023-01-03 12:51:59 +0000182 MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100183
Jerry Yu0c354a22022-08-29 15:25:36 +0800184#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
David Horstmann71159f42023-01-03 12:51:59 +0000185 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yu0c354a22022-08-29 15:25:36 +0800186#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
David Horstmann71159f42023-01-03 12:51:59 +0000187 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100188}
189#endif /* MBEDTLS_SSL_ALPN */
190
Ronald Cronfbd9f992022-03-17 15:22:07 +0100191#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
192 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
193/*
194 * Function for writing a supported groups (TLS 1.3) or supported elliptic
195 * curves (TLS 1.2) extension.
196 *
197 * The "extension_data" field of a supported groups extension contains a
198 * "NamedGroupList" value (TLS 1.3 RFC8446):
199 * enum {
200 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
201 * x25519(0x001D), x448(0x001E),
202 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
203 * ffdhe6144(0x0103), ffdhe8192(0x0104),
204 * ffdhe_private_use(0x01FC..0x01FF),
205 * ecdhe_private_use(0xFE00..0xFEFF),
206 * (0xFFFF)
207 * } NamedGroup;
208 * struct {
209 * NamedGroup named_group_list<2..2^16-1>;
210 * } NamedGroupList;
211 *
212 * The "extension_data" field of a supported elliptic curves extension contains
213 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
214 * enum {
215 * deprecated(1..22),
216 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
217 * x25519(29), x448(30),
218 * reserved (0xFE00..0xFEFF),
219 * deprecated(0xFF01..0xFF02),
220 * (0xFFFF)
221 * } NamedCurve;
222 * struct {
223 * NamedCurve named_curve_list<2..2^16-1>
224 * } NamedCurveList;
225 *
226 * The TLS 1.3 supported groups extension was defined to be a compatible
227 * generalization of the TLS 1.2 supported elliptic curves extension. They both
228 * share the same extension identifier.
229 *
230 * DHE groups are not supported yet.
231 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200232MBEDTLS_CHECK_RETURN_CRITICAL
David Horstmann71159f42023-01-03 12:51:59 +0000233static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
234 unsigned char *buf,
235 const unsigned char *end,
236 size_t *out_len)
Ronald Cronfbd9f992022-03-17 15:22:07 +0100237{
David Horstmann71159f42023-01-03 12:51:59 +0000238 unsigned char *p = buf;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100239 unsigned char *named_group_list; /* Start of named_group_list */
240 size_t named_group_list_len; /* Length of named_group_list */
David Horstmann71159f42023-01-03 12:51:59 +0000241 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100242
243 *out_len = 0;
244
David Horstmann71159f42023-01-03 12:51:59 +0000245 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100246
247 /* Check if we have space for header and length fields:
248 * - extension_type (2 bytes)
249 * - extension_data_length (2 bytes)
250 * - named_group_list_length (2 bytes)
251 */
David Horstmann71159f42023-01-03 12:51:59 +0000252 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100253 p += 6;
254
255 named_group_list = p;
256
David Horstmann71159f42023-01-03 12:51:59 +0000257 if (group_list == NULL) {
258 return MBEDTLS_ERR_SSL_BAD_CONFIG;
259 }
Ronald Cronfbd9f992022-03-17 15:22:07 +0100260
David Horstmann71159f42023-01-03 12:51:59 +0000261 for (; *group_list != 0; group_list++) {
262 MBEDTLS_SSL_DEBUG_MSG(1, ("got supported group(%04x)", *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100263
264#if defined(MBEDTLS_ECP_C)
David Horstmann71159f42023-01-03 12:51:59 +0000265 if ((mbedtls_ssl_conf_is_tls13_enabled(ssl->conf) &&
266 mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) ||
267 (mbedtls_ssl_conf_is_tls12_enabled(ssl->conf) &&
268 mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list))) {
Valerio Setti5f10bed2022-12-30 17:44:24 +0100269 if (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) ==
270 MBEDTLS_ECP_DP_NONE) {
Ronald Cronfbd9f992022-03-17 15:22:07 +0100271 continue;
David Horstmann71159f42023-01-03 12:51:59 +0000272 }
273 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
274 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100275 p += 2;
David Horstmann71159f42023-01-03 12:51:59 +0000276 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
Valerio Setti5f10bed2022-12-30 17:44:24 +0100277 mbedtls_ssl_get_curve_name_from_tls_id(*group_list),
278 *group_list));
Ronald Cronfbd9f992022-03-17 15:22:07 +0100279 }
280#endif /* MBEDTLS_ECP_C */
281 /* Add DHE groups here */
282
283 }
284
285 /* Length of named_group_list */
286 named_group_list_len = p - named_group_list;
David Horstmann71159f42023-01-03 12:51:59 +0000287 if (named_group_list_len == 0) {
288 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
289 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100290 }
291
292 /* Write extension_type */
David Horstmann71159f42023-01-03 12:51:59 +0000293 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100294 /* Write extension_data_length */
David Horstmann71159f42023-01-03 12:51:59 +0000295 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100296 /* Write length of named_group_list */
David Horstmann71159f42023-01-03 12:51:59 +0000297 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100298
David Horstmann71159f42023-01-03 12:51:59 +0000299 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
300 buf + 4, named_group_list_len + 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100301
302 *out_len = p - buf;
303
304#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800305 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
David Horstmann71159f42023-01-03 12:51:59 +0000306 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100307#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
308
David Horstmann71159f42023-01-03 12:51:59 +0000309 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100310}
311
312#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
313 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
314
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100316static int ssl_write_client_hello_cipher_suites(
David Horstmann71159f42023-01-03 12:51:59 +0000317 mbedtls_ssl_context *ssl,
318 unsigned char *buf,
319 unsigned char *end,
320 int *tls12_uses_ec,
321 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100322{
323 unsigned char *p = buf;
324 const int *ciphersuite_list;
325 unsigned char *cipher_suites; /* Start of the cipher_suites list */
326 size_t cipher_suites_len;
327
Ronald Crond491c2d2022-02-19 18:30:46 +0100328 *tls12_uses_ec = 0;
329 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100330
331 /*
332 * Ciphersuite list
333 *
334 * This is a list of the symmetric cipher options supported by
335 * the client, specifically the record protection algorithm
336 * ( including secret key length ) and a hash to be used with
337 * HKDF, in descending order of client preference.
338 */
339 ciphersuite_list = ssl->conf->ciphersuite_list;
340
341 /* Check there is space for the cipher suite list length (2 bytes). */
David Horstmann71159f42023-01-03 12:51:59 +0000342 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100343 p += 2;
344
Ronald Cron64767262022-03-31 14:13:57 +0200345 /* Write cipher_suites
346 * CipherSuite cipher_suites<2..2^16-2>;
347 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100348 cipher_suites = p;
David Horstmann71159f42023-01-03 12:51:59 +0000349 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100350 int cipher_suite = ciphersuite_list[i];
351 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
352
David Horstmann71159f42023-01-03 12:51:59 +0000353 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Ronald Crond491c2d2022-02-19 18:30:46 +0100354
David Horstmann71159f42023-01-03 12:51:59 +0000355 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
356 ssl->handshake->min_tls_version,
357 ssl->tls_version) != 0) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100358 continue;
David Horstmann71159f42023-01-03 12:51:59 +0000359 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100360
361#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
David Horstmann71159f42023-01-03 12:51:59 +0000362 (defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
363 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
364 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
Ronald Crond491c2d2022-02-19 18:30:46 +0100365#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100366
David Horstmann71159f42023-01-03 12:51:59 +0000367 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
368 (unsigned int) cipher_suite,
369 ciphersuite_info->name));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100370
371 /* Check there is space for the cipher suite identifier (2 bytes). */
David Horstmann71159f42023-01-03 12:51:59 +0000372 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
373 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100374 p += 2;
375 }
376
Ronald Crond491c2d2022-02-19 18:30:46 +0100377 /*
378 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
379 */
David Horstmann4a285632022-10-06 18:30:10 +0100380 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100381#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann71159f42023-01-03 12:51:59 +0000382 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
Ronald Crond491c2d2022-02-19 18:30:46 +0100383#endif
David Horstmann71159f42023-01-03 12:51:59 +0000384 if (!renegotiating) {
385 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
386 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
387 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
Ronald Crond491c2d2022-02-19 18:30:46 +0100388 p += 2;
389 }
390
Ronald Cron3d580bf2022-02-18 17:24:56 +0100391 /* Write the cipher_suites length in number of bytes */
392 cipher_suites_len = p - cipher_suites;
David Horstmann71159f42023-01-03 12:51:59 +0000393 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
394 MBEDTLS_SSL_DEBUG_MSG(3,
395 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
396 cipher_suites_len/2));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100397
398 /* Output the total length of cipher_suites field. */
399 *out_len = p - buf;
400
David Horstmann71159f42023-01-03 12:51:59 +0000401 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100402}
403
404/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100405 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100406 *
407 * struct {
408 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
409 * Random random;
410 * opaque legacy_session_id<0..32>;
411 * CipherSuite cipher_suites<2..2^16-2>;
412 * opaque legacy_compression_methods<1..2^8-1>;
413 * Extension extensions<8..2^16-1>;
414 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100415 *
416 * Structure of the (D)TLS 1.2 ClientHello message:
417 *
418 * struct {
419 * ProtocolVersion client_version;
420 * Random random;
421 * SessionID session_id;
422 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
423 * CipherSuite cipher_suites<2..2^16-2>;
424 * CompressionMethod compression_methods<1..2^8-1>;
425 * select (extensions_present) {
426 * case false:
427 * struct {};
428 * case true:
429 * Extension extensions<0..2^16-1>;
430 * };
431 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100432 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200433MBEDTLS_CHECK_RETURN_CRITICAL
David Horstmann71159f42023-01-03 12:51:59 +0000434static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
435 unsigned char *buf,
436 unsigned char *end,
437 size_t *out_len,
438 size_t *binders_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100439{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100440 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100441 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100442 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100443 unsigned char *p_extensions_len; /* Pointer to extensions length */
444 size_t output_len; /* Length of buffer used by function */
445 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100446 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100447
Ronald Cron3d580bf2022-02-18 17:24:56 +0100448 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000449 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100450
Ronald Cron4079abc2022-02-20 10:35:26 +0100451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200452 unsigned char propose_tls12 =
David Horstmann71159f42023-01-03 12:51:59 +0000453 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200454 &&
David Horstmann71159f42023-01-03 12:51:59 +0000455 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100456#endif
457#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200458 unsigned char propose_tls13 =
David Horstmann71159f42023-01-03 12:51:59 +0000459 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200460 &&
David Horstmann71159f42023-01-03 12:51:59 +0000461 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100462#endif
463
Ronald Cron3d580bf2022-02-18 17:24:56 +0100464 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100465 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100466 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100467 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100468 */
David Horstmann71159f42023-01-03 12:51:59 +0000469 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
470 mbedtls_ssl_write_version(p, ssl->conf->transport,
471 MBEDTLS_SSL_VERSION_TLS1_2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100472 p += 2;
473
Ronald Cron58b80382022-02-18 18:41:08 +0100474 /* ...
475 * Random random;
476 * ...
477 *
Ronald Cron58b80382022-02-18 18:41:08 +0100478 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100479 * the handshake->randbytes buffer and are copied here into the output
480 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100481 */
David Horstmann71159f42023-01-03 12:51:59 +0000482 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
483 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
484 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
485 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100486 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
487
Ronald Cron021b1782022-02-19 17:32:53 +0100488 /* TLS 1.2:
489 * ...
490 * SessionID session_id;
491 * ...
492 * with
493 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100494 *
Ronald Cron021b1782022-02-19 17:32:53 +0100495 * TLS 1.3:
496 * ...
497 * opaque legacy_session_id<0..32>;
498 * ...
499 *
Ronald Cronda41b382022-03-30 09:57:11 +0200500 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100501 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
502 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100503 */
David Horstmann71159f42023-01-03 12:51:59 +0000504 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
505 *p++ = (unsigned char) ssl->session_negotiate->id_len;
506 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100507 p += ssl->session_negotiate->id_len;
508
David Horstmann71159f42023-01-03 12:51:59 +0000509 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
510 ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100511
Ronald Crona874aa82022-02-19 18:11:26 +0100512 /* DTLS 1.2 ONLY
513 * ...
514 * opaque cookie<0..2^8-1>;
515 * ...
516 */
517#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
David Horstmann71159f42023-01-03 12:51:59 +0000518 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yue01304f2022-04-07 10:51:55 +0800519#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
520 uint8_t cookie_len = 0;
521#else
522 uint16_t cookie_len = 0;
523#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100524
David Horstmann71159f42023-01-03 12:51:59 +0000525 if (handshake->cookie != NULL) {
526 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
527 handshake->cookie,
528 handshake->cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800529 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100530 }
531
David Horstmann71159f42023-01-03 12:51:59 +0000532 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
533 *p++ = (unsigned char) cookie_len;
534 if (cookie_len > 0) {
535 memcpy(p, handshake->cookie, cookie_len);
Ronald Crona874aa82022-02-19 18:11:26 +0100536 p += cookie_len;
537 }
538 }
539#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
540
Ronald Cron3d580bf2022-02-18 17:24:56 +0100541 /* Write cipher_suites */
David Horstmann71159f42023-01-03 12:51:59 +0000542 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
543 &tls12_uses_ec,
544 &output_len);
545 if (ret != 0) {
546 return ret;
547 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100548 p += output_len;
549
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100550 /* Write legacy_compression_methods (TLS 1.3) or
551 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100552 *
553 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
554 * one byte set to zero, which corresponds to the 'null' compression
555 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100556 *
557 * For TLS 1.2 ClientHello, for security reasons we do not support
558 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100559 */
David Horstmann71159f42023-01-03 12:51:59 +0000560 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100561 *p++ = 1;
562 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
563
564 /* Write extensions */
565
566#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
567 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800568 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100569#endif
570
571 /* First write extensions, then the total length */
David Horstmann71159f42023-01-03 12:51:59 +0000572 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100573 p_extensions_len = p;
574 p += 2;
575
Ronald Crondf823bf2022-03-29 18:57:54 +0200576#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
577 /* Write server name extension */
David Horstmann71159f42023-01-03 12:51:59 +0000578 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
579 if (ret != 0) {
580 return ret;
581 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100582 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200583#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100584
585#if defined(MBEDTLS_SSL_ALPN)
David Horstmann71159f42023-01-03 12:51:59 +0000586 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
587 if (ret != 0) {
588 return ret;
589 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100590 p += output_len;
591#endif /* MBEDTLS_SSL_ALPN */
592
593#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
David Horstmann71159f42023-01-03 12:51:59 +0000594 if (propose_tls13) {
595 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
596 &output_len);
597 if (ret != 0) {
598 return ret;
599 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100600 p += output_len;
601 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200602#endif
603
Ronald Cron4079abc2022-02-20 10:35:26 +0100604#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
605 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
David Horstmann71159f42023-01-03 12:51:59 +0000606 if (
Ronald Crondf823bf2022-03-29 18:57:54 +0200607#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
David Horstmann71159f42023-01-03 12:51:59 +0000608 (propose_tls13 &&
609 mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100610#endif
611#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
David Horstmann71159f42023-01-03 12:51:59 +0000612 (propose_tls12 && tls12_uses_ec) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100613#endif
David Horstmann71159f42023-01-03 12:51:59 +0000614 0) {
615 ret = ssl_write_supported_groups_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 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100621#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100622
Ronald Crone68ab4f2022-10-05 12:46:29 +0200623#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
David Horstmann71159f42023-01-03 12:51:59 +0000624 if (
Ronald Cron4079abc2022-02-20 10:35:26 +0100625#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
David Horstmann71159f42023-01-03 12:51:59 +0000626 (propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl)) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100627#endif
628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
629 propose_tls12 ||
630#endif
David Horstmann71159f42023-01-03 12:51:59 +0000631 0) {
632 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
633 if (ret != 0) {
634 return ret;
635 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100636 p += output_len;
637 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200638#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100639
Ronald Cron4079abc2022-02-20 10:35:26 +0100640#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
David Horstmann71159f42023-01-03 12:51:59 +0000641 if (propose_tls12) {
642 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
643 tls12_uses_ec,
644 &output_len);
645 if (ret != 0) {
646 return ret;
647 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100648 p += output_len;
649 }
650#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100651
Ronald Cron41a443a2022-10-04 16:38:25 +0200652#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000653 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
654 * MUST be the last extension in the ClientHello.
655 */
David Horstmann71159f42023-01-03 12:51:59 +0000656 if (propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000657 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
David Horstmann71159f42023-01-03 12:51:59 +0000658 ssl, p, end, &output_len, binders_len);
659 if (ret != 0) {
660 return ret;
661 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000662 p += output_len;
663 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200664#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000665
Ronald Cron3d580bf2022-02-18 17:24:56 +0100666 /* Write the length of the list of extensions. */
667 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100668
David Horstmann71159f42023-01-03 12:51:59 +0000669 if (extensions_len == 0) {
670 p = p_extensions_len;
671 } else {
672 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
673 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
674 MBEDTLS_PRINTF_SIZET, extensions_len));
675 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
676 p_extensions_len, extensions_len);
Ronald Cron4079abc2022-02-20 10:35:26 +0100677 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100678
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800679#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu7de2ff02022-11-08 21:43:46 +0800680 MBEDTLS_SSL_PRINT_EXTS(
David Horstmann71159f42023-01-03 12:51:59 +0000681 3, MBEDTLS_SSL_HS_CLIENT_HELLO, handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800682#endif
683
Ronald Cron3d580bf2022-02-18 17:24:56 +0100684 *out_len = p - buf;
David Horstmann71159f42023-01-03 12:51:59 +0000685 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100686}
687
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200688MBEDTLS_CHECK_RETURN_CRITICAL
David Horstmann71159f42023-01-03 12:51:59 +0000689static int ssl_generate_random(mbedtls_ssl_context *ssl)
Ronald Cron58b80382022-02-18 18:41:08 +0100690{
691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
692 unsigned char *randbytes = ssl->handshake->randbytes;
693 size_t gmt_unix_time_len = 0;
694
695 /*
696 * Generate the random bytes
697 *
698 * TLS 1.2 case:
699 * struct {
700 * uint32 gmt_unix_time;
701 * opaque random_bytes[28];
702 * } Random;
703 *
704 * TLS 1.3 case:
705 * opaque Random[32];
706 */
David Horstmann71159f42023-01-03 12:51:59 +0000707 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Ronald Cron58b80382022-02-18 18:41:08 +0100708#if defined(MBEDTLS_HAVE_TIME)
David Horstmann71159f42023-01-03 12:51:59 +0000709 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
710 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
Ronald Cron58b80382022-02-18 18:41:08 +0100711 gmt_unix_time_len = 4;
712
David Horstmann71159f42023-01-03 12:51:59 +0000713 MBEDTLS_SSL_DEBUG_MSG(3,
714 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
715 (long long) gmt_unix_time));
Ronald Cron58b80382022-02-18 18:41:08 +0100716#endif /* MBEDTLS_HAVE_TIME */
717 }
718
David Horstmann71159f42023-01-03 12:51:59 +0000719 ret = ssl->conf->f_rng(ssl->conf->p_rng,
720 randbytes + gmt_unix_time_len,
721 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
722 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100723}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000724
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200725MBEDTLS_CHECK_RETURN_CRITICAL
David Horstmann71159f42023-01-03 12:51:59 +0000726static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100727{
728 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100729 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800730 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
731
David Horstmann71159f42023-01-03 12:51:59 +0000732 if (session_negotiate == NULL) {
733 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
734 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100735
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800736#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
737 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
738 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800739
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800740 /* Check if a tls13 ticket has been configured. */
David Horstmann71159f42023-01-03 12:51:59 +0000741 if (ssl->handshake->resume != 0 &&
Jerry Yu22c18c12022-10-11 15:58:51 +0800742 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
David Horstmann71159f42023-01-03 12:51:59 +0000743 session_negotiate->ticket != NULL) {
744 mbedtls_time_t now = mbedtls_time(NULL);
745 uint64_t age = (uint64_t) (now - session_negotiate->ticket_received);
746 if (session_negotiate->ticket_received > now ||
747 age > session_negotiate->ticket_lifetime) {
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800748 /* Without valid ticket, disable session resumption.*/
749 MBEDTLS_SSL_DEBUG_MSG(
David Horstmann71159f42023-01-03 12:51:59 +0000750 3, ("Ticket expired, disable session resumption"));
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800751 ssl->handshake->resume = 0;
752 }
753 }
754#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
755 MBEDTLS_SSL_SESSION_TICKETS &&
756 MBEDTLS_HAVE_TIME */
757
David Horstmann71159f42023-01-03 12:51:59 +0000758 if (ssl->conf->f_rng == NULL) {
759 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
760 return MBEDTLS_ERR_SSL_NO_RNG;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100761 }
762
Ronald Cron86a477f2022-02-18 17:45:10 +0100763 /* Bet on the highest configured version if we are not in a TLS 1.2
764 * renegotiation or session resumption.
765 */
766#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann71159f42023-01-03 12:51:59 +0000767 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Glenn Strausscd78df62022-04-07 19:07:11 -0400768 ssl->handshake->min_tls_version = ssl->tls_version;
David Horstmann71159f42023-01-03 12:51:59 +0000769 } else
Ronald Cron86a477f2022-02-18 17:45:10 +0100770#endif
771 {
David Horstmann71159f42023-01-03 12:51:59 +0000772 if (ssl->handshake->resume) {
773 ssl->tls_version = session_negotiate->tls_version;
774 ssl->handshake->min_tls_version = ssl->tls_version;
775 } else {
776 ssl->tls_version = ssl->conf->max_tls_version;
777 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100778 }
779 }
780
Ronald Cron58b80382022-02-18 18:41:08 +0100781 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200782 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000783 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200784 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100785 */
786#if defined(MBEDTLS_SSL_PROTO_DTLS)
David Horstmann71159f42023-01-03 12:51:59 +0000787 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
788 (ssl->handshake->cookie == NULL))
Ronald Cron58b80382022-02-18 18:41:08 +0100789#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100790 {
David Horstmann71159f42023-01-03 12:51:59 +0000791 ret = ssl_generate_random(ssl);
792 if (ret != 0) {
793 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
794 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100795 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100796 }
797
Ronald Cron3d580bf2022-02-18 17:24:56 +0100798 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200799 * Prepare session identifier. At that point, the length of the session
800 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
801 * to zero, except in the case of a TLS 1.2 session renegotiation or
802 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100803 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800804 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100805
806#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
David Horstmann71159f42023-01-03 12:51:59 +0000807 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
808 if (session_id_len < 16 || session_id_len > 32 ||
Ronald Cron021b1782022-02-19 17:32:53 +0100809#if defined(MBEDTLS_SSL_RENEGOTIATION)
810 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
811#endif
David Horstmann71159f42023-01-03 12:51:59 +0000812 ssl->handshake->resume == 0) {
Ronald Cron021b1782022-02-19 17:32:53 +0100813 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100814 }
Ronald Cron021b1782022-02-19 17:32:53 +0100815
816#if defined(MBEDTLS_SSL_SESSION_TICKETS)
David Horstmann71159f42023-01-03 12:51:59 +0000817 /*
818 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
819 * generate and include a Session ID in the TLS ClientHello."
820 */
David Horstmann4a285632022-10-06 18:30:10 +0100821 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100822#if defined(MBEDTLS_SSL_RENEGOTIATION)
David Horstmann71159f42023-01-03 12:51:59 +0000823 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann7aee0ec2022-10-25 10:38:25 +0100824 renegotiating = 1;
David Horstmann71159f42023-01-03 12:51:59 +0000825 }
Ronald Cron021b1782022-02-19 17:32:53 +0100826#endif
David Horstmann71159f42023-01-03 12:51:59 +0000827 if (!renegotiating) {
828 if ((session_negotiate->ticket != NULL) &&
829 (session_negotiate->ticket_len != 0)) {
Ronald Cron021b1782022-02-19 17:32:53 +0100830 session_id_len = 32;
831 }
832 }
833#endif /* MBEDTLS_SSL_SESSION_TICKETS */
834 }
835#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
836
837#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
David Horstmann71159f42023-01-03 12:51:59 +0000838 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron021b1782022-02-19 17:32:53 +0100839 /*
840 * Create a legacy session identifier for the purpose of middlebox
841 * compatibility only if one has not been created already, which is
842 * the case if we are here for the TLS 1.3 second ClientHello.
843 *
844 * Versions of TLS before TLS 1.3 supported a "session resumption"
845 * feature which has been merged with pre-shared keys in TLS 1.3
846 * version. A client which has a cached session ID set by a pre-TLS 1.3
847 * server SHOULD set this field to that value. In compatibility mode,
848 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
849 * session MUST generate a new 32-byte value. This value need not be
850 * random but SHOULD be unpredictable to avoid implementations fixating
851 * on a specific value (also known as ossification). Otherwise, it MUST
852 * be set as a zero-length vector ( i.e., a zero-valued single byte
853 * length field ).
854 */
855 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100856 }
857#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
858
David Horstmann71159f42023-01-03 12:51:59 +0000859 if (session_id_len != session_negotiate->id_len) {
Jerry Yu22c18c12022-10-11 15:58:51 +0800860 session_negotiate->id_len = session_id_len;
David Horstmann71159f42023-01-03 12:51:59 +0000861 if (session_id_len > 0) {
862 ret = ssl->conf->f_rng(ssl->conf->p_rng,
863 session_negotiate->id,
864 session_id_len);
865 if (ret != 0) {
866 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
867 return ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100868 }
869 }
870 }
871
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000872#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000873 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000874 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
David Horstmann71159f42023-01-03 12:51:59 +0000875 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
876 ssl->handshake->resume) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000877 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000878 session_negotiate->hostname != NULL;
David Horstmann71159f42023-01-03 12:51:59 +0000879 if (ssl->hostname != NULL && session_negotiate->hostname != NULL) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000880 hostname_mismatch = strcmp(
David Horstmann71159f42023-01-03 12:51:59 +0000881 ssl->hostname, session_negotiate->hostname) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000882 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000883
David Horstmann71159f42023-01-03 12:51:59 +0000884 if (hostname_mismatch) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000885 MBEDTLS_SSL_DEBUG_MSG(
David Horstmann71159f42023-01-03 12:51:59 +0000886 1, ("Hostname mismatch the session ticket, "
887 "disable session resumption."));
888 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000889 }
David Horstmann71159f42023-01-03 12:51:59 +0000890 } else {
891 return mbedtls_ssl_session_set_hostname(session_negotiate,
892 ssl->hostname);
Xiaokang Qian03409292022-10-12 02:49:52 +0000893 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000894#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000895 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000896 MBEDTLS_SSL_SERVER_NAME_INDICATION */
897
David Horstmann71159f42023-01-03 12:51:59 +0000898 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100899}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100900/*
901 * Write ClientHello handshake message.
902 * Handler for MBEDTLS_SSL_CLIENT_HELLO
903 */
David Horstmann71159f42023-01-03 12:51:59 +0000904int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100905{
906 int ret = 0;
907 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000908 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100909
David Horstmann71159f42023-01-03 12:51:59 +0000910 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100911
David Horstmann71159f42023-01-03 12:51:59 +0000912 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100913
David Horstmann71159f42023-01-03 12:51:59 +0000914 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
915 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
916 &buf, &buf_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100917
David Horstmann71159f42023-01-03 12:51:59 +0000918 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
919 buf + buf_len,
920 &msg_len,
921 &binders_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100922
Ronald Cron5f4e9122022-02-21 09:50:36 +0100923#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
David Horstmann71159f42023-01-03 12:51:59 +0000924 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Ronald Cron5f4e9122022-02-21 09:50:36 +0100925 ssl->out_msglen = msg_len + 4;
David Horstmann71159f42023-01-03 12:51:59 +0000926 mbedtls_ssl_send_flight_completed(ssl);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100927
Ronald Cron8ecd9932022-03-29 12:26:54 +0200928 /*
929 * The two functions below may try to send data on the network and
930 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
931 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200932 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200933 * set the handshake state to the next state at that point to ensure
934 * that we will not write and send again a ClientHello when we
935 * eventually succeed in sending the pending data.
936 */
David Horstmann71159f42023-01-03 12:51:59 +0000937 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron8ecd9932022-03-29 12:26:54 +0200938
David Horstmann71159f42023-01-03 12:51:59 +0000939 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
940 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
941 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100942 }
943
David Horstmann71159f42023-01-03 12:51:59 +0000944 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
945 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
946 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100947 }
David Horstmann71159f42023-01-03 12:51:59 +0000948 } else
Ronald Cron5f4e9122022-02-21 09:50:36 +0100949#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
950 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000951
David Horstmann71159f42023-01-03 12:51:59 +0000952 mbedtls_ssl_add_hs_hdr_to_checksum(ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
953 msg_len);
954 ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
Ronald Cron41a443a2022-10-04 16:38:25 +0200955#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
David Horstmann71159f42023-01-03 12:51:59 +0000956 if (binders_len > 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000957 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000958 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
David Horstmann71159f42023-01-03 12:51:59 +0000959 ssl, buf + msg_len - binders_len, buf + msg_len));
960 ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
961 binders_len);
XiaokangQianeb69aee2022-07-05 08:21:43 +0000962 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200963#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000964
David Horstmann71159f42023-01-03 12:51:59 +0000965 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
966 buf_len,
967 msg_len));
968 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron5f4e9122022-02-21 09:50:36 +0100969 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100970
Ronald Cron3d580bf2022-02-18 17:24:56 +0100971
972cleanup:
973
David Horstmann71159f42023-01-03 12:51:59 +0000974 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100975 return ret;
976}
977
978#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
979#endif /* MBEDTLS_SSL_CLI_C */