blob: f6e145b294c7bb63060d0e4f9af1de5897c41f7e [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 Yu65dd2cc2021-08-18 16:38:40 +080033/* Main entry point; orchestrates the other functions */
Jerry Yua13c7e72021-08-17 10:44:40 +080034static int ssl_client_hello_process( mbedtls_ssl_context* ssl );
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080035
Jerry Yub9930e72021-08-06 17:11:51 +080036int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )
37{
Jerry Yua13c7e72021-08-17 10:44:40 +080038 int ret = 0;
39
40 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
41 {
42 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
43 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
44 }
45
46 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
47
48 switch( ssl->state )
49 {
50 case MBEDTLS_SSL_HELLO_REQUEST:
51 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
52 break;
53
54 case MBEDTLS_SSL_CLIENT_HELLO:
55 ret = ssl_client_hello_process( ssl );
56 break;
57
58 case MBEDTLS_SSL_SERVER_HELLO:
59 // Stop here : we haven't finished whole flow
60 ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
61 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
62 break;
63
64 default:
65 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
66 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
67 }
68
69 return( ret );
70}
71
Jerry Yu65dd2cc2021-08-18 16:38:40 +080072
73static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl );
74static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl,
75 unsigned char* buf, size_t buflen,
76 size_t* len_without_binders,
77 size_t* len_with_binders );
78static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl );
79
Jerry Yua13c7e72021-08-17 10:44:40 +080080static int ssl_client_hello_process( mbedtls_ssl_context* ssl )
81{
82 int ret = 0;
Jerry Yu65dd2cc2021-08-18 16:38:40 +080083 unsigned char *buf;
84 size_t buf_len, msg_len;
85 size_t len_without_binders = 0;
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
94 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len,
95 &len_without_binders,
96 &msg_len ) );
97
98 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
99 msg_len );
100 ssl->handshake->update_checksum( ssl, buf, len_without_binders );
101
102 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) );
103 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) );
Jerry Yua13c7e72021-08-17 10:44:40 +0800104
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800105cleanup:
106
Jerry Yua13c7e72021-08-17 10:44:40 +0800107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
108 /* client_hello_process haven't finished */
109 ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
110 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +0800111}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800112
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800113static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl )
114{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800115 int ret;
116 size_t rand_bytes_len;
117
118 if( ssl->conf->f_rng == NULL )
119 {
120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
121 return( MBEDTLS_ERR_SSL_NO_RNG );
122 }
123
124 rand_bytes_len = 32;
125
126 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->handshake->randbytes, rand_bytes_len ) ) != 0 )
127 {
128 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
129 return( ret );
130 }
131
132 return( 0 );
133}
134
135static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl )
136{
137 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
138
139 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800140}
141
Jerry Yubc20bdd2021-08-24 15:59:48 +0800142/* Write extensions */
143
144static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl,
145 unsigned char* buf,
146 unsigned char* end,
147 size_t* olen );
148
149#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
150
151static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
152 unsigned char* buf,
153 unsigned char* end,
154 size_t* olen );
155
156static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl,
157 unsigned char* buf,
158 unsigned char* end,
159 size_t* olen );
160
161#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
162
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800163static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl,
164 unsigned char* buf, size_t buflen,
165 size_t* len_without_binders,
166 size_t* len_with_binders )
167{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800168 /* Extensions */
169
170 /* extension_start
171 * Used during extension writing where the
172 * buffer pointer to the beginning of the
173 * extension list must be kept to write
174 * the total extension list size in the end.
175 */
Jerry Yu32cd5b12021-08-24 18:07:13 +0800176#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800177 int ret;
Jerry Yu32cd5b12021-08-24 18:07:13 +0800178#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800179 unsigned char* extension_start;
180 size_t cur_ext_len; /* Size of the current extension */
181 size_t total_ext_len; /* Size of list of extensions */
182
183 /* Length information */
184 size_t rand_bytes_len;
185 size_t version_len;
186
187 /* Buffer management */
188 unsigned char* start = buf;
189 unsigned char* end = buf + buflen;
190
191 /* Ciphersuite-related variables */
192 const int* ciphersuites;
193 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
194 size_t i; /* used to iterate through ciphersuite list */
195 /* ciphersuite_start points to the start of the ciphersuite list, i.e. to the length field*/
196 unsigned char* ciphersuite_start;
197 size_t ciphersuite_count;
198
199 /* Keeping track of the included extensions */
200 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
201
202 rand_bytes_len = 32;
203
204 /* NOTE:
205 * Even for DTLS 1.3, we are writing a TLS handshake header here.
206 * The actual DTLS 1.3 handshake header is inserted in
207 * the record writing routine mbedtls_ssl_write_record().
208 *
209 * For cTLS the length, and the version field
210 * are elided. The random bytes are shorter.
211 */
212 version_len = 2;
213
214 if( ssl->conf->max_major_ver == 0 )
215 {
216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
217 "consider using mbedtls_ssl_config_defaults()" ) );
218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
219 }
220
221 ssl->major_ver = ssl->conf->min_major_ver;
222 ssl->minor_ver = ssl->conf->min_minor_ver;
223
224 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
225 * instead of the true version number.
226 *
227 * For DTLS 1.3 we use the legacy version number
228 * {254,253}.
229 *
230 * In cTLS the version number is elided.
231 */
232 *buf++ = 0x03;
233 *buf++ = 0x03;
234 buflen -= version_len;
235
236 /* Write random bytes */
237 memcpy( buf, ssl->handshake->randbytes, rand_bytes_len );
238 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, rand_bytes_len );
239
240 buf += rand_bytes_len;
241 buflen -= rand_bytes_len;
242
243 /* Versions of TLS before TLS 1.3 supported a
244 * "session resumption" feature which has been merged with pre-shared
245 * keys in this version. A client which has a
246 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
247 * field to that value. In compatibility mode,
248 * this field MUST be non-empty, so a client not offering a
249 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
250 * need not be random but SHOULD be unpredictable to avoid
251 * implementations fixating on a specific value ( also known as
252 * ossification ). Otherwise, it MUST be set as a zero-length vector
253 * ( i.e., a zero-valued single byte length field ).
254 */
255 if( buflen < 1 )
256 {
257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
258 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
259 }
260
261 *buf++ = 0; /* session id length set to zero */
262 buflen -= 1;
263
264 /*
265 * Ciphersuite list
266 *
267 * This is a list of the symmetric cipher options supported by
268 * the client, specifically the record protection algorithm
269 * ( including secret key length ) and a hash to be used with
270 * HKDF, in descending order of client preference.
271 */
272 ciphersuites = ssl->conf->ciphersuite_list;
273
274 if( buflen < 2 /* for ciphersuite list length */ )
275 {
276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
277 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
278 }
279
280 /* Skip writing ciphersuite length for now */
281 ciphersuite_count = 0;
282 ciphersuite_start = buf;
283 buf += 2;
284 buflen -= 2;
285
286 for ( i = 0; ciphersuites[i] != 0; i++ )
287 {
288 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
289
290 if( ciphersuite_info == NULL )
291 continue;
292
293 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
294 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
295 continue;
296
297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
298 (unsigned int) ciphersuites[i], ciphersuite_info->name ) );
299
300 ciphersuite_count++;
301
302 if( buflen < 2 /* for ciphersuite list length */ )
303 {
304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
305 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
306 }
307
308 *buf++ = (unsigned char)( ciphersuites[i] >> 8 );
309 *buf++ = (unsigned char)( ciphersuites[i] );
310
311 buflen -= 2;
312
313 }
314
315 /* write ciphersuite length now */
316 *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 >> 8 );
317 *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 );
318
319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", ciphersuite_count ) );
320
321 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
322 * one byte set to zero, which corresponds to the 'null' compression
323 * method in prior versions of TLS.
324 *
325 * For cTLS this field is elided.
326 */
327 if( buflen < 2 /* for ciphersuite list length */ )
328 {
329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
330 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
331 }
332
333 *buf++ = 1;
334 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
335
336 buflen -= 2;
337
338 /* First write extensions, then the total length */
339 extension_start = buf;
340 total_ext_len = 0;
341 buf += 2;
342
343 /* Supported Versions Extension is mandatory with TLS 1.3.
344 *
345 * For cTLS we only need to provide it if there is more than one version
346 * and currently there is only one.
347 */
348 ssl_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
349 total_ext_len += cur_ext_len;
350 buf += cur_ext_len;
351
352#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
353 /* The supported_groups and the key_share extensions are
354 * REQUIRED for ECDHE ciphersuites.
355 */
356 ret = ssl_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
357 if( ret != 0 )
358 return( ret );
359
360 total_ext_len += cur_ext_len;
361 buf += cur_ext_len;
362
363 /* The supported_signature_algorithms extension is REQUIRED for
364 * certificate authenticated ciphersuites. */
365 ret = mbedtls_ssl_write_signature_algorithms_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
372 /* We need to send the key shares under three conditions:
373 * 1 ) A certificate-based ciphersuite is being offered. In this case
374 * supported_groups and supported_signature extensions have been successfully added.
375 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
376 * psk_key_exchange_modes has been added as the last extension.
377 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
378 */
379
380 ret = ssl_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
381 if( ret != 0 )
382 return( ret );
383
384 total_ext_len += cur_ext_len;
385 buf += cur_ext_len;
386#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
387
388 /* Add more extensions here */
389
390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
391 total_ext_len ) );
392
393 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
394
395 /* Write extension length */
396 *extension_start++ = (unsigned char)( ( total_ext_len >> 8 ) & 0xFF );
397 *extension_start++ = (unsigned char)( ( total_ext_len ) & 0xFF );
398
399 *len_without_binders = buf - start;
400 *len_with_binders = ( extension_start + total_ext_len ) - start;
401 return( 0 );
402}
403
Jerry Yuef6b36b2021-08-24 16:29:02 +0800404/*
405 * ssl_write_supported_versions_ext():
406 *
407 * struct {
408 * ProtocolVersion versions<2..254>;
409 * } SupportedVersions;
410 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800411static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl,
412 unsigned char* buf,
413 unsigned char* end,
414 size_t* olen )
415{
Jerry Yuef6b36b2021-08-24 16:29:02 +0800416 unsigned char *p = buf;
417
418 *olen = 0;
419
420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) );
421
422 if( end < p || (size_t)( end - p ) < 7 )
423 {
424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
425 return;
426 }
427
428 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS >> 8 ) & 0xFF );
429 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS ) & 0xFF );
430
431 /* total length */
432 *p++ = 0x00;
433 *p++ = 3;
434
435 /* length of next field */
436 *p++ = 0x2;
437
438 /* This implementation only supports a single TLS version, and only
439 * advertises a single value.
440 */
441 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
442 ssl->conf->transport, p );
443
444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
445
446 *olen = 7;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800447}
448
449#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
450
451static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
452 unsigned char* buf,
453 unsigned char* end,
454 size_t* olen )
455{
456 ((void) ssl);
457 ((void) buf);
458 ((void) end);
459 ((void) olen);
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800460 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
461}
462
Jerry Yubc20bdd2021-08-24 15:59:48 +0800463static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl,
464 unsigned char* buf,
465 unsigned char* end,
466 size_t* olen )
467{
468 ((void) ssl);
469 ((void) buf);
470 ((void) end);
471 ((void) olen);
472 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
473}
Jerry Yuc8a392c2021-08-18 16:46:28 +0800474
Jerry Yubc20bdd2021-08-24 15:59:48 +0800475#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800476
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800477#endif /* MBEDTLS_SSL_CLI_C */
478
479#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */