blob: 8323b6778208783a4f6e5368fbb16c6ea612f243 [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 Yu56fc07f2021-09-01 17:48:49 +080031#include "mbedtls/debug.h"
32#include "mbedtls/error.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080033
Jerry Yu08906d02021-08-31 11:05:27 +080034#define CLIENT_HELLO_RANDOM_LEN 32
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 Yu6b64fe32021-09-01 17:05:13 +080097/*
98 * Functions for writing supported_groups extension.
99 *
100 * Stucture of supported_groups:
101 * enum {
102 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
103 * x25519(0x001D), x448(0x001E),
104 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
105 * ffdhe6144(0x0103), ffdhe8192(0x0104),
106 * ffdhe_private_use(0x01FC..0x01FF),
107 * ecdhe_private_use(0xFE00..0xFEFF),
108 * (0xFFFF)
109 * } NamedGroup;
110 * struct {
111 * NamedGroup named_group_list<2..2^16-1>;
112 * } NamedGroupList;
113 */
114/* Find out available ecdhe named groups in current configuration */
115#if defined(MBEDTLS_ECDH_C)
116/*
117 * In versions of TLS prior to TLS 1.3, this extension was named
118 * 'elliptic_curves' and only contained elliptic curve groups.
119 */
120static int ssl_tls13_write_named_group_ecdhe( mbedtls_ssl_context *ssl,
121 unsigned char *buf,
122 unsigned char *end,
123 size_t *olen )
124{
125 unsigned char *p = buf;
126#if !defined(MBEDTLS_ECP_C)
127 ((void) ssl);
128#endif
129
130 *olen = 0;
131
132#if defined(MBEDTLS_ECP_C)
133 for ( const mbedtls_ecp_group_id *grp_id = ssl->conf->curve_list;
134 *grp_id != MBEDTLS_ECP_DP_NONE;
135 grp_id++ )
136 {
137 const mbedtls_ecp_curve_info *info;
138 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
139 if( info == NULL )
140 continue;
141#else
142 for ( const mbedtls_ecp_curve_info *info = mbedtls_ecp_curve_list();
143 info->grp_id != MBEDTLS_ECP_DP_NONE;
144 info++ )
145 {
146#endif
147 if( !mbedtls_ssl_named_group_is_ecdhe( info->tls_id ) )
148 continue;
149
150 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2);
151 MBEDTLS_PUT_UINT16_BE( info->tls_id, p, 0 );
152 p += 2;
153
154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
155 mbedtls_ecp_curve_info_from_tls_id( info->tls_id )->name,
156 info->tls_id ) );
157 }
158
159 *olen = p - buf;
160
161 return( 0 );
162}
163#else
164static int ssl_tls13_write_named_group_ecdhe( mbedtls_ssl_context *ssl,
165 unsigned char *buf,
166 unsigned char *end,
167 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +0800168{
169 ((void) ssl);
170 ((void) buf);
171 ((void) end);
Jerry Yu75336352021-09-01 15:59:36 +0800172 *olen = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
174}
175#endif /* MBEDTLS_ECDH_C */
176
177/* Find out available dhe named groups in current configuration */
178static int ssl_tls13_write_named_group_dhe( mbedtls_ssl_context *ssl,
179 unsigned char *buf,
180 unsigned char *end,
181 size_t *olen )
182{
183 ((void) ssl);
184 ((void) buf);
185 ((void) end);
186 *olen = 0;
187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) );
188 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
189}
190
191/*
192 * Supported Groups Extension (supported_groups)
193 */
194static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
195 unsigned char *buf,
196 unsigned char *end,
197 size_t *olen )
198{
199 unsigned char *p = buf ;
200 unsigned char *named_group_ptr; /* Start of named_group_list */
201 size_t named_group_len = 0;
202 int ret = 0, ret_ecdhe, ret_dhe;
203
204 *olen = 0;
205
206 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
207 return( 0 );
208
209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
210
211 /* Check there is space for extension header */
212 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
213 p += 6;
214
215 named_group_ptr = p;
216 ret_ecdhe = ssl_tls13_write_named_group_ecdhe( ssl, p, end, &named_group_len );
217 if( ret_ecdhe != 0 )
218 {
219 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_ecdhe", ret );
220 }
221 p += named_group_len;
222
223 ret_dhe = ssl_tls13_write_named_group_dhe( ssl, p, end, &named_group_len );
224 if( ret_dhe != 0 )
225 {
226 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_dhe", ret );
227 }
228 p += named_group_len;
229
230 /* Both ECDHE and DHE Fail. */
231 if( ret_ecdhe != 0 && ret_dhe != 0 )
232 {
233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) );
234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
235 }
236
237 /* Length of named_group_list*/
238 named_group_len = p - named_group_ptr;
239 if( named_group_len == 0 )
240 {
241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No Named Group Available." ) );
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 /* Write extension_type */
246 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
247 /* Write extension_data_length */
248 MBEDTLS_PUT_UINT16_BE( named_group_len + 2, buf, 2 );
249 /* Write length of named_group_list */
250 MBEDTLS_PUT_UINT16_BE( named_group_len, buf, 4 );
251
252 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_len + 2 );
253
254 *olen = p - buf;
255
256 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
257
258 return( ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800259}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800260
Jerry Yu56fc07f2021-09-01 17:48:49 +0800261/*
262 * Functions for writing key_share extension.
263 */
264#if defined(MBEDTLS_ECDH_C)
265static int ssl_key_share_gen_and_write_ecdhe( mbedtls_ssl_context *ssl,
266 uint16_t named_group,
267 unsigned char *buf,
268 unsigned char *end,
269 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +0800270{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
272
273 const mbedtls_ecp_curve_info *curve_info =
274 mbedtls_ecp_curve_info_from_tls_id( named_group );
275
276 if( curve_info == NULL )
277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
278
279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
280
281 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
282 curve_info->grp_id ) ) != 0 )
283 {
284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
285 return( ret );
286 }
287
288 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, olen,
289 buf, end - buf,
290 ssl->conf->f_rng, ssl->conf->p_rng );
291 if( ret != 0 )
292 {
293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
294 return( ret );
295 }
296
297 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
298 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800299 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800300}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800301#endif /* MBEDTLS_ECDH_C */
302
303static int ssl_named_group_get_default_id( mbedtls_ssl_context *ssl,
304 uint16_t *named_group_id )
305{
306 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
307
308 /* Pick first entry of curve list.
309 *
310 * TODO: When we introduce PQC KEMs, we'll have a NamedGroup
311 * list instead, and can just return its first element.
312 */
313
314 /* Check if ecdhe named groups are available and pick first entry */
315#if defined(MBEDTLS_ECDH_C)
316#if !defined(MBEDTLS_ECP_C)
317 ((void) ssl);
318#endif
319#if defined(MBEDTLS_ECP_C)
320 for ( const mbedtls_ecp_group_id * grp_id = ssl->conf->curve_list;
321 *grp_id != MBEDTLS_ECP_DP_NONE;
322 grp_id++ )
323 {
324 const mbedtls_ecp_curve_info *info;
325 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
326#else
327 for ( const mbedtls_ecp_curve_info *info = mbedtls_ecp_curve_list();
328 info->grp_id != MBEDTLS_ECP_DP_NONE;
329 info++ )
330 {
331#endif
332 if( info != NULL && mbedtls_ssl_named_group_is_ecdhe( info->tls_id ) )
333 {
334 *named_group_id = info->tls_id;
335 return( 0 );
336 }
337 }
338#else
339 ((void) ssl);
340 ((void) named_group_id);
341#endif /* MBEDTLS_ECDH_C */
342
343 /*
344 * Add DHE named groups here.
345 * Check if ecdhe named groups are available and pick first entry
346 */
347
348 return( ret );
349}
350
351/*
352 * ssl_tls13_write_key_share_ext
353 *
354 * Structure of key_share extension in ClientHelo:
355 *
356 * struct {
357 * NamedGroup group;
358 * opaque key_exchange<1..2^16-1>;
359 * } KeyShareEntry;
360 * struct {
361 * KeyShareEntry client_shares<0..2^16-1>;
362 * } KeyShareClientHello;
363 */
364static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
365 unsigned char *buf,
366 unsigned char *end,
367 size_t *olen )
368{
369 unsigned char *p = buf;
370 unsigned char *client_shares_ptr; /* Start of client_shares */
371 uint16_t group_id;
372
373 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
374
375 *olen = 0;
376
377 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
378 return( 0 );
379
380 /* Check if we have space for headers and length fields:
381 * - extension_type (2 bytes)
382 * - extension_data_length (2 bytes)
383 * - client_shares_length (2 bytes)
384 */
385 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
386 p += 6;
387
388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
389
390 /* HRR could already have requested something else. */
391 group_id = ssl->handshake->offered_group_id;
392 if( !mbedtls_ssl_named_group_is_ecdhe( group_id ) &&
393 !mbedtls_ssl_named_group_is_dhe( group_id ) )
394 {
395 MBEDTLS_SSL_PROC_CHK( ssl_named_group_get_default_id( ssl,
396 &group_id ) );
397 }
398
399 /*
400 * Dispatch to type-specific key generation function.
401 *
402 * So far, we're only supporting ECDHE. With the introduction
403 * of PQC KEMs, we'll want to have multiple branches, one per
404 * type of KEM, and dispatch to the corresponding crypto. And
405 * only one key share entry is allowed.
406 */
407 client_shares_ptr = p;
408#if defined(MBEDTLS_ECDH_C)
409 if( mbedtls_ssl_named_group_is_ecdhe( group_id ) )
410 {
411 /* Pointer of group */
412 unsigned char *group_id_ptr = p;
413 /* Length of key_exchange */
414 size_t key_exchange_len;
415
416 /* Check there is space for header of KeyShareEntry
417 * - group (2 bytes)
418 * - key_exchange_length (2 bytes)
419 */
420 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
421 p += 4;
422 ret = ssl_key_share_gen_and_write_ecdhe( ssl, group_id,
423 p, end,
424 &key_exchange_len );
425 p += key_exchange_len;
426 if( ret != 0 )
427 return( ret );
428
429 /* Write group */
430 MBEDTLS_PUT_UINT16_BE( group_id, group_id_ptr, 0 );
431 /* Write key_exchange_length */
432 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group_id_ptr, 2 );
433 }
434 else
435#endif /* MBEDTLS_ECDH_C */
436 if( 0 /* other KEMs? */ )
437 {
438 /* Do something */
439 }
440 else
441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
442
443 /* Write extension_type */
444 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
445 /* Write extension_data_length */
446 MBEDTLS_PUT_UINT16_BE( p - client_shares_ptr + 2, buf, 2 );
447 /* Write client_shares_length */
448 MBEDTLS_PUT_UINT16_BE( p - client_shares_ptr, buf, 4 );
449
450 /* Update offered_group_id field */
451 ssl->handshake->offered_group_id = group_id;
452
453 /* Output the total length of key_share extension. */
454 *olen = p - buf;
455
456 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *olen );
457
458 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
459
460cleanup:
461
462 return( ret );
463}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800464
465#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
466
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800467/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800468 * CipherSuite cipher_suites<2..2^16-2>;
469 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800470static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800471 mbedtls_ssl_context *ssl,
472 unsigned char *buf,
473 unsigned char *end,
474 size_t *olen )
475{
Jerry Yufec982e2021-09-07 17:26:06 +0800476 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800477 const int *ciphersuite_list;
Jerry Yubbe09522021-09-06 21:17:54 +0800478 unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800479 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800480
Jerry Yu6a643102021-08-31 14:40:36 +0800481 *olen = 0 ;
482
483 /*
484 * Ciphersuite list
485 *
486 * This is a list of the symmetric cipher options supported by
487 * the client, specifically the record protection algorithm
488 * ( including secret key length ) and a hash to be used with
489 * HKDF, in descending order of client preference.
490 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800491 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800492
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800493 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800494 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
495 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800496
Jerry Yu0c63af62021-09-02 12:59:12 +0800497 /* Write cipher_suites */
Jerry Yu4e388282021-09-06 21:28:08 +0800498 cipher_suites_ptr = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800499 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800500 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800501 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800502 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800503
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800504 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800505 if( ciphersuite_info == NULL )
506 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800507 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
508 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800509 continue;
510
511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800512 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800513 ciphersuite_info->name ) );
514
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800515 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800516 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
517 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
518 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800519 }
520
Jerry Yu0c63af62021-09-02 12:59:12 +0800521 /* Write the cipher_suites length in number of bytes */
Jerry Yubbe09522021-09-06 21:17:54 +0800522 cipher_suites_len = p - cipher_suites_ptr;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800523 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800524 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800525 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
526 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800527
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800528 /* Output the total length of cipher_suites field. */
Jerry Yubbe09522021-09-06 21:17:54 +0800529 *olen = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800530
Jerry Yu6a643102021-08-31 14:40:36 +0800531 return( 0 );
532}
533
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800534/*
535 * Structure of ClientHello message:
536 *
537 * struct {
538 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
539 * Random random;
540 * opaque legacy_session_id<0..32>;
541 * CipherSuite cipher_suites<2..2^16-2>;
542 * opaque legacy_compression_methods<1..2^8-1>;
543 * Extension extensions<8..2^16-1>;
544 * } ClientHello;
545 */
Jerry Yu08906d02021-08-31 11:05:27 +0800546static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800547 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800548 unsigned char *end,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800549 size_t *olen )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800550{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800551
Jerry Yubc20bdd2021-08-24 15:59:48 +0800552 int ret;
Jerry Yu8c02bb42021-09-03 21:09:22 +0800553 unsigned char *extensions_len_ptr; /* Pointer to extensions length */
Jerry Yu790656a2021-09-01 15:51:48 +0800554 size_t output_len; /* Length of buffer used by function */
555 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800556
Jerry Yubc20bdd2021-08-24 15:59:48 +0800557 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800558 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800559
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800560 *olen = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800561
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800562 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800563 ssl->major_ver = ssl->conf->min_major_ver;
564 ssl->minor_ver = ssl->conf->min_minor_ver;
565
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800566 /*
567 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800568 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800569 *
570 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800571 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800572 */
Jerry Yufec982e2021-09-07 17:26:06 +0800573 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800574 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800575 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800576
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800577 /* Write the random bytes ( random ).*/
Jerry Yubbe09522021-09-06 21:17:54 +0800578 MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_RANDOM_LEN );
579 memcpy( p, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800580 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yubbe09522021-09-06 21:17:54 +0800581 p, CLIENT_HELLO_RANDOM_LEN );
582 p += CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800583
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800584 /*
585 * Write legacy_session_id
586 *
587 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
588 * which has been merged with pre-shared keys in this version. A client
589 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
590 * this field to that value. In compatibility mode, this field MUST be
591 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
592 * a new 32-byte value. This value need not be random but SHOULD be
593 * unpredictable to avoid implementations fixating on a specific value
594 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
595 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800596 */
Jerry Yubbe09522021-09-06 21:17:54 +0800597 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
598 *p++ = 0; /* session id length set to zero */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800599
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800600 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800601 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800602 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800603 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800604 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800605
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800606 /* Write legacy_compression_methods
607 *
608 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800609 * one byte set to zero, which corresponds to the 'null' compression
610 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800611 */
Jerry Yubbe09522021-09-06 21:17:54 +0800612 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
613 *p++ = 1;
614 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800615
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800616 /* Write extensions */
617
618 /* Keeping track of the included extensions */
619 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800620
621 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800622 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
623 extensions_len_ptr = p;
624 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800625
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800626 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800627 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800628 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800629 */
Jerry Yubbe09522021-09-06 21:17:54 +0800630 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800631 if( ret != 0 )
632 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800633 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800634
635#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800636 /* Write supported_groups extension
637 *
638 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800639 */
Jerry Yubbe09522021-09-06 21:17:54 +0800640 ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800641 if( ret != 0 )
642 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800643 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800644
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800645 /* Write key_share extension
646 *
647 * We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800648 * 1) A certificate-based ciphersuite is being offered. In this case
649 * supported_groups and supported_signature extensions have been
650 * successfully added.
651 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800652 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800653 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
654 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800655 */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800656 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800657 if( ret != 0 )
658 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800659 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800660
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800661 /* Write signature_algorithms extension
662 *
663 * It is REQUIRED for certificate authenticated cipher_suites.
664 */
Jerry Yubbe09522021-09-06 21:17:54 +0800665 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800666 if( ret != 0 )
667 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800668 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800669
Jerry Yubc20bdd2021-08-24 15:59:48 +0800670#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
671
672 /* Add more extensions here */
673
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800674 /* Write the length of the list of extensions. */
Jerry Yubbe09522021-09-06 21:17:54 +0800675 extensions_len = p - extensions_len_ptr - 2;
Jerry Yu790656a2021-09-01 15:51:48 +0800676 MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800678 extensions_len ) );
679 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extensions_len_ptr, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800680
Jerry Yubbe09522021-09-06 21:17:54 +0800681 *olen = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800682 return( 0 );
683}
684
Jerry Yu92c6b402021-08-27 16:59:09 +0800685static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800686{
Jerry Yu92c6b402021-08-27 16:59:09 +0800687 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
688 return( 0 );
689}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800690
Jerry Yu92c6b402021-08-27 16:59:09 +0800691static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
692{
693 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800694
Jerry Yu92c6b402021-08-27 16:59:09 +0800695 if( ssl->conf->f_rng == NULL )
696 {
697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
698 return( MBEDTLS_ERR_SSL_NO_RNG );
699 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800700
Jerry Yu92c6b402021-08-27 16:59:09 +0800701 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
702 ssl->handshake->randbytes,
Jerry Yu08906d02021-08-31 11:05:27 +0800703 CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800704 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800705 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800706 return( ret );
707 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800708
709 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800710}
711
Jerry Yu92c6b402021-08-27 16:59:09 +0800712/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800713 * Write ClientHello handshake message.
Jerry Yu92c6b402021-08-27 16:59:09 +0800714 */
715static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800716{
Jerry Yu92c6b402021-08-27 16:59:09 +0800717 int ret = 0;
718 unsigned char *buf;
719 size_t buf_len, msg_len;
720
721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
722
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800723 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800724
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800725 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
726 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
727 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800728
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800729 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800730 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800731 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800732
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800733 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
734 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800735 msg_len );
736 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800737
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800738 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
739 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
740 buf_len,
741 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800742
743cleanup:
744
745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
746 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800747}
748
Jerry Yu92c6b402021-08-27 16:59:09 +0800749int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800750{
Jerry Yu92c6b402021-08-27 16:59:09 +0800751 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800752
Jerry Yu92c6b402021-08-27 16:59:09 +0800753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
754
755 switch( ssl->state )
756 {
757 /*
Jerry Yu0c63af62021-09-02 12:59:12 +0800758 * ssl->state is initialized as HELLO_REQUEST. It is the same
759 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +0800760 */
761 case MBEDTLS_SSL_HELLO_REQUEST:
762 case MBEDTLS_SSL_CLIENT_HELLO:
763 ret = ssl_tls13_write_client_hello( ssl );
764 break;
765
766 case MBEDTLS_SSL_SERVER_HELLO:
767 // Stop here : we haven't finished whole flow
768 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
769 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
770 break;
771
772 default:
773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
775 }
776
777 return( ret );
778}
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800779
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800780#endif /* MBEDTLS_SSL_CLI_C */
781
782#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */