blob: f5a6e20cab9c2a0855f5e4eb91396ceb279ce1c8 [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 Yu9e42f6e2021-08-27 15:14:01 +0800119 if( ssl->conf->f_rng == NULL )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
122 return( MBEDTLS_ERR_SSL_NO_RNG );
123 }
124
Jerry Yue885b762021-08-26 17:32:34 +0800125 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
126 ssl->handshake->randbytes,
Jerry Yu6f13f642021-08-26 17:18:15 +0800127 CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 )
Jerry Yuc8a392c2021-08-18 16:46:28 +0800128 {
129 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
130 return( ret );
131 }
132
133 return( 0 );
134}
135
Jerry Yuf4436812021-08-26 22:59:56 +0800136static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yuc8a392c2021-08-18 16:46:28 +0800137{
138 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
139
140 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800141}
142
Jerry Yubc20bdd2021-08-24 15:59:48 +0800143/* Write extensions */
144
Jerry Yuf4436812021-08-26 22:59:56 +0800145static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800146 unsigned char *buf,
147 unsigned char *end,
148 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800149
150#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
151
Jerry Yuf4436812021-08-26 22:59:56 +0800152static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800153 unsigned char *buf,
154 unsigned char *end,
155 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800156
Jerry Yuf4436812021-08-26 22:59:56 +0800157static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800158 unsigned char *buf,
159 unsigned char *end,
160 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800161
162#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
163
Jerry Yuf4436812021-08-26 22:59:56 +0800164static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800165 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800166 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800167{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800168 /* Extensions */
169
170 /* extension_start
171 * Used during extension writing where the
172 * buffer pointer to the beginning of the
173 * extension list must be kept to write
174 * the total extension list size in the end.
175 */
Jerry Yu32cd5b12021-08-24 18:07:13 +0800176#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800177 int ret;
Jerry Yu32cd5b12021-08-24 18:07:13 +0800178#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800179 unsigned char* extension_start;
180 size_t cur_ext_len; /* Size of the current extension */
181 size_t total_ext_len; /* Size of list of extensions */
182
Jerry Yubc20bdd2021-08-24 15:59:48 +0800183 /* Buffer management */
184 unsigned char* start = buf;
185 unsigned char* end = buf + buflen;
186
187 /* Ciphersuite-related variables */
188 const int* ciphersuites;
189 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yue885b762021-08-26 17:32:34 +0800190 /* ciphersuite_start points to the start of
191 the ciphersuite list, i.e. to the length field*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800192 unsigned char* ciphersuite_start;
193 size_t ciphersuite_count;
194
195 /* Keeping track of the included extensions */
196 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
197
Jerry Yubc20bdd2021-08-24 15:59:48 +0800198 /* NOTE:
199 * Even for DTLS 1.3, we are writing a TLS handshake header here.
200 * The actual DTLS 1.3 handshake header is inserted in
201 * the record writing routine mbedtls_ssl_write_record().
202 *
203 * For cTLS the length, and the version field
204 * are elided. The random bytes are shorter.
205 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800206
207 if( ssl->conf->max_major_ver == 0 )
208 {
209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
210 "consider using mbedtls_ssl_config_defaults()" ) );
211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
212 }
213
214 ssl->major_ver = ssl->conf->min_major_ver;
215 ssl->minor_ver = ssl->conf->min_minor_ver;
216
217 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
218 * instead of the true version number.
219 *
220 * For DTLS 1.3 we use the legacy version number
221 * {254,253}.
222 *
223 * In cTLS the version number is elided.
224 */
Jerry Yu6f13f642021-08-26 17:18:15 +0800225 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN);
Jerry Yu2ac64192021-08-26 18:38:58 +0800226 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0);
227 buf += 2;
Jerry Yu6f13f642021-08-26 17:18:15 +0800228 buflen -= CLIENT_HELLO_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800229
230 /* Write random bytes */
Jerry Yu6f13f642021-08-26 17:18:15 +0800231 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN);
232 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800233 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
234 buf, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800235
Jerry Yu6f13f642021-08-26 17:18:15 +0800236 buf += CLIENT_HELLO_RAND_BYTES_LEN;
237 buflen -= CLIENT_HELLO_RAND_BYTES_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800238
239 /* Versions of TLS before TLS 1.3 supported a
240 * "session resumption" feature which has been merged with pre-shared
241 * keys in this version. A client which has a
242 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
243 * field to that value. In compatibility mode,
244 * this field MUST be non-empty, so a client not offering a
245 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
246 * need not be random but SHOULD be unpredictable to avoid
247 * implementations fixating on a specific value ( also known as
248 * ossification ). Otherwise, it MUST be set as a zero-length vector
249 * ( i.e., a zero-valued single byte length field ).
250 */
251 if( buflen < 1 )
252 {
253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
254 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
255 }
256
257 *buf++ = 0; /* session id length set to zero */
258 buflen -= 1;
259
260 /*
261 * Ciphersuite list
262 *
263 * This is a list of the symmetric cipher options supported by
264 * the client, specifically the record protection algorithm
265 * ( including secret key length ) and a hash to be used with
266 * HKDF, in descending order of client preference.
267 */
268 ciphersuites = ssl->conf->ciphersuite_list;
269
270 if( buflen < 2 /* for ciphersuite list length */ )
271 {
272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
273 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
274 }
275
276 /* Skip writing ciphersuite length for now */
277 ciphersuite_count = 0;
278 ciphersuite_start = buf;
279 buf += 2;
280 buflen -= 2;
281
Jerry Yue885b762021-08-26 17:32:34 +0800282 for ( size_t i = 0; ciphersuites[i] != 0; i++ )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800283 {
284 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
285
286 if( ciphersuite_info == NULL )
287 continue;
288
289 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
290 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
291 continue;
292
293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yue885b762021-08-26 17:32:34 +0800294 (unsigned int) ciphersuites[i],
295 ciphersuite_info->name ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800296
297 ciphersuite_count++;
298
299 if( buflen < 2 /* for ciphersuite list length */ )
300 {
301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
302 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
303 }
304
Jerry Yu2ac64192021-08-26 18:38:58 +0800305 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0);
Jerry Yubc20bdd2021-08-24 15:59:48 +0800306
Jerry Yu2ac64192021-08-26 18:38:58 +0800307 buf += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800308 buflen -= 2;
309
310 }
311
312 /* write ciphersuite length now */
Jerry Yu2ac64192021-08-26 18:38:58 +0800313 MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0);
314 ciphersuite_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800315
Jerry Yue885b762021-08-26 17:32:34 +0800316 MBEDTLS_SSL_DEBUG_MSG( 3,
317 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites",
318 ciphersuite_count ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800319
320 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
321 * one byte set to zero, which corresponds to the 'null' compression
322 * method in prior versions of TLS.
323 *
324 * For cTLS this field is elided.
325 */
326 if( buflen < 2 /* for ciphersuite list length */ )
327 {
328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
329 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
330 }
331
332 *buf++ = 1;
333 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
334
335 buflen -= 2;
336
337 /* First write extensions, then the total length */
338 extension_start = buf;
339 total_ext_len = 0;
340 buf += 2;
341
342 /* Supported Versions Extension is mandatory with TLS 1.3.
343 *
344 * For cTLS we only need to provide it if there is more than one version
345 * and currently there is only one.
346 */
Jerry Yuf4436812021-08-26 22:59:56 +0800347 ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800348 total_ext_len += cur_ext_len;
349 buf += cur_ext_len;
350
351#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
352 /* The supported_groups and the key_share extensions are
353 * REQUIRED for ECDHE ciphersuites.
354 */
Jerry Yuf4436812021-08-26 22:59:56 +0800355 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800356 if( ret != 0 )
357 return( ret );
358
359 total_ext_len += cur_ext_len;
360 buf += cur_ext_len;
361
362 /* The supported_signature_algorithms extension is REQUIRED for
363 * certificate authenticated ciphersuites. */
Jerry Yuf4436812021-08-26 22:59:56 +0800364 ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf,
365 end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800366 if( ret != 0 )
367 return( ret );
368
369 total_ext_len += cur_ext_len;
370 buf += cur_ext_len;
371
372 /* We need to send the key shares under three conditions:
373 * 1 ) A certificate-based ciphersuite is being offered. In this case
374 * supported_groups and supported_signature extensions have been successfully added.
375 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
376 * psk_key_exchange_modes has been added as the last extension.
377 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
378 */
379
Jerry Yuf4436812021-08-26 22:59:56 +0800380 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800381 if( ret != 0 )
382 return( ret );
383
384 total_ext_len += cur_ext_len;
385 buf += cur_ext_len;
386#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
387
388 /* Add more extensions here */
389
390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
391 total_ext_len ) );
392
393 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
394
395 /* Write extension length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800396 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0);
397 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800398
Jerry Yubc20bdd2021-08-24 15:59:48 +0800399 *len_with_binders = ( extension_start + total_ext_len ) - start;
400 return( 0 );
401}
402
Jerry Yuef6b36b2021-08-24 16:29:02 +0800403/*
Jerry Yuf4436812021-08-26 22:59:56 +0800404 * ssl_tls13_write_supported_versions_ext():
Jerry Yuef6b36b2021-08-24 16:29:02 +0800405 *
406 * struct {
407 * ProtocolVersion versions<2..254>;
408 * } SupportedVersions;
409 */
Jerry Yuf4436812021-08-26 22:59:56 +0800410static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800411 unsigned char *buf,
412 unsigned char *end,
413 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800414{
Jerry Yuef6b36b2021-08-24 16:29:02 +0800415 unsigned char *p = buf;
416
417 *olen = 0;
418
419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) );
420
Jerry Yu6f13f642021-08-26 17:18:15 +0800421 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
Jerry Yuef6b36b2021-08-24 16:29:02 +0800422
Jerry Yu2ac64192021-08-26 18:38:58 +0800423 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
Jerry Yuef6b36b2021-08-24 16:29:02 +0800424
425 /* total length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800426 MBEDTLS_PUT_UINT16_BE( 3, p, 2);
427
428 p+=4;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800429
430 /* length of next field */
431 *p++ = 0x2;
432
433 /* This implementation only supports a single TLS version, and only
434 * advertises a single value.
435 */
436 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
437 ssl->conf->transport, p );
438
Jerry Yue885b762021-08-26 17:32:34 +0800439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
440 ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
Jerry Yuef6b36b2021-08-24 16:29:02 +0800441
442 *olen = 7;
Jerry Yu6f13f642021-08-26 17:18:15 +0800443
444 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800445}
446
447#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
448
Jerry Yuf4436812021-08-26 22:59:56 +0800449static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800450 unsigned char *buf,
451 unsigned char *end,
452 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800453{
454 ((void) ssl);
455 ((void) buf);
456 ((void) end);
457 ((void) olen);
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800458 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
459}
460
Jerry Yuf4436812021-08-26 22:59:56 +0800461static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800462 unsigned char *buf,
463 unsigned char *end,
464 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800465{
466 ((void) ssl);
467 ((void) buf);
468 ((void) end);
469 ((void) olen);
470 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
471}
Jerry Yuc8a392c2021-08-18 16:46:28 +0800472
Jerry Yubc20bdd2021-08-24 15:59:48 +0800473#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800474
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800475#endif /* MBEDTLS_SSL_CLI_C */
476
477#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */