blob: 41d2a321ec793f7f3ef96ccd71482f313315016a [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 Yu08906d02021-08-31 11:05:27 +080033#define CLIENT_HELLO_RANDOM_LEN 32
34#define CLIENT_HELLO_LEGACY_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 Yueecfbf02021-08-30 18:32:07 +080046 unsigned char *buf,
47 unsigned char *end,
48 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +080049{
50 unsigned char *p = buf;
51
52 *olen = 0;
53
Jerry Yu159c5a02021-08-31 12:51:25 +080054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080055
Jerry Yu159c5a02021-08-31 12:51:25 +080056 /*
57 * ExtensionType 2
58 * ExtensionLength 2
59 * VersionSLength 1
60 * Version 2
61 */
Jerry Yu92c6b402021-08-27 16:59:09 +080062 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
63
Jerry Yu159c5a02021-08-31 12:51:25 +080064 /* Write Extension Type */
Jerry Yueecfbf02021-08-30 18:32:07 +080065 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080066
Jerry Yu159c5a02021-08-31 12:51:25 +080067 /* Write Extension Length */
Jerry Yub7ab3362021-08-31 16:16:19 +080068 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080069 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080070
Jerry Yu159c5a02021-08-31 12:51:25 +080071 /* Length of the SupportedVersions field data */
Jerry Yu92c6b402021-08-27 16:59:09 +080072 *p++ = 0x2;
73
74 /* This implementation only supports a single TLS version, and only
75 * advertises a single value.
76 */
Jerry Yueecfbf02021-08-30 18:32:07 +080077 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
78 ssl->conf->max_minor_ver,
79 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080080
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080082 ssl->conf->max_major_ver,
83 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080084
85 *olen = 7;
86
87 return( 0 );
88}
Jerry Yubc20bdd2021-08-24 15:59:48 +080089
90#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
91
Jerry Yuf4436812021-08-26 22:59:56 +080092static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080093 unsigned char *buf,
94 unsigned char *end,
95 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +080096{
97 ((void) ssl);
98 ((void) buf);
99 ((void) end);
100 ((void) olen);
101 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
102}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800103
Jerry Yuf4436812021-08-26 22:59:56 +0800104static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800105 unsigned char *buf,
106 unsigned char *end,
107 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +0800108{
109 ((void) ssl);
110 ((void) buf);
111 ((void) end);
112 ((void) olen);
113 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
114}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800115
116#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
117
Jerry Yu6a643102021-08-31 14:40:36 +0800118/* Write ciphersuites
119 * CipherSuite cipher_suites<2..2^16-2>;
120 */
121static int ssl_tls13_write_client_hello_ciphersuites(
122 mbedtls_ssl_context *ssl,
123 unsigned char *buf,
124 unsigned char *end,
125 size_t *olen )
126{
127 /* Ciphersuite-related variables */
128 const int *ciphersuites;
129 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
130 /* ciphersuite_start points to the start of
131 the ciphersuite list, i.e. to the length field*/
132 unsigned char *ciphersuite_start, *ciphersuite_iter;
133 size_t buf_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800134
Jerry Yu6a643102021-08-31 14:40:36 +0800135 *olen = 0 ;
136
137 /*
138 * Ciphersuite list
139 *
140 * This is a list of the symmetric cipher options supported by
141 * the client, specifically the record protection algorithm
142 * ( including secret key length ) and a hash to be used with
143 * HKDF, in descending order of client preference.
144 */
145 ciphersuites = ssl->conf->ciphersuite_list;
146
147 /* Check available spaces for ciphersuite */
148 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
149
150 /* Write ciphersuites */
151 ciphersuite_start = buf + 2;
152 ciphersuite_iter = ciphersuite_start;
153
154 for ( size_t i = 0; ciphersuites[i] != 0; i++ )
155 {
156 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
157
158 if( ciphersuite_info == NULL )
159 continue;
160
161 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
162 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
163 continue;
164
165 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
166 (unsigned int) ciphersuites[i],
167 ciphersuite_info->name ) );
168
169 /* Check for available spaces */
170 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
171
Jerry Yub7ab3362021-08-31 16:16:19 +0800172 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], ciphersuite_iter, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800173 ciphersuite_iter += 2;
174
175 }
176
177 buf_len = ciphersuite_iter - ciphersuite_start;
178
179 /* write ciphersuite buf length */
180 MBEDTLS_PUT_UINT16_BE( buf_len, buf, 0 );
181
182
183 MBEDTLS_SSL_DEBUG_MSG( 3,
184 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites",
185 buf_len/2 ) );
186
Jerry Yuf171e832021-08-31 18:31:09 +0800187 *olen = ciphersuite_iter - buf;
188
Jerry Yu6a643102021-08-31 14:40:36 +0800189 return( 0 );
190}
191
192/* Functions for writing ClientHello message */
Jerry Yu08906d02021-08-31 11:05:27 +0800193static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800194 unsigned char *buf,
195 size_t buflen,
196 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800197{
Jerry Yuc4d22442021-08-27 20:04:33 +0800198 /* Extensions */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800199
200 /* extension_start
201 * Used during extension writing where the
202 * buffer pointer to the beginning of the
203 * extension list must be kept to write
204 * the total extension list size in the end.
205 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800206 int ret;
Jerry Yueecfbf02021-08-30 18:32:07 +0800207 unsigned char *extension_start;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800208 size_t cur_ext_len; /* Size of the current extension */
209 size_t total_ext_len; /* Size of list of extensions */
210
Jerry Yubc20bdd2021-08-24 15:59:48 +0800211 /* Buffer management */
Jerry Yueecfbf02021-08-30 18:32:07 +0800212 unsigned char *start = buf;
213 unsigned char *end = buf + buflen;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800214
Jerry Yu6a643102021-08-31 14:40:36 +0800215 *len_with_binders = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800216
217 /* Keeping track of the included extensions */
218 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
219
Jerry Yubc20bdd2021-08-24 15:59:48 +0800220 /* NOTE:
221 * Even for DTLS 1.3, we are writing a TLS handshake header here.
222 * The actual DTLS 1.3 handshake header is inserted in
223 * the record writing routine mbedtls_ssl_write_record().
224 *
225 * For cTLS the length, and the version field
226 * are elided. The random bytes are shorter.
227 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800228
229 if( ssl->conf->max_major_ver == 0 )
230 {
231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
232 "consider using mbedtls_ssl_config_defaults()" ) );
233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
234 }
235
236 ssl->major_ver = ssl->conf->min_major_ver;
237 ssl->minor_ver = ssl->conf->min_minor_ver;
238
Jerry Yu6a643102021-08-31 14:40:36 +0800239 /* Write legacy_version
240 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
241 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800242 * instead of the true version number.
243 *
244 * For DTLS 1.3 we use the legacy version number
245 * {254,253}.
246 *
247 * In cTLS the version number is elided.
248 */
Jerry Yu08906d02021-08-31 11:05:27 +0800249 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN );
Jerry Yub7ab3362021-08-31 16:16:19 +0800250 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0 );
Jerry Yu08906d02021-08-31 11:05:27 +0800251 buf += CLIENT_HELLO_LEGACY_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800252
Jerry Yu6a643102021-08-31 14:40:36 +0800253 /* Write random bytes
254 Random random
255 */
Jerry Yu08906d02021-08-31 11:05:27 +0800256 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN );
257 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800258 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yu08906d02021-08-31 11:05:27 +0800259 buf, CLIENT_HELLO_RANDOM_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800260
Jerry Yu08906d02021-08-31 11:05:27 +0800261 buf += CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800262
263 /* Versions of TLS before TLS 1.3 supported a
264 * "session resumption" feature which has been merged with pre-shared
265 * keys in this version. A client which has a
266 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
267 * field to that value. In compatibility mode,
268 * this field MUST be non-empty, so a client not offering a
269 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
270 * need not be random but SHOULD be unpredictable to avoid
271 * implementations fixating on a specific value ( also known as
272 * ossification ). Otherwise, it MUST be set as a zero-length vector
273 * ( i.e., a zero-valued single byte length field ).
274 */
Jerry Yu6a643102021-08-31 14:40:36 +0800275 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800276 *buf++ = 0; /* session id length set to zero */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800277
Jerry Yu6a643102021-08-31 14:40:36 +0800278 /* Write ciphersuites */
279 ret = ssl_tls13_write_client_hello_ciphersuites( ssl, buf, end, &cur_ext_len );
280 if( ret != 0)
281 return( ret );
282 buf += cur_ext_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800283
284 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
285 * one byte set to zero, which corresponds to the 'null' compression
286 * method in prior versions of TLS.
287 *
288 * For cTLS this field is elided.
289 */
Jerry Yu6a643102021-08-31 14:40:36 +0800290 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800291 *buf++ = 1;
292 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
293
Jerry Yubc20bdd2021-08-24 15:59:48 +0800294
295 /* First write extensions, then the total length */
296 extension_start = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800297 buf += 2;
298
299 /* Supported Versions Extension is mandatory with TLS 1.3.
300 *
301 * For cTLS we only need to provide it if there is more than one version
302 * and currently there is only one.
303 */
Jerry Yu92c6b402021-08-27 16:59:09 +0800304 ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
305 if( ret != 0 )
306 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800307 buf += cur_ext_len;
308
309#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
310 /* The supported_groups and the key_share extensions are
311 * REQUIRED for ECDHE ciphersuites.
312 */
Jerry Yuf4436812021-08-26 22:59:56 +0800313 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800314 if( ret != 0 )
315 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800316 buf += cur_ext_len;
317
318 /* The supported_signature_algorithms extension is REQUIRED for
319 * certificate authenticated ciphersuites. */
Jerry Yue41dec02021-08-31 10:57:07 +0800320 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800321 if( ret != 0 )
322 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800323 buf += cur_ext_len;
324
325 /* We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800326 * 1) A certificate-based ciphersuite is being offered. In this case
327 * supported_groups and supported_signature extensions have been
328 * successfully added.
329 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800330 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800331 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
332 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800333 */
334
Jerry Yuf4436812021-08-26 22:59:56 +0800335 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800336 if( ret != 0 )
337 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800338 buf += cur_ext_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800339
Jerry Yubc20bdd2021-08-24 15:59:48 +0800340#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
341
342 /* Add more extensions here */
343
Jerry Yu6a643102021-08-31 14:40:36 +0800344 total_ext_len = buf - extension_start - 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
346 total_ext_len ) );
347
348 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
349
350 /* Write extension length */
Jerry Yueecfbf02021-08-30 18:32:07 +0800351 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 );
Jerry Yu2ac64192021-08-26 18:38:58 +0800352 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800353
Jerry Yu6a643102021-08-31 14:40:36 +0800354 *len_with_binders = buf - start;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800355 return( 0 );
356}
357
Jerry Yu92c6b402021-08-27 16:59:09 +0800358static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800359{
Jerry Yu92c6b402021-08-27 16:59:09 +0800360 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
361 return( 0 );
362}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800363
Jerry Yu92c6b402021-08-27 16:59:09 +0800364static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
365{
366 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800367
Jerry Yu92c6b402021-08-27 16:59:09 +0800368 if( ssl->conf->f_rng == NULL )
369 {
370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
371 return( MBEDTLS_ERR_SSL_NO_RNG );
372 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800373
Jerry Yu92c6b402021-08-27 16:59:09 +0800374 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
375 ssl->handshake->randbytes,
Jerry Yu08906d02021-08-31 11:05:27 +0800376 CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800377 {
378 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
379 return( ret );
380 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800381
382 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800383}
384
Jerry Yu92c6b402021-08-27 16:59:09 +0800385/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800386 * Write ClientHello handshake message.
387 *
388 * Structure of this message:
389 *
Jerry Yu159c5a02021-08-31 12:51:25 +0800390 * struct {
391 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
392 * Random random;
393 * opaque legacy_session_id<0..32>;
394 * CipherSuite cipher_suites<2..2^16-2>;
395 * opaque legacy_compression_methods<1..2^8-1>;
396 * Extension extensions<8..2^16-1>;
397 * } ClientHello;
Jerry Yu92c6b402021-08-27 16:59:09 +0800398 */
399static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800400{
Jerry Yu92c6b402021-08-27 16:59:09 +0800401 int ret = 0;
402 unsigned char *buf;
403 size_t buf_len, msg_len;
404
405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
406
407 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) );
408
409 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg,
410 ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
411 &buf, &buf_len ) );
412
Jerry Yu08906d02021-08-31 11:05:27 +0800413 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body,
Jerry Yu92c6b402021-08-27 16:59:09 +0800414 ( ssl, buf, buf_len, &msg_len ) );
415
416 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
417 msg_len );
418 ssl->handshake->update_checksum( ssl, buf, 0 );
419
420 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) );
421 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg,
422 ( ssl, buf_len, msg_len ) );
423
424cleanup:
425
426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
427 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800428}
429
Jerry Yu92c6b402021-08-27 16:59:09 +0800430int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800431{
Jerry Yu92c6b402021-08-27 16:59:09 +0800432 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800433
Jerry Yu92c6b402021-08-27 16:59:09 +0800434 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
435 {
436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
438 }
439
440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
441
442 switch( ssl->state )
443 {
444 /*
445 * ssl->state is initialized as HELLO_REQUEST. It is same
446 * with CLIENT_HELLO status
447 */
448 case MBEDTLS_SSL_HELLO_REQUEST:
449 case MBEDTLS_SSL_CLIENT_HELLO:
450 ret = ssl_tls13_write_client_hello( ssl );
451 break;
452
453 case MBEDTLS_SSL_SERVER_HELLO:
454 // Stop here : we haven't finished whole flow
455 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
456 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
457 break;
458
459 default:
460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
462 }
463
464 return( ret );
465}
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800466
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800467#endif /* MBEDTLS_SSL_CLI_C */
468
469#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */