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