blob: 57a0b2882aaec6014724fb5f6fab2b4e442d6de9 [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 Yu56fc07f2021-09-01 17:48:49 +080030#include "mbedtls/debug.h"
31#include "mbedtls/error.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080032#include "mbedtls/platform.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080033
Jerry Yubdc71882021-09-14 19:30:36 +080034#include "ssl_misc.h"
35#include "ecdh_misc.h"
Jerry Yue1b9c292021-09-10 10:08:31 +080036#include "ssl_tls13_keys.h"
Jerry Yubdc71882021-09-14 19:30:36 +080037
Jerry Yu08906d02021-08-31 11:05:27 +080038#define CLIENT_HELLO_RANDOM_LEN 32
Jerry Yue1b9c292021-09-10 10:08:31 +080039#define SERVER_HELLO_RANDOM_LEN 32
Jerry Yu65dd2cc2021-08-18 16:38:40 +080040
Jerry Yubc20bdd2021-08-24 15:59:48 +080041/* Write extensions */
42
Jerry Yu92c6b402021-08-27 16:59:09 +080043/*
44 * ssl_tls13_write_supported_versions_ext():
45 *
46 * struct {
47 * ProtocolVersion versions<2..254>;
48 * } SupportedVersions;
49 */
Jerry Yuf4436812021-08-26 22:59:56 +080050static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080051 unsigned char *buf,
52 unsigned char *end,
53 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +080054{
55 unsigned char *p = buf;
56
57 *olen = 0;
58
Jerry Yu159c5a02021-08-31 12:51:25 +080059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080060
Jerry Yu388bd0d2021-09-15 18:41:02 +080061 /* Check if we have space to write the extension:
Jerry Yub60e3cf2021-09-08 16:41:02 +080062 * - extension_type (2 bytes)
63 * - extension_data_length (2 bytes)
64 * - versions_length (1 byte )
65 * - versions (2 bytes)
Jerry Yu159c5a02021-08-31 12:51:25 +080066 */
Jerry Yu92c6b402021-08-27 16:59:09 +080067 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
68
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080069 /* Write extension_type */
Jerry Yueecfbf02021-08-30 18:32:07 +080070 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +080071
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080072 /* Write extension_data_length */
Jerry Yub7ab3362021-08-31 16:16:19 +080073 MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
Jerry Yueecfbf02021-08-30 18:32:07 +080074 p += 4;
Jerry Yu92c6b402021-08-27 16:59:09 +080075
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080076 /* Length of versions */
Jerry Yu92c6b402021-08-27 16:59:09 +080077 *p++ = 0x2;
78
Jerry Yu0c63af62021-09-02 12:59:12 +080079 /* Write values of supported versions.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080080 *
Jerry Yu0c63af62021-09-02 12:59:12 +080081 * They are defined by the configuration.
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080082 *
Jerry Yu0c63af62021-09-02 12:59:12 +080083 * Currently, only one version is advertised.
Jerry Yu92c6b402021-08-27 16:59:09 +080084 */
Jerry Yueecfbf02021-08-30 18:32:07 +080085 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
86 ssl->conf->max_minor_ver,
87 ssl->conf->transport, p );
Jerry Yu92c6b402021-08-27 16:59:09 +080088
89 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
Jerry Yueecfbf02021-08-30 18:32:07 +080090 ssl->conf->max_major_ver,
91 ssl->conf->max_minor_ver ) );
Jerry Yu92c6b402021-08-27 16:59:09 +080092
93 *olen = 7;
94
95 return( 0 );
96}
Jerry Yubc20bdd2021-08-24 15:59:48 +080097
Jerry Yue1b9c292021-09-10 10:08:31 +080098static int ssl_tls1_3_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
99 const unsigned char *buf,
100 size_t len )
101{
102 /* TODO: Implement full version and remove force version set in
103 * ssl_tls_parse_server_hello.
104 *
105 * From page 40,RFC 8446
106 * If supported_versions extension is present, clients MUST ignore the
107 * ServerHello.legacy_version value and MUST use only the
108 * "supported_versions" extension to determine the selected version. If
109 * the "supported_versions" extension in the ServerHello contains a
110 * version not offered by the client or contains a version prior to
111 * TLS 1.3, the client MUST abort the handshake with an
112 * "illegal_parameter" alert.
113 */
114
115 ((void) ssl);
116
117 if( len != 2 ||
118 buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
119 buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
122 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
123 }
124
125 return( 0 );
126}
127
Jerry Yubc20bdd2021-08-24 15:59:48 +0800128#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
129
Jerry Yu6b64fe32021-09-01 17:05:13 +0800130/*
131 * Functions for writing supported_groups extension.
132 *
133 * Stucture of supported_groups:
134 * enum {
135 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
136 * x25519(0x001D), x448(0x001E),
137 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
138 * ffdhe6144(0x0103), ffdhe8192(0x0104),
139 * ffdhe_private_use(0x01FC..0x01FF),
140 * ecdhe_private_use(0xFE00..0xFEFF),
141 * (0xFFFF)
142 * } NamedGroup;
143 * struct {
144 * NamedGroup named_group_list<2..2^16-1>;
145 * } NamedGroupList;
146 */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800147#if defined(MBEDTLS_ECDH_C)
148/*
149 * In versions of TLS prior to TLS 1.3, this extension was named
150 * 'elliptic_curves' and only contained elliptic curve groups.
151 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800152static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
153 unsigned char *buf,
154 unsigned char *end,
155 size_t *olen )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800156{
157 unsigned char *p = buf;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800158
159 *olen = 0;
160
Jerry Yu7c522d42021-09-08 17:55:09 +0800161 if( ssl->conf->curve_list == NULL )
162 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
163
Jerry Yu6b64fe32021-09-01 17:05:13 +0800164 for ( const mbedtls_ecp_group_id *grp_id = ssl->conf->curve_list;
165 *grp_id != MBEDTLS_ECP_DP_NONE;
166 grp_id++ )
167 {
168 const mbedtls_ecp_curve_info *info;
169 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
170 if( info == NULL )
171 continue;
Jerry Yu7c522d42021-09-08 17:55:09 +0800172
Jerry Yub60e3cf2021-09-08 16:41:02 +0800173 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( info->tls_id ) )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800174 continue;
175
176 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2);
177 MBEDTLS_PUT_UINT16_BE( info->tls_id, p, 0 );
178 p += 2;
179
180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
181 mbedtls_ecp_curve_info_from_tls_id( info->tls_id )->name,
182 info->tls_id ) );
183 }
184
185 *olen = p - buf;
186
187 return( 0 );
188}
189#else
Jerry Yub60e3cf2021-09-08 16:41:02 +0800190static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
191 unsigned char *buf,
192 unsigned char *end,
193 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +0800194{
195 ((void) ssl);
196 ((void) buf);
197 ((void) end);
Jerry Yu75336352021-09-01 15:59:36 +0800198 *olen = 0;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800199 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
200}
201#endif /* MBEDTLS_ECDH_C */
202
Jerry Yub60e3cf2021-09-08 16:41:02 +0800203static int ssl_tls13_write_named_group_list_dhe( mbedtls_ssl_context *ssl,
204 unsigned char *buf,
205 unsigned char *end,
206 size_t *olen )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800207{
208 ((void) ssl);
209 ((void) buf);
210 ((void) end);
211 *olen = 0;
212 MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) );
213 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
214}
215
Jerry Yu6b64fe32021-09-01 17:05:13 +0800216static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
217 unsigned char *buf,
218 unsigned char *end,
219 size_t *olen )
220{
221 unsigned char *p = buf ;
Jerry Yu72fc69b2021-09-10 10:23:24 +0800222 unsigned char *named_group_list_ptr; /* Start of named_group_list */
223 size_t named_group_list_len; /* Length of named_group_list */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800224 size_t output_len = 0;
225 int ret_ecdhe, ret_dhe;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800226
227 *olen = 0;
228
229 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
230 return( 0 );
231
232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
233
Jerry Yub60e3cf2021-09-08 16:41:02 +0800234 /* Check if we have space for header and length fields:
235 * - extension_type (2 bytes)
236 * - extension_data_length (2 bytes)
237 * - named_group_list_length (2 bytes)
238 */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800239 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
240 p += 6;
241
Jerry Yu72fc69b2021-09-10 10:23:24 +0800242 named_group_list_ptr = p;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800243 ret_ecdhe = ssl_tls13_write_named_group_list_ecdhe( ssl, p, end, &output_len );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800244 if( ret_ecdhe != 0 )
245 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800246 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_ecdhe", ret_ecdhe );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800247 }
Jerry Yub60e3cf2021-09-08 16:41:02 +0800248 p += output_len;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800249
Jerry Yub60e3cf2021-09-08 16:41:02 +0800250 ret_dhe = ssl_tls13_write_named_group_list_dhe( ssl, p, end, &output_len );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800251 if( ret_dhe != 0 )
252 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800253 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_dhe", ret_dhe );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800254 }
Jerry Yub60e3cf2021-09-08 16:41:02 +0800255 p += output_len;
Jerry Yu6b64fe32021-09-01 17:05:13 +0800256
Jerry Yu388bd0d2021-09-15 18:41:02 +0800257 /* Both ECDHE and DHE failed. */
Jerry Yu6b64fe32021-09-01 17:05:13 +0800258 if( ret_ecdhe != 0 && ret_dhe != 0 )
259 {
260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) );
261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
262 }
263
264 /* Length of named_group_list*/
Jerry Yu72fc69b2021-09-10 10:23:24 +0800265 named_group_list_len = p - named_group_list_ptr;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800266 if( named_group_list_len == 0 )
Jerry Yu6b64fe32021-09-01 17:05:13 +0800267 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
270 }
271
272 /* Write extension_type */
273 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
274 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800275 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800276 /* Write length of named_group_list */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800277 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800278
Jerry Yub60e3cf2021-09-08 16:41:02 +0800279 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_list_len + 2 );
Jerry Yu6b64fe32021-09-01 17:05:13 +0800280
281 *olen = p - buf;
282
283 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
284
Jerry Yub60e3cf2021-09-08 16:41:02 +0800285 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800286}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800287
Jerry Yu56fc07f2021-09-01 17:48:49 +0800288/*
289 * Functions for writing key_share extension.
290 */
291#if defined(MBEDTLS_ECDH_C)
Jerry Yu7c522d42021-09-08 17:55:09 +0800292static int ssl_tls13_generate_and_write_ecdh_key_exchange(
Jerry Yub60e3cf2021-09-08 16:41:02 +0800293 mbedtls_ssl_context *ssl,
294 uint16_t named_group,
295 unsigned char *buf,
296 unsigned char *end,
297 size_t *olen )
Jerry Yu92c6b402021-08-27 16:59:09 +0800298{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800300 const mbedtls_ecp_curve_info *curve_info =
301 mbedtls_ecp_curve_info_from_tls_id( named_group );
302
303 if( curve_info == NULL )
304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
305
306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
307
Jerry Yudd1fb9e2021-09-15 11:10:15 +0800308 if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
309 curve_info->grp_id ) ) != 0 )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800310 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800312 return( ret );
313 }
314
315 ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, olen,
316 buf, end - buf,
317 ssl->conf->f_rng, ssl->conf->p_rng );
318 if( ret != 0 )
319 {
320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
321 return( ret );
322 }
323
324 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
325 MBEDTLS_DEBUG_ECDH_Q );
Jerry Yu75336352021-09-01 15:59:36 +0800326 return( 0 );
Jerry Yu92c6b402021-08-27 16:59:09 +0800327}
Jerry Yu56fc07f2021-09-01 17:48:49 +0800328#endif /* MBEDTLS_ECDH_C */
329
Jerry Yub60e3cf2021-09-08 16:41:02 +0800330static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
331 uint16_t *group_id )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800332{
333 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
334
Jerry Yu56fc07f2021-09-01 17:48:49 +0800335
Jerry Yu56fc07f2021-09-01 17:48:49 +0800336#if defined(MBEDTLS_ECDH_C)
Jerry Yu388bd0d2021-09-15 18:41:02 +0800337 /* Pick first available ECDHE group compatible with TLS 1.3 */
338 if( ssl->conf->curve_list == NULL )
339 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
340
341 for ( const mbedtls_ecp_group_id *grp_id = ssl->conf->curve_list;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800342 *grp_id != MBEDTLS_ECP_DP_NONE;
343 grp_id++ )
344 {
345 const mbedtls_ecp_curve_info *info;
346 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
Jerry Yu388bd0d2021-09-15 18:41:02 +0800347 if( info != NULL &&
348 mbedtls_ssl_tls13_named_group_is_ecdhe( info->tls_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800349 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800350 *group_id = info->tls_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800351 return( 0 );
352 }
353 }
354#else
355 ((void) ssl);
Jerry Yub60e3cf2021-09-08 16:41:02 +0800356 ((void) group_id);
Jerry Yu56fc07f2021-09-01 17:48:49 +0800357#endif /* MBEDTLS_ECDH_C */
358
359 /*
360 * Add DHE named groups here.
Jerry Yu388bd0d2021-09-15 18:41:02 +0800361 * Pick first available DHE group compatible with TLS 1.3
Jerry Yu56fc07f2021-09-01 17:48:49 +0800362 */
363
364 return( ret );
365}
366
367/*
368 * ssl_tls13_write_key_share_ext
369 *
Jerry Yu388bd0d2021-09-15 18:41:02 +0800370 * Structure of key_share extension in ClientHello:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800371 *
372 * struct {
373 * NamedGroup group;
374 * opaque key_exchange<1..2^16-1>;
375 * } KeyShareEntry;
376 * struct {
377 * KeyShareEntry client_shares<0..2^16-1>;
378 * } KeyShareClientHello;
379 */
380static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
381 unsigned char *buf,
382 unsigned char *end,
383 size_t *olen )
384{
385 unsigned char *p = buf;
386 unsigned char *client_shares_ptr; /* Start of client_shares */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800387 size_t client_shares_len; /* Length of client_shares */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800388 uint16_t group_id;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800389 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
390
391 *olen = 0;
392
393 if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
394 return( 0 );
395
Jerry Yub60e3cf2021-09-08 16:41:02 +0800396 /* Check if we have space for header and length fields:
Jerry Yu56fc07f2021-09-01 17:48:49 +0800397 * - extension_type (2 bytes)
398 * - extension_data_length (2 bytes)
399 * - client_shares_length (2 bytes)
400 */
401 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
402 p += 6;
403
404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
405
406 /* HRR could already have requested something else. */
407 group_id = ssl->handshake->offered_group_id;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800408 if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
409 !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800410 {
Jerry Yub60e3cf2021-09-08 16:41:02 +0800411 MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
Jerry Yu56fc07f2021-09-01 17:48:49 +0800412 &group_id ) );
413 }
414
415 /*
416 * Dispatch to type-specific key generation function.
417 *
418 * So far, we're only supporting ECDHE. With the introduction
419 * of PQC KEMs, we'll want to have multiple branches, one per
420 * type of KEM, and dispatch to the corresponding crypto. And
421 * only one key share entry is allowed.
422 */
423 client_shares_ptr = p;
424#if defined(MBEDTLS_ECDH_C)
Jerry Yub60e3cf2021-09-08 16:41:02 +0800425 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800426 {
Jerry Yu388bd0d2021-09-15 18:41:02 +0800427 /* Pointer to group */
428 unsigned char *group_ptr = p;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800429 /* Length of key_exchange */
430 size_t key_exchange_len;
431
432 /* Check there is space for header of KeyShareEntry
433 * - group (2 bytes)
434 * - key_exchange_length (2 bytes)
435 */
436 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
437 p += 4;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800438 ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
439 p, end,
440 &key_exchange_len );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800441 p += key_exchange_len;
442 if( ret != 0 )
443 return( ret );
444
445 /* Write group */
Jerry Yu388bd0d2021-09-15 18:41:02 +0800446 MBEDTLS_PUT_UINT16_BE( group_id, group_ptr, 0 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800447 /* Write key_exchange_length */
Jerry Yu388bd0d2021-09-15 18:41:02 +0800448 MBEDTLS_PUT_UINT16_BE( key_exchange_len, group_ptr, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800449 }
450 else
451#endif /* MBEDTLS_ECDH_C */
452 if( 0 /* other KEMs? */ )
453 {
454 /* Do something */
455 }
456 else
457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
458
Jerry Yub60e3cf2021-09-08 16:41:02 +0800459 /* Length of client_shares */
460 client_shares_len = p - client_shares_ptr;
461 if( client_shares_len == 0)
462 {
463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7c522d42021-09-08 17:55:09 +0800465 }
Jerry Yu56fc07f2021-09-01 17:48:49 +0800466 /* Write extension_type */
467 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
468 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800469 MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800470 /* Write client_shares_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800471 MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800472
473 /* Update offered_group_id field */
474 ssl->handshake->offered_group_id = group_id;
475
476 /* Output the total length of key_share extension. */
477 *olen = p - buf;
478
479 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *olen );
480
481 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
482
483cleanup:
484
485 return( ret );
486}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800487
Jerry Yue1b9c292021-09-10 10:08:31 +0800488#if defined(MBEDTLS_ECDH_C)
489
490/* TODO: Code for MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED missing */
491static int ssl_tls1_3_check_ecdh_params( const mbedtls_ssl_context *ssl )
492{
493 const mbedtls_ecp_curve_info *curve_info;
494 mbedtls_ecp_group_id grp_id;
495#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
496 grp_id = ssl->handshake->ecdh_ctx.grp.id;
497#else
498 grp_id = ssl->handshake->ecdh_ctx.grp_id;
499#endif
500
501 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
502 if( curve_info == NULL )
503 {
504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
506 }
507
508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
509
510#if defined(MBEDTLS_ECP_C)
511 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
512#else
513 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
514 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
515#endif
516 return( -1 );
517
518 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
519 MBEDTLS_DEBUG_ECDH_QP );
520
521 return( 0 );
522}
523
524/* The ssl_tls1_3_parse_key_share_ext() function is used
525 * by the client to parse a KeyShare extension in
526 * a ServerHello message.
527 *
528 * The server only provides a single KeyShareEntry.
529 */
530static int ssl_tls1_3_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
531 const unsigned char *buf,
532 size_t len )
533{
534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
535
536 ret = mbedtls_ecdh_tls1_3_read_public( &ssl->handshake->ecdh_ctx,
537 buf, len );
538 if( ret != 0 )
539 {
540 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
541 return( ret );
542 }
543
544 if( ssl_tls1_3_check_ecdh_params( ssl ) != 0 )
545 {
546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls1_3_check_ecdh_params() failed!" ) );
547 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
548 }
549
550 return( 0 );
551}
552#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
553
554/*
555 * Parse key_share extension in Server Hello
556 * struct {
557 * KeyShareEntry server_share;
558 * } KeyShareServerHello;
559 * struct {
560 * NamedGroup group;
561 * opaque key_exchange<1..2^16-1>;
562 * } KeyShareEntry;
563 */
564static int ssl_tls1_3_parse_key_share_ext( mbedtls_ssl_context *ssl,
565 const unsigned char *buf,
566 const unsigned char *end )
567{
568 int ret = 0;
569 const unsigned char *p = buf;
570 uint16_t server_share_group, offered_group;
571
572 /* server_share_group (2 bytes) */
Jerry Yude4fb2c2021-09-19 18:05:08 +0800573 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +0800574 server_share_group = MBEDTLS_GET_UINT16_BE( p, 0 );
575 p += 2;
576
577 /* Check that chosen group matches the one we offered. */
578 offered_group = ssl->handshake->offered_group_id;
579 if( offered_group != server_share_group )
580 {
581 MBEDTLS_SSL_DEBUG_MSG( 1,
582 ( "Invalid server key share, our group %u, their group %u",
583 (unsigned) offered_group, (unsigned) server_share_group ) );
584 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
585 }
586
587#if defined(MBEDTLS_ECDH_C)
588 if( mbedtls_ssl_tls13_named_group_is_ecdhe( server_share_group ) )
589 {
590 /* Complete ECDHE key agreement */
591 ret = ssl_tls1_3_read_public_ecdhe_share( ssl, p, end - p );
592 if( ret != 0 )
593 return( ret );
594 }
595#endif /* MBEDTLS_ECDH_C */
596 else if( 0 /* other KEMs? */ )
597 {
598 /* Do something */
599 }
600 else
601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
602
603 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
604 return( ret );
605}
606
Jerry Yubc20bdd2021-08-24 15:59:48 +0800607#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
608
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800609/* Write cipher_suites
Jerry Yu6a643102021-08-31 14:40:36 +0800610 * CipherSuite cipher_suites<2..2^16-2>;
611 */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800612static int ssl_tls13_write_client_hello_cipher_suites(
Jerry Yu6a643102021-08-31 14:40:36 +0800613 mbedtls_ssl_context *ssl,
614 unsigned char *buf,
615 unsigned char *end,
616 size_t *olen )
617{
Jerry Yufec982e2021-09-07 17:26:06 +0800618 unsigned char *p = buf;
Jerry Yu0c63af62021-09-02 12:59:12 +0800619 const int *ciphersuite_list;
Jerry Yubbe09522021-09-06 21:17:54 +0800620 unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800621 size_t cipher_suites_len;
Jerry Yu92c6b402021-08-27 16:59:09 +0800622
Jerry Yu6a643102021-08-31 14:40:36 +0800623 *olen = 0 ;
624
625 /*
626 * Ciphersuite list
627 *
628 * This is a list of the symmetric cipher options supported by
629 * the client, specifically the record protection algorithm
630 * ( including secret key length ) and a hash to be used with
631 * HKDF, in descending order of client preference.
632 */
Jerry Yu0c63af62021-09-02 12:59:12 +0800633 ciphersuite_list = ssl->conf->ciphersuite_list;
Jerry Yu6a643102021-08-31 14:40:36 +0800634
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800635 /* Check there is space for the cipher suite list length (2 bytes). */
Jerry Yu4e388282021-09-06 21:28:08 +0800636 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
637 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800638
Jerry Yu0c63af62021-09-02 12:59:12 +0800639 /* Write cipher_suites */
Jerry Yu4e388282021-09-06 21:28:08 +0800640 cipher_suites_ptr = p;
Jerry Yu0c63af62021-09-02 12:59:12 +0800641 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
Jerry Yu6a643102021-08-31 14:40:36 +0800642 {
Jerry Yu0c63af62021-09-02 12:59:12 +0800643 int cipher_suite = ciphersuite_list[i];
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800644 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu6a643102021-08-31 14:40:36 +0800645
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800646 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
Jerry Yu6a643102021-08-31 14:40:36 +0800647 if( ciphersuite_info == NULL )
648 continue;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800649 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
650 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
Jerry Yu6a643102021-08-31 14:40:36 +0800651 continue;
652
653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800654 (unsigned int) cipher_suite,
Jerry Yu6a643102021-08-31 14:40:36 +0800655 ciphersuite_info->name ) );
656
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800657 /* Check there is space for the cipher suite identifier (2 bytes). */
Jerry Yubbe09522021-09-06 21:17:54 +0800658 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
659 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
660 p += 2;
Jerry Yu6a643102021-08-31 14:40:36 +0800661 }
662
Jerry Yu0c63af62021-09-02 12:59:12 +0800663 /* Write the cipher_suites length in number of bytes */
Jerry Yubbe09522021-09-06 21:17:54 +0800664 cipher_suites_len = p - cipher_suites_ptr;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800665 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
Jerry Yu6a643102021-08-31 14:40:36 +0800666 MBEDTLS_SSL_DEBUG_MSG( 3,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800667 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
668 cipher_suites_len/2 ) );
Jerry Yu6a643102021-08-31 14:40:36 +0800669
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800670 /* Output the total length of cipher_suites field. */
Jerry Yubbe09522021-09-06 21:17:54 +0800671 *olen = p - buf;
Jerry Yuf171e832021-08-31 18:31:09 +0800672
Jerry Yu6a643102021-08-31 14:40:36 +0800673 return( 0 );
674}
675
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800676/*
677 * Structure of ClientHello message:
678 *
679 * struct {
680 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
681 * Random random;
682 * opaque legacy_session_id<0..32>;
683 * CipherSuite cipher_suites<2..2^16-2>;
684 * opaque legacy_compression_methods<1..2^8-1>;
685 * Extension extensions<8..2^16-1>;
686 * } ClientHello;
687 */
Jerry Yu08906d02021-08-31 11:05:27 +0800688static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800689 unsigned char *buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800690 unsigned char *end,
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800691 size_t *olen )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800692{
Jerry Yubc20bdd2021-08-24 15:59:48 +0800693
Jerry Yubc20bdd2021-08-24 15:59:48 +0800694 int ret;
Jerry Yu8c02bb42021-09-03 21:09:22 +0800695 unsigned char *extensions_len_ptr; /* Pointer to extensions length */
Jerry Yu790656a2021-09-01 15:51:48 +0800696 size_t output_len; /* Length of buffer used by function */
697 size_t extensions_len; /* Length of the list of extensions*/
Jerry Yubc20bdd2021-08-24 15:59:48 +0800698
Jerry Yubc20bdd2021-08-24 15:59:48 +0800699 /* Buffer management */
Jerry Yubbe09522021-09-06 21:17:54 +0800700 unsigned char *p = buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800701
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800702 *olen = 0;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800703
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800704 /* No validation needed here. It has been done by ssl_conf_check() */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800705 ssl->major_ver = ssl->conf->min_major_ver;
706 ssl->minor_ver = ssl->conf->min_minor_ver;
707
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800708 /*
709 * Write legacy_version
Jerry Yu6a643102021-08-31 14:40:36 +0800710 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800711 *
712 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
Jerry Yubc20bdd2021-08-24 15:59:48 +0800713 * instead of the true version number.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800714 */
Jerry Yufec982e2021-09-07 17:26:06 +0800715 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yubbe09522021-09-06 21:17:54 +0800716 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
Jerry Yufec982e2021-09-07 17:26:06 +0800717 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800718
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800719 /* Write the random bytes ( random ).*/
Jerry Yubbe09522021-09-06 21:17:54 +0800720 MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_RANDOM_LEN );
721 memcpy( p, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN );
Jerry Yue885b762021-08-26 17:32:34 +0800722 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
Jerry Yubbe09522021-09-06 21:17:54 +0800723 p, CLIENT_HELLO_RANDOM_LEN );
724 p += CLIENT_HELLO_RANDOM_LEN;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800725
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800726 /*
727 * Write legacy_session_id
728 *
729 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
730 * which has been merged with pre-shared keys in this version. A client
731 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
732 * this field to that value. In compatibility mode, this field MUST be
733 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
734 * a new 32-byte value. This value need not be random but SHOULD be
735 * unpredictable to avoid implementations fixating on a specific value
736 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
737 * vector ( i.e., a zero-valued single byte length field ).
Jerry Yubc20bdd2021-08-24 15:59:48 +0800738 */
Jerry Yubbe09522021-09-06 21:17:54 +0800739 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
740 *p++ = 0; /* session id length set to zero */
Jerry Yubc20bdd2021-08-24 15:59:48 +0800741
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800742 /* Write cipher_suites */
Jerry Yubbe09522021-09-06 21:17:54 +0800743 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800744 if( ret != 0 )
Jerry Yu6a643102021-08-31 14:40:36 +0800745 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800746 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800747
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800748 /* Write legacy_compression_methods
749 *
750 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
Jerry Yubc20bdd2021-08-24 15:59:48 +0800751 * one byte set to zero, which corresponds to the 'null' compression
752 * method in prior versions of TLS.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800753 */
Jerry Yubbe09522021-09-06 21:17:54 +0800754 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
755 *p++ = 1;
756 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800757
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800758 /* Write extensions */
759
760 /* Keeping track of the included extensions */
761 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800762
763 /* First write extensions, then the total length */
Jerry Yubbe09522021-09-06 21:17:54 +0800764 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
765 extensions_len_ptr = p;
766 p += 2;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800767
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800768 /* Write supported_versions extension
Jerry Yubc20bdd2021-08-24 15:59:48 +0800769 *
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800770 * Supported Versions Extension is mandatory with TLS 1.3.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800771 */
Jerry Yubbe09522021-09-06 21:17:54 +0800772 ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800773 if( ret != 0 )
774 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800775 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800776
777#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800778 /* Write supported_groups extension
779 *
780 * It is REQUIRED for ECDHE cipher_suites.
Jerry Yubc20bdd2021-08-24 15:59:48 +0800781 */
Jerry Yubbe09522021-09-06 21:17:54 +0800782 ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800783 if( ret != 0 )
784 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800785 p += output_len;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800786
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800787 /* Write key_share extension
788 *
789 * We need to send the key shares under three conditions:
Jerry Yu159c5a02021-08-31 12:51:25 +0800790 * 1) A certificate-based ciphersuite is being offered. In this case
791 * supported_groups and supported_signature extensions have been
792 * successfully added.
793 * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
Jerry Yubc20bdd2021-08-24 15:59:48 +0800794 * psk_key_exchange_modes has been added as the last extension.
Jerry Yu159c5a02021-08-31 12:51:25 +0800795 * 3) Or, in case all ciphers are supported ( which includes #1 and #2
796 * from above )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800797 */
Jerry Yu56fc07f2021-09-01 17:48:49 +0800798 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800799 if( ret != 0 )
800 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800801 p += output_len;
Jerry Yu6a643102021-08-31 14:40:36 +0800802
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800803 /* Write signature_algorithms extension
804 *
805 * It is REQUIRED for certificate authenticated cipher_suites.
806 */
Jerry Yubbe09522021-09-06 21:17:54 +0800807 ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800808 if( ret != 0 )
809 return( ret );
Jerry Yubbe09522021-09-06 21:17:54 +0800810 p += output_len;
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800811
Jerry Yubc20bdd2021-08-24 15:59:48 +0800812#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
813
814 /* Add more extensions here */
815
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800816 /* Write the length of the list of extensions. */
Jerry Yubbe09522021-09-06 21:17:54 +0800817 extensions_len = p - extensions_len_ptr - 2;
Jerry Yu790656a2021-09-01 15:51:48 +0800818 MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
Jerry Yu790656a2021-09-01 15:51:48 +0800820 extensions_len ) );
821 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extensions_len_ptr, extensions_len );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800822
Jerry Yubbe09522021-09-06 21:17:54 +0800823 *olen = p - buf;
Jerry Yubc20bdd2021-08-24 15:59:48 +0800824 return( 0 );
825}
826
Jerry Yu335aca92021-09-12 20:18:56 +0800827static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800828{
Jerry Yu92c6b402021-08-27 16:59:09 +0800829 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
830 return( 0 );
831}
Jerry Yuef6b36b2021-08-24 16:29:02 +0800832
Jerry Yu92c6b402021-08-27 16:59:09 +0800833static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
834{
835 int ret;
Jerry Yuef6b36b2021-08-24 16:29:02 +0800836
Jerry Yu92c6b402021-08-27 16:59:09 +0800837 if( ssl->conf->f_rng == NULL )
838 {
839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
840 return( MBEDTLS_ERR_SSL_NO_RNG );
841 }
Jerry Yuef6b36b2021-08-24 16:29:02 +0800842
Jerry Yu92c6b402021-08-27 16:59:09 +0800843 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
844 ssl->handshake->randbytes,
Jerry Yu08906d02021-08-31 11:05:27 +0800845 CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
Jerry Yu92c6b402021-08-27 16:59:09 +0800846 {
Jerry Yu8c02bb42021-09-03 21:09:22 +0800847 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Jerry Yu92c6b402021-08-27 16:59:09 +0800848 return( ret );
849 }
Jerry Yu6f13f642021-08-26 17:18:15 +0800850
851 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800852}
853
Jerry Yu92c6b402021-08-27 16:59:09 +0800854/*
Jerry Yu159c5a02021-08-31 12:51:25 +0800855 * Write ClientHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +0800856 * Handler for MBEDTLS_SSL_CLIENT_HELLO
Jerry Yu92c6b402021-08-27 16:59:09 +0800857 */
858static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800859{
Jerry Yu92c6b402021-08-27 16:59:09 +0800860 int ret = 0;
861 unsigned char *buf;
862 size_t buf_len, msg_len;
863
864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
865
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800866 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800867
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800868 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
869 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
870 &buf, &buf_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800871
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800872 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
Jerry Yuef387d72021-09-02 13:59:41 +0800873 buf + buf_len,
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800874 &msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800875
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800876 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
877 MBEDTLS_SSL_HS_CLIENT_HELLO,
Jerry Yu0c63af62021-09-02 12:59:12 +0800878 msg_len );
879 ssl->handshake->update_checksum( ssl, buf, msg_len );
Jerry Yu92c6b402021-08-27 16:59:09 +0800880
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800881 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
882 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
883 buf_len,
884 msg_len ) );
Jerry Yu92c6b402021-08-27 16:59:09 +0800885
886cleanup:
887
888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
889 return ret;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800890}
891
Jerry Yu687101b2021-09-14 16:03:56 +0800892/*
Jerry Yue1b9c292021-09-10 10:08:31 +0800893 * Functions for parsing and processing ServerHello
894 */
895static int ssl_server_hello_is_hrr( unsigned const char *buf )
896{
897 const unsigned char magic_hrr_string[32] =
898 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
899 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
900 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
901 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
902
903 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
904 *
905 * ServerHello and HRR are only distinguished by Random set to the
906 * special value of the SHA-256 of "HelloRetryRequest".
907 *
908 * struct {
909 * ProtocolVersion legacy_version = 0x0303;
910 * Random random;
911 * opaque legacy_session_id_echo<0..32>;
912 * CipherSuite cipher_suite;
913 * uint8 legacy_compression_method = 0;
914 * Extension extensions<6..2 ^ 16 - 1>;
915 * } ServerHello;
916 *
917 */
918 if( memcmp( buf + 2, magic_hrr_string,
919 sizeof( magic_hrr_string ) ) == 0 )
920 {
921 return( 1 );
922 }
923
924 return( 0 );
925}
926
927/* Fetch and preprocess
928 * Returns a negative value on failure, and otherwise
929 * - SSL_SERVER_HELLO_COORDINATE_HELLO or
930 * - SSL_SERVER_HELLO_COORDINATE_HRR
931 * to indicate which message is expected and to be parsed next. */
932#define SSL_SERVER_HELLO_COORDINATE_HELLO 0
933#define SSL_SERVER_HELLO_COORDINATE_HRR 1
934static int ssl_server_hello_coordinate( mbedtls_ssl_context *ssl,
935 unsigned char **buf,
936 size_t *buf_len )
937{
938 int ret;
939
940 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
941
942 /* TBD: If we do an HRR, keep track of the number
943 * of ClientHello's we sent, and fail if it
944 * exceeds the configured threshold. */
945
946 if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
947 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
948 {
949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
950
951 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
952 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
953 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
954 }
955
956 *buf = ssl->in_msg + 4;
957 *buf_len = ssl->in_hslen - 4;
958
959 if( ssl_server_hello_is_hrr( ssl->in_msg + 4 ) )
960 {
961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
962 ret = SSL_SERVER_HELLO_COORDINATE_HRR;
963 }
964 else
965 {
966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
967 ret = SSL_SERVER_HELLO_COORDINATE_HELLO;
968 }
969
970cleanup:
971
972 return( ret );
973}
974
975static int ssl_tls1_3_check_server_hello_session_id( mbedtls_ssl_context *ssl,
976 const unsigned char **buf,
977 const unsigned char *end )
978{
979 const unsigned char *p = *buf;
980 size_t recv_id_len;
981
Jerry Yude4fb2c2021-09-19 18:05:08 +0800982 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +0800983 recv_id_len = *p++ ;
984
Jerry Yude4fb2c2021-09-19 18:05:08 +0800985 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, recv_id_len );
Jerry Yue1b9c292021-09-10 10:08:31 +0800986
987 /* legacy_session_id_echo */
988 if( ssl->session_negotiate->id_len != recv_id_len )
989 {
990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Mismatch of session id length:"
991 " id_len = %" MBEDTLS_PRINTF_SIZET
992 " , recv_id_len = %" MBEDTLS_PRINTF_SIZET,
993 ssl->session_negotiate->id_len, recv_id_len ) );
994 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
995 }
996
997 if( memcmp( ssl->session_negotiate->id, p , recv_id_len ) != 0 )
998 {
999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unexpected legacy_session_id_echo" ) );
1000 MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
1001 ssl->session_negotiate->id,
1002 ssl->session_negotiate->id_len );
1003 MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
1004 ssl->session_negotiate->id_len );
1005 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1006 }
1007
1008 p += recv_id_len;
1009 *buf = p;
1010
1011 MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
1012 recv_id_len );
1013 return( 0 );
1014}
1015
1016static int ssl_tls1_3_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
1017 uint16_t cipher_suite )
1018{
1019 /* Check whether we have offered this ciphersuite */
1020 for ( int i = 0; ssl->conf->ciphersuite_list[i] != 0; i++ )
1021 {
1022 if( ssl->conf->ciphersuite_list[i] == cipher_suite )
1023 {
1024 return( 1 );
1025 }
1026 }
1027 return( 0 );
1028}
1029
1030/* Parse ServerHello message and configure context
1031 *
1032 * struct {
1033 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1034 * Random random;
1035 * opaque legacy_session_id_echo<0..32>;
1036 * CipherSuite cipher_suite;
1037 * uint8 legacy_compression_method = 0;
1038 * Extension extensions<6..2 ^ 16 - 1>;
1039 * } ServerHello;
1040 */
1041static int ssl_tls1_3_parse_server_hello( mbedtls_ssl_context *ssl,
1042 const unsigned char *buf,
1043 const unsigned char *end )
1044{
Jerry Yue1b9c292021-09-10 10:08:31 +08001045 int ret;
1046 const unsigned char *p = buf;
1047 size_t field_len; /* Length of field */
1048 const unsigned char *ext_end; /* Pointer to end of individual extension */
1049 uint16_t cipher_suite;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001050 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yue1b9c292021-09-10 10:08:31 +08001051
1052 /*
1053 * Check there is space for minimal fields
1054 *
1055 * - legacy_version ( 2 bytes)
1056 * - random (32 bytes)
1057 * - legacy_session_id_echo ( 1 byte ), minimum size
1058 * - cipher_suite ( 2 bytes)
1059 * - legacy_compression_method ( 1 byte )
1060 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001061 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001062
1063 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
1064
1065 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
1066
1067 /* legacy_version must be 0x0303 (TLS 1.2) */
1068 if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1069 p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
1070 {
1071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
1072 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1073 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1074 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
1075 }
1076 p += 2;
1077 /* Internally we use the correct 1.3 version
1078 * TODO: Remove below lines after supported_versions extension
1079 * finished.
1080 */
1081 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1082 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
1083
1084 /* Store server-provided random values */
1085 memcpy( ssl->handshake->randbytes + CLIENT_HELLO_RANDOM_LEN, p,
1086 SERVER_HELLO_RANDOM_LEN );
1087 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
1088 p, SERVER_HELLO_RANDOM_LEN );
1089 p += SERVER_HELLO_RANDOM_LEN;
1090
1091 /* Read and store session id (legacy_session_id_echo) */
1092 if( ssl_tls1_3_check_server_hello_session_id( ssl, &p, end ) != 0 )
1093 {
1094 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1095 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1096 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1097 }
1098
1099 /* Read server-selected ciphersuite,
1100 Check if there is space for cipher_suite. */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001101 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2);
Jerry Yue1b9c292021-09-10 10:08:31 +08001102 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
1103 p += 2;
1104
1105 /* Configure ciphersuites */
1106 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1107 ssl->handshake->ciphersuite_info = ciphersuite_info;
1108 if( ciphersuite_info == NULL )
1109 {
1110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
1111 cipher_suite ) );
1112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1113
1114 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1115 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1117 }
1118
1119 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
1120
1121 ssl->session_negotiate->ciphersuite = cipher_suite;
1122
1123 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1124
1125 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
1126 cipher_suite, ciphersuite_info->name ) );
1127
1128#if defined(MBEDTLS_HAVE_TIME)
1129 ssl->session_negotiate->start = time( NULL );
1130#endif /* MBEDTLS_HAVE_TIME */
1131
1132 /* Check whether we have offered this ciphersuite */
1133 /* Via the force_ciphersuite version we may have instructed the client */
1134 /* to use a difference ciphersuite. */
1135 if( ssl_tls1_3_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
1136 {
1137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) is not in offered list",
1138 cipher_suite ) );
1139 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1140 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1141 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1142 }
1143
1144 /* Ensure that compression method is set to zero
1145 *
1146 * legacy_compression_method == 0 ( 1 byte)
1147 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001148 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001149 if( p[0] != 0 )
1150 {
1151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1152 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1153 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1154 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
1155 }
1156 p++;
1157
1158 /* Check there is space fore extensions_length */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001159 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001160 /* Get length of extensions field (2 bytes)*/
Jerry Yue1b9c292021-09-10 10:08:31 +08001161 field_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1162 p += 2;
Jerry Yude4fb2c2021-09-19 18:05:08 +08001163
Jerry Yue1b9c292021-09-10 10:08:31 +08001164 /* Check there is space for extensions_data */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001165 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, field_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001166 /* Set end of extensions */
1167 ext_end = p + field_len;
1168
1169 MBEDTLS_SSL_DEBUG_MSG( 3,
1170 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
1171 field_len ) );
1172
1173 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, field_len );
1174
1175 while ( p < ext_end )
1176 {
1177 unsigned int extension_type;
1178 size_t extension_data_len;
1179
1180 /*
1181 * ....
1182 * Extension extensions<6..2 ^ 16 - 1>;
1183 * ....
1184 * struct {
1185 * ExtensionType extension_type;
1186 * opaque extension_data<0..2^16-1>;
1187 * } Extension;
1188 * extension_type (2 bytes)
1189 * extension_data_length (2 bytes)
1190 */
Jerry Yude4fb2c2021-09-19 18:05:08 +08001191 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, ext_end, 4 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001192 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
1193 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
1194 p += 4;
1195
Jerry Yude4fb2c2021-09-19 18:05:08 +08001196 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, ext_end, extension_data_len );
Jerry Yue1b9c292021-09-10 10:08:31 +08001197
1198 switch( extension_type )
1199 {
1200 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1201 MBEDTLS_SSL_DEBUG_MSG( 3,
1202 ( "found supported_versions extension" ) );
1203
1204 ret = ssl_tls1_3_parse_supported_versions_ext( ssl,
1205 p, extension_data_len );
1206 if( ret != 0 )
1207 return( ret );
1208 break;
1209
1210 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
1212 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
1213 break;
1214
1215#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1216 case MBEDTLS_TLS_EXT_KEY_SHARE:
1217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
1218 if( ( ret = ssl_tls1_3_parse_key_share_ext( ssl,
1219 p, p + extension_data_len ) ) != 0 )
1220 {
1221 MBEDTLS_SSL_DEBUG_RET( 1,
1222 "ssl_tls1_3_parse_key_share_ext",
1223 ret );
1224 return( ret );
1225 }
1226 break;
1227#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1228
1229 default:
1230 MBEDTLS_SSL_DEBUG_MSG( 3,
1231 ( "unknown extension found: %u ( ignoring )",
1232 extension_type ) );
1233 }
1234
1235 p += extension_data_len;
1236 }
1237
1238 return( 0 );
1239}
1240
Jerry Yu0b177842021-09-05 19:41:30 +08001241static int ssl_tls1_3_finalize_server_hello( mbedtls_ssl_context *ssl )
Jerry Yue1b9c292021-09-10 10:08:31 +08001242{
Jerry Yu0b177842021-09-05 19:41:30 +08001243 int ret;
1244 mbedtls_ssl_key_set traffic_keys;
1245 mbedtls_ssl_transform *transform_handshake;
1246
1247 /* We need to set the key exchange algorithm based on the
1248 * following rules:
1249 *
Jerry Yufd532e52021-09-19 15:57:53 +08001250 * 1) IF PRE_SHARED_KEY extension was received
1251 * THEN set KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1252 * 2) IF PRE_SHARED_KEY extension && KEY_SHARE was received
1253 * THEN set KEY_EXCHANGE_MODE_PSK;
1254 * 3) IF KEY_SHARES extension was received && SIG_ALG extension received
1255 * THEN set KEY_EXCHANGE_MODE_EPHEMERAL
Jerry Yu0b177842021-09-05 19:41:30 +08001256 * ELSE unknown key exchange mechanism.
1257 */
Jerry Yu0b177842021-09-05 19:41:30 +08001258 if( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY )
1259 {
1260 if( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE )
Jerry Yufd532e52021-09-19 15:57:53 +08001261 {
1262 /* Condition 2) */
1263 ssl->handshake->tls1_3_kex_modes =
1264 MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1265 }
Jerry Yu0b177842021-09-05 19:41:30 +08001266 else
Jerry Yufd532e52021-09-19 15:57:53 +08001267 {
1268 /* Condition 1) */
1269 ssl->handshake->tls1_3_kex_modes =
1270 MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK;
1271 }
Jerry Yu0b177842021-09-05 19:41:30 +08001272 }
Jerry Yufd532e52021-09-19 15:57:53 +08001273 else if( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) )
1274 {
1275 /* Condition 3) */
1276 ssl->handshake->tls1_3_kex_modes =
1277 MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL;
1278 }
Jerry Yu0b177842021-09-05 19:41:30 +08001279 else
1280 {
Jerry Yufd532e52021-09-19 15:57:53 +08001281 /* ELSE case */
Jerry Yu0b177842021-09-05 19:41:30 +08001282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
1283 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1284 }
1285
1286 /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
1287 *
1288 * TODO: We don't have to do this in case we offered 0-RTT and the
1289 * server accepted it. In this case, we could skip generating
1290 * the early secret. */
1291 ret = mbedtls_ssl_tls1_3_key_schedule_stage_early( ssl );
1292 if( ret != 0 )
1293 {
1294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_key_schedule_stage_early_data",
1295 ret );
1296 return( ret );
1297 }
1298
1299 /* Compute handshake secret */
1300 ret = mbedtls_ssl_tls1_3_key_schedule_stage_handshake( ssl );
1301 if( ret != 0 )
1302 {
1303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_derive_master_secret", ret );
1304 return( ret );
1305 }
1306
1307 /* Next evolution in key schedule: Establish handshake secret and
1308 * key material. */
1309 ret = mbedtls_ssl_tls1_3_generate_handshake_keys( ssl, &traffic_keys );
1310 if( ret != 0 )
1311 {
1312 MBEDTLS_SSL_DEBUG_RET( 1,
1313 "mbedtls_ssl_tls1_3_generate_handshake_keys", ret );
1314 return( ret );
1315 }
1316
1317 transform_handshake =
1318 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1319 if( transform_handshake == NULL )
1320 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1321
1322 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1323 ssl->conf->endpoint,
1324 ssl->session_negotiate->ciphersuite,
1325 &traffic_keys,
1326 ssl );
1327 if( ret != 0 )
1328 {
1329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1330 return( ret );
1331 }
1332
1333 ssl->handshake->transform_handshake = transform_handshake;
1334 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
1335
1336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1337 ssl->session_in = ssl->session_negotiate;
1338
1339 /*
1340 * State machine update
1341 */
1342 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1343
1344 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1345 return( 0 );
Jerry Yue1b9c292021-09-10 10:08:31 +08001346}
1347
1348/*
1349 * Wait and Parse ServerHello handshake message.
Jerry Yu687101b2021-09-14 16:03:56 +08001350 * Handler for MBEDTLS_SSL_SERVER_HELLO
1351 */
Jerry Yu860b4ee2021-09-27 13:16:13 +08001352static int ssl_tls1_3_process_server_hello( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001353{
Jerry Yue1b9c292021-09-10 10:08:31 +08001354 int ret = 0;
1355 unsigned char *buf;
1356 size_t buf_len;
1357
1358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
1359
1360 /* Coordination step
1361 * - Fetch record
1362 * - Make sure it's either a ServerHello or a HRR.
1363 * - Switch processing routine in case of HRR
1364 */
1365
1366 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1367 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
1368
Jerry Yue1b9c292021-09-10 10:08:31 +08001369 ret = ssl_server_hello_coordinate( ssl, &buf, &buf_len );
1370 /* Parsing step
1371 * We know what message to expect by now and call
1372 * the respective parsing function.
1373 */
1374 if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
1375 {
1376 MBEDTLS_SSL_PROC_CHK( ssl_tls1_3_parse_server_hello( ssl, buf,
1377 buf + buf_len ) );
1378
1379 mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl,
1380 MBEDTLS_SSL_HS_SERVER_HELLO,
1381 buf, buf_len );
1382
Jerry Yu0b177842021-09-05 19:41:30 +08001383 MBEDTLS_SSL_PROC_CHK( ssl_tls1_3_finalize_server_hello( ssl ) );
Jerry Yue1b9c292021-09-10 10:08:31 +08001384 }
1385 else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
1386 {
1387 /* TODO: Implement HRR in future #4915 */
1388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR hasn't been implemented" ) );
1389
1390 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1391 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1392 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1393 }
1394
1395cleanup:
1396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
1397 return( ret );
Jerry Yu687101b2021-09-14 16:03:56 +08001398}
1399
1400/*
1401 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1402 */
Jerry Yu860b4ee2021-09-27 13:16:13 +08001403static int ssl_tls1_3_process_encrypted_extensions( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001404{
1405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
1406 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
1407 return( 0 );
1408}
1409
1410/*
1411 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
1412 */
Jerry Yu860b4ee2021-09-27 13:16:13 +08001413static int ssl_tls1_3_process_certificate_request( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001414{
1415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
1416 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1417 return( 0 );
1418}
1419
1420/*
1421 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
1422 */
Jerry Yu860b4ee2021-09-27 13:16:13 +08001423static int ssl_tls1_3_process_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001424{
1425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
1426 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1427 return( 0 );
1428}
1429
1430/*
1431 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
1432 */
Jerry Yu860b4ee2021-09-27 13:16:13 +08001433static int ssl_tls1_3_process_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001434{
1435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
1436 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1437 return( 0 );
1438}
1439
1440/*
1441 * Handler for MBEDTLS_SSL_SERVER_FINISHED
1442 */
Jerry Yu860b4ee2021-09-27 13:16:13 +08001443static int ssl_tls1_3_process_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu687101b2021-09-14 16:03:56 +08001444{
Jerry Yu687101b2021-09-14 16:03:56 +08001445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
Jerry Yu435756f2021-09-24 13:44:29 +08001446 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yu687101b2021-09-14 16:03:56 +08001447 return( 0 );
1448}
1449
1450/*
1451 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
1452 */
1453static int ssl_tls1_3_write_client_certificate( mbedtls_ssl_context *ssl )
1454{
Jerry Yu687101b2021-09-14 16:03:56 +08001455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001456 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
Jerry Yu687101b2021-09-14 16:03:56 +08001457 return( 0 );
1458}
1459
1460/*
1461 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
1462 */
1463static int ssl_tls1_3_write_client_certificate_verify( mbedtls_ssl_context *ssl )
1464{
Jerry Yu687101b2021-09-14 16:03:56 +08001465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001466 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Jerry Yu687101b2021-09-14 16:03:56 +08001467 return( 0 );
1468}
1469
1470/*
1471 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
1472 */
1473static int ssl_tls1_3_write_client_finished( mbedtls_ssl_context *ssl )
1474{
Jerry Yu687101b2021-09-14 16:03:56 +08001475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001476 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
Jerry Yu687101b2021-09-14 16:03:56 +08001477 return( 0 );
1478}
1479
1480/*
1481 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
1482 */
1483static int ssl_tls1_3_flush_buffers( mbedtls_ssl_context *ssl )
1484{
Jerry Yu687101b2021-09-14 16:03:56 +08001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
Jerry Yuad8d0ba2021-09-28 17:58:26 +08001486 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yu687101b2021-09-14 16:03:56 +08001487 return( 0 );
1488}
1489
1490/*
1491 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
1492 */
1493static int ssl_tls1_3_handshake_wrapup( mbedtls_ssl_context *ssl )
1494{
1495 ((void) ssl);
1496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "%s hasn't been implemented", __func__ ) );
1497 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1498}
1499
Jerry Yu92c6b402021-08-27 16:59:09 +08001500int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
Jerry Yubc20bdd2021-08-24 15:59:48 +08001501{
Jerry Yu92c6b402021-08-27 16:59:09 +08001502 int ret = 0;
Jerry Yuc8a392c2021-08-18 16:46:28 +08001503
Jerry Yu6e81b272021-09-27 11:16:17 +08001504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls1_3 client state: %d", ssl->state ) );
Jerry Yu92c6b402021-08-27 16:59:09 +08001505
1506 switch( ssl->state )
1507 {
1508 /*
Jerry Yu0c63af62021-09-02 12:59:12 +08001509 * ssl->state is initialized as HELLO_REQUEST. It is the same
1510 * as CLIENT_HELLO state.
Jerry Yu92c6b402021-08-27 16:59:09 +08001511 */
1512 case MBEDTLS_SSL_HELLO_REQUEST:
1513 case MBEDTLS_SSL_CLIENT_HELLO:
1514 ret = ssl_tls13_write_client_hello( ssl );
1515 break;
1516
1517 case MBEDTLS_SSL_SERVER_HELLO:
Jerry Yu860b4ee2021-09-27 13:16:13 +08001518 ret = ssl_tls1_3_process_server_hello( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001519 break;
1520
1521 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu860b4ee2021-09-27 13:16:13 +08001522 ret = ssl_tls1_3_process_encrypted_extensions( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001523 break;
1524
1525 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Jerry Yu860b4ee2021-09-27 13:16:13 +08001526 ret = ssl_tls1_3_process_certificate_request( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001527 break;
1528
1529 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Jerry Yu860b4ee2021-09-27 13:16:13 +08001530 ret = ssl_tls1_3_process_server_certificate( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001531 break;
1532
1533 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Jerry Yu860b4ee2021-09-27 13:16:13 +08001534 ret = ssl_tls1_3_process_certificate_verify( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001535 break;
1536
1537 case MBEDTLS_SSL_SERVER_FINISHED:
Jerry Yu860b4ee2021-09-27 13:16:13 +08001538 ret = ssl_tls1_3_process_server_finished( ssl );
Jerry Yu687101b2021-09-14 16:03:56 +08001539 break;
1540
1541 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1542 ret = ssl_tls1_3_write_client_certificate( ssl );
1543 break;
1544
1545 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1546 ret = ssl_tls1_3_write_client_certificate_verify( ssl );
1547 break;
1548
1549 case MBEDTLS_SSL_CLIENT_FINISHED:
1550 ret = ssl_tls1_3_write_client_finished( ssl );
1551 break;
1552
1553 case MBEDTLS_SSL_FLUSH_BUFFERS:
1554 ret = ssl_tls1_3_flush_buffers( ssl );
1555 break;
1556
1557 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
1558 ret = ssl_tls1_3_handshake_wrapup( ssl );
Jerry Yu92c6b402021-08-27 16:59:09 +08001559 break;
1560
1561 default:
1562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564 }
1565
1566 return( ret );
1567}
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001568
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001569#endif /* MBEDTLS_SSL_CLI_C */
1570
1571#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */