blob: f9cfff5217745518b24aa38358317866e1d7dee8 [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 Yu92c6b402021-08-27 16:59:09 +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
172 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], ciphersuite_iter, 0);
173 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
187 return( 0 );
188}
189
190/* Functions for writing ClientHello message */
Jerry Yu08906d02021-08-31 11:05:27 +0800191static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800192 unsigned char *buf,
193 size_t buflen,
194 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800195{
Jerry Yuc4d22442021-08-27 20:04:33 +0800196 /* Extensions */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800197
198 /* extension_start
199 * Used during extension writing where the
200 * buffer pointer to the beginning of the
201 * extension list must be kept to write
202 * the total extension list size in the end.
203 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800204 int ret;
Jerry Yueecfbf02021-08-30 18:32:07 +0800205 unsigned char *extension_start;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800206 size_t cur_ext_len; /* Size of the current extension */
207 size_t total_ext_len; /* Size of list of extensions */
208
Jerry Yubc20bdd2021-08-24 15:59:48 +0800209 /* Buffer management */
Jerry Yueecfbf02021-08-30 18:32:07 +0800210 unsigned char *start = buf;
211 unsigned char *end = buf + buflen;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800212
Jerry Yu6a643102021-08-31 14:40:36 +0800213 *len_with_binders = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800214
215 /* Keeping track of the included extensions */
216 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
217
Jerry Yubc20bdd2021-08-24 15:59:48 +0800218 /* NOTE:
219 * Even for DTLS 1.3, we are writing a TLS handshake header here.
220 * The actual DTLS 1.3 handshake header is inserted in
221 * the record writing routine mbedtls_ssl_write_record().
222 *
223 * For cTLS the length, and the version field
224 * are elided. The random bytes are shorter.
225 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800226
227 if( ssl->conf->max_major_ver == 0 )
228 {
229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
230 "consider using mbedtls_ssl_config_defaults()" ) );
231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
232 }
233
234 ssl->major_ver = ssl->conf->min_major_ver;
235 ssl->minor_ver = ssl->conf->min_minor_ver;
236
Jerry Yu6a643102021-08-31 14:40:36 +0800237 /* Write legacy_version
238 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
239 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800240 * instead of the true version number.
241 *
242 * For DTLS 1.3 we use the legacy version number
243 * {254,253}.
244 *
245 * In cTLS the version number is elided.
246 */
Jerry Yu08906d02021-08-31 11:05:27 +0800247 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN );
Jerry Yu2ac64192021-08-26 18:38:58 +0800248 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0);
Jerry Yu08906d02021-08-31 11:05:27 +0800249 buf += CLIENT_HELLO_LEGACY_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800250
Jerry Yu6a643102021-08-31 14:40:36 +0800251 /* Write random bytes
252 Random random
253 */
Jerry Yu08906d02021-08-31 11:05:27 +0800254 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN );
255 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800256 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yu08906d02021-08-31 11:05:27 +0800257 buf, CLIENT_HELLO_RANDOM_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800258
Jerry Yu08906d02021-08-31 11:05:27 +0800259 buf += CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800260
261 /* Versions of TLS before TLS 1.3 supported a
262 * "session resumption" feature which has been merged with pre-shared
263 * keys in this version. A client which has a
264 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
265 * field to that value. In compatibility mode,
266 * this field MUST be non-empty, so a client not offering a
267 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
268 * need not be random but SHOULD be unpredictable to avoid
269 * implementations fixating on a specific value ( also known as
270 * ossification ). Otherwise, it MUST be set as a zero-length vector
271 * ( i.e., a zero-valued single byte length field ).
272 */
Jerry Yu6a643102021-08-31 14:40:36 +0800273 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800274 *buf++ = 0; /* session id length set to zero */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800275
Jerry Yu6a643102021-08-31 14:40:36 +0800276 /* Write ciphersuites */
277 ret = ssl_tls13_write_client_hello_ciphersuites( ssl, buf, end, &cur_ext_len );
278 if( ret != 0)
279 return( ret );
280 buf += cur_ext_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800281
282 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
283 * one byte set to zero, which corresponds to the 'null' compression
284 * method in prior versions of TLS.
285 *
286 * For cTLS this field is elided.
287 */
Jerry Yu6a643102021-08-31 14:40:36 +0800288 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800289 *buf++ = 1;
290 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
291
Jerry Yubc20bdd2021-08-24 15:59:48 +0800292
293 /* First write extensions, then the total length */
294 extension_start = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800295 buf += 2;
296
297 /* Supported Versions Extension is mandatory with TLS 1.3.
298 *
299 * For cTLS we only need to provide it if there is more than one version
300 * and currently there is only one.
301 */
Jerry Yu92c6b402021-08-27 16:59:09 +0800302 ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
303 if( ret != 0 )
304 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800305 buf += cur_ext_len;
306
307#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
308 /* The supported_groups and the key_share extensions are
309 * REQUIRED for ECDHE ciphersuites.
310 */
Jerry Yuf4436812021-08-26 22:59:56 +0800311 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800312 if( ret != 0 )
313 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800314 buf += cur_ext_len;
315
316 /* The supported_signature_algorithms extension is REQUIRED for
317 * certificate authenticated ciphersuites. */
Jerry Yue41dec02021-08-31 10:57:07 +0800318 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800319 if( ret != 0 )
320 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800321 buf += cur_ext_len;
322
323 /* We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800324 * 1) A certificate-based ciphersuite is being offered. In this case
325 * supported_groups and supported_signature extensions have been
326 * successfully added.
327 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800328 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800329 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
330 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800331 */
332
Jerry Yuf4436812021-08-26 22:59:56 +0800333 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800334 if( ret != 0 )
335 return( ret );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800336 buf += cur_ext_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800337
Jerry Yubc20bdd2021-08-24 15:59:48 +0800338#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
339
340 /* Add more extensions here */
341
Jerry Yu6a643102021-08-31 14:40:36 +0800342 total_ext_len = buf - extension_start - 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
344 total_ext_len ) );
345
346 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
347
348 /* Write extension length */
Jerry Yueecfbf02021-08-30 18:32:07 +0800349 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 );
Jerry Yu2ac64192021-08-26 18:38:58 +0800350 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800351
Jerry Yu6a643102021-08-31 14:40:36 +0800352 *len_with_binders = buf - start;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800353 return( 0 );
354}
355
Jerry Yu92c6b402021-08-27 16:59:09 +0800356static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800357{
Jerry Yu92c6b402021-08-27 16:59:09 +0800358 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
359 return( 0 );
360}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800361
Jerry Yu92c6b402021-08-27 16:59:09 +0800362static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
363{
364 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800365
Jerry Yu92c6b402021-08-27 16:59:09 +0800366 if( ssl->conf->f_rng == NULL )
367 {
368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
369 return( MBEDTLS_ERR_SSL_NO_RNG );
370 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800371
Jerry Yu92c6b402021-08-27 16:59:09 +0800372 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
373 ssl->handshake->randbytes,
Jerry Yu08906d02021-08-31 11:05:27 +0800374 CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800375 {
376 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
377 return( ret );
378 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800379
380 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800381}
382
Jerry Yu92c6b402021-08-27 16:59:09 +0800383/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800384 * Write ClientHello handshake message.
385 *
386 * Structure of this message:
387 *
Jerry Yu159c5a02021-08-31 12:51:25 +0800388 * struct {
389 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
390 * Random random;
391 * opaque legacy_session_id<0..32>;
392 * CipherSuite cipher_suites<2..2^16-2>;
393 * opaque legacy_compression_methods<1..2^8-1>;
394 * Extension extensions<8..2^16-1>;
395 * } ClientHello;
Jerry Yu92c6b402021-08-27 16:59:09 +0800396 */
397static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800398{
Jerry Yu92c6b402021-08-27 16:59:09 +0800399 int ret = 0;
400 unsigned char *buf;
401 size_t buf_len, msg_len;
402
403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
404
405 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) );
406
407 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg,
408 ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
409 &buf, &buf_len ) );
410
Jerry Yu08906d02021-08-31 11:05:27 +0800411 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body,
Jerry Yu92c6b402021-08-27 16:59:09 +0800412 ( ssl, buf, buf_len, &msg_len ) );
413
414 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
415 msg_len );
416 ssl->handshake->update_checksum( ssl, buf, 0 );
417
418 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) );
419 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg,
420 ( ssl, buf_len, msg_len ) );
421
422cleanup:
423
424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
425 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800426}
427
Jerry Yu92c6b402021-08-27 16:59:09 +0800428int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800429{
Jerry Yu92c6b402021-08-27 16:59:09 +0800430 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800431
Jerry Yu92c6b402021-08-27 16:59:09 +0800432 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
433 {
434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
436 }
437
438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
439
440 switch( ssl->state )
441 {
442 /*
443 * ssl->state is initialized as HELLO_REQUEST. It is same
444 * with CLIENT_HELLO status
445 */
446 case MBEDTLS_SSL_HELLO_REQUEST:
447 case MBEDTLS_SSL_CLIENT_HELLO:
448 ret = ssl_tls13_write_client_hello( ssl );
449 break;
450
451 case MBEDTLS_SSL_SERVER_HELLO:
452 // Stop here : we haven't finished whole flow
453 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
454 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
455 break;
456
457 default:
458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
460 }
461
462 return( ret );
463}
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 */