blob: a03aa8e461e392e579faf1a182259b55f1a9fe9c [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 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_PROTO_TLS1_3_EXPERIMENTAL)
25
26#if defined(MBEDTLS_SSL_CLI_C)
27
Jerry Yubc20bdd2021-08-24 15:59:48 +080028#include <string.h>
29
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080030#include "ssl_misc.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080031#include <mbedtls/debug.h>
32
Jerry Yu6f13f642021-08-26 17:18:15 +080033#define CLIENT_HELLO_RAND_BYTES_LEN 32
34#define CLIENT_HELLO_VERSION_LEN 2
Jerry Yu65dd2cc2021-08-18 16:38:40 +080035
Jerry Yubc20bdd2021-08-24 15:59:48 +080036/* Write extensions */
37
Jerry Yu92c6b402021-08-27 16:59:09 +080038/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
Jerry Yuf4436812021-08-26 22:59:56 +080045static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +080046 unsigned char *buf,
47 unsigned char *end,
Jerry Yu92c6b402021-08-27 16:59:09 +080048 size_t *olen )
49{
50 unsigned char *p = buf;
51
52 *olen = 0;
53
54 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) );
55
56 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
57
58 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
59
60 /* total length */
61 MBEDTLS_PUT_UINT16_BE( 3, p, 2);
62 p+=4;
63
64 /* length of next field */
65 *p++ = 0x2;
66
67 /* This implementation only supports a single TLS version, and only
68 * advertises a single value.
69 */
70 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
71 ssl->conf->transport, p );
72
73 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
74 ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
75
76 *olen = 7;
77
78 return( 0 );
79}
Jerry Yubc20bdd2021-08-24 15:59:48 +080080
81#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
82
Jerry Yuf4436812021-08-26 22:59:56 +080083static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu92c6b402021-08-27 16:59:09 +080084 unsigned char *buf,
85 unsigned char *end,
86 size_t *olen )
87{
88 ((void) ssl);
89 ((void) buf);
90 ((void) end);
91 ((void) olen);
92 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
Jerry Yuf4436812021-08-26 22:59:56 +080095static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +080096 unsigned char *buf,
97 unsigned char *end,
Jerry Yu92c6b402021-08-27 16:59:09 +080098 size_t *olen )
99{
100 ((void) ssl);
101 ((void) buf);
102 ((void) end);
103 ((void) olen);
104 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
105}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800106
107#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
108
Jerry Yu92c6b402021-08-27 16:59:09 +0800109/* Functions for ClientHello */
110
Jerry Yuf4436812021-08-26 22:59:56 +0800111static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800112 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800113 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800114{
Jerry Yuc4d22442021-08-27 20:04:33 +0800115 /* Extensions */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800116
117 /* extension_start
118 * Used during extension writing where the
119 * buffer pointer to the beginning of the
120 * extension list must be kept to write
121 * the total extension list size in the end.
122 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800123 int ret;
124 unsigned char* extension_start;
125 size_t cur_ext_len; /* Size of the current extension */
126 size_t total_ext_len; /* Size of list of extensions */
127
Jerry Yubc20bdd2021-08-24 15:59:48 +0800128 /* Buffer management */
129 unsigned char* start = buf;
130 unsigned char* end = buf + buflen;
131
132 /* Ciphersuite-related variables */
133 const int* ciphersuites;
134 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yue885b762021-08-26 17:32:34 +0800135 /* ciphersuite_start points to the start of
136 the ciphersuite list, i.e. to the length field*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800137 unsigned char* ciphersuite_start;
138 size_t ciphersuite_count;
139
140 /* Keeping track of the included extensions */
141 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
142
Jerry Yubc20bdd2021-08-24 15:59:48 +0800143 /* NOTE:
144 * Even for DTLS 1.3, we are writing a TLS handshake header here.
145 * The actual DTLS 1.3 handshake header is inserted in
146 * the record writing routine mbedtls_ssl_write_record().
147 *
148 * For cTLS the length, and the version field
149 * are elided. The random bytes are shorter.
150 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800151
152 if( ssl->conf->max_major_ver == 0 )
153 {
154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
155 "consider using mbedtls_ssl_config_defaults()" ) );
156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
157 }
158
159 ssl->major_ver = ssl->conf->min_major_ver;
160 ssl->minor_ver = ssl->conf->min_minor_ver;
161
162 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
163 * instead of the true version number.
164 *
165 * For DTLS 1.3 we use the legacy version number
166 * {254,253}.
167 *
168 * In cTLS the version number is elided.
169 */
Jerry Yu6f13f642021-08-26 17:18:15 +0800170 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN);
Jerry Yu2ac64192021-08-26 18:38:58 +0800171 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0);
172 buf += 2;
Jerry Yu6f13f642021-08-26 17:18:15 +0800173 buflen -= CLIENT_HELLO_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800174
175 /* Write random bytes */
Jerry Yu6f13f642021-08-26 17:18:15 +0800176 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN);
177 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800178 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
179 buf, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800180
Jerry Yu6f13f642021-08-26 17:18:15 +0800181 buf += CLIENT_HELLO_RAND_BYTES_LEN;
182 buflen -= CLIENT_HELLO_RAND_BYTES_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800183
184 /* Versions of TLS before TLS 1.3 supported a
185 * "session resumption" feature which has been merged with pre-shared
186 * keys in this version. A client which has a
187 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
188 * field to that value. In compatibility mode,
189 * this field MUST be non-empty, so a client not offering a
190 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
191 * need not be random but SHOULD be unpredictable to avoid
192 * implementations fixating on a specific value ( also known as
193 * ossification ). Otherwise, it MUST be set as a zero-length vector
194 * ( i.e., a zero-valued single byte length field ).
195 */
196 if( buflen < 1 )
197 {
198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
199 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
200 }
201
202 *buf++ = 0; /* session id length set to zero */
203 buflen -= 1;
204
205 /*
206 * Ciphersuite list
207 *
208 * This is a list of the symmetric cipher options supported by
209 * the client, specifically the record protection algorithm
210 * ( including secret key length ) and a hash to be used with
211 * HKDF, in descending order of client preference.
212 */
213 ciphersuites = ssl->conf->ciphersuite_list;
214
215 if( buflen < 2 /* for ciphersuite list length */ )
216 {
217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
218 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
219 }
220
221 /* Skip writing ciphersuite length for now */
222 ciphersuite_count = 0;
223 ciphersuite_start = buf;
224 buf += 2;
225 buflen -= 2;
226
Jerry Yue885b762021-08-26 17:32:34 +0800227 for ( size_t i = 0; ciphersuites[i] != 0; i++ )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800228 {
229 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
230
231 if( ciphersuite_info == NULL )
232 continue;
233
234 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
235 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
236 continue;
237
238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yue885b762021-08-26 17:32:34 +0800239 (unsigned int) ciphersuites[i],
240 ciphersuite_info->name ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800241
242 ciphersuite_count++;
243
244 if( buflen < 2 /* for ciphersuite list length */ )
245 {
246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
247 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
248 }
249
Jerry Yu2ac64192021-08-26 18:38:58 +0800250 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0);
Jerry Yubc20bdd2021-08-24 15:59:48 +0800251
Jerry Yu2ac64192021-08-26 18:38:58 +0800252 buf += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800253 buflen -= 2;
254
255 }
256
257 /* write ciphersuite length now */
Jerry Yu2ac64192021-08-26 18:38:58 +0800258 MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0);
259 ciphersuite_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800260
Jerry Yue885b762021-08-26 17:32:34 +0800261 MBEDTLS_SSL_DEBUG_MSG( 3,
262 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites",
263 ciphersuite_count ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800264
265 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
266 * one byte set to zero, which corresponds to the 'null' compression
267 * method in prior versions of TLS.
268 *
269 * For cTLS this field is elided.
270 */
271 if( buflen < 2 /* for ciphersuite list length */ )
272 {
273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
274 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
275 }
276
277 *buf++ = 1;
278 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
279
280 buflen -= 2;
281
282 /* First write extensions, then the total length */
283 extension_start = buf;
284 total_ext_len = 0;
285 buf += 2;
286
287 /* Supported Versions Extension is mandatory with TLS 1.3.
288 *
289 * For cTLS we only need to provide it if there is more than one version
290 * and currently there is only one.
291 */
Jerry Yu92c6b402021-08-27 16:59:09 +0800292 ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
293 if( ret != 0 )
294 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800295 total_ext_len += cur_ext_len;
296 buf += cur_ext_len;
297
298#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
299 /* The supported_groups and the key_share extensions are
300 * REQUIRED for ECDHE ciphersuites.
301 */
Jerry Yuf4436812021-08-26 22:59:56 +0800302 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800303 if( ret != 0 )
304 return( ret );
305
306 total_ext_len += cur_ext_len;
307 buf += cur_ext_len;
308
309 /* The supported_signature_algorithms extension is REQUIRED for
310 * certificate authenticated ciphersuites. */
Jerry Yu9176c3a2021-08-27 14:58:49 +0800311 ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf,
Jerry Yuf4436812021-08-26 22:59:56 +0800312 end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800313 if( ret != 0 )
314 return( ret );
315
316 total_ext_len += cur_ext_len;
317 buf += cur_ext_len;
318
319 /* We need to send the key shares under three conditions:
320 * 1 ) A certificate-based ciphersuite is being offered. In this case
321 * supported_groups and supported_signature extensions have been successfully added.
322 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
323 * psk_key_exchange_modes has been added as the last extension.
324 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
325 */
326
Jerry Yuf4436812021-08-26 22:59:56 +0800327 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800328 if( ret != 0 )
329 return( ret );
330
331 total_ext_len += cur_ext_len;
332 buf += cur_ext_len;
333#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
334
335 /* Add more extensions here */
336
337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
338 total_ext_len ) );
339
340 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
341
342 /* Write extension length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800343 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0);
344 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800345
Jerry Yubc20bdd2021-08-24 15:59:48 +0800346 *len_with_binders = ( extension_start + total_ext_len ) - start;
347 return( 0 );
348}
349
Jerry Yu92c6b402021-08-27 16:59:09 +0800350static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800351{
Jerry Yu92c6b402021-08-27 16:59:09 +0800352 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
353 return( 0 );
354}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800355
Jerry Yu92c6b402021-08-27 16:59:09 +0800356static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
357{
358 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800359
Jerry Yu92c6b402021-08-27 16:59:09 +0800360 if( ssl->conf->f_rng == NULL )
361 {
362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
363 return( MBEDTLS_ERR_SSL_NO_RNG );
364 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800365
Jerry Yu92c6b402021-08-27 16:59:09 +0800366 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
367 ssl->handshake->randbytes,
368 CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 )
369 {
370 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
371 return( ret );
372 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800373
374 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800375}
376
Jerry Yu92c6b402021-08-27 16:59:09 +0800377/*
378 * ClientHello Main entry point.
379 * orchestrates the other functions.
380 */
381static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800382{
Jerry Yu92c6b402021-08-27 16:59:09 +0800383 int ret = 0;
384 unsigned char *buf;
385 size_t buf_len, msg_len;
386
387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
388
389 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) );
390
391 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg,
392 ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
393 &buf, &buf_len ) );
394
395 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello,
396 ( ssl, buf, buf_len, &msg_len ) );
397
398 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
399 msg_len );
400 ssl->handshake->update_checksum( ssl, buf, 0 );
401
402 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) );
403 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg,
404 ( ssl, buf_len, msg_len ) );
405
406cleanup:
407
408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
409 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800410}
411
Jerry Yu92c6b402021-08-27 16:59:09 +0800412int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800413{
Jerry Yu92c6b402021-08-27 16:59:09 +0800414 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800415
Jerry Yu92c6b402021-08-27 16:59:09 +0800416 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
417 {
418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
420 }
421
422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
423
424 switch( ssl->state )
425 {
426 /*
427 * ssl->state is initialized as HELLO_REQUEST. It is same
428 * with CLIENT_HELLO status
429 */
430 case MBEDTLS_SSL_HELLO_REQUEST:
431 case MBEDTLS_SSL_CLIENT_HELLO:
432 ret = ssl_tls13_write_client_hello( ssl );
433 break;
434
435 case MBEDTLS_SSL_SERVER_HELLO:
436 // Stop here : we haven't finished whole flow
437 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
438 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
439 break;
440
441 default:
442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
444 }
445
446 return( ret );
447}
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800448
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800449#endif /* MBEDTLS_SSL_CLI_C */
450
451#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */