blob: 62b6ce18f8cc7f5e9fb5f174291c15d8f8c26c65 [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/* Main entry point; orchestrates the other functions */
Jerry Yu6f13f642021-08-26 17:18:15 +080036static int ssl_client_hello_process( mbedtls_ssl_context *ssl );
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080037
Jerry Yub9930e72021-08-06 17:11:51 +080038int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )
39{
Jerry Yua13c7e72021-08-17 10:44:40 +080040 int ret = 0;
41
42 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
43 {
44 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
45 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
46 }
47
48 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
49
50 switch( ssl->state )
51 {
52 case MBEDTLS_SSL_HELLO_REQUEST:
53 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
54 break;
55
56 case MBEDTLS_SSL_CLIENT_HELLO:
57 ret = ssl_client_hello_process( ssl );
58 break;
59
60 case MBEDTLS_SSL_SERVER_HELLO:
61 // Stop here : we haven't finished whole flow
62 ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
63 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
64 break;
65
66 default:
67 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
68 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
69 }
70
71 return( ret );
72}
73
Jerry Yu65dd2cc2021-08-18 16:38:40 +080074
Jerry Yu6f13f642021-08-26 17:18:15 +080075static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl );
76static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl,
77 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +080078 size_t *len_with_binders );
Jerry Yu6f13f642021-08-26 17:18:15 +080079static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080080
Jerry Yu6f13f642021-08-26 17:18:15 +080081static int ssl_client_hello_process( mbedtls_ssl_context *ssl )
Jerry Yua13c7e72021-08-17 10:44:40 +080082{
83 int ret = 0;
Jerry Yu65dd2cc2021-08-18 16:38:40 +080084 unsigned char *buf;
85 size_t buf_len, msg_len;
Jerry Yua13c7e72021-08-17 10:44:40 +080086
87 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
88
Jerry Yu65dd2cc2021-08-18 16:38:40 +080089 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_prepare, ( ssl ) );
90
91 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, ( ssl,
92 MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) );
93
Jerry Yuc7ddeec2021-08-26 16:23:47 +080094 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len, &msg_len ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080095
96 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
97 msg_len );
Jerry Yuc7ddeec2021-08-26 16:23:47 +080098 ssl->handshake->update_checksum( ssl, buf, 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080099
100 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) );
101 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) );
Jerry Yua13c7e72021-08-17 10:44:40 +0800102
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800103cleanup:
104
Jerry Yua13c7e72021-08-17 10:44:40 +0800105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
106 /* client_hello_process haven't finished */
107 ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
108 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +0800109}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800110
Jerry Yu6f13f642021-08-26 17:18:15 +0800111static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800112{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800113 int ret;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800114
Jerry Yu6f13f642021-08-26 17:18:15 +0800115 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
116 ssl->handshake->randbytes,
117 CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 )
Jerry Yuc8a392c2021-08-18 16:46:28 +0800118 {
119 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
120 return( ret );
121 }
122
123 return( 0 );
124}
125
126static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl )
127{
128 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
129
130 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800131}
132
Jerry Yubc20bdd2021-08-24 15:59:48 +0800133/* Write extensions */
134
Jerry Yu6f13f642021-08-26 17:18:15 +0800135static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl,
136 unsigned char *buf,
137 unsigned char *end,
138 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800139
140#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
141
142static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800143 unsigned char *buf,
144 unsigned char *end,
145 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800146
147static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800148 unsigned char *buf,
149 unsigned char *end,
150 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800151
152#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
153
Jerry Yu6f13f642021-08-26 17:18:15 +0800154static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl,
155 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800156 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800157{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800158 /* Extensions */
159
160 /* extension_start
161 * Used during extension writing where the
162 * buffer pointer to the beginning of the
163 * extension list must be kept to write
164 * the total extension list size in the end.
165 */
Jerry Yu32cd5b12021-08-24 18:07:13 +0800166#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800167 int ret;
Jerry Yu32cd5b12021-08-24 18:07:13 +0800168#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800169 unsigned char* extension_start;
170 size_t cur_ext_len; /* Size of the current extension */
171 size_t total_ext_len; /* Size of list of extensions */
172
Jerry Yubc20bdd2021-08-24 15:59:48 +0800173 /* Buffer management */
174 unsigned char* start = buf;
175 unsigned char* end = buf + buflen;
176
177 /* Ciphersuite-related variables */
178 const int* ciphersuites;
179 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
180 size_t i; /* used to iterate through ciphersuite list */
181 /* ciphersuite_start points to the start of the ciphersuite list, i.e. to the length field*/
182 unsigned char* ciphersuite_start;
183 size_t ciphersuite_count;
184
185 /* Keeping track of the included extensions */
186 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
187
Jerry Yubc20bdd2021-08-24 15:59:48 +0800188 /* NOTE:
189 * Even for DTLS 1.3, we are writing a TLS handshake header here.
190 * The actual DTLS 1.3 handshake header is inserted in
191 * the record writing routine mbedtls_ssl_write_record().
192 *
193 * For cTLS the length, and the version field
194 * are elided. The random bytes are shorter.
195 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800196
197 if( ssl->conf->max_major_ver == 0 )
198 {
199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
200 "consider using mbedtls_ssl_config_defaults()" ) );
201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
202 }
203
204 ssl->major_ver = ssl->conf->min_major_ver;
205 ssl->minor_ver = ssl->conf->min_minor_ver;
206
207 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
208 * instead of the true version number.
209 *
210 * For DTLS 1.3 we use the legacy version number
211 * {254,253}.
212 *
213 * In cTLS the version number is elided.
214 */
Jerry Yu6f13f642021-08-26 17:18:15 +0800215 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN);
Jerry Yubc20bdd2021-08-24 15:59:48 +0800216 *buf++ = 0x03;
217 *buf++ = 0x03;
Jerry Yu6f13f642021-08-26 17:18:15 +0800218 buflen -= CLIENT_HELLO_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800219
220 /* Write random bytes */
Jerry Yu6f13f642021-08-26 17:18:15 +0800221 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN);
222 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN );
223 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800224
Jerry Yu6f13f642021-08-26 17:18:15 +0800225 buf += CLIENT_HELLO_RAND_BYTES_LEN;
226 buflen -= CLIENT_HELLO_RAND_BYTES_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800227
228 /* Versions of TLS before TLS 1.3 supported a
229 * "session resumption" feature which has been merged with pre-shared
230 * keys in this version. A client which has a
231 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
232 * field to that value. In compatibility mode,
233 * this field MUST be non-empty, so a client not offering a
234 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
235 * need not be random but SHOULD be unpredictable to avoid
236 * implementations fixating on a specific value ( also known as
237 * ossification ). Otherwise, it MUST be set as a zero-length vector
238 * ( i.e., a zero-valued single byte length field ).
239 */
240 if( buflen < 1 )
241 {
242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
243 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
244 }
245
246 *buf++ = 0; /* session id length set to zero */
247 buflen -= 1;
248
249 /*
250 * Ciphersuite list
251 *
252 * This is a list of the symmetric cipher options supported by
253 * the client, specifically the record protection algorithm
254 * ( including secret key length ) and a hash to be used with
255 * HKDF, in descending order of client preference.
256 */
257 ciphersuites = ssl->conf->ciphersuite_list;
258
259 if( buflen < 2 /* for ciphersuite list length */ )
260 {
261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
262 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
263 }
264
265 /* Skip writing ciphersuite length for now */
266 ciphersuite_count = 0;
267 ciphersuite_start = buf;
268 buf += 2;
269 buflen -= 2;
270
271 for ( i = 0; ciphersuites[i] != 0; i++ )
272 {
273 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
274
275 if( ciphersuite_info == NULL )
276 continue;
277
278 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
279 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
280 continue;
281
282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
283 (unsigned int) ciphersuites[i], ciphersuite_info->name ) );
284
285 ciphersuite_count++;
286
287 if( buflen < 2 /* for ciphersuite list length */ )
288 {
289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
290 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
291 }
292
293 *buf++ = (unsigned char)( ciphersuites[i] >> 8 );
294 *buf++ = (unsigned char)( ciphersuites[i] );
295
296 buflen -= 2;
297
298 }
299
300 /* write ciphersuite length now */
301 *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 >> 8 );
302 *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 );
303
304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", ciphersuite_count ) );
305
306 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
307 * one byte set to zero, which corresponds to the 'null' compression
308 * method in prior versions of TLS.
309 *
310 * For cTLS this field is elided.
311 */
312 if( buflen < 2 /* for ciphersuite list length */ )
313 {
314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
315 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
316 }
317
318 *buf++ = 1;
319 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
320
321 buflen -= 2;
322
323 /* First write extensions, then the total length */
324 extension_start = buf;
325 total_ext_len = 0;
326 buf += 2;
327
328 /* Supported Versions Extension is mandatory with TLS 1.3.
329 *
330 * For cTLS we only need to provide it if there is more than one version
331 * and currently there is only one.
332 */
333 ssl_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
334 total_ext_len += cur_ext_len;
335 buf += cur_ext_len;
336
337#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
338 /* The supported_groups and the key_share extensions are
339 * REQUIRED for ECDHE ciphersuites.
340 */
341 ret = ssl_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
342 if( ret != 0 )
343 return( ret );
344
345 total_ext_len += cur_ext_len;
346 buf += cur_ext_len;
347
348 /* The supported_signature_algorithms extension is REQUIRED for
349 * certificate authenticated ciphersuites. */
350 ret = mbedtls_ssl_write_signature_algorithms_ext( ssl, buf, end, &cur_ext_len );
351 if( ret != 0 )
352 return( ret );
353
354 total_ext_len += cur_ext_len;
355 buf += cur_ext_len;
356
357 /* We need to send the key shares under three conditions:
358 * 1 ) A certificate-based ciphersuite is being offered. In this case
359 * supported_groups and supported_signature extensions have been successfully added.
360 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
361 * psk_key_exchange_modes has been added as the last extension.
362 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
363 */
364
365 ret = ssl_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
366 if( ret != 0 )
367 return( ret );
368
369 total_ext_len += cur_ext_len;
370 buf += cur_ext_len;
371#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
372
373 /* Add more extensions here */
374
375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
376 total_ext_len ) );
377
378 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
379
380 /* Write extension length */
381 *extension_start++ = (unsigned char)( ( total_ext_len >> 8 ) & 0xFF );
382 *extension_start++ = (unsigned char)( ( total_ext_len ) & 0xFF );
383
Jerry Yubc20bdd2021-08-24 15:59:48 +0800384 *len_with_binders = ( extension_start + total_ext_len ) - start;
385 return( 0 );
386}
387
Jerry Yuef6b36b2021-08-24 16:29:02 +0800388/*
389 * ssl_write_supported_versions_ext():
390 *
391 * struct {
392 * ProtocolVersion versions<2..254>;
393 * } SupportedVersions;
394 */
Jerry Yu6f13f642021-08-26 17:18:15 +0800395static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl,
396 unsigned char *buf,
397 unsigned char *end,
398 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800399{
Jerry Yuef6b36b2021-08-24 16:29:02 +0800400 unsigned char *p = buf;
401
402 *olen = 0;
403
404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) );
405
Jerry Yu6f13f642021-08-26 17:18:15 +0800406 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
Jerry Yuef6b36b2021-08-24 16:29:02 +0800407
408 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS >> 8 ) & 0xFF );
409 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS ) & 0xFF );
410
411 /* total length */
412 *p++ = 0x00;
413 *p++ = 3;
414
415 /* length of next field */
416 *p++ = 0x2;
417
418 /* This implementation only supports a single TLS version, and only
419 * advertises a single value.
420 */
421 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
422 ssl->conf->transport, p );
423
424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
425
426 *olen = 7;
Jerry Yu6f13f642021-08-26 17:18:15 +0800427
428 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800429}
430
431#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
432
433static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800434 unsigned char *buf,
435 unsigned char *end,
436 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800437{
438 ((void) ssl);
439 ((void) buf);
440 ((void) end);
441 ((void) olen);
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800442 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
443}
444
Jerry Yubc20bdd2021-08-24 15:59:48 +0800445static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800446 unsigned char *buf,
447 unsigned char *end,
448 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800449{
450 ((void) ssl);
451 ((void) buf);
452 ((void) end);
453 ((void) olen);
454 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
455}
Jerry Yuc8a392c2021-08-18 16:46:28 +0800456
Jerry Yubc20bdd2021-08-24 15:59:48 +0800457#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800458
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800459#endif /* MBEDTLS_SSL_CLI_C */
460
461#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */