blob: 2cada0decaaf2086856b8a2399b162049113b90e [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,
Jerry Yuc7ddeec2021-08-26 16:23:47 +080076 size_t *len_with_binders );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080077static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl );
78
Jerry Yua13c7e72021-08-17 10:44:40 +080079static int ssl_client_hello_process( mbedtls_ssl_context* ssl )
80{
81 int ret = 0;
Jerry Yu65dd2cc2021-08-18 16:38:40 +080082 unsigned char *buf;
83 size_t buf_len, msg_len;
Jerry Yua13c7e72021-08-17 10:44:40 +080084
85 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
86
Jerry Yu65dd2cc2021-08-18 16:38:40 +080087 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_prepare, ( ssl ) );
88
89 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, ( ssl,
90 MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) );
91
Jerry Yuc7ddeec2021-08-26 16:23:47 +080092 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len, &msg_len ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080093
94 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
95 msg_len );
Jerry Yuc7ddeec2021-08-26 16:23:47 +080096 ssl->handshake->update_checksum( ssl, buf, 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080097
98 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) );
99 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) );
Jerry Yua13c7e72021-08-17 10:44:40 +0800100
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800101cleanup:
102
Jerry Yua13c7e72021-08-17 10:44:40 +0800103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
104 /* client_hello_process haven't finished */
105 ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
106 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +0800107}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800108
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800109static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl )
110{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800111 int ret;
112 size_t rand_bytes_len;
113
114 if( ssl->conf->f_rng == NULL )
115 {
116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
117 return( MBEDTLS_ERR_SSL_NO_RNG );
118 }
119
120 rand_bytes_len = 32;
121
122 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->handshake->randbytes, rand_bytes_len ) ) != 0 )
123 {
124 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
125 return( ret );
126 }
127
128 return( 0 );
129}
130
131static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl )
132{
133 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
134
135 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800136}
137
Jerry Yubc20bdd2021-08-24 15:59:48 +0800138/* Write extensions */
139
140static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl,
141 unsigned char* buf,
142 unsigned char* end,
143 size_t* olen );
144
145#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
146
147static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
148 unsigned char* buf,
149 unsigned char* end,
150 size_t* olen );
151
152static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl,
153 unsigned char* buf,
154 unsigned char* end,
155 size_t* olen );
156
157#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
158
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800159static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl,
160 unsigned char* buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800161 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800162{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800163 /* Extensions */
164
165 /* extension_start
166 * Used during extension writing where the
167 * buffer pointer to the beginning of the
168 * extension list must be kept to write
169 * the total extension list size in the end.
170 */
Jerry Yu32cd5b12021-08-24 18:07:13 +0800171#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800172 int ret;
Jerry Yu32cd5b12021-08-24 18:07:13 +0800173#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800174 unsigned char* extension_start;
175 size_t cur_ext_len; /* Size of the current extension */
176 size_t total_ext_len; /* Size of list of extensions */
177
178 /* Length information */
179 size_t rand_bytes_len;
180 size_t version_len;
181
182 /* Buffer management */
183 unsigned char* start = buf;
184 unsigned char* end = buf + buflen;
185
186 /* Ciphersuite-related variables */
187 const int* ciphersuites;
188 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
189 size_t i; /* used to iterate through ciphersuite list */
190 /* ciphersuite_start points to the start of the ciphersuite list, i.e. to the length field*/
191 unsigned char* ciphersuite_start;
192 size_t ciphersuite_count;
193
194 /* Keeping track of the included extensions */
195 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
196
197 rand_bytes_len = 32;
198
199 /* NOTE:
200 * Even for DTLS 1.3, we are writing a TLS handshake header here.
201 * The actual DTLS 1.3 handshake header is inserted in
202 * the record writing routine mbedtls_ssl_write_record().
203 *
204 * For cTLS the length, and the version field
205 * are elided. The random bytes are shorter.
206 */
207 version_len = 2;
208
209 if( ssl->conf->max_major_ver == 0 )
210 {
211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
212 "consider using mbedtls_ssl_config_defaults()" ) );
213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
214 }
215
216 ssl->major_ver = ssl->conf->min_major_ver;
217 ssl->minor_ver = ssl->conf->min_minor_ver;
218
219 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
220 * instead of the true version number.
221 *
222 * For DTLS 1.3 we use the legacy version number
223 * {254,253}.
224 *
225 * In cTLS the version number is elided.
226 */
227 *buf++ = 0x03;
228 *buf++ = 0x03;
229 buflen -= version_len;
230
231 /* Write random bytes */
232 memcpy( buf, ssl->handshake->randbytes, rand_bytes_len );
233 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, rand_bytes_len );
234
235 buf += rand_bytes_len;
236 buflen -= rand_bytes_len;
237
238 /* Versions of TLS before TLS 1.3 supported a
239 * "session resumption" feature which has been merged with pre-shared
240 * keys in this version. A client which has a
241 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
242 * field to that value. In compatibility mode,
243 * this field MUST be non-empty, so a client not offering a
244 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
245 * need not be random but SHOULD be unpredictable to avoid
246 * implementations fixating on a specific value ( also known as
247 * ossification ). Otherwise, it MUST be set as a zero-length vector
248 * ( i.e., a zero-valued single byte length field ).
249 */
250 if( buflen < 1 )
251 {
252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
253 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
254 }
255
256 *buf++ = 0; /* session id length set to zero */
257 buflen -= 1;
258
259 /*
260 * Ciphersuite list
261 *
262 * This is a list of the symmetric cipher options supported by
263 * the client, specifically the record protection algorithm
264 * ( including secret key length ) and a hash to be used with
265 * HKDF, in descending order of client preference.
266 */
267 ciphersuites = ssl->conf->ciphersuite_list;
268
269 if( buflen < 2 /* for ciphersuite list length */ )
270 {
271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
272 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
273 }
274
275 /* Skip writing ciphersuite length for now */
276 ciphersuite_count = 0;
277 ciphersuite_start = buf;
278 buf += 2;
279 buflen -= 2;
280
281 for ( i = 0; ciphersuites[i] != 0; i++ )
282 {
283 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
284
285 if( ciphersuite_info == NULL )
286 continue;
287
288 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
289 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
290 continue;
291
292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
293 (unsigned int) ciphersuites[i], ciphersuite_info->name ) );
294
295 ciphersuite_count++;
296
297 if( buflen < 2 /* for ciphersuite list length */ )
298 {
299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
300 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
301 }
302
303 *buf++ = (unsigned char)( ciphersuites[i] >> 8 );
304 *buf++ = (unsigned char)( ciphersuites[i] );
305
306 buflen -= 2;
307
308 }
309
310 /* write ciphersuite length now */
311 *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 >> 8 );
312 *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 );
313
314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", ciphersuite_count ) );
315
316 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
317 * one byte set to zero, which corresponds to the 'null' compression
318 * method in prior versions of TLS.
319 *
320 * For cTLS this field is elided.
321 */
322 if( buflen < 2 /* for ciphersuite list length */ )
323 {
324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
325 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
326 }
327
328 *buf++ = 1;
329 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
330
331 buflen -= 2;
332
333 /* First write extensions, then the total length */
334 extension_start = buf;
335 total_ext_len = 0;
336 buf += 2;
337
338 /* Supported Versions Extension is mandatory with TLS 1.3.
339 *
340 * For cTLS we only need to provide it if there is more than one version
341 * and currently there is only one.
342 */
343 ssl_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
344 total_ext_len += cur_ext_len;
345 buf += cur_ext_len;
346
347#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
348 /* The supported_groups and the key_share extensions are
349 * REQUIRED for ECDHE ciphersuites.
350 */
351 ret = ssl_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
352 if( ret != 0 )
353 return( ret );
354
355 total_ext_len += cur_ext_len;
356 buf += cur_ext_len;
357
358 /* The supported_signature_algorithms extension is REQUIRED for
359 * certificate authenticated ciphersuites. */
360 ret = mbedtls_ssl_write_signature_algorithms_ext( ssl, buf, end, &cur_ext_len );
361 if( ret != 0 )
362 return( ret );
363
364 total_ext_len += cur_ext_len;
365 buf += cur_ext_len;
366
367 /* We need to send the key shares under three conditions:
368 * 1 ) A certificate-based ciphersuite is being offered. In this case
369 * supported_groups and supported_signature extensions have been successfully added.
370 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
371 * psk_key_exchange_modes has been added as the last extension.
372 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
373 */
374
375 ret = ssl_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
376 if( ret != 0 )
377 return( ret );
378
379 total_ext_len += cur_ext_len;
380 buf += cur_ext_len;
381#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
382
383 /* Add more extensions here */
384
385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
386 total_ext_len ) );
387
388 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
389
390 /* Write extension length */
391 *extension_start++ = (unsigned char)( ( total_ext_len >> 8 ) & 0xFF );
392 *extension_start++ = (unsigned char)( ( total_ext_len ) & 0xFF );
393
Jerry Yubc20bdd2021-08-24 15:59:48 +0800394 *len_with_binders = ( extension_start + total_ext_len ) - start;
395 return( 0 );
396}
397
Jerry Yuef6b36b2021-08-24 16:29:02 +0800398/*
399 * ssl_write_supported_versions_ext():
400 *
401 * struct {
402 * ProtocolVersion versions<2..254>;
403 * } SupportedVersions;
404 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800405static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl,
406 unsigned char* buf,
407 unsigned char* end,
408 size_t* olen )
409{
Jerry Yuef6b36b2021-08-24 16:29:02 +0800410 unsigned char *p = buf;
411
412 *olen = 0;
413
414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) );
415
416 if( end < p || (size_t)( end - p ) < 7 )
417 {
418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
419 return;
420 }
421
422 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS >> 8 ) & 0xFF );
423 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS ) & 0xFF );
424
425 /* total length */
426 *p++ = 0x00;
427 *p++ = 3;
428
429 /* length of next field */
430 *p++ = 0x2;
431
432 /* This implementation only supports a single TLS version, and only
433 * advertises a single value.
434 */
435 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
436 ssl->conf->transport, p );
437
438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
439
440 *olen = 7;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800441}
442
443#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
444
445static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
446 unsigned char* buf,
447 unsigned char* end,
448 size_t* olen )
449{
450 ((void) ssl);
451 ((void) buf);
452 ((void) end);
453 ((void) olen);
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800454 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
455}
456
Jerry Yubc20bdd2021-08-24 15:59:48 +0800457static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl,
458 unsigned char* buf,
459 unsigned char* end,
460 size_t* olen )
461{
462 ((void) ssl);
463 ((void) buf);
464 ((void) end);
465 ((void) olen);
466 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
467}
Jerry Yuc8a392c2021-08-18 16:46:28 +0800468
Jerry Yubc20bdd2021-08-24 15:59:48 +0800469#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800470
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800471#endif /* MBEDTLS_SSL_CLI_C */
472
473#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */