blob: 7fa3737578f456166a32e848783abec34c5cd95a [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) || \
188 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
189/*
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 Stekiel060012c2023-05-18 14:10:02 +0200277 if ((mbedtls_ssl_conf_is_tls13_enabled(ssl->conf) &&
278 mbedtls_ssl_tls13_named_group_is_dhe(*group_list))) {
279 const char *ffdh_group = NULL;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100280
Przemek Stekiel060012c2023-05-18 14:10:02 +0200281 switch (*group_list) {
282 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
283 ffdh_group = "ffdhe2048";
284 break;
285 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
286 ffdh_group = "ffdhe3072";
287 break;
288 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
289 ffdh_group = "ffdhe4096";
290 break;
291 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
292 ffdh_group = "ffdhe6144";
293 break;
294 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
295 ffdh_group = "ffdhe8192";
296 break;
297 default:
298 break;
299 }
300
301 if (ffdh_group == NULL) {
302 continue;
303 }
304
305 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
306 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
307 p += 2;
308 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
309 ffdh_group, *group_list));
310
Ronald Cronfbd9f992022-03-17 15:22:07 +0100311 }
312
313 /* Length of named_group_list */
314 named_group_list_len = p - named_group_list;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (named_group_list_len == 0) {
316 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
317 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100318 }
319
320 /* Write extension_type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100322 /* Write extension_data_length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100324 /* Write length of named_group_list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
328 buf + 4, named_group_list_len + 2);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100329
330 *out_len = p - buf;
331
332#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800333 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
Ronald Cronfbd9f992022-03-17 15:22:07 +0100335#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return 0;
Ronald Cronfbd9f992022-03-17 15:22:07 +0100338}
339
340#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
341 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
342
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200343MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron71c23322022-02-18 17:29:39 +0100344static int ssl_write_client_hello_cipher_suites(
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 mbedtls_ssl_context *ssl,
346 unsigned char *buf,
347 unsigned char *end,
348 int *tls12_uses_ec,
349 size_t *out_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100350{
351 unsigned char *p = buf;
352 const int *ciphersuite_list;
353 unsigned char *cipher_suites; /* Start of the cipher_suites list */
354 size_t cipher_suites_len;
355
Ronald Crond491c2d2022-02-19 18:30:46 +0100356 *tls12_uses_ec = 0;
357 *out_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100358
359 /*
360 * Ciphersuite list
361 *
362 * This is a list of the symmetric cipher options supported by
363 * the client, specifically the record protection algorithm
364 * ( including secret key length ) and a hash to be used with
365 * HKDF, in descending order of client preference.
366 */
367 ciphersuite_list = ssl->conf->ciphersuite_list;
368
369 /* Check there is space for the cipher suite list length (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100371 p += 2;
372
Ronald Cron64767262022-03-31 14:13:57 +0200373 /* Write cipher_suites
374 * CipherSuite cipher_suites<2..2^16-2>;
375 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100376 cipher_suites = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100378 int cipher_suite = ciphersuite_list[i];
379 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
Ronald Crond491c2d2022-02-19 18:30:46 +0100382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
384 ssl->handshake->min_tls_version,
385 ssl->tls_version) != 0) {
Ronald Cron3d580bf2022-02-18 17:24:56 +0100386 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
Ronald Crond491c2d2022-02-19 18:30:46 +0100388
389#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 (defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
391 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
392 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
Ronald Crond491c2d2022-02-19 18:30:46 +0100393#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
396 (unsigned int) cipher_suite,
397 ciphersuite_info->name));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100398
399 /* Check there is space for the cipher suite identifier (2 bytes). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
401 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100402 p += 2;
403 }
404
Ronald Crond491c2d2022-02-19 18:30:46 +0100405 /*
406 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
407 */
David Horstmann4a285632022-10-06 18:30:10 +0100408 int renegotiating = 0;
Ronald Crond491c2d2022-02-19 18:30:46 +0100409#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
Ronald Crond491c2d2022-02-19 18:30:46 +0100411#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (!renegotiating) {
413 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
414 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
415 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
Ronald Crond491c2d2022-02-19 18:30:46 +0100416 p += 2;
417 }
418
Ronald Cron3d580bf2022-02-18 17:24:56 +0100419 /* Write the cipher_suites length in number of bytes */
420 cipher_suites_len = p - cipher_suites;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
422 MBEDTLS_SSL_DEBUG_MSG(3,
423 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
424 cipher_suites_len/2));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100425
426 /* Output the total length of cipher_suites field. */
427 *out_len = p - buf;
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100430}
431
432/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100433 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100434 *
435 * struct {
436 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
437 * Random random;
438 * opaque legacy_session_id<0..32>;
439 * CipherSuite cipher_suites<2..2^16-2>;
440 * opaque legacy_compression_methods<1..2^8-1>;
441 * Extension extensions<8..2^16-1>;
442 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100443 *
444 * Structure of the (D)TLS 1.2 ClientHello message:
445 *
446 * struct {
447 * ProtocolVersion client_version;
448 * Random random;
449 * SessionID session_id;
450 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
451 * CipherSuite cipher_suites<2..2^16-2>;
452 * CompressionMethod compression_methods<1..2^8-1>;
453 * select (extensions_present) {
454 * case false:
455 * struct {};
456 * case true:
457 * Extension extensions<0..2^16-1>;
458 * };
459 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100460 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100462static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
463 unsigned char *buf,
464 unsigned char *end,
465 size_t *out_len,
466 size_t *binders_len)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100467{
Ronald Cron3d580bf2022-02-18 17:24:56 +0100468 int ret;
Ronald Cron4079abc2022-02-20 10:35:26 +0100469 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron4079abc2022-02-20 10:35:26 +0100470 unsigned char *p = buf;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100471 unsigned char *p_extensions_len; /* Pointer to extensions length */
472 size_t output_len; /* Length of buffer used by function */
473 size_t extensions_len; /* Length of the list of extensions*/
Ronald Cron4079abc2022-02-20 10:35:26 +0100474 int tls12_uses_ec = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100475
Ronald Cron3d580bf2022-02-18 17:24:56 +0100476 *out_len = 0;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000477 *binders_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100478
Ronald Cron4079abc2022-02-20 10:35:26 +0100479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200480 unsigned char propose_tls12 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
Ronald Cron150d5792022-03-30 20:24:51 +0200482 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100484#endif
485#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200486 unsigned char propose_tls13 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
Ronald Cron150d5792022-03-30 20:24:51 +0200488 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
Ronald Cron4079abc2022-02-20 10:35:26 +0100490#endif
491
Ronald Cron3d580bf2022-02-18 17:24:56 +0100492 /*
Ronald Cron1614eb62022-02-18 17:53:01 +0100493 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100494 *
Ronald Cron1614eb62022-02-18 17:53:01 +0100495 * In all cases this is the TLS 1.2 version.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
498 mbedtls_ssl_write_version(p, ssl->conf->transport,
499 MBEDTLS_SSL_VERSION_TLS1_2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100500 p += 2;
501
Ronald Cron58b80382022-02-18 18:41:08 +0100502 /* ...
503 * Random random;
504 * ...
505 *
Ronald Cron58b80382022-02-18 18:41:08 +0100506 * The random bytes have been prepared by ssl_prepare_client_hello() into
Ronald Cron4079abc2022-02-20 10:35:26 +0100507 * the handshake->randbytes buffer and are copied here into the output
508 * buffer.
Ronald Cron58b80382022-02-18 18:41:08 +0100509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
511 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
512 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
513 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100514 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
515
Ronald Cron021b1782022-02-19 17:32:53 +0100516 /* TLS 1.2:
517 * ...
518 * SessionID session_id;
519 * ...
520 * with
521 * opaque SessionID<0..32>;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100522 *
Ronald Cron021b1782022-02-19 17:32:53 +0100523 * TLS 1.3:
524 * ...
525 * opaque legacy_session_id<0..32>;
526 * ...
527 *
Ronald Cronda41b382022-03-30 09:57:11 +0200528 * The (legacy) session identifier bytes have been prepared by
Ronald Cron021b1782022-02-19 17:32:53 +0100529 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
530 * and are copied here into the output buffer.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
533 *p++ = (unsigned char) ssl->session_negotiate->id_len;
534 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100535 p += ssl->session_negotiate->id_len;
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
538 ssl->session_negotiate->id_len);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100539
Ronald Crona874aa82022-02-19 18:11:26 +0100540 /* DTLS 1.2 ONLY
541 * ...
542 * opaque cookie<0..2^8-1>;
543 * ...
544 */
545#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yue01304f2022-04-07 10:51:55 +0800547#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
548 uint8_t cookie_len = 0;
549#else
550 uint16_t cookie_len = 0;
551#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crona874aa82022-02-19 18:11:26 +0100552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (handshake->cookie != NULL) {
554 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
555 handshake->cookie,
556 handshake->cookie_len);
Jerry Yuac5ca5a2022-03-04 12:50:46 +0800557 cookie_len = handshake->cookie_len;
Ronald Crona874aa82022-02-19 18:11:26 +0100558 }
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
561 *p++ = (unsigned char) cookie_len;
562 if (cookie_len > 0) {
563 memcpy(p, handshake->cookie, cookie_len);
Ronald Crona874aa82022-02-19 18:11:26 +0100564 p += cookie_len;
565 }
566 }
567#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
568
Ronald Cron3d580bf2022-02-18 17:24:56 +0100569 /* Write cipher_suites */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
571 &tls12_uses_ec,
572 &output_len);
573 if (ret != 0) {
574 return ret;
575 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100576 p += output_len;
577
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100578 /* Write legacy_compression_methods (TLS 1.3) or
579 * compression_methods (TLS 1.2)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100580 *
581 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
582 * one byte set to zero, which corresponds to the 'null' compression
583 * method in prior versions of TLS.
Ronald Cron42c1cbf2022-02-20 10:24:39 +0100584 *
585 * For TLS 1.2 ClientHello, for security reasons we do not support
586 * compression anymore, thus also just the 'null' compression method.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100587 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100589 *p++ = 1;
590 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
591
592 /* Write extensions */
593
594#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
595 /* Keeping track of the included extensions */
Jerry Yu63a459c2022-10-31 13:38:40 +0800596 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100597#endif
598
599 /* First write extensions, then the total length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100601 p_extensions_len = p;
602 p += 2;
603
Ronald Crondf823bf2022-03-29 18:57:54 +0200604#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
605 /* Write server name extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
607 if (ret != 0) {
608 return ret;
609 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100610 p += output_len;
Ronald Crondf823bf2022-03-29 18:57:54 +0200611#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100612
613#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
615 if (ret != 0) {
616 return ret;
617 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100618 p += output_len;
619#endif /* MBEDTLS_SSL_ALPN */
620
621#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (propose_tls13) {
623 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
624 &output_len);
625 if (ret != 0) {
626 return ret;
627 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100628 p += output_len;
629 }
Ronald Crondf823bf2022-03-29 18:57:54 +0200630#endif
631
Ronald Cron4079abc2022-02-20 10:35:26 +0100632#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
633 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (
Ronald Crondf823bf2022-03-29 18:57:54 +0200635#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 (propose_tls13 &&
637 mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100638#endif
639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 (propose_tls12 && tls12_uses_ec) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100641#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 0) {
643 ret = ssl_write_supported_groups_ext(ssl, p, end, &output_len);
644 if (ret != 0) {
645 return ret;
646 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100647 p += output_len;
648 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100649#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100650
Ronald Crone68ab4f2022-10-05 12:46:29 +0200651#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (
Ronald Cron4079abc2022-02-20 10:35:26 +0100653#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 (propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl)) ||
Ronald Cron4079abc2022-02-20 10:35:26 +0100655#endif
656#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
657 propose_tls12 ||
658#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 0) {
660 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
661 if (ret != 0) {
662 return ret;
663 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100664 p += output_len;
665 }
Ronald Crone68ab4f2022-10-05 12:46:29 +0200666#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100667
Ronald Cron4079abc2022-02-20 10:35:26 +0100668#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (propose_tls12) {
670 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
671 tls12_uses_ec,
672 &output_len);
673 if (ret != 0) {
674 return ret;
675 }
Ronald Cron4079abc2022-02-20 10:35:26 +0100676 p += output_len;
677 }
678#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Cron3d580bf2022-02-18 17:24:56 +0100679
Ronald Cron41a443a2022-10-04 16:38:25 +0200680#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian86981952022-07-19 09:51:50 +0000681 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
682 * MUST be the last extension in the ClientHello.
683 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (propose_tls13 && mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
XiaokangQian3ad67bf2022-07-21 02:26:21 +0000685 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 ssl, p, end, &output_len, binders_len);
687 if (ret != 0) {
688 return ret;
689 }
XiaokangQianeb69aee2022-07-05 08:21:43 +0000690 p += output_len;
691 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200692#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +0000693
Ronald Cron3d580bf2022-02-18 17:24:56 +0100694 /* Write the length of the list of extensions. */
695 extensions_len = p - p_extensions_len - 2;
Ronald Cron4079abc2022-02-20 10:35:26 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (extensions_len == 0) {
698 p = p_extensions_len;
699 } else {
700 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
701 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
702 MBEDTLS_PRINTF_SIZET, extensions_len));
703 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
704 p_extensions_len, extensions_len);
Ronald Cron4079abc2022-02-20 10:35:26 +0100705 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100706
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800707#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu7de2ff02022-11-08 21:43:46 +0800708 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 3, MBEDTLS_SSL_HS_CLIENT_HELLO, handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800710#endif
711
Ronald Cron3d580bf2022-02-18 17:24:56 +0100712 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100714}
715
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200716MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100717static int ssl_generate_random(mbedtls_ssl_context *ssl)
Ronald Cron58b80382022-02-18 18:41:08 +0100718{
719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
720 unsigned char *randbytes = ssl->handshake->randbytes;
721 size_t gmt_unix_time_len = 0;
722
723 /*
724 * Generate the random bytes
725 *
726 * TLS 1.2 case:
727 * struct {
728 * uint32 gmt_unix_time;
729 * opaque random_bytes[28];
730 * } Random;
731 *
732 * TLS 1.3 case:
733 * opaque Random[32];
734 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Ronald Cron58b80382022-02-18 18:41:08 +0100736#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
738 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
Ronald Cron58b80382022-02-18 18:41:08 +0100739 gmt_unix_time_len = 4;
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 MBEDTLS_SSL_DEBUG_MSG(3,
742 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
743 (long long) gmt_unix_time));
Ronald Cron58b80382022-02-18 18:41:08 +0100744#endif /* MBEDTLS_HAVE_TIME */
745 }
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 ret = ssl->conf->f_rng(ssl->conf->p_rng,
748 randbytes + gmt_unix_time_len,
749 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
750 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100751}
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000752
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200753MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100754static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100755{
756 int ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100757 size_t session_id_len;
Jerry Yu22c18c12022-10-11 15:58:51 +0800758 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (session_negotiate == NULL) {
761 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
762 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100763
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800764#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
765 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
766 defined(MBEDTLS_HAVE_TIME)
Jerry Yu22c18c12022-10-11 15:58:51 +0800767
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800768 /* Check if a tls13 ticket has been configured. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (ssl->handshake->resume != 0 &&
Jerry Yu22c18c12022-10-11 15:58:51 +0800770 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 session_negotiate->ticket != NULL) {
772 mbedtls_time_t now = mbedtls_time(NULL);
773 uint64_t age = (uint64_t) (now - session_negotiate->ticket_received);
774 if (session_negotiate->ticket_received > now ||
775 age > session_negotiate->ticket_lifetime) {
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800776 /* Without valid ticket, disable session resumption.*/
777 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 3, ("Ticket expired, disable session resumption"));
Jerry Yu4f77ecf2022-10-10 22:10:08 +0800779 ssl->handshake->resume = 0;
780 }
781 }
782#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
783 MBEDTLS_SSL_SESSION_TICKETS &&
784 MBEDTLS_HAVE_TIME */
785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (ssl->conf->f_rng == NULL) {
787 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
788 return MBEDTLS_ERR_SSL_NO_RNG;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100789 }
790
Ronald Cron86a477f2022-02-18 17:45:10 +0100791 /* Bet on the highest configured version if we are not in a TLS 1.2
792 * renegotiation or session resumption.
793 */
794#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
Glenn Strausscd78df62022-04-07 19:07:11 -0400796 ssl->handshake->min_tls_version = ssl->tls_version;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 } else
Ronald Cron86a477f2022-02-18 17:45:10 +0100798#endif
799 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (ssl->handshake->resume) {
801 ssl->tls_version = session_negotiate->tls_version;
802 ssl->handshake->min_tls_version = ssl->tls_version;
803 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
Ronald Cron86a477f2022-02-18 17:45:10 +0100805 }
806 }
807
Ronald Cron58b80382022-02-18 18:41:08 +0100808 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200809 * Generate the random bytes, except when responding to a verify request
Tom Cosgrove1797b052022-12-04 17:19:59 +0000810 * where we MUST reuse the previously generated random bytes
Ronald Cronda41b382022-03-30 09:57:11 +0200811 * (RFC 6347 4.2.1).
Ronald Cron58b80382022-02-18 18:41:08 +0100812 */
813#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
815 (ssl->handshake->cookie == NULL))
Ronald Cron58b80382022-02-18 18:41:08 +0100816#endif
Ronald Cron3d580bf2022-02-18 17:24:56 +0100817 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 ret = ssl_generate_random(ssl);
819 if (ret != 0) {
820 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
821 return ret;
Ronald Cron58b80382022-02-18 18:41:08 +0100822 }
Ronald Cron3d580bf2022-02-18 17:24:56 +0100823 }
824
Ronald Cron3d580bf2022-02-18 17:24:56 +0100825 /*
Ronald Cronda41b382022-03-30 09:57:11 +0200826 * Prepare session identifier. At that point, the length of the session
827 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
828 * to zero, except in the case of a TLS 1.2 session renegotiation or
829 * session resumption.
Ronald Cron3d580bf2022-02-18 17:24:56 +0100830 */
Jerry Yu22c18c12022-10-11 15:58:51 +0800831 session_id_len = session_negotiate->id_len;
Ronald Cron021b1782022-02-19 17:32:53 +0100832
833#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
835 if (session_id_len < 16 || session_id_len > 32 ||
Ronald Cron021b1782022-02-19 17:32:53 +0100836#if defined(MBEDTLS_SSL_RENEGOTIATION)
837 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
838#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 ssl->handshake->resume == 0) {
Ronald Cron021b1782022-02-19 17:32:53 +0100840 session_id_len = 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100841 }
Ronald Cron021b1782022-02-19 17:32:53 +0100842
843#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 /*
845 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
846 * generate and include a Session ID in the TLS ClientHello."
847 */
David Horstmann4a285632022-10-06 18:30:10 +0100848 int renegotiating = 0;
Ronald Cron021b1782022-02-19 17:32:53 +0100849#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
David Horstmann7aee0ec2022-10-25 10:38:25 +0100851 renegotiating = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 }
Ronald Cron021b1782022-02-19 17:32:53 +0100853#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (!renegotiating) {
855 if ((session_negotiate->ticket != NULL) &&
856 (session_negotiate->ticket_len != 0)) {
Ronald Cron021b1782022-02-19 17:32:53 +0100857 session_id_len = 32;
858 }
859 }
860#endif /* MBEDTLS_SSL_SESSION_TICKETS */
861 }
862#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
863
864#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron021b1782022-02-19 17:32:53 +0100866 /*
867 * Create a legacy session identifier for the purpose of middlebox
868 * compatibility only if one has not been created already, which is
869 * the case if we are here for the TLS 1.3 second ClientHello.
870 *
871 * Versions of TLS before TLS 1.3 supported a "session resumption"
872 * feature which has been merged with pre-shared keys in TLS 1.3
873 * version. A client which has a cached session ID set by a pre-TLS 1.3
874 * server SHOULD set this field to that value. In compatibility mode,
875 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
876 * session MUST generate a new 32-byte value. This value need not be
877 * random but SHOULD be unpredictable to avoid implementations fixating
878 * on a specific value (also known as ossification). Otherwise, it MUST
879 * be set as a zero-length vector ( i.e., a zero-valued single byte
880 * length field ).
881 */
882 session_id_len = 32;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100883 }
884#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (session_id_len != session_negotiate->id_len) {
Jerry Yu22c18c12022-10-11 15:58:51 +0800887 session_negotiate->id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (session_id_len > 0) {
889 ret = ssl->conf->f_rng(ssl->conf->p_rng,
890 session_negotiate->id,
891 session_id_len);
892 if (ret != 0) {
893 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
894 return ret;
Ronald Cron021b1782022-02-19 17:32:53 +0100895 }
896 }
897 }
898
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000899#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +0000900 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000901 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
903 ssl->handshake->resume) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000904 int hostname_mismatch = ssl->hostname != NULL ||
Xiaokang Qian307a7302022-10-12 11:14:32 +0000905 session_negotiate->hostname != NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (ssl->hostname != NULL && session_negotiate->hostname != NULL) {
Xiaokang Qiand7adc372022-10-11 09:05:11 +0000907 hostname_mismatch = strcmp(
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ssl->hostname, session_negotiate->hostname) != 0;
Xiaokang Qianed3afcd2022-10-12 08:31:11 +0000909 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +0000910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (hostname_mismatch) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +0000912 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 1, ("Hostname mismatch the session ticket, "
914 "disable session resumption."));
915 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000916 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 } else {
918 return mbedtls_ssl_session_set_hostname(session_negotiate,
919 ssl->hostname);
Xiaokang Qian03409292022-10-12 02:49:52 +0000920 }
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000921#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
Xiaokang Qian03409292022-10-12 02:49:52 +0000922 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianbc663a02022-10-09 11:14:39 +0000923 MBEDTLS_SSL_SERVER_NAME_INDICATION */
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return 0;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100926}
Ronald Cron3d580bf2022-02-18 17:24:56 +0100927/*
928 * Write ClientHello handshake message.
929 * Handler for MBEDTLS_SSL_CLIENT_HELLO
930 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
Ronald Cron3d580bf2022-02-18 17:24:56 +0100932{
933 int ret = 0;
934 unsigned char *buf;
XiaokangQianeb69aee2022-07-05 08:21:43 +0000935 size_t buf_len, msg_len, binders_len;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
942 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
943 &buf, &buf_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
946 buf + buf_len,
947 &msg_len,
948 &binders_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +0100949
Ronald Cron5f4e9122022-02-21 09:50:36 +0100950#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Ronald Cron5f4e9122022-02-21 09:50:36 +0100952 ssl->out_msglen = msg_len + 4;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_ssl_send_flight_completed(ssl);
Ronald Cron3d580bf2022-02-18 17:24:56 +0100954
Ronald Cron8ecd9932022-03-29 12:26:54 +0200955 /*
956 * The two functions below may try to send data on the network and
957 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
958 * fail to do so and the transmission has to be retried later. In that
Ronald Cronda41b382022-03-30 09:57:11 +0200959 * case as in fatal error cases, we return immediately. But we must have
Ronald Cron8ecd9932022-03-29 12:26:54 +0200960 * set the handshake state to the next state at that point to ensure
961 * that we will not write and send again a ClientHello when we
962 * eventually succeed in sending the pending data.
963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Ronald Cron8ecd9932022-03-29 12:26:54 +0200965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
968 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100969 }
970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
972 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
973 return ret;
Ronald Cron5f4e9122022-02-21 09:50:36 +0100974 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 } else
Ronald Cron5f4e9122022-02-21 09:50:36 +0100976#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
977 {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000978
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100979 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
980 MBEDTLS_SSL_HS_CLIENT_HELLO,
981 msg_len);
982 if (ret != 0) {
983 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
984 return ret;
985 }
986 ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
987 if (ret != 0) {
988 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
989 return ret;
990 }
Ronald Cron41a443a2022-10-04 16:38:25 +0200991#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if (binders_len > 0) {
XiaokangQianeb69aee2022-07-05 08:21:43 +0000993 MBEDTLS_SSL_PROC_CHK(
XiaokangQian86981952022-07-19 09:51:50 +0000994 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 ssl, buf + msg_len - binders_len, buf + msg_len));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100996 ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100997 binders_len);
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100998 if (ret != 0) {
999 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1000 return ret;
1001 }
XiaokangQianeb69aee2022-07-05 08:21:43 +00001002 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001003#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
XiaokangQianeb69aee2022-07-05 08:21:43 +00001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
1006 buf_len,
1007 msg_len));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001008
Xiaokang Qian44051f62023-02-02 06:57:26 +00001009 /*
1010 * Set next state. Note that if TLS 1.3 is proposed, this may be
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001011 * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
Xiaokang Qian44051f62023-02-02 06:57:26 +00001012 */
1013 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001014
Xiaokang Qian44051f62023-02-02 06:57:26 +00001015#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1016 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
1017 MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00001018 ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
Xiaokang Qian44051f62023-02-02 06:57:26 +00001019 }
1020#endif
Xiaokang Qiandf6f52e2022-12-15 14:42:45 +00001021 }
Ronald Cron3d580bf2022-02-18 17:24:56 +01001022
1023cleanup:
1024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
Ronald Cron3d580bf2022-02-18 17:24:56 +01001026 return ret;
1027}
1028
1029#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1030#endif /* MBEDTLS_SSL_CLI_C */