blob: f30d4082305dfef44cfe1e7aa9949a7c828feac0 [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 Yu159c5a02021-08-31 12:51:25 +0800118/* Functions for writing ClientHello message */
Jerry Yu92c6b402021-08-27 16:59:09 +0800119
Jerry Yu08906d02021-08-31 11:05:27 +0800120static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800121 unsigned char *buf,
122 size_t buflen,
123 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800124{
Jerry Yuc4d22442021-08-27 20:04:33 +0800125 /* Extensions */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800126
127 /* extension_start
128 * Used during extension writing where the
129 * buffer pointer to the beginning of the
130 * extension list must be kept to write
131 * the total extension list size in the end.
132 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800133 int ret;
Jerry Yueecfbf02021-08-30 18:32:07 +0800134 unsigned char *extension_start;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800135 size_t cur_ext_len; /* Size of the current extension */
136 size_t total_ext_len; /* Size of list of extensions */
137
Jerry Yubc20bdd2021-08-24 15:59:48 +0800138 /* Buffer management */
Jerry Yueecfbf02021-08-30 18:32:07 +0800139 unsigned char *start = buf;
140 unsigned char *end = buf + buflen;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800141
142 /* Ciphersuite-related variables */
Jerry Yueecfbf02021-08-30 18:32:07 +0800143 const int *ciphersuites;
144 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue885b762021-08-26 17:32:34 +0800145 /* ciphersuite_start points to the start of
146 the ciphersuite list, i.e. to the length field*/
Jerry Yueecfbf02021-08-30 18:32:07 +0800147 unsigned char *ciphersuite_start;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800148 size_t ciphersuite_count;
149
150 /* Keeping track of the included extensions */
151 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
152
Jerry Yubc20bdd2021-08-24 15:59:48 +0800153 /* NOTE:
154 * Even for DTLS 1.3, we are writing a TLS handshake header here.
155 * The actual DTLS 1.3 handshake header is inserted in
156 * the record writing routine mbedtls_ssl_write_record().
157 *
158 * For cTLS the length, and the version field
159 * are elided. The random bytes are shorter.
160 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800161
162 if( ssl->conf->max_major_ver == 0 )
163 {
164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
165 "consider using mbedtls_ssl_config_defaults()" ) );
166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
167 }
168
169 ssl->major_ver = ssl->conf->min_major_ver;
170 ssl->minor_ver = ssl->conf->min_minor_ver;
171
172 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
173 * instead of the true version number.
174 *
175 * For DTLS 1.3 we use the legacy version number
176 * {254,253}.
177 *
178 * In cTLS the version number is elided.
179 */
Jerry Yu08906d02021-08-31 11:05:27 +0800180 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN );
Jerry Yu2ac64192021-08-26 18:38:58 +0800181 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0);
Jerry Yu08906d02021-08-31 11:05:27 +0800182 buf += CLIENT_HELLO_LEGACY_VERSION_LEN;
183 buflen -= CLIENT_HELLO_LEGACY_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800184
185 /* Write random bytes */
Jerry Yu08906d02021-08-31 11:05:27 +0800186 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN );
187 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800188 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yu08906d02021-08-31 11:05:27 +0800189 buf, CLIENT_HELLO_RANDOM_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800190
Jerry Yu08906d02021-08-31 11:05:27 +0800191 buf += CLIENT_HELLO_RANDOM_LEN;
192 buflen -= CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800193
194 /* Versions of TLS before TLS 1.3 supported a
195 * "session resumption" feature which has been merged with pre-shared
196 * keys in this version. A client which has a
197 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
198 * field to that value. In compatibility mode,
199 * this field MUST be non-empty, so a client not offering a
200 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
201 * need not be random but SHOULD be unpredictable to avoid
202 * implementations fixating on a specific value ( also known as
203 * ossification ). Otherwise, it MUST be set as a zero-length vector
204 * ( i.e., a zero-valued single byte length field ).
205 */
206 if( buflen < 1 )
207 {
208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
209 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
210 }
211
212 *buf++ = 0; /* session id length set to zero */
213 buflen -= 1;
214
215 /*
216 * Ciphersuite list
217 *
218 * This is a list of the symmetric cipher options supported by
219 * the client, specifically the record protection algorithm
220 * ( including secret key length ) and a hash to be used with
221 * HKDF, in descending order of client preference.
222 */
223 ciphersuites = ssl->conf->ciphersuite_list;
224
225 if( buflen < 2 /* for ciphersuite list length */ )
226 {
227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
228 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
229 }
230
231 /* Skip writing ciphersuite length for now */
232 ciphersuite_count = 0;
233 ciphersuite_start = buf;
234 buf += 2;
235 buflen -= 2;
236
Jerry Yue885b762021-08-26 17:32:34 +0800237 for ( size_t i = 0; ciphersuites[i] != 0; i++ )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800238 {
239 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
240
241 if( ciphersuite_info == NULL )
242 continue;
243
244 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
245 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
246 continue;
247
248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yue885b762021-08-26 17:32:34 +0800249 (unsigned int) ciphersuites[i],
250 ciphersuite_info->name ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800251
252 ciphersuite_count++;
253
254 if( buflen < 2 /* for ciphersuite list length */ )
255 {
256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
257 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
258 }
259
Jerry Yu2ac64192021-08-26 18:38:58 +0800260 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0);
Jerry Yubc20bdd2021-08-24 15:59:48 +0800261
Jerry Yu2ac64192021-08-26 18:38:58 +0800262 buf += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800263 buflen -= 2;
264
265 }
266
267 /* write ciphersuite length now */
Jerry Yueecfbf02021-08-30 18:32:07 +0800268 MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0 );
Jerry Yu2ac64192021-08-26 18:38:58 +0800269 ciphersuite_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800270
Jerry Yue885b762021-08-26 17:32:34 +0800271 MBEDTLS_SSL_DEBUG_MSG( 3,
272 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites",
273 ciphersuite_count ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800274
275 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
276 * one byte set to zero, which corresponds to the 'null' compression
277 * method in prior versions of TLS.
278 *
279 * For cTLS this field is elided.
280 */
281 if( buflen < 2 /* for ciphersuite list length */ )
282 {
283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
284 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
285 }
286
287 *buf++ = 1;
288 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
289
290 buflen -= 2;
291
292 /* First write extensions, then the total length */
293 extension_start = buf;
294 total_ext_len = 0;
295 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 total_ext_len += cur_ext_len;
306 buf += cur_ext_len;
307
308#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
309 /* The supported_groups and the key_share extensions are
310 * REQUIRED for ECDHE ciphersuites.
311 */
Jerry Yuf4436812021-08-26 22:59:56 +0800312 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800313 if( ret != 0 )
314 return( ret );
315
316 total_ext_len += cur_ext_len;
317 buf += cur_ext_len;
318
319 /* The supported_signature_algorithms extension is REQUIRED for
320 * certificate authenticated ciphersuites. */
Jerry Yue41dec02021-08-31 10:57:07 +0800321 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800322 if( ret != 0 )
323 return( ret );
324
325 total_ext_len += cur_ext_len;
326 buf += cur_ext_len;
327
328 /* We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800329 * 1) A certificate-based ciphersuite is being offered. In this case
330 * supported_groups and supported_signature extensions have been
331 * successfully added.
332 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800333 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800334 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
335 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800336 */
337
Jerry Yuf4436812021-08-26 22:59:56 +0800338 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800339 if( ret != 0 )
340 return( ret );
341
342 total_ext_len += cur_ext_len;
343 buf += cur_ext_len;
344#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
345
346 /* Add more extensions here */
347
348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
349 total_ext_len ) );
350
351 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
352
353 /* Write extension length */
Jerry Yueecfbf02021-08-30 18:32:07 +0800354 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 );
Jerry Yu2ac64192021-08-26 18:38:58 +0800355 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800356
Jerry Yubc20bdd2021-08-24 15:59:48 +0800357 *len_with_binders = ( extension_start + total_ext_len ) - start;
358 return( 0 );
359}
360
Jerry Yu92c6b402021-08-27 16:59:09 +0800361static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800362{
Jerry Yu92c6b402021-08-27 16:59:09 +0800363 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
364 return( 0 );
365}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800366
Jerry Yu92c6b402021-08-27 16:59:09 +0800367static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
368{
369 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800370
Jerry Yu92c6b402021-08-27 16:59:09 +0800371 if( ssl->conf->f_rng == NULL )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
374 return( MBEDTLS_ERR_SSL_NO_RNG );
375 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800376
Jerry Yu92c6b402021-08-27 16:59:09 +0800377 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
378 ssl->handshake->randbytes,
Jerry Yu08906d02021-08-31 11:05:27 +0800379 CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800380 {
381 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
382 return( ret );
383 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800384
385 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800386}
387
Jerry Yu92c6b402021-08-27 16:59:09 +0800388/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800389 * Write ClientHello handshake message.
390 *
391 * Structure of this message:
392 *
393 * uint16 ProtocolVersion;
394 * opaque Random[32];
395 * uint8 CipherSuite[2]; // Cryptographic suite selector
396 * struct {
397 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
398 * Random random;
399 * opaque legacy_session_id<0..32>;
400 * CipherSuite cipher_suites<2..2^16-2>;
401 * opaque legacy_compression_methods<1..2^8-1>;
402 * Extension extensions<8..2^16-1>;
403 * } ClientHello;
Jerry Yu92c6b402021-08-27 16:59:09 +0800404 */
405static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800406{
Jerry Yu92c6b402021-08-27 16:59:09 +0800407 int ret = 0;
408 unsigned char *buf;
409 size_t buf_len, msg_len;
410
411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
412
413 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) );
414
415 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg,
416 ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
417 &buf, &buf_len ) );
418
Jerry Yu08906d02021-08-31 11:05:27 +0800419 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body,
Jerry Yu92c6b402021-08-27 16:59:09 +0800420 ( ssl, buf, buf_len, &msg_len ) );
421
422 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
423 msg_len );
424 ssl->handshake->update_checksum( ssl, buf, 0 );
425
426 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) );
427 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg,
428 ( ssl, buf_len, msg_len ) );
429
430cleanup:
431
432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
433 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800434}
435
Jerry Yu92c6b402021-08-27 16:59:09 +0800436int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800437{
Jerry Yu92c6b402021-08-27 16:59:09 +0800438 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800439
Jerry Yu92c6b402021-08-27 16:59:09 +0800440 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
441 {
442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
444 }
445
446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
447
448 switch( ssl->state )
449 {
450 /*
451 * ssl->state is initialized as HELLO_REQUEST. It is same
452 * with CLIENT_HELLO status
453 */
454 case MBEDTLS_SSL_HELLO_REQUEST:
455 case MBEDTLS_SSL_CLIENT_HELLO:
456 ret = ssl_tls13_write_client_hello( ssl );
457 break;
458
459 case MBEDTLS_SSL_SERVER_HELLO:
460 // Stop here : we haven't finished whole flow
461 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
462 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
463 break;
464
465 default:
466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
468 }
469
470 return( ret );
471}
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800472
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800473#endif /* MBEDTLS_SSL_CLI_C */
474
475#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */