blob: ceb692ea5612f44979fc6e2aa8025127ac3597ba [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 {
52 case MBEDTLS_SSL_HELLO_REQUEST:
53 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
54 break;
55
56 case MBEDTLS_SSL_CLIENT_HELLO:
Jerry Yuf4436812021-08-26 22:59:56 +080057 ret = ssl_tls13_write_client_hello( ssl );
Jerry Yua13c7e72021-08-17 10:44:40 +080058 break;
59
60 case MBEDTLS_SSL_SERVER_HELLO:
61 // Stop here : we haven't finished whole flow
Jerry Yue885b762021-08-26 17:32:34 +080062 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yua13c7e72021-08-17 10:44:40 +080063 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
64 break;
65
66 default:
67 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
68 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
69 }
70
71 return( ret );
72}
73
Jerry Yu65dd2cc2021-08-18 16:38:40 +080074
Jerry Yuf4436812021-08-26 22:59:56 +080075static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl );
76static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +080077 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +080078 size_t *len_with_binders );
Jerry Yuf4436812021-08-26 22:59:56 +080079static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080080
Jerry Yuf4436812021-08-26 22:59:56 +080081static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yua13c7e72021-08-17 10:44:40 +080082{
83 int ret = 0;
Jerry Yu65dd2cc2021-08-18 16:38:40 +080084 unsigned char *buf;
85 size_t buf_len, msg_len;
Jerry Yua13c7e72021-08-17 10:44:40 +080086
87 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
88
Jerry Yuf4436812021-08-26 22:59:56 +080089 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080090
Jerry Yuf4436812021-08-26 22:59:56 +080091 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg,
Jerry Yue885b762021-08-26 17:32:34 +080092 ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
93 &buf, &buf_len ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080094
Jerry Yuf4436812021-08-26 22:59:56 +080095 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello,
Jerry Yue885b762021-08-26 17:32:34 +080096 ( ssl, buf, buf_len, &msg_len ) );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080097
Jerry Yuf4436812021-08-26 22:59:56 +080098 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu65dd2cc2021-08-18 16:38:40 +080099 msg_len );
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800100 ssl->handshake->update_checksum( ssl, buf, 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800101
Jerry Yuf4436812021-08-26 22:59:56 +0800102 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) );
103 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg,
Jerry Yue885b762021-08-26 17:32:34 +0800104 ( ssl, buf_len, msg_len ) );
Jerry Yua13c7e72021-08-17 10:44:40 +0800105
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800106cleanup:
107
Jerry Yua13c7e72021-08-17 10:44:40 +0800108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
109 /* client_hello_process haven't finished */
Jerry Yu55b90382021-08-26 18:42:05 +0800110 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yua13c7e72021-08-17 10:44:40 +0800111 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +0800112}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800113
Jerry Yuf4436812021-08-26 22:59:56 +0800114static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800115{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800116 int ret;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800117
Jerry Yue885b762021-08-26 17:32:34 +0800118 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
119 ssl->handshake->randbytes,
Jerry Yu6f13f642021-08-26 17:18:15 +0800120 CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 )
Jerry Yuc8a392c2021-08-18 16:46:28 +0800121 {
122 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
123 return( ret );
124 }
125
126 return( 0 );
127}
128
Jerry Yuf4436812021-08-26 22:59:56 +0800129static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yuc8a392c2021-08-18 16:46:28 +0800130{
131 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
132
133 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800134}
135
Jerry Yubc20bdd2021-08-24 15:59:48 +0800136/* Write extensions */
137
Jerry Yuf4436812021-08-26 22:59:56 +0800138static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800139 unsigned char *buf,
140 unsigned char *end,
141 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800142
143#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
144
Jerry Yuf4436812021-08-26 22:59:56 +0800145static int ssl_tls13_write_supported_groups_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
Jerry Yuf4436812021-08-26 22:59:56 +0800150static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800151 unsigned char *buf,
152 unsigned char *end,
153 size_t *olen );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800154
155#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
156
Jerry Yuf4436812021-08-26 22:59:56 +0800157static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800158 unsigned char *buf, size_t buflen,
Jerry Yuc7ddeec2021-08-26 16:23:47 +0800159 size_t *len_with_binders )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800160{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800161 /* Extensions */
162
163 /* extension_start
164 * Used during extension writing where the
165 * buffer pointer to the beginning of the
166 * extension list must be kept to write
167 * the total extension list size in the end.
168 */
Jerry Yu32cd5b12021-08-24 18:07:13 +0800169#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yubc20bdd2021-08-24 15:59:48 +0800170 int ret;
Jerry Yu32cd5b12021-08-24 18:07:13 +0800171#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800172 unsigned char* extension_start;
173 size_t cur_ext_len; /* Size of the current extension */
174 size_t total_ext_len; /* Size of list of extensions */
175
Jerry Yubc20bdd2021-08-24 15:59:48 +0800176 /* Buffer management */
177 unsigned char* start = buf;
178 unsigned char* end = buf + buflen;
179
180 /* Ciphersuite-related variables */
181 const int* ciphersuites;
182 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
Jerry Yue885b762021-08-26 17:32:34 +0800183 /* ciphersuite_start points to the start of
184 the ciphersuite list, i.e. to the length field*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800185 unsigned char* ciphersuite_start;
186 size_t ciphersuite_count;
187
188 /* Keeping track of the included extensions */
189 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
190
Jerry Yubc20bdd2021-08-24 15:59:48 +0800191 /* NOTE:
192 * Even for DTLS 1.3, we are writing a TLS handshake header here.
193 * The actual DTLS 1.3 handshake header is inserted in
194 * the record writing routine mbedtls_ssl_write_record().
195 *
196 * For cTLS the length, and the version field
197 * are elided. The random bytes are shorter.
198 */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800199
200 if( ssl->conf->max_major_ver == 0 )
201 {
202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
203 "consider using mbedtls_ssl_config_defaults()" ) );
204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
205 }
206
207 ssl->major_ver = ssl->conf->min_major_ver;
208 ssl->minor_ver = ssl->conf->min_minor_ver;
209
210 /* For TLS 1.3 we use the legacy version number {0x03, 0x03}
211 * instead of the true version number.
212 *
213 * For DTLS 1.3 we use the legacy version number
214 * {254,253}.
215 *
216 * In cTLS the version number is elided.
217 */
Jerry Yu6f13f642021-08-26 17:18:15 +0800218 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN);
Jerry Yu2ac64192021-08-26 18:38:58 +0800219 MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0);
220 buf += 2;
Jerry Yu6f13f642021-08-26 17:18:15 +0800221 buflen -= CLIENT_HELLO_VERSION_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800222
223 /* Write random bytes */
Jerry Yu6f13f642021-08-26 17:18:15 +0800224 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN);
225 memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800226 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
227 buf, CLIENT_HELLO_RAND_BYTES_LEN );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800228
Jerry Yu6f13f642021-08-26 17:18:15 +0800229 buf += CLIENT_HELLO_RAND_BYTES_LEN;
230 buflen -= CLIENT_HELLO_RAND_BYTES_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800231
232 /* Versions of TLS before TLS 1.3 supported a
233 * "session resumption" feature which has been merged with pre-shared
234 * keys in this version. A client which has a
235 * cached session ID set by a pre-TLS 1.3 server SHOULD set this
236 * field to that value. In compatibility mode,
237 * this field MUST be non-empty, so a client not offering a
238 * pre-TLS 1.3 session MUST generate a new 32-byte value. This value
239 * need not be random but SHOULD be unpredictable to avoid
240 * implementations fixating on a specific value ( also known as
241 * ossification ). Otherwise, it MUST be set as a zero-length vector
242 * ( i.e., a zero-valued single byte length field ).
243 */
244 if( buflen < 1 )
245 {
246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
247 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
248 }
249
250 *buf++ = 0; /* session id length set to zero */
251 buflen -= 1;
252
253 /*
254 * Ciphersuite list
255 *
256 * This is a list of the symmetric cipher options supported by
257 * the client, specifically the record protection algorithm
258 * ( including secret key length ) and a hash to be used with
259 * HKDF, in descending order of client preference.
260 */
261 ciphersuites = ssl->conf->ciphersuite_list;
262
263 if( buflen < 2 /* for ciphersuite list length */ )
264 {
265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
266 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
267 }
268
269 /* Skip writing ciphersuite length for now */
270 ciphersuite_count = 0;
271 ciphersuite_start = buf;
272 buf += 2;
273 buflen -= 2;
274
Jerry Yue885b762021-08-26 17:32:34 +0800275 for ( size_t i = 0; ciphersuites[i] != 0; i++ )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800276 {
277 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
278
279 if( ciphersuite_info == NULL )
280 continue;
281
282 if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ||
283 ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
284 continue;
285
286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yue885b762021-08-26 17:32:34 +0800287 (unsigned int) ciphersuites[i],
288 ciphersuite_info->name ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800289
290 ciphersuite_count++;
291
292 if( buflen < 2 /* for ciphersuite list length */ )
293 {
294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
295 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
296 }
297
Jerry Yu2ac64192021-08-26 18:38:58 +0800298 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0);
Jerry Yubc20bdd2021-08-24 15:59:48 +0800299
Jerry Yu2ac64192021-08-26 18:38:58 +0800300 buf += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800301 buflen -= 2;
302
303 }
304
305 /* write ciphersuite length now */
Jerry Yu2ac64192021-08-26 18:38:58 +0800306 MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0);
307 ciphersuite_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800308
Jerry Yue885b762021-08-26 17:32:34 +0800309 MBEDTLS_SSL_DEBUG_MSG( 3,
310 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites",
311 ciphersuite_count ) );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800312
313 /* For every TLS 1.3 ClientHello, this vector MUST contain exactly
314 * one byte set to zero, which corresponds to the 'null' compression
315 * method in prior versions of TLS.
316 *
317 * For cTLS this field is elided.
318 */
319 if( buflen < 2 /* for ciphersuite list length */ )
320 {
321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) );
322 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
323 }
324
325 *buf++ = 1;
326 *buf++ = MBEDTLS_SSL_COMPRESS_NULL;
327
328 buflen -= 2;
329
330 /* First write extensions, then the total length */
331 extension_start = buf;
332 total_ext_len = 0;
333 buf += 2;
334
335 /* Supported Versions Extension is mandatory with TLS 1.3.
336 *
337 * For cTLS we only need to provide it if there is more than one version
338 * and currently there is only one.
339 */
Jerry Yuf4436812021-08-26 22:59:56 +0800340 ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800341 total_ext_len += cur_ext_len;
342 buf += cur_ext_len;
343
344#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
345 /* The supported_groups and the key_share extensions are
346 * REQUIRED for ECDHE ciphersuites.
347 */
Jerry Yuf4436812021-08-26 22:59:56 +0800348 ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800349 if( ret != 0 )
350 return( ret );
351
352 total_ext_len += cur_ext_len;
353 buf += cur_ext_len;
354
355 /* The supported_signature_algorithms extension is REQUIRED for
356 * certificate authenticated ciphersuites. */
Jerry Yuf4436812021-08-26 22:59:56 +0800357 ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf,
358 end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800359 if( ret != 0 )
360 return( ret );
361
362 total_ext_len += cur_ext_len;
363 buf += cur_ext_len;
364
365 /* We need to send the key shares under three conditions:
366 * 1 ) A certificate-based ciphersuite is being offered. In this case
367 * supported_groups and supported_signature extensions have been successfully added.
368 * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the
369 * psk_key_exchange_modes has been added as the last extension.
370 * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above )
371 */
372
Jerry Yuf4436812021-08-26 22:59:56 +0800373 ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800374 if( ret != 0 )
375 return( ret );
376
377 total_ext_len += cur_ext_len;
378 buf += cur_ext_len;
379#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
380
381 /* Add more extensions here */
382
383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
384 total_ext_len ) );
385
386 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len );
387
388 /* Write extension length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800389 MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0);
390 extension_start += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800391
Jerry Yubc20bdd2021-08-24 15:59:48 +0800392 *len_with_binders = ( extension_start + total_ext_len ) - start;
393 return( 0 );
394}
395
Jerry Yuef6b36b2021-08-24 16:29:02 +0800396/*
Jerry Yuf4436812021-08-26 22:59:56 +0800397 * ssl_tls13_write_supported_versions_ext():
Jerry Yuef6b36b2021-08-24 16:29:02 +0800398 *
399 * struct {
400 * ProtocolVersion versions<2..254>;
401 * } SupportedVersions;
402 */
Jerry Yuf4436812021-08-26 22:59:56 +0800403static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800404 unsigned char *buf,
405 unsigned char *end,
406 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800407{
Jerry Yuef6b36b2021-08-24 16:29:02 +0800408 unsigned char *p = buf;
409
410 *olen = 0;
411
412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) );
413
Jerry Yu6f13f642021-08-26 17:18:15 +0800414 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
Jerry Yuef6b36b2021-08-24 16:29:02 +0800415
Jerry Yu2ac64192021-08-26 18:38:58 +0800416 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
Jerry Yuef6b36b2021-08-24 16:29:02 +0800417
418 /* total length */
Jerry Yu2ac64192021-08-26 18:38:58 +0800419 MBEDTLS_PUT_UINT16_BE( 3, p, 2);
420
421 p+=4;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800422
423 /* length of next field */
424 *p++ = 0x2;
425
426 /* This implementation only supports a single TLS version, and only
427 * advertises a single value.
428 */
429 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
430 ssl->conf->transport, p );
431
Jerry Yue885b762021-08-26 17:32:34 +0800432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
433 ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
Jerry Yuef6b36b2021-08-24 16:29:02 +0800434
435 *olen = 7;
Jerry Yu6f13f642021-08-26 17:18:15 +0800436
437 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800438}
439
440#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
441
Jerry Yuf4436812021-08-26 22:59:56 +0800442static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800443 unsigned char *buf,
444 unsigned char *end,
445 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800446{
447 ((void) ssl);
448 ((void) buf);
449 ((void) end);
450 ((void) olen);
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800451 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
452}
453
Jerry Yuf4436812021-08-26 22:59:56 +0800454static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl,
Jerry Yu6f13f642021-08-26 17:18:15 +0800455 unsigned char *buf,
456 unsigned char *end,
457 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800458{
459 ((void) ssl);
460 ((void) buf);
461 ((void) end);
462 ((void) olen);
463 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
464}
Jerry Yuc8a392c2021-08-18 16:46:28 +0800465
Jerry Yubc20bdd2021-08-24 15:59:48 +0800466#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800467
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800468#endif /* MBEDTLS_SSL_CLI_C */
469
470#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */