blob: 0a07e06c40406c93c3647d6c537e8412da1dc594 [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 Yu6f13f642021-08-26 17:18:15 +080033#define CLIENT_HELLO_RAND_BYTES_LEN 32
34#define CLIENT_HELLO_VERSION_LEN 2
Jerry Yu65dd2cc2021-08-18 16:38:40 +080035/* Main entry point; orchestrates the other functions */
Jerry Yuf4436812021-08-26 22:59:56 +080036static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl );
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080037
Jerry Yuf4436812021-08-26 22:59:56 +080038int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +080039{
Jerry Yua13c7e72021-08-17 10:44:40 +080040 int ret = 0;
41
42 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
43 {
44 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
45 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
46 }
47
48 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
49
50 switch( ssl->state )
51 {
Jerry Yud532fe72021-08-26 23:11:55 +080052 /*
53 * ssl->state is initialized as HELLO_REQUEST. It is same
54 * with CLIENT_HELLO status
55 */
Jerry Yua13c7e72021-08-17 10:44:40 +080056 case MBEDTLS_SSL_HELLO_REQUEST:
Jerry Yua13c7e72021-08-17 10:44:40 +080057 case MBEDTLS_SSL_CLIENT_HELLO:
Jerry Yuf4436812021-08-26 22:59:56 +080058 ret = ssl_tls13_write_client_hello( ssl );
Jerry Yua13c7e72021-08-17 10:44:40 +080059 break;
60
61 case MBEDTLS_SSL_SERVER_HELLO:
62 // Stop here : we haven't finished whole flow
Jerry Yue885b762021-08-26 17:32:34 +080063 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yua13c7e72021-08-17 10:44:40 +080064 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
65 break;
66
67 default:
68 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
69 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
70 }
71
72 return( ret );
73}
74
Jerry Yu65dd2cc2021-08-18 16:38:40 +080075
Jerry Yuf4436812021-08-26 22:59:56 +080076static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl );
77static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +080078 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +080079 size_t *len_with_binders );
Jerry Yuf4436812021-08-26 22:59:56 +080080static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080081
Jerry Yuf4436812021-08-26 22:59:56 +080082static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yua13c7e72021-08-17 10:44:40 +080083{
84 int ret = 0;
Jerry Yu65dd2cc2021-08-18 16:38:40 +080085 unsigned char *buf;
86 size_t buf_len, msg_len;
Jerry Yua13c7e72021-08-17 10:44:40 +080087
88 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
89
Jerry Yuf4436812021-08-26 22:59:56 +080090 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080091
Jerry Yuf4436812021-08-26 22:59:56 +080092 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg,
Jerry Yue885b762021-08-26 17:32:34 +080093 ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
94 &buf, &buf_len ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080095
Jerry Yuf4436812021-08-26 22:59:56 +080096 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello,
Jerry Yue885b762021-08-26 17:32:34 +080097 ( ssl, buf, buf_len, &msg_len ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080098
Jerry Yuf4436812021-08-26 22:59:56 +080099 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800100 msg_len );
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800101 ssl->handshake->update_checksum( ssl, buf, 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800102
Jerry Yuf4436812021-08-26 22:59:56 +0800103 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) );
104 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg,
Jerry Yue885b762021-08-26 17:32:34 +0800105 ( ssl, buf_len, msg_len ) );
Jerry Yua13c7e72021-08-17 10:44:40 +0800106
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800107cleanup:
108
Jerry Yua13c7e72021-08-17 10:44:40 +0800109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
110 /* client_hello_process haven't finished */
Jerry Yu55b90382021-08-26 18:42:05 +0800111 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yua13c7e72021-08-17 10:44:40 +0800112 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +0800113}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800114
Jerry Yuf4436812021-08-26 22:59:56 +0800115static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800116{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800117 int ret;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800118
Jerry Yue885b762021-08-26 17:32:34 +0800119 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
120 ssl->handshake->randbytes,
Jerry Yu6f13f642021-08-26 17:18:15 +0800121 CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 )
Jerry Yuc8a392c2021-08-18 16:46:28 +0800122 {
123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
124 return( ret );
125 }
126
127 return( 0 );
128}
129
Jerry Yuf4436812021-08-26 22:59:56 +0800130static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yuc8a392c2021-08-18 16:46:28 +0800131{
132 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
133
134 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800135}
136
Jerry Yubc20bdd2021-08-24 15:59:48 +0800137/* Write extensions */
138
Jerry Yuf4436812021-08-26 22:59:56 +0800139static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800140 unsigned char *buf,
141 unsigned char *end,
142 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800143
144#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
145
Jerry Yuf4436812021-08-26 22:59:56 +0800146static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800147 unsigned char *buf,
148 unsigned char *end,
149 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800150
Jerry Yuf4436812021-08-26 22:59:56 +0800151static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800152 unsigned char *buf,
153 unsigned char *end,
154 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800155
156#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
157
Jerry Yuf4436812021-08-26 22:59:56 +0800158static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800159 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800160 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800161{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800162 /* Extensions */
163
164 /* extension_start
165 * Used during extension writing where the
166 * buffer pointer to the beginning of the
167 * extension list must be kept to write
168 * the total extension list size in the end.
169 */
Jerry Yu32cd5b12021-08-24 18:07:13 +0800170#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800171 int ret;
Jerry Yu32cd5b12021-08-24 18:07:13 +0800172#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800173 unsigned char* extension_start;
174 size_t cur_ext_len; /* Size of the current extension */
175 size_t total_ext_len; /* Size of list of extensions */
176
Jerry Yubc20bdd2021-08-24 15:59:48 +0800177 /* Buffer management */
178 unsigned char* start = buf;
179 unsigned char* end = buf + buflen;
180
181 /* Ciphersuite-related variables */
182 const int* ciphersuites;
183 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yue885b762021-08-26 17:32:34 +0800184 /* ciphersuite_start points to the start of
185 the ciphersuite list, i.e. to the length field*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800186 unsigned char* ciphersuite_start;
187 size_t ciphersuite_count;
188
189 /* Keeping track of the included extensions */
190 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
191
Jerry Yubc20bdd2021-08-24 15:59:48 +0800192 /* NOTE:
193 * Even for DTLS 1.3, we are writing a TLS handshake header here.
194 * The actual DTLS 1.3 handshake header is inserted in
195 * the record writing routine mbedtls_ssl_write_record().
196 *
197 * For cTLS the length, and the version field
198 * are elided. The random bytes are shorter.
199 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800200
201 if( ssl->conf->max_major_ver == 0 )
202 {
203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
204 "consider using mbedtls_ssl_config_defaults()" ) );
205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
206 }
207
208 ssl->major_ver = ssl->conf->min_major_ver;
209 ssl->minor_ver = ssl->conf->min_minor_ver;
210
211 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
212 * instead of the true version number.
213 *
214 * For DTLS 1.3 we use the legacy version number
215 * {254,253}.
216 *
217 * In cTLS the version number is elided.
218 */
Jerry Yu6f13f642021-08-26 17:18:15 +0800219 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN);
Jerry Yu2ac64192021-08-26 18:38:58 +0800220 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0);
221 buf += 2;
Jerry Yu6f13f642021-08-26 17:18:15 +0800222 buflen -= CLIENT_HELLO_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800223
224 /* Write random bytes */
Jerry Yu6f13f642021-08-26 17:18:15 +0800225 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN);
226 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800227 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
228 buf, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800229
Jerry Yu6f13f642021-08-26 17:18:15 +0800230 buf += CLIENT_HELLO_RAND_BYTES_LEN;
231 buflen -= CLIENT_HELLO_RAND_BYTES_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800232
233 /* Versions of TLS before TLS 1.3 supported a
234 * "session resumption" feature which has been merged with pre-shared
235 * keys in this version. A client which has a
236 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
237 * field to that value. In compatibility mode,
238 * this field MUST be non-empty, so a client not offering a
239 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
240 * need not be random but SHOULD be unpredictable to avoid
241 * implementations fixating on a specific value ( also known as
242 * ossification ). Otherwise, it MUST be set as a zero-length vector
243 * ( i.e., a zero-valued single byte length field ).
244 */
245 if( buflen < 1 )
246 {
247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
248 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
249 }
250
251 *buf++ = 0; /* session id length set to zero */
252 buflen -= 1;
253
254 /*
255 * Ciphersuite list
256 *
257 * This is a list of the symmetric cipher options supported by
258 * the client, specifically the record protection algorithm
259 * ( including secret key length ) and a hash to be used with
260 * HKDF, in descending order of client preference.
261 */
262 ciphersuites = ssl->conf->ciphersuite_list;
263
264 if( buflen < 2 /* for ciphersuite list length */ )
265 {
266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
267 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
268 }
269
270 /* Skip writing ciphersuite length for now */
271 ciphersuite_count = 0;
272 ciphersuite_start = buf;
273 buf += 2;
274 buflen -= 2;
275
Jerry Yue885b762021-08-26 17:32:34 +0800276 for ( size_t i = 0; ciphersuites[i] != 0; i++ )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800277 {
278 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
279
280 if( ciphersuite_info == NULL )
281 continue;
282
283 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
284 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
285 continue;
286
287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yue885b762021-08-26 17:32:34 +0800288 (unsigned int) ciphersuites[i],
289 ciphersuite_info->name ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800290
291 ciphersuite_count++;
292
293 if( buflen < 2 /* for ciphersuite list length */ )
294 {
295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
296 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
297 }
298
Jerry Yu2ac64192021-08-26 18:38:58 +0800299 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0);
Jerry Yubc20bdd2021-08-24 15:59:48 +0800300
Jerry Yu2ac64192021-08-26 18:38:58 +0800301 buf += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800302 buflen -= 2;
303
304 }
305
306 /* write ciphersuite length now */
Jerry Yu2ac64192021-08-26 18:38:58 +0800307 MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0);
308 ciphersuite_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800309
Jerry Yue885b762021-08-26 17:32:34 +0800310 MBEDTLS_SSL_DEBUG_MSG( 3,
311 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites",
312 ciphersuite_count ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800313
314 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
315 * one byte set to zero, which corresponds to the 'null' compression
316 * method in prior versions of TLS.
317 *
318 * For cTLS this field is elided.
319 */
320 if( buflen < 2 /* for ciphersuite list length */ )
321 {
322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
323 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
324 }
325
326 *buf++ = 1;
327 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
328
329 buflen -= 2;
330
331 /* First write extensions, then the total length */
332 extension_start = buf;
333 total_ext_len = 0;
334 buf += 2;
335
336 /* Supported Versions Extension is mandatory with TLS 1.3.
337 *
338 * For cTLS we only need to provide it if there is more than one version
339 * and currently there is only one.
340 */
Jerry Yuf4436812021-08-26 22:59:56 +0800341 ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800342 total_ext_len += cur_ext_len;
343 buf += cur_ext_len;
344
345#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
346 /* The supported_groups and the key_share extensions are
347 * REQUIRED for ECDHE ciphersuites.
348 */
Jerry Yuf4436812021-08-26 22:59:56 +0800349 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800350 if( ret != 0 )
351 return( ret );
352
353 total_ext_len += cur_ext_len;
354 buf += cur_ext_len;
355
356 /* The supported_signature_algorithms extension is REQUIRED for
357 * certificate authenticated ciphersuites. */
Jerry Yuf4436812021-08-26 22:59:56 +0800358 ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf,
359 end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800360 if( ret != 0 )
361 return( ret );
362
363 total_ext_len += cur_ext_len;
364 buf += cur_ext_len;
365
366 /* We need to send the key shares under three conditions:
367 * 1 ) A certificate-based ciphersuite is being offered. In this case
368 * supported_groups and supported_signature extensions have been successfully added.
369 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
370 * psk_key_exchange_modes has been added as the last extension.
371 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
372 */
373
Jerry Yuf4436812021-08-26 22:59:56 +0800374 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800375 if( ret != 0 )
376 return( ret );
377
378 total_ext_len += cur_ext_len;
379 buf += cur_ext_len;
380#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
381
382 /* Add more extensions here */
383
384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
385 total_ext_len ) );
386
387 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
388
389 /* Write extension length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800390 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0);
391 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800392
Jerry Yubc20bdd2021-08-24 15:59:48 +0800393 *len_with_binders = ( extension_start + total_ext_len ) - start;
394 return( 0 );
395}
396
Jerry Yuef6b36b2021-08-24 16:29:02 +0800397/*
Jerry Yuf4436812021-08-26 22:59:56 +0800398 * ssl_tls13_write_supported_versions_ext():
Jerry Yuef6b36b2021-08-24 16:29:02 +0800399 *
400 * struct {
401 * ProtocolVersion versions<2..254>;
402 * } SupportedVersions;
403 */
Jerry Yuf4436812021-08-26 22:59:56 +0800404static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800405 unsigned char *buf,
406 unsigned char *end,
407 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800408{
Jerry Yuef6b36b2021-08-24 16:29:02 +0800409 unsigned char *p = buf;
410
411 *olen = 0;
412
413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) );
414
Jerry Yu6f13f642021-08-26 17:18:15 +0800415 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
Jerry Yuef6b36b2021-08-24 16:29:02 +0800416
Jerry Yu2ac64192021-08-26 18:38:58 +0800417 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
Jerry Yuef6b36b2021-08-24 16:29:02 +0800418
419 /* total length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800420 MBEDTLS_PUT_UINT16_BE( 3, p, 2);
421
422 p+=4;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800423
424 /* length of next field */
425 *p++ = 0x2;
426
427 /* This implementation only supports a single TLS version, and only
428 * advertises a single value.
429 */
430 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
431 ssl->conf->transport, p );
432
Jerry Yue885b762021-08-26 17:32:34 +0800433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
434 ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
Jerry Yuef6b36b2021-08-24 16:29:02 +0800435
436 *olen = 7;
Jerry Yu6f13f642021-08-26 17:18:15 +0800437
438 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800439}
440
441#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
442
Jerry Yuf4436812021-08-26 22:59:56 +0800443static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800444 unsigned char *buf,
445 unsigned char *end,
446 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800447{
448 ((void) ssl);
449 ((void) buf);
450 ((void) end);
451 ((void) olen);
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800452 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
453}
454
Jerry Yuf4436812021-08-26 22:59:56 +0800455static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800456 unsigned char *buf,
457 unsigned char *end,
458 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800459{
460 ((void) ssl);
461 ((void) buf);
462 ((void) end);
463 ((void) olen);
464 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
465}
Jerry Yuc8a392c2021-08-18 16:46:28 +0800466
Jerry Yubc20bdd2021-08-24 15:59:48 +0800467#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800468
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800469#endif /* MBEDTLS_SSL_CLI_C */
470
471#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */