blob: 41b133437de3097c606e87ca9bbe45a135e51617 [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 Yubc20bdd2021-08-24 15:59:48 +0800115 /* Extensions */
116
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 Yu32cd5b12021-08-24 18:07:13 +0800123#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800124 int ret;
Jerry Yu32cd5b12021-08-24 18:07:13 +0800125#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800126 unsigned char* extension_start;
127 size_t cur_ext_len; /* Size of the current extension */
128 size_t total_ext_len; /* Size of list of extensions */
129
Jerry Yubc20bdd2021-08-24 15:59:48 +0800130 /* Buffer management */
131 unsigned char* start = buf;
132 unsigned char* end = buf + buflen;
133
134 /* Ciphersuite-related variables */
135 const int* ciphersuites;
136 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yue885b762021-08-26 17:32:34 +0800137 /* ciphersuite_start points to the start of
138 the ciphersuite list, i.e. to the length field*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800139 unsigned char* ciphersuite_start;
140 size_t ciphersuite_count;
141
142 /* Keeping track of the included extensions */
143 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
144
Jerry Yubc20bdd2021-08-24 15:59:48 +0800145 /* NOTE:
146 * Even for DTLS 1.3, we are writing a TLS handshake header here.
147 * The actual DTLS 1.3 handshake header is inserted in
148 * the record writing routine mbedtls_ssl_write_record().
149 *
150 * For cTLS the length, and the version field
151 * are elided. The random bytes are shorter.
152 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800153
154 if( ssl->conf->max_major_ver == 0 )
155 {
156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
157 "consider using mbedtls_ssl_config_defaults()" ) );
158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
159 }
160
161 ssl->major_ver = ssl->conf->min_major_ver;
162 ssl->minor_ver = ssl->conf->min_minor_ver;
163
164 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
165 * instead of the true version number.
166 *
167 * For DTLS 1.3 we use the legacy version number
168 * {254,253}.
169 *
170 * In cTLS the version number is elided.
171 */
Jerry Yu6f13f642021-08-26 17:18:15 +0800172 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN);
Jerry Yu2ac64192021-08-26 18:38:58 +0800173 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0);
174 buf += 2;
Jerry Yu6f13f642021-08-26 17:18:15 +0800175 buflen -= CLIENT_HELLO_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800176
177 /* Write random bytes */
Jerry Yu6f13f642021-08-26 17:18:15 +0800178 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN);
179 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800180 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
181 buf, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800182
Jerry Yu6f13f642021-08-26 17:18:15 +0800183 buf += CLIENT_HELLO_RAND_BYTES_LEN;
184 buflen -= CLIENT_HELLO_RAND_BYTES_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800185
186 /* Versions of TLS before TLS 1.3 supported a
187 * "session resumption" feature which has been merged with pre-shared
188 * keys in this version. A client which has a
189 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
190 * field to that value. In compatibility mode,
191 * this field MUST be non-empty, so a client not offering a
192 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
193 * need not be random but SHOULD be unpredictable to avoid
194 * implementations fixating on a specific value ( also known as
195 * ossification ). Otherwise, it MUST be set as a zero-length vector
196 * ( i.e., a zero-valued single byte length field ).
197 */
198 if( buflen < 1 )
199 {
200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
201 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
202 }
203
204 *buf++ = 0; /* session id length set to zero */
205 buflen -= 1;
206
207 /*
208 * Ciphersuite list
209 *
210 * This is a list of the symmetric cipher options supported by
211 * the client, specifically the record protection algorithm
212 * ( including secret key length ) and a hash to be used with
213 * HKDF, in descending order of client preference.
214 */
215 ciphersuites = ssl->conf->ciphersuite_list;
216
217 if( buflen < 2 /* for ciphersuite list length */ )
218 {
219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
220 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
221 }
222
223 /* Skip writing ciphersuite length for now */
224 ciphersuite_count = 0;
225 ciphersuite_start = buf;
226 buf += 2;
227 buflen -= 2;
228
Jerry Yue885b762021-08-26 17:32:34 +0800229 for ( size_t i = 0; ciphersuites[i] != 0; i++ )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800230 {
231 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
232
233 if( ciphersuite_info == NULL )
234 continue;
235
236 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
237 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
238 continue;
239
240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yue885b762021-08-26 17:32:34 +0800241 (unsigned int) ciphersuites[i],
242 ciphersuite_info->name ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800243
244 ciphersuite_count++;
245
246 if( buflen < 2 /* for ciphersuite list length */ )
247 {
248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
249 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
250 }
251
Jerry Yu2ac64192021-08-26 18:38:58 +0800252 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0);
Jerry Yubc20bdd2021-08-24 15:59:48 +0800253
Jerry Yu2ac64192021-08-26 18:38:58 +0800254 buf += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800255 buflen -= 2;
256
257 }
258
259 /* write ciphersuite length now */
Jerry Yu2ac64192021-08-26 18:38:58 +0800260 MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0);
261 ciphersuite_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800262
Jerry Yue885b762021-08-26 17:32:34 +0800263 MBEDTLS_SSL_DEBUG_MSG( 3,
264 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites",
265 ciphersuite_count ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800266
267 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
268 * one byte set to zero, which corresponds to the 'null' compression
269 * method in prior versions of TLS.
270 *
271 * For cTLS this field is elided.
272 */
273 if( buflen < 2 /* for ciphersuite list length */ )
274 {
275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
276 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
277 }
278
279 *buf++ = 1;
280 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
281
282 buflen -= 2;
283
284 /* First write extensions, then the total length */
285 extension_start = buf;
286 total_ext_len = 0;
287 buf += 2;
288
289 /* Supported Versions Extension is mandatory with TLS 1.3.
290 *
291 * For cTLS we only need to provide it if there is more than one version
292 * and currently there is only one.
293 */
Jerry Yu92c6b402021-08-27 16:59:09 +0800294 ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
295 if( ret != 0 )
296 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800297 total_ext_len += cur_ext_len;
298 buf += cur_ext_len;
299
300#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
301 /* The supported_groups and the key_share extensions are
302 * REQUIRED for ECDHE ciphersuites.
303 */
Jerry Yuf4436812021-08-26 22:59:56 +0800304 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800305 if( ret != 0 )
306 return( ret );
307
308 total_ext_len += cur_ext_len;
309 buf += cur_ext_len;
310
311 /* The supported_signature_algorithms extension is REQUIRED for
312 * certificate authenticated ciphersuites. */
Jerry Yu9176c3a2021-08-27 14:58:49 +0800313 ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf,
Jerry Yuf4436812021-08-26 22:59:56 +0800314 end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800315 if( ret != 0 )
316 return( ret );
317
318 total_ext_len += cur_ext_len;
319 buf += cur_ext_len;
320
321 /* We need to send the key shares under three conditions:
322 * 1 ) A certificate-based ciphersuite is being offered. In this case
323 * supported_groups and supported_signature extensions have been successfully added.
324 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
325 * psk_key_exchange_modes has been added as the last extension.
326 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
327 */
328
Jerry Yuf4436812021-08-26 22:59:56 +0800329 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800330 if( ret != 0 )
331 return( ret );
332
333 total_ext_len += cur_ext_len;
334 buf += cur_ext_len;
335#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
336
337 /* Add more extensions here */
338
339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
340 total_ext_len ) );
341
342 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
343
344 /* Write extension length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800345 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0);
346 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800347
Jerry Yubc20bdd2021-08-24 15:59:48 +0800348 *len_with_binders = ( extension_start + total_ext_len ) - start;
349 return( 0 );
350}
351
Jerry Yu92c6b402021-08-27 16:59:09 +0800352static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800353{
Jerry Yu92c6b402021-08-27 16:59:09 +0800354 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
355 return( 0 );
356}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800357
Jerry Yu92c6b402021-08-27 16:59:09 +0800358static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
359{
360 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800361
Jerry Yu92c6b402021-08-27 16:59:09 +0800362 if( ssl->conf->f_rng == NULL )
363 {
364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
365 return( MBEDTLS_ERR_SSL_NO_RNG );
366 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800367
Jerry Yu92c6b402021-08-27 16:59:09 +0800368 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
369 ssl->handshake->randbytes,
370 CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 )
371 {
372 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
373 return( ret );
374 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800375
376 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800377}
378
Jerry Yu92c6b402021-08-27 16:59:09 +0800379/*
380 * ClientHello Main entry point.
381 * orchestrates the other functions.
382 */
383static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800384{
Jerry Yu92c6b402021-08-27 16:59:09 +0800385 int ret = 0;
386 unsigned char *buf;
387 size_t buf_len, msg_len;
388
389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
390
391 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) );
392
393 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg,
394 ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
395 &buf, &buf_len ) );
396
397 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello,
398 ( ssl, buf, buf_len, &msg_len ) );
399
400 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
401 msg_len );
402 ssl->handshake->update_checksum( ssl, buf, 0 );
403
404 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) );
405 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg,
406 ( ssl, buf_len, msg_len ) );
407
408cleanup:
409
410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
411 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800412}
413
Jerry Yu92c6b402021-08-27 16:59:09 +0800414int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800415{
Jerry Yu92c6b402021-08-27 16:59:09 +0800416 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800417
Jerry Yu92c6b402021-08-27 16:59:09 +0800418 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
419 {
420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
422 }
423
424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
425
426 switch( ssl->state )
427 {
428 /*
429 * ssl->state is initialized as HELLO_REQUEST. It is same
430 * with CLIENT_HELLO status
431 */
432 case MBEDTLS_SSL_HELLO_REQUEST:
433 case MBEDTLS_SSL_CLIENT_HELLO:
434 ret = ssl_tls13_write_client_hello( ssl );
435 break;
436
437 case MBEDTLS_SSL_SERVER_HELLO:
438 // Stop here : we haven't finished whole flow
439 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
440 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
441 break;
442
443 default:
444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
446 }
447
448 return( ret );
449}
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800450
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800451#endif /* MBEDTLS_SSL_CLI_C */
452
453#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */