blob: 6b89273353fdceb2e8c39af16f447c2dbb9e5f66 [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
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080034#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 /*
Jerry Yu0c63af62021-09-02 12:59:12 +080057 * Check space for extension header.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080058 *
59 * extension_type 2
60 * extension_data_length 2
61 * version_length 1
62 * versions 2
Jerry Yu159c5a02021-08-31 12:51:25 +080063 */
Jerry Yu92c6b402021-08-27 16:59:09 +080064 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
65
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080066 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080067 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080068
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080069 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080070 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080071 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080072
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080073 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080074 *p++ = 0x2;
75
Jerry Yu0c63af62021-09-02 12:59:12 +080076 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080077 *
Jerry Yu0c63af62021-09-02 12:59:12 +080078 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080079 *
Jerry Yu0c63af62021-09-02 12:59:12 +080080 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080081 */
Jerry Yueecfbf02021-08-30 18:32:07 +080082 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
83 ssl->conf->max_minor_ver,
84 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080085
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080087 ssl->conf->max_major_ver,
88 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080089
90 *olen = 7;
91
92 return( 0 );
93}
Jerry Yubc20bdd2021-08-24 15:59:48 +080094
95#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
96
Jerry Yuf4436812021-08-26 22:59:56 +080097static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080098 unsigned char *buf,
99 unsigned char *end,
100 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +0800101{
102 ((void) ssl);
103 ((void) buf);
104 ((void) end);
105 ((void) olen);
106 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
107}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800108
Jerry Yuf4436812021-08-26 22:59:56 +0800109static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800110 unsigned char *buf,
111 unsigned char *end,
112 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +0800113{
114 ((void) ssl);
115 ((void) buf);
116 ((void) end);
117 ((void) olen);
118 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
119}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800120
121#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
122
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800123/*
124 * Functions for writing ClientHello message.
125 */
126/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800127 * CipherSuite cipher_suites<2..2^16-2>;
128 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800129static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800130 mbedtls_ssl_context *ssl,
131 unsigned char *buf,
132 unsigned char *end,
133 size_t *olen )
134{
Jerry Yu0c63af62021-09-02 12:59:12 +0800135 const int *ciphersuite_list;
136 unsigned char *cipher_suites_start; /* Start of the cipher_suites list */
137 unsigned char *cipher_suites_iter; /* Iteration over the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800138 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800139
Jerry Yu6a643102021-08-31 14:40:36 +0800140 *olen = 0 ;
141
142 /*
143 * Ciphersuite list
144 *
145 * This is a list of the symmetric cipher options supported by
146 * the client, specifically the record protection algorithm
147 * ( including secret key length ) and a hash to be used with
148 * HKDF, in descending order of client preference.
149 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800150 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800151
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800152 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu6a643102021-08-31 14:40:36 +0800153 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
154
Jerry Yu0c63af62021-09-02 12:59:12 +0800155 /* Write cipher_suites */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800156 cipher_suites_start = buf + 2;
157 cipher_suites_iter = cipher_suites_start;
Jerry Yu6a643102021-08-31 14:40:36 +0800158
Jerry Yu0c63af62021-09-02 12:59:12 +0800159 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800160 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800161 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800162 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800163
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800164 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800165 if( ciphersuite_info == NULL )
166 continue;
Jerry Yu6a643102021-08-31 14:40:36 +0800167 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
168 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
169 continue;
170
171 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800172 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800173 ciphersuite_info->name ) );
174
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800175 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yu6a643102021-08-31 14:40:36 +0800176 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800177 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
178 cipher_suites_iter += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800179 }
180
Jerry Yu0c63af62021-09-02 12:59:12 +0800181 /* Write the cipher_suites length in number of bytes */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800182 cipher_suites_len = cipher_suites_iter - cipher_suites_start;
183 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800184 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800185 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
186 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800187
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800188 /* Output the total length of cipher_suites field. */
189 *olen = cipher_suites_iter - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800190
Jerry Yu6a643102021-08-31 14:40:36 +0800191 return( 0 );
192}
193
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800194/*
195 * Structure of ClientHello message:
196 *
197 * struct {
198 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
199 * Random random;
200 * opaque legacy_session_id<0..32>;
201 * CipherSuite cipher_suites<2..2^16-2>;
202 * opaque legacy_compression_methods<1..2^8-1>;
203 * Extension extensions<8..2^16-1>;
204 * } ClientHello;
205 */
Jerry Yu08906d02021-08-31 11:05:27 +0800206static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800207 unsigned char *buf,
208 size_t buflen,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800209 size_t *olen )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800210{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800211
Jerry Yubc20bdd2021-08-24 15:59:48 +0800212 int ret;
Jerry Yu0c63af62021-09-02 12:59:12 +0800213 unsigned char *extensions_len_ptr; /* Pointer of extensions length */
Jerry Yu790656a2021-09-01 15:51:48 +0800214 size_t output_len; /* Length of buffer used by function */
215 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800216
Jerry Yubc20bdd2021-08-24 15:59:48 +0800217 /* Buffer management */
Jerry Yueecfbf02021-08-30 18:32:07 +0800218 unsigned char *start = buf;
219 unsigned char *end = buf + buflen;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800220
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800221 *olen = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800222
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800223 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800224 ssl->major_ver = ssl->conf->min_major_ver;
225 ssl->minor_ver = ssl->conf->min_minor_ver;
226
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800227 /*
228 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800229 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800230 *
231 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800232 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800233 */
Jerry Yu08906d02021-08-31 11:05:27 +0800234 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN );
Jerry Yub7ab3362021-08-31 16:16:19 +0800235 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0 );
Jerry Yu08906d02021-08-31 11:05:27 +0800236 buf += CLIENT_HELLO_LEGACY_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800237
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800238 /* Write the random bytes ( random ).*/
Jerry Yu08906d02021-08-31 11:05:27 +0800239 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN );
240 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800241 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yu08906d02021-08-31 11:05:27 +0800242 buf, CLIENT_HELLO_RANDOM_LEN );
Jerry Yu08906d02021-08-31 11:05:27 +0800243 buf += CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800244
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800245 /*
246 * Write legacy_session_id
247 *
248 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
249 * which has been merged with pre-shared keys in this version. A client
250 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
251 * this field to that value. In compatibility mode, this field MUST be
252 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
253 * a new 32-byte value. This value need not be random but SHOULD be
254 * unpredictable to avoid implementations fixating on a specific value
255 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
256 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800257 */
Jerry Yu6a643102021-08-31 14:40:36 +0800258 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800259 *buf++ = 0; /* session id length set to zero */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800260
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800261 /* Write cipher_suites */
Jerry Yu790656a2021-09-01 15:51:48 +0800262 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
Jerry Yu6a643102021-08-31 14:40:36 +0800263 if( ret != 0)
264 return( ret );
Jerry Yu790656a2021-09-01 15:51:48 +0800265 buf += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800266
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800267 /* Write legacy_compression_methods
268 *
269 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800270 * one byte set to zero, which corresponds to the 'null' compression
271 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800272 */
Jerry Yu6a643102021-08-31 14:40:36 +0800273 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800274 *buf++ = 1;
275 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
276
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800277 /* Write extensions */
278
279 /* Keeping track of the included extensions */
280 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800281
282 /* First write extensions, then the total length */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800283 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 );
Jerry Yu790656a2021-09-01 15:51:48 +0800284 extensions_len_ptr = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800285 buf += 2;
286
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800287 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800288 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800289 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800290 */
Jerry Yu790656a2021-09-01 15:51:48 +0800291 ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800292 if( ret != 0 )
293 return( ret );
Jerry Yu790656a2021-09-01 15:51:48 +0800294 buf += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800295
296#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800297 /* Write supported_groups extension
298 *
299 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800300 */
Jerry Yu790656a2021-09-01 15:51:48 +0800301 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800302 if( ret != 0 )
303 return( ret );
Jerry Yu790656a2021-09-01 15:51:48 +0800304 buf += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800305
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800306 /* Write key_share extension
307 *
308 * We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800309 * 1) A certificate-based ciphersuite is being offered. In this case
310 * supported_groups and supported_signature extensions have been
311 * successfully added.
312 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800313 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800314 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
315 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800316 */
Jerry Yu790656a2021-09-01 15:51:48 +0800317 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800318 if( ret != 0 )
319 return( ret );
Jerry Yu790656a2021-09-01 15:51:48 +0800320 buf += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800321
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800322 /* Write signature_algorithms extension
323 *
324 * It is REQUIRED for certificate authenticated cipher_suites.
325 */
Jerry Yu790656a2021-09-01 15:51:48 +0800326 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &output_len );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800327 if( ret != 0 )
328 return( ret );
Jerry Yu790656a2021-09-01 15:51:48 +0800329 buf += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800330
Jerry Yubc20bdd2021-08-24 15:59:48 +0800331#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
332
333 /* Add more extensions here */
334
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800335 /* Write the length of the list of extensions. */
Jerry Yu790656a2021-09-01 15:51:48 +0800336 extensions_len = buf - extensions_len_ptr - 2;
337 MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800338 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800339 extensions_len ) );
340 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extensions_len_ptr, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800341
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800342 *olen = buf - start;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800343 return( 0 );
344}
345
Jerry Yu92c6b402021-08-27 16:59:09 +0800346static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800347{
Jerry Yu92c6b402021-08-27 16:59:09 +0800348 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
349 return( 0 );
350}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800351
Jerry Yu92c6b402021-08-27 16:59:09 +0800352static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
353{
354 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800355
Jerry Yu92c6b402021-08-27 16:59:09 +0800356 if( ssl->conf->f_rng == NULL )
357 {
358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
359 return( MBEDTLS_ERR_SSL_NO_RNG );
360 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800361
Jerry Yu92c6b402021-08-27 16:59:09 +0800362 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
363 ssl->handshake->randbytes,
Jerry Yu08906d02021-08-31 11:05:27 +0800364 CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800365 {
366 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
367 return( ret );
368 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800369
370 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800371}
372
Jerry Yu92c6b402021-08-27 16:59:09 +0800373/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800374 * Write ClientHello handshake message.
Jerry Yu92c6b402021-08-27 16:59:09 +0800375 */
376static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800377{
Jerry Yu92c6b402021-08-27 16:59:09 +0800378 int ret = 0;
379 unsigned char *buf;
380 size_t buf_len, msg_len;
381
382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
383
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800384 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800385
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800386 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
387 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
388 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800389
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800390 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
391 buf_len,
392 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800393
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800394 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
395 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800396 msg_len );
397 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800398
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800399 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
400 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
401 buf_len,
402 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800403
404cleanup:
405
406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
407 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800408}
409
Jerry Yu92c6b402021-08-27 16:59:09 +0800410int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800411{
Jerry Yu92c6b402021-08-27 16:59:09 +0800412 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800413
Jerry Yu92c6b402021-08-27 16:59:09 +0800414 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
415 {
416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
418 }
419
420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
421
422 switch( ssl->state )
423 {
424 /*
Jerry Yu0c63af62021-09-02 12:59:12 +0800425 * ssl->state is initialized as HELLO_REQUEST. It is the same
426 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +0800427 */
428 case MBEDTLS_SSL_HELLO_REQUEST:
429 case MBEDTLS_SSL_CLIENT_HELLO:
430 ret = ssl_tls13_write_client_hello( ssl );
431 break;
432
433 case MBEDTLS_SSL_SERVER_HELLO:
434 // Stop here : we haven't finished whole flow
435 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
436 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
437 break;
438
439 default:
440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
442 }
443
444 return( ret );
445}
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800446
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800447#endif /* MBEDTLS_SSL_CLI_C */
448
449#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */