scorpiox-bot-plugin-fetch
←ssl_client.cc
1023 lines · 34 KBHEADBlameDownload
1/*
2 * TLS 1.2 and 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8#include "common.h"
9
10#if defined(MBEDTLS_SSL_CLI_C)
11#if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
12
13#include <string.h>
14
15#include "debug_internal.h"
16#include "mbedtls/error.h"
17#include "mbedtls/platform.h"
18
19#include "ssl_client.h"
20#include "ssl_misc.h"
21#include "ssl_tls13_keys.h"
22#include "ssl_debug_helpers.h"
23
24#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
25MBEDTLS_CHECK_RETURN_CRITICAL
26static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
27 unsigned char *buf,
28 const unsigned char *end,
29 size_t *olen)
30{
31 unsigned char *p = buf;
32 const char *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
33 size_t hostname_len;
34
35 *olen = 0;
36
37 if (hostname == NULL) {
38 return 0;
39 }
40
41 MBEDTLS_SSL_DEBUG_MSG(3,
42 ("client hello, adding server name extension: %s",
43 hostname));
44
45 hostname_len = strlen(hostname);
46
47 MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
48
49 /*
50 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
51 *
52 * In order to provide any of the server names, clients MAY include an
53 * extension of type "server_name" in the (extended) client hello. The
54 * "extension_data" field of this extension SHALL contain
55 * "ServerNameList" where:
56 *
57 * struct {
58 * NameType name_type;
59 * select (name_type) {
60 * case host_name: HostName;
61 * } name;
62 * } ServerName;
63 *
64 * enum {
65 * host_name(0), (255)
66 * } NameType;
67 *
68 * opaque HostName<1..2^16-1>;
69 *
70 * struct {
71 * ServerName server_name_list<1..2^16-1>
72 * } ServerNameList;
73 *
74 */
75 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
76 p += 2;
77
78 MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
79 p += 2;
80
81 MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
82 p += 2;
83
84 *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
85
86 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
87 p += 2;
88
89 memcpy(p, hostname, hostname_len);
90
91 *olen = hostname_len + 9;
92
93#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
94 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
95#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
96 return 0;
97}
98#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
99
100#if defined(MBEDTLS_SSL_ALPN)
101/*
102 * ssl_write_alpn_ext()
103 *
104 * Structure of the application_layer_protocol_negotiation extension in
105 * ClientHello:
106 *
107 * opaque ProtocolName<1..2^8-1>;
108 *
109 * struct {
110 * ProtocolName protocol_name_list<2..2^16-1>
111 * } ProtocolNameList;
112 *
113 */
114MBEDTLS_CHECK_RETURN_CRITICAL
115static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
116 unsigned char *buf,
117 const unsigned char *end,
118 size_t *out_len)
119{
120 unsigned char *p = buf;
121
122 *out_len = 0;
123
124 if (ssl->conf->alpn_list == NULL) {
125 return 0;
126 }
127
128 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
129
130
131 /* Check we have enough space for the extension type (2 bytes), the
132 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
133 */
134 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
135 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
136 /* Skip writing extension and list length for now */
137 p += 6;
138
139 /*
140 * opaque ProtocolName<1..2^8-1>;
141 *
142 * struct {
143 * ProtocolName protocol_name_list<2..2^16-1>
144 * } ProtocolNameList;
145 */
146 for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
147 /*
148 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
149 * protocol names is less than 255.
150 */
151 size_t protocol_name_len = strlen(*cur);
152
153 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + protocol_name_len);
154 *p++ = (unsigned char) protocol_name_len;
155 memcpy(p, *cur, protocol_name_len);
156 p += protocol_name_len;
157 }
158
159 *out_len = (size_t) (p - buf);
160
161 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
162 MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
163
164 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
165 MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
166
167#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
168 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
169#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
170 return 0;
171}
172#endif /* MBEDTLS_SSL_ALPN */
173
174#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
175 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
176/*
177 * Function for writing a supported groups (TLS 1.3) or supported elliptic
178 * curves (TLS 1.2) extension.
179 *
180 * The "extension_data" field of a supported groups extension contains a
181 * "NamedGroupList" value (TLS 1.3 RFC8446):
182 * enum {
183 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
184 * x25519(0x001D), x448(0x001E),
185 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
186 * ffdhe6144(0x0103), ffdhe8192(0x0104),
187 * ffdhe_private_use(0x01FC..0x01FF),
188 * ecdhe_private_use(0xFE00..0xFEFF),
189 * (0xFFFF)
190 * } NamedGroup;
191 * struct {
192 * NamedGroup named_group_list<2..2^16-1>;
193 * } NamedGroupList;
194 *
195 * The "extension_data" field of a supported elliptic curves extension contains
196 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
197 * enum {
198 * deprecated(1..22),
199 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
200 * x25519(29), x448(30),
201 * reserved (0xFE00..0xFEFF),
202 * deprecated(0xFF01..0xFF02),
203 * (0xFFFF)
204 * } NamedCurve;
205 * struct {
206 * NamedCurve named_curve_list<2..2^16-1>
207 * } NamedCurveList;
208 *
209 * The TLS 1.3 supported groups extension was defined to be a compatible
210 * generalization of the TLS 1.2 supported elliptic curves extension. They both
211 * share the same extension identifier.
212 *
213 * If the TLS 1.3 logic for selecting proposed groups changes, the TLS 1.3
214 * group filtering in the function `ssl_tls13_parse_hrr_key_share_ext()` must
215 * be updated accordingly.
216 *
217 */
218#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG 1
219#define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG 2
220
221MBEDTLS_CHECK_RETURN_CRITICAL
222static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
223 unsigned char *buf,
224 const unsigned char *end,
225 int flags,
226 size_t *out_len)
227{
228 unsigned char *p = buf;
229 unsigned char *named_group_list; /* Start of named_group_list */
230 size_t named_group_list_len; /* Length of named_group_list */
231 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
232
233 *out_len = 0;
234
235 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
236
237 /* Check if we have space for header and length fields:
238 * - extension_type (2 bytes)
239 * - extension_data_length (2 bytes)
240 * - named_group_list_length (2 bytes)
241 */
242 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
243 p += 6;
244
245 named_group_list = p;
246
247 if (group_list == NULL) {
248 return MBEDTLS_ERR_SSL_BAD_CONFIG;
249 }
250
251 for (; *group_list != 0; group_list++) {
252 int propose_group = 0;
253
254 MBEDTLS_SSL_DEBUG_MSG(3, ("got supported group(%04x)", *group_list));
255
256#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
257 if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
258#if defined(PSA_WANT_ALG_ECDH)
259 if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list) &&
260 mbedtls_ssl_get_psa_curve_info_from_tls_id(
261 *group_list, NULL, NULL) == PSA_SUCCESS) {
262 propose_group = 1;
263 }
264#endif
265#if defined(PSA_WANT_ALG_FFDH)
266 if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
267 propose_group = 1;
268 }
269#endif
270 }
271#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
272
273#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
274 if ((flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG) &&
275 mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list) &&
276 (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
277 MBEDTLS_ECP_DP_NONE)) {
278 propose_group = 1;
279 }
280#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC */
281
282 if (propose_group) {
283 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
284 MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
285 p += 2;
286 MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
287 mbedtls_ssl_named_group_to_str(*group_list),
288 *group_list));
289 }
290 }
291
292 /* Length of named_group_list */
293 named_group_list_len = (size_t) (p - named_group_list);
294 if (named_group_list_len == 0) {
295 MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
296 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
297 }
298
299 /* Write extension_type */
300 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
301 /* Write extension_data_length */
302 MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
303 /* Write length of named_group_list */
304 MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
305
306 MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
307 buf + 4, named_group_list_len + 2);
308
309 *out_len = (size_t) (p - buf);
310
311#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
312 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
313 ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
314#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
315
316 return 0;
317}
318#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
319 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
320
321MBEDTLS_CHECK_RETURN_CRITICAL
322static int ssl_write_client_hello_cipher_suites(
323 mbedtls_ssl_context *ssl,
324 unsigned char *buf,
325 unsigned char *end,
326 int *tls12_uses_ec,
327 size_t *out_len)
328{
329 unsigned char *p = buf;
330 const int *ciphersuite_list;
331 unsigned char *cipher_suites; /* Start of the cipher_suites list */
332 size_t cipher_suites_len;
333
334 *tls12_uses_ec = 0;
335 *out_len = 0;
336
337 /*
338 * Ciphersuite list
339 *
340 * This is a list of the symmetric cipher options supported by
341 * the client, specifically the record protection algorithm
342 * ( including secret key length ) and a hash to be used with
343 * HKDF, in descending order of client preference.
344 */
345 ciphersuite_list = ssl->conf->ciphersuite_list;
346
347 /* Check there is space for the cipher suite list length (2 bytes). */
348 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
349 p += 2;
350
351 /* Write cipher_suites
352 * CipherSuite cipher_suites<2..2^16-2>;
353 */
354 cipher_suites = p;
355 for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
356 int cipher_suite = ciphersuite_list[i];
357 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
358
359 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
360
361 if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
362 ssl->handshake->min_tls_version,
363 ssl->tls_version) != 0) {
364 continue;
365 }
366
367#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
368 (defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
369 defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
370 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
371 *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
372#endif
373
374 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
375 (unsigned int) cipher_suite,
376 ciphersuite_info->name));
377
378 /* Check there is space for the cipher suite identifier (2 bytes). */
379 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
380 MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
381 p += 2;
382 }
383
384 /*
385 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
386 */
387 int renegotiating = 0;
388#if defined(MBEDTLS_SSL_RENEGOTIATION)
389 renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
390#endif
391 if (!renegotiating) {
392 MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
393 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
394 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
395 p += 2;
396 }
397
398 /* Write the cipher_suites length in number of bytes */
399 cipher_suites_len = (size_t) (p - cipher_suites);
400 MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
401 MBEDTLS_SSL_DEBUG_MSG(3,
402 ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
403 cipher_suites_len/2));
404
405 /* Output the total length of cipher_suites field. */
406 *out_len = (size_t) (p - buf);
407
408 return 0;
409}
410
411/*
412 * Structure of the TLS 1.3 ClientHello message:
413 *
414 * struct {
415 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
416 * Random random;
417 * opaque legacy_session_id<0..32>;
418 * CipherSuite cipher_suites<2..2^16-2>;
419 * opaque legacy_compression_methods<1..2^8-1>;
420 * Extension extensions<8..2^16-1>;
421 * } ClientHello;
422 *
423 * Structure of the (D)TLS 1.2 ClientHello message:
424 *
425 * struct {
426 * ProtocolVersion client_version;
427 * Random random;
428 * SessionID session_id;
429 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
430 * CipherSuite cipher_suites<2..2^16-2>;
431 * CompressionMethod compression_methods<1..2^8-1>;
432 * select (extensions_present) {
433 * case false:
434 * struct {};
435 * case true:
436 * Extension extensions<0..2^16-1>;
437 * };
438 * } ClientHello;
439 */
440MBEDTLS_CHECK_RETURN_CRITICAL
441static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
442 unsigned char *buf,
443 unsigned char *end,
444 size_t *out_len,
445 size_t *binders_len)
446{
447 int ret;
448 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
449 unsigned char *p = buf;
450 unsigned char *p_extensions_len; /* Pointer to extensions length */
451 size_t output_len; /* Length of buffer used by function */
452 size_t extensions_len; /* Length of the list of extensions*/
453 int tls12_uses_ec = 0;
454
455 *out_len = 0;
456 *binders_len = 0;
457
458#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
459 unsigned char propose_tls12 =
460 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
461 &&
462 (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
463#endif
464#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
465 unsigned char propose_tls13 =
466 (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
467 &&
468 (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
469#endif
470
471 /*
472 * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
473 *
474 * In all cases this is the TLS 1.2 version.
475 */
476 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
477 mbedtls_ssl_write_version(p, ssl->conf->transport,
478 MBEDTLS_SSL_VERSION_TLS1_2);
479 p += 2;
480
481 /* ...
482 * Random random;
483 * ...
484 *
485 * The random bytes have been prepared by ssl_prepare_client_hello() into
486 * the handshake->randbytes buffer and are copied here into the output
487 * buffer.
488 */
489 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
490 memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
491 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
492 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
493 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
494
495 /* TLS 1.2:
496 * ...
497 * SessionID session_id;
498 * ...
499 * with
500 * opaque SessionID<0..32>;
501 *
502 * TLS 1.3:
503 * ...
504 * opaque legacy_session_id<0..32>;
505 * ...
506 *
507 * The (legacy) session identifier bytes have been prepared by
508 * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
509 * and are copied here into the output buffer.
510 */
511 MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
512 *p++ = (unsigned char) ssl->session_negotiate->id_len;
513 memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
514 p += ssl->session_negotiate->id_len;
515
516 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
517 ssl->session_negotiate->id_len);
518
519 /* DTLS 1.2 ONLY
520 * ...
521 * opaque cookie<0..2^8-1>;
522 * ...
523 */
524#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
525 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
526#if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
527 uint8_t cookie_len = 0;
528#else
529 uint16_t cookie_len = 0;
530#endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
531
532 if (handshake->cookie != NULL) {
533 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
534 handshake->cookie,
535 handshake->cookie_len);
536 cookie_len = handshake->cookie_len;
537 }
538
539 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
540 *p++ = (unsigned char) cookie_len;
541 if (cookie_len > 0) {
542 memcpy(p, handshake->cookie, cookie_len);
543 p += cookie_len;
544 }
545 }
546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
547
548 /* Write cipher_suites */
549 ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
550 &tls12_uses_ec,
551 &output_len);
552 if (ret != 0) {
553 return ret;
554 }
555 p += output_len;
556
557 /* Write legacy_compression_methods (TLS 1.3) or
558 * compression_methods (TLS 1.2)
559 *
560 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
561 * one byte set to zero, which corresponds to the 'null' compression
562 * method in prior versions of TLS.
563 *
564 * For TLS 1.2 ClientHello, for security reasons we do not support
565 * compression anymore, thus also just the 'null' compression method.
566 */
567 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
568 *p++ = 1;
569 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
570
571 /* Write extensions */
572
573#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
574 /* Keeping track of the included extensions */
575 handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
576#endif
577
578 /* First write extensions, then the total length */
579 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
580 p_extensions_len = p;
581 p += 2;
582
583#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
584 /* Write server name extension */
585 ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
586 if (ret != 0) {
587 return ret;
588 }
589 p += output_len;
590#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
591
592#if defined(MBEDTLS_SSL_ALPN)
593 ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
594 if (ret != 0) {
595 return ret;
596 }
597 p += output_len;
598#endif /* MBEDTLS_SSL_ALPN */
599
600#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
601 if (propose_tls13) {
602 ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
603 &output_len);
604 if (ret != 0) {
605 return ret;
606 }
607 p += output_len;
608 }
609#endif
610
611#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
612 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
613 {
614 int ssl_write_supported_groups_ext_flags = 0;
615
616#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
617 if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
618 ssl_write_supported_groups_ext_flags |=
619 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG;
620 }
621#endif
622#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
623 if (propose_tls12 && tls12_uses_ec) {
624 ssl_write_supported_groups_ext_flags |=
625 SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG;
626 }
627#endif
628 if (ssl_write_supported_groups_ext_flags != 0) {
629 ret = ssl_write_supported_groups_ext(ssl, p, end,
630 ssl_write_supported_groups_ext_flags,
631 &output_len);
632 if (ret != 0) {
633 return ret;
634 }
635 p += output_len;
636 }
637 }
638#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
639 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
640
641#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
642 int write_sig_alg_ext = 0;
643#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
644 write_sig_alg_ext = write_sig_alg_ext ||
645 (propose_tls13 && mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl));
646#endif
647#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
648 write_sig_alg_ext = write_sig_alg_ext || propose_tls12;
649#endif
650
651 if (write_sig_alg_ext) {
652 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
653 if (ret != 0) {
654 return ret;
655 }
656 p += output_len;
657 }
658#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
659
660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
661 if (propose_tls12) {
662 ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
663 tls12_uses_ec,
664 &output_len);
665 if (ret != 0) {
666 return ret;
667 }
668 p += output_len;
669 }
670#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
671
672#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
673 /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
674 * MUST be the last extension in the ClientHello.
675 */
676 if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl)) {
677 ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
678 ssl, p, end, &output_len, binders_len);
679 if (ret != 0) {
680 return ret;
681 }
682 p += output_len;
683 }
684#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
685
686 /* Write the length of the list of extensions. */
687 extensions_len = (size_t) (p - p_extensions_len) - 2;
688
689 if (extensions_len == 0) {
690 p = p_extensions_len;
691 } else {
692 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
693 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
694 MBEDTLS_PRINTF_SIZET, extensions_len));
695 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
696 p_extensions_len, extensions_len);
697 }
698
699 *out_len = (size_t) (p - buf);
700 return 0;
701}
702
703MBEDTLS_CHECK_RETURN_CRITICAL
704static int ssl_generate_random(mbedtls_ssl_context *ssl)
705{
706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
707 unsigned char *randbytes = ssl->handshake->randbytes;
708 size_t gmt_unix_time_len = 0;
709
710 /*
711 * Generate the random bytes
712 *
713 * TLS 1.2 case:
714 * struct {
715 * uint32 gmt_unix_time;
716 * opaque random_bytes[28];
717 * } Random;
718 *
719 * TLS 1.3 case:
720 * opaque Random[32];
721 */
722 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
723#if defined(MBEDTLS_HAVE_TIME)
724 mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
725 MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
726 gmt_unix_time_len = 4;
727
728 MBEDTLS_SSL_DEBUG_MSG(3,
729 ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
730 (long long) gmt_unix_time));
731#endif /* MBEDTLS_HAVE_TIME */
732 }
733
734 ret = ssl->conf->f_rng(ssl->conf->p_rng,
735 randbytes + gmt_unix_time_len,
736 MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
737 return ret;
738}
739
740MBEDTLS_CHECK_RETURN_CRITICAL
741static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
742{
743 int ret;
744 size_t session_id_len;
745 mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
746
747 if (session_negotiate == NULL) {
748 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
749 }
750
751#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
752 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
753 defined(MBEDTLS_HAVE_TIME)
754
755 /* Check if a tls13 ticket has been configured. */
756 if (ssl->handshake->resume != 0 &&
757 session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
758 session_negotiate->ticket != NULL) {
759 mbedtls_ms_time_t now = mbedtls_ms_time();
760 mbedtls_ms_time_t age = now - session_negotiate->ticket_reception_time;
761 if (age < 0 ||
762 age > (mbedtls_ms_time_t) session_negotiate->ticket_lifetime * 1000) {
763 /* Without valid ticket, disable session resumption.*/
764 MBEDTLS_SSL_DEBUG_MSG(
765 3, ("Ticket expired, disable session resumption"));
766 ssl->handshake->resume = 0;
767 }
768 }
769#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
770 MBEDTLS_SSL_SESSION_TICKETS &&
771 MBEDTLS_HAVE_TIME */
772
773 /* Bet on the highest configured version if we are not in a TLS 1.2
774 * renegotiation or session resumption.
775 */
776#if defined(MBEDTLS_SSL_RENEGOTIATION)
777 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
778 ssl->handshake->min_tls_version = ssl->tls_version;
779 } else
780#endif
781 {
782 if (ssl->handshake->resume) {
783 ssl->tls_version = session_negotiate->tls_version;
784 ssl->handshake->min_tls_version = ssl->tls_version;
785 } else {
786 ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
787 }
788 }
789
790 /*
791 * Generate the random bytes, except when responding to a verify request
792 * where we MUST reuse the previously generated random bytes
793 * (RFC 6347 4.2.1).
794 */
795#if defined(MBEDTLS_SSL_PROTO_DTLS)
796 if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
797 (ssl->handshake->cookie == NULL))
798#endif
799 {
800#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
801 if (!ssl->handshake->hello_retry_request_flag)
802#endif
803 {
804 ret = ssl_generate_random(ssl);
805 if (ret != 0) {
806 MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
807 return ret;
808 }
809 }
810 }
811
812 /*
813 * Prepare session identifier. At that point, the length of the session
814 * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
815 * to zero, except in the case of a TLS 1.2 session renegotiation or
816 * session resumption.
817 */
818 session_id_len = session_negotiate->id_len;
819
820#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
821 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
822 if (session_id_len < 16 || session_id_len > 32 ||
823#if defined(MBEDTLS_SSL_RENEGOTIATION)
824 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
825#endif
826 ssl->handshake->resume == 0) {
827 session_id_len = 0;
828 }
829
830#if defined(MBEDTLS_SSL_SESSION_TICKETS)
831 /*
832 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
833 * generate and include a Session ID in the TLS ClientHello."
834 */
835 int renegotiating = 0;
836#if defined(MBEDTLS_SSL_RENEGOTIATION)
837 if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
838 renegotiating = 1;
839 }
840#endif
841 if (!renegotiating) {
842 if ((session_negotiate->ticket != NULL) &&
843 (session_negotiate->ticket_len != 0)) {
844 session_id_len = 32;
845 }
846 }
847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
848 }
849#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
850
851#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
852 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
853 /*
854 * Create a legacy session identifier for the purpose of middlebox
855 * compatibility only if one has not been created already, which is
856 * the case if we are here for the TLS 1.3 second ClientHello.
857 *
858 * Versions of TLS before TLS 1.3 supported a "session resumption"
859 * feature which has been merged with pre-shared keys in TLS 1.3
860 * version. A client which has a cached session ID set by a pre-TLS 1.3
861 * server SHOULD set this field to that value. In compatibility mode,
862 * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
863 * session MUST generate a new 32-byte value. This value need not be
864 * random but SHOULD be unpredictable to avoid implementations fixating
865 * on a specific value (also known as ossification). Otherwise, it MUST
866 * be set as a zero-length vector ( i.e., a zero-valued single byte
867 * length field ).
868 */
869 session_id_len = 32;
870 }
871#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
872
873 if (session_id_len != session_negotiate->id_len) {
874 session_negotiate->id_len = session_id_len;
875 if (session_id_len > 0) {
876 ret = ssl->conf->f_rng(ssl->conf->p_rng,
877 session_negotiate->id,
878 session_id_len);
879 if (ret != 0) {
880 MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
881 return ret;
882 }
883 }
884 }
885
886#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
887 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
888 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
889 const char *context_hostname = mbedtls_ssl_get_hostname_pointer(ssl);
890 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
891 ssl->handshake->resume) {
892 int hostname_mismatch = context_hostname != NULL ||
893 session_negotiate->hostname != NULL;
894 if (context_hostname != NULL && session_negotiate->hostname != NULL) {
895 hostname_mismatch = strcmp(
896 context_hostname, session_negotiate->hostname) != 0;
897 }
898
899 if (hostname_mismatch) {
900 MBEDTLS_SSL_DEBUG_MSG(
901 1, ("Hostname mismatch the session ticket, "
902 "disable session resumption."));
903 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
904 }
905 } else {
906 return mbedtls_ssl_session_set_hostname(session_negotiate,
907 context_hostname);
908 }
909#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
910 MBEDTLS_SSL_SESSION_TICKETS &&
911 MBEDTLS_SSL_SERVER_NAME_INDICATION */
912
913 return 0;
914}
915/*
916 * Write ClientHello handshake message.
917 * Handler for MBEDTLS_SSL_CLIENT_HELLO
918 */
919int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
920{
921 int ret = 0;
922 unsigned char *buf;
923 size_t buf_len, msg_len, binders_len;
924
925 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
926
927 MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
928
929 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
930 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
931 &buf, &buf_len));
932
933 MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
934 buf + buf_len,
935 &msg_len,
936 &binders_len));
937
938#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
939 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
940 ssl->out_msglen = msg_len + 4;
941 mbedtls_ssl_send_flight_completed(ssl);
942
943 /*
944 * The two functions below may try to send data on the network and
945 * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
946 * fail to do so and the transmission has to be retried later. In that
947 * case as in fatal error cases, we return immediately. But we must have
948 * set the handshake state to the next state at that point to ensure
949 * that we will not write and send again a ClientHello when we
950 * eventually succeed in sending the pending data.
951 */
952 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
953
954 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
955 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
956 return ret;
957 }
958
959 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
960 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
961 return ret;
962 }
963 } else
964#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
965 {
966
967 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
968 MBEDTLS_SSL_HS_CLIENT_HELLO,
969 msg_len);
970 if (ret != 0) {
971 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
972 return ret;
973 }
974 ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
975 if (ret != 0) {
976 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
977 return ret;
978 }
979#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
980 if (binders_len > 0) {
981 MBEDTLS_SSL_PROC_CHK(
982 mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
983 ssl, buf + msg_len - binders_len, buf + msg_len));
984 ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
985 binders_len);
986 if (ret != 0) {
987 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
988 return ret;
989 }
990 }
991#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
992
993 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
994 buf_len,
995 msg_len));
996
997 /*
998 * Set next state. Note that if TLS 1.3 is proposed, this may be
999 * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
1000 */
1001 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1002
1003#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1004 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
1005 MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
1006 ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
1007 }
1008#endif
1009 }
1010
1011#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1012 MBEDTLS_SSL_PRINT_EXTS(
1013 3, MBEDTLS_SSL_HS_CLIENT_HELLO, ssl->handshake->sent_extensions);
1014#endif
1015
1016cleanup:
1017
1018 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
1019 return ret;
1020}
1021
1022#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
1023#endif /* MBEDTLS_SSL_CLI_C */