blob: a8aed63a26b24032cce3d4ae991fc60d407701aa [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
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
20#include "common.h"
21
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080023
Jerry Yu687101b2021-09-14 16:03:56 +080024#include "mbedtls/debug.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000025#include "mbedtls/error.h"
26#include "mbedtls/platform.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080027
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000028#include "ssl_misc.h"
29#include "ssl_tls13_keys.h"
30#include "ssl_debug_helpers.h"
31
XiaokangQian7807f9f2022-02-15 10:04:37 +000032#if defined(MBEDTLS_ECP_C)
33#include "mbedtls/ecp.h"
XiaokangQian7807f9f2022-02-15 10:04:37 +000034#endif /* MBEDTLS_ECP_C */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080035
XiaokangQiana9c58412022-02-17 09:41:26 +000036#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#else
39#include <stdlib.h>
40#define mbedtls_calloc calloc
41#define mbedtls_free free
42#endif /* MBEDTLS_PLATFORM_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +000043
Jerry Yu4d3841a2022-04-16 12:37:19 +080044#include "ssl_misc.h"
45#include "ssl_tls13_keys.h"
46#include "ssl_debug_helpers.h"
47
XiaokangQian7807f9f2022-02-15 10:04:37 +000048/* From RFC 8446:
49 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +000050 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +000051 * } SupportedVersions;
52 */
53static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
54 const unsigned char *buf,
55 const unsigned char *end )
56{
XiaokangQian7807f9f2022-02-15 10:04:37 +000057 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +000058 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +000059 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +000060 uint16_t tls_version;
XiaokangQian8f9dfe42022-04-15 02:52:39 +000061 int tls13_supported = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +000062
63 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
XiaokangQian4080a7f2022-04-11 09:55:18 +000064 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +000065 p += 1;
66
XiaokangQian4080a7f2022-04-11 09:55:18 +000067 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, versions_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +000068 versions_end = p + versions_len;
69 while( p < versions_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +000070 {
XiaokangQiancfd925f2022-04-14 07:10:37 +000071 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, versions_end, 2 );
XiaokangQiande333912022-04-20 08:49:42 +000072 tls_version = mbedtls_ssl_read_version( p, ssl->conf->transport );
XiaokangQianb67384d2022-04-19 00:02:38 +000073 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +000074
75 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
XiaokangQiande333912022-04-20 08:49:42 +000076 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
XiaokangQian7807f9f2022-02-15 10:04:37 +000077 {
78 tls13_supported = 1;
79 break;
80 }
XiaokangQian7807f9f2022-02-15 10:04:37 +000081 }
82
XiaokangQianb67384d2022-04-19 00:02:38 +000083 if( !tls13_supported )
XiaokangQian7807f9f2022-02-15 10:04:37 +000084 {
XiaokangQian7807f9f2022-02-15 10:04:37 +000085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) );
86
87 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
88 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
89 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
90 }
91
XiaokangQiande333912022-04-20 08:49:42 +000092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%04x]",
XiaokangQian0a1b54e2022-04-21 03:01:38 +000093 (unsigned int)tls_version ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +000094
XiaokangQian7807f9f2022-02-15 10:04:37 +000095 return( 0 );
96}
97
XiaokangQian8f9dfe42022-04-15 02:52:39 +000098#if defined(MBEDTLS_ECDH_C)
XiaokangQiane8ff3502022-04-22 02:34:40 +000099/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000100 *
101 * From RFC 8446:
102 * enum {
103 * ... (0xFFFF)
104 * } NamedGroup;
105 * struct {
106 * NamedGroup named_group_list<2..2^16-1>;
107 * } NamedGroupList;
108 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800109static int ssl_tls13_parse_supported_groups_ext( mbedtls_ssl_context *ssl,
110 const unsigned char *buf,
111 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000112{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000113 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000114 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000115 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000116
117 MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000118 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000119 named_group_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000120 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000121 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, named_group_list_len );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000122 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000123 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000124
XiaokangQian08037552022-04-20 07:16:41 +0000125 while( p < named_group_list_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000126 {
XiaokangQian08037552022-04-20 07:16:41 +0000127 uint16_t named_group;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000128 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, named_group_list_end, 2 );
XiaokangQian08037552022-04-20 07:16:41 +0000129 named_group = MBEDTLS_GET_UINT16_BE( p, 0 );
130 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000131
Jerry Yuc1be19f2022-04-23 16:11:39 +0800132 MBEDTLS_SSL_DEBUG_MSG( 2,
133 ( "got named group: %s(%04x)",
134 mbedtls_ssl_named_group_to_str( named_group ),
135 named_group ) );
XiaokangQian08037552022-04-20 07:16:41 +0000136
137 if( ! mbedtls_ssl_named_group_is_offered( ssl, named_group ) ||
138 ! mbedtls_ssl_named_group_is_supported( named_group ) ||
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000139 ssl->handshake->hrr_selected_group != 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000140 {
XiaokangQian08037552022-04-20 07:16:41 +0000141 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000142 }
143
Jerry Yuc1be19f2022-04-23 16:11:39 +0800144 MBEDTLS_SSL_DEBUG_MSG( 2,
145 ( "add named group %s(%04x) into received list.",
146 mbedtls_ssl_named_group_to_str( named_group ),
147 named_group ) );
148
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000149 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000150 }
151
152 return( 0 );
153
154}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000155#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000156
XiaokangQian08037552022-04-20 07:16:41 +0000157#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
158
XiaokangQian88408882022-04-02 10:15:03 +0000159#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000160/*
161 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000162 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000163 *
164 * Possible return values are:
165 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000166 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000167 * does not match a group supported by the server. A HelloRetryRequest will
168 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000169 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800170 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000171static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl,
172 const unsigned char *buf,
173 const unsigned char *end )
174{
XiaokangQianb67384d2022-04-19 00:02:38 +0000175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000176 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000177 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800178 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000179
180 /* From RFC 8446:
181 *
182 * struct {
183 * KeyShareEntry client_shares<0..2^16-1>;
184 * } KeyShareClientHello;
185 *
186 */
187
XiaokangQian7807f9f2022-02-15 10:04:37 +0000188 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000189 client_shares_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000190 p += 2;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000191 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, client_shares_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000192
193 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000194 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000195
196 /* We try to find a suitable key share entry and copy it to the
197 * handshake context. Later, we have to find out whether we can do
198 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000199 * dismiss it and send a HelloRetryRequest message.
200 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000201
Jerry Yuc1be19f2022-04-23 16:11:39 +0800202 while( p < client_shares_end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000203 {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000204 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800205 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800206 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000207
208 /*
209 * struct {
210 * NamedGroup group;
211 * opaque key_exchange<1..2^16-1>;
212 * } KeyShareEntry;
213 */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000214 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, 4 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000215 group = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yuc1be19f2022-04-23 16:11:39 +0800216 key_exchange_len = MBEDTLS_GET_UINT16_BE( p, 2 );
217 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800218 key_exchange = p;
XiaokangQianb67384d2022-04-19 00:02:38 +0000219 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, client_shares_end, key_exchange_len );
Jerry Yu086edc22022-05-05 10:50:38 +0800220 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000221
222 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000223 * for input validation purposes.
224 */
XiaokangQian060d8672022-04-21 09:24:56 +0000225 if( ! mbedtls_ssl_named_group_is_offered( ssl, group ) ||
Jerry Yuc1be19f2022-04-23 16:11:39 +0800226 ! mbedtls_ssl_named_group_is_supported( group ) ||
227 ssl->handshake->offered_group_id != 0 )
XiaokangQian060d8672022-04-21 09:24:56 +0000228 {
229 continue;
230 }
XiaokangQian060d8672022-04-21 09:24:56 +0000231
XiaokangQian7807f9f2022-02-15 10:04:37 +0000232 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000233 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000234 */
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000235 if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
236 {
Jerry Yuc1be19f2022-04-23 16:11:39 +0800237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH group: %s (%04x)",
238 mbedtls_ssl_named_group_to_str( group ),
239 group ) );
XiaokangQian318dc762022-04-20 09:43:51 +0000240 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Jerry Yu086edc22022-05-05 10:50:38 +0800241 ssl, key_exchange - 2, key_exchange_len + 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000242 if( ret != 0 )
243 return( ret );
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000244
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000245 }
246 else
XiaokangQian7807f9f2022-02-15 10:04:37 +0000247 {
248 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u",
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000249 (unsigned) group ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000250 continue;
251 }
252
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000253 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000254 }
255
Jerry Yuc1be19f2022-04-23 16:11:39 +0800256
257 if( ssl->handshake->offered_group_id == 0 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000258 {
259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) );
XiaokangQian08037552022-04-20 07:16:41 +0000260 return( SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000261 }
262 return( 0 );
263}
XiaokangQian88408882022-04-02 10:15:03 +0000264#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000265
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000266#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000267static void ssl_tls13_debug_print_client_hello_exts( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000268{
XiaokangQian3207a322022-02-23 03:15:27 +0000269 ((void) ssl);
270
XiaokangQian7807f9f2022-02-15 10:04:37 +0000271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) );
XiaokangQianb67384d2022-04-19 00:02:38 +0000272 MBEDTLS_SSL_DEBUG_MSG( 3,
273 ( "- KEY_SHARE_EXTENSION ( %s )",
274 ( ( ssl->handshake->extensions_present
275 & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ? "TRUE" : "FALSE" ) );
276 MBEDTLS_SSL_DEBUG_MSG( 3,
277 ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )",
278 ( ( ssl->handshake->extensions_present
279 & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ?
280 "TRUE" : "FALSE" ) );
281 MBEDTLS_SSL_DEBUG_MSG( 3,
282 ( "- PRE_SHARED_KEY_EXTENSION ( %s )",
283 ( ( ssl->handshake->extensions_present
284 & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ? "TRUE" : "FALSE" ) );
285 MBEDTLS_SSL_DEBUG_MSG( 3,
286 ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )",
287 ( ( ssl->handshake->extensions_present
288 & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ? "TRUE" : "FALSE" ) );
289 MBEDTLS_SSL_DEBUG_MSG( 3,
290 ( "- SUPPORTED_GROUPS_EXTENSION ( %s )",
291 ( ( ssl->handshake->extensions_present
292 & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ?
293 "TRUE" : "FALSE" ) );
294 MBEDTLS_SSL_DEBUG_MSG( 3,
295 ( "- SUPPORTED_VERSION_EXTENSION ( %s )",
296 ( ( ssl->handshake->extensions_present
297 & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ?
298 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000299#if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION )
XiaokangQianb67384d2022-04-19 00:02:38 +0000300 MBEDTLS_SSL_DEBUG_MSG( 3,
301 ( "- SERVERNAME_EXTENSION ( %s )",
302 ( ( ssl->handshake->extensions_present
303 & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ?
304 "TRUE" : "FALSE" ) );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000305#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000306}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000307#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000308
XiaokangQian4080a7f2022-04-11 09:55:18 +0000309static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000310 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000311{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000312 int masked = ssl->handshake->extensions_present & exts_mask;
313 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000314}
315
XiaokangQianb67384d2022-04-19 00:02:38 +0000316static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
317 mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000318{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000319 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000320 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
321 MBEDTLS_SSL_EXT_KEY_SHARE |
322 MBEDTLS_SSL_EXT_SIG_ALG ) );
323}
324
XiaokangQianb67384d2022-04-19 00:02:38 +0000325static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000326{
327 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
328 return( 0 );
329
XiaokangQianb67384d2022-04-19 00:02:38 +0000330 if( !ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000331 return( 0 );
332
XiaokangQianb67384d2022-04-19 00:02:38 +0000333 ssl->handshake->tls13_kex_modes =
334 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000335 return( 1 );
336}
337
XiaokangQian81802f42022-06-10 13:25:22 +0000338#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
339 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian23c5be62022-06-07 02:04:34 +0000340/*
XiaokangQianfb665a82022-06-15 03:57:21 +0000341 * Pick best ( private key, certificate chain ) pair based on the signature
342 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +0000343 */
XiaokangQian07aad072022-06-14 05:35:09 +0000344static int ssl_tls13_pick_key_cert( mbedtls_ssl_context *ssl )
XiaokangQian23c5be62022-06-07 02:04:34 +0000345{
XiaokangQianfb665a82022-06-15 03:57:21 +0000346 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +0000347 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQianfb665a82022-06-15 03:57:21 +0000348 uint16_t key_sig_alg;
XiaokangQian23c5be62022-06-07 02:04:34 +0000349
350#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
351 if( ssl->handshake->sni_key_cert != NULL )
XiaokangQianfb665a82022-06-15 03:57:21 +0000352 key_cert_list = ssl->handshake->sni_key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000353 else
354#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianfb665a82022-06-15 03:57:21 +0000355 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000356
XiaokangQianfb665a82022-06-15 03:57:21 +0000357 if( key_cert_list == NULL )
XiaokangQian23c5be62022-06-07 02:04:34 +0000358 {
359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
360 return( -1 );
361 }
362
XiaokangQian81802f42022-06-10 13:25:22 +0000363 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
XiaokangQian23c5be62022-06-07 02:04:34 +0000364 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000365 for( key_cert = key_cert_list; key_cert != NULL;
366 key_cert = key_cert->next )
XiaokangQian23c5be62022-06-07 02:04:34 +0000367 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000368 int ret;
369 MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
370 key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000371
372 /*
373 * This avoids sending the client a cert it'll reject based on
374 * keyUsage or other extensions.
375 */
376 if( mbedtls_x509_crt_check_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000377 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +0000378 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000379 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
380 MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
XiaokangQian81802f42022-06-10 13:25:22 +0000381 {
382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
XiaokangQianfb665a82022-06-15 03:57:21 +0000383 "(extended) key usage extension" ) );
XiaokangQian81802f42022-06-10 13:25:22 +0000384 continue;
385 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000386
XiaokangQianfb665a82022-06-15 03:57:21 +0000387 ret = mbedtls_ssl_tls13_get_sig_alg_from_pk(
388 ssl, &key_cert->cert->pk, &key_sig_alg );
389 if( ret != 0 )
390 continue;
391 if( *sig_alg == key_sig_alg )
XiaokangQian81802f42022-06-10 13:25:22 +0000392 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000393 ssl->handshake->key_cert = key_cert;
394 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000395 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000396 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000397 return( 0 );
398 }
XiaokangQian81802f42022-06-10 13:25:22 +0000399 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000400 }
401
402 return( -1 );
403}
XiaokangQian81802f42022-06-10 13:25:22 +0000404#endif /* MBEDTLS_X509_CRT_PARSE_C &&
405 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000406
XiaokangQian4080a7f2022-04-11 09:55:18 +0000407/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000408 *
409 * STATE HANDLING: ClientHello
410 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000411 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000412 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000413 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000414 *
415 * In this case, the server progresses to sending its ServerHello.
416 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000417 * 2) The ClientHello was well-formed but didn't match the server's
418 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000419 *
420 * For example, the client might not have offered a key share which
421 * the server supports, or the server might require a cookie.
422 *
423 * In this case, the server sends a HelloRetryRequest.
424 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000425 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000426 *
427 * In this case, we abort the handshake.
428 *
429 */
430
431/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000432 * Structure of this message:
433 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000434 * uint16 ProtocolVersion;
435 * opaque Random[32];
436 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000437 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000438 * struct {
439 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
440 * Random random;
441 * opaque legacy_session_id<0..32>;
442 * CipherSuite cipher_suites<2..2^16-2>;
443 * opaque legacy_compression_methods<1..2^8-1>;
444 * Extension extensions<8..2^16-1>;
445 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000446 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000447
448#define SSL_CLIENT_HELLO_OK 0
449#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
450
XiaokangQian4080a7f2022-04-11 09:55:18 +0000451static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
452 const unsigned char *buf,
453 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000454{
XiaokangQianb67384d2022-04-19 00:02:38 +0000455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
456 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000457 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000458 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000459 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000460 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000461 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000462 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800463 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000464
XiaokangQian7807f9f2022-02-15 10:04:37 +0000465 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000466
467 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
468
469 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000470 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000471 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000472 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000473 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000474 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000475 * .. . .. ciphersuite list length ( 2 bytes )
476 * .. . .. ciphersuite list
477 * .. . .. compression alg. list length ( 1 byte )
478 * .. . .. compression alg. list
479 * .. . .. extensions length ( 2 bytes, optional )
480 * .. . .. extensions ( optional )
481 */
482
XiaokangQianb67384d2022-04-19 00:02:38 +0000483 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400484 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000485 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
486 * read at least up to session id length without worrying.
487 */
488 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
489
490 /* ...
491 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
492 * ...
493 * with ProtocolVersion defined as:
494 * uint16 ProtocolVersion;
495 */
XiaokangQiande333912022-04-20 08:49:42 +0000496 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
497 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000498 {
499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
500 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
501 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000502 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000503 }
504 p += 2;
505
506 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000507 * Only support TLS 1.3 currently, temporarily set the version.
508 */
XiaokangQiande333912022-04-20 08:49:42 +0000509 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000510
Jerry Yuc1be19f2022-04-23 16:11:39 +0800511 /* ...
512 * Random random;
513 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000514 * with Random defined as:
515 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000516 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000517 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +0000518 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000519
XiaokangQian08037552022-04-20 07:16:41 +0000520 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
521 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000522
Jerry Yuc1be19f2022-04-23 16:11:39 +0800523 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000524 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800525 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000526 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000527 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000528 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000529
XiaokangQianb67384d2022-04-19 00:02:38 +0000530 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000531 {
532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
533 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
534 }
535
XiaokangQian4080a7f2022-04-11 09:55:18 +0000536 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000537 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +0000538 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000539 /*
540 * Check we have enough data for the legacy session identifier
541 * and the ciphersuite list length.
542 */
543 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000544
XiaokangQianed582dd2022-04-13 08:21:05 +0000545 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000546 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000547
XiaokangQian7807f9f2022-02-15 10:04:37 +0000548 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
549 p += 2;
550
XiaokangQianb67384d2022-04-19 00:02:38 +0000551 /* Check we have enough data for the ciphersuite list, the legacy
552 * compression methods and the length of the extensions.
553 */
554 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000555
Jerry Yuc1be19f2022-04-23 16:11:39 +0800556 /* ...
XiaokangQian08037552022-04-20 07:16:41 +0000557 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800558 * ...
XiaokangQian08037552022-04-20 07:16:41 +0000559 * with CipherSuite defined as:
560 * uint8 CipherSuite[2];
561 */
XiaokangQian060d8672022-04-21 09:24:56 +0000562 cipher_suites = p;
563 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000564 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
565 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +0000566 /*
567 * Search for a matching ciphersuite
568 */
XiaokangQian318dc762022-04-20 09:43:51 +0000569 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +0000570 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +0000571 {
XiaokangQiane8ff3502022-04-22 02:34:40 +0000572 uint16_t cipher_suite;
573 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
574 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
575 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +0000576 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +0000577 * Check whether this ciphersuite is valid and offered.
578 */
XiaokangQian08037552022-04-20 07:16:41 +0000579 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800580 ssl, ciphersuite_info, ssl->tls_version,
581 ssl->tls_version ) != 0 ) ||
582 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
583 {
XiaokangQian08037552022-04-20 07:16:41 +0000584 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800585 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000586
XiaokangQian08037552022-04-20 07:16:41 +0000587 ssl->session_negotiate->ciphersuite = cipher_suite;
588 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +0000589 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +0000590
XiaokangQian08037552022-04-20 07:16:41 +0000591 break;
XiaokangQian17f974c2022-04-19 09:57:41 +0000592
XiaokangQian17f974c2022-04-19 09:57:41 +0000593 }
594
Jerry Yuc1be19f2022-04-23 16:11:39 +0800595 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +0000596 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000597 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
598 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
599 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +0000600 }
XiaokangQian17f974c2022-04-19 09:57:41 +0000601
602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
603 ciphersuite_info->name ) );
604
XiaokangQian060d8672022-04-21 09:24:56 +0000605 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800606
XiaokangQian4080a7f2022-04-11 09:55:18 +0000607 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000608 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000609 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000610 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000611 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000612 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
614 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
615 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
616 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000617 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000618 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000619
Jerry Yuc1be19f2022-04-23 16:11:39 +0800620 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000621 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800622 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000623 * with Extension defined as:
624 * struct {
625 * ExtensionType extension_type;
626 * opaque extension_data<0..2^16-1>;
627 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000628 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000629 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000630 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000631 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +0000632 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000633
XiaokangQian4080a7f2022-04-11 09:55:18 +0000634 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000635
636 while( p < extensions_end )
637 {
638 unsigned int extension_type;
639 size_t extension_data_len;
640 const unsigned char *extension_data_end;
641
XiaokangQian318dc762022-04-20 09:43:51 +0000642 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000643 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
644 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
645 p += 4;
646
647 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
648 extension_data_end = p + extension_data_len;
649
650 switch( extension_type )
651 {
XiaokangQian40a35232022-05-07 09:02:40 +0000652#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
653 case MBEDTLS_TLS_EXT_SERVERNAME:
654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +0000655 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
656 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +0000657 if( ret != 0 )
658 {
659 MBEDTLS_SSL_DEBUG_RET(
660 1, "mbedtls_ssl_parse_servername_ext", ret );
661 return( ret );
662 }
663 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
664 break;
665#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
666
XiaokangQianb67384d2022-04-19 00:02:38 +0000667#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000668 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
670
671 /* Supported Groups Extension
672 *
673 * When sent by the client, the "supported_groups" extension
674 * indicates the named groups which the client supports,
675 * ordered from most preferred to least preferred.
676 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800677 ret = ssl_tls13_parse_supported_groups_ext(
678 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000679 if( ret != 0 )
680 {
681 MBEDTLS_SSL_DEBUG_RET( 1,
682 "mbedtls_ssl_parse_supported_groups_ext", ret );
683 return( ret );
684 }
685
686 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
687 break;
XiaokangQianb67384d2022-04-19 00:02:38 +0000688#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000689
XiaokangQian88408882022-04-02 10:15:03 +0000690#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000691 case MBEDTLS_TLS_EXT_KEY_SHARE:
692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
693
694 /*
695 * Key Share Extension
696 *
697 * When sent by the client, the "key_share" extension
698 * contains the endpoint's cryptographic parameters for
699 * ECDHE/DHE key establishment methods.
700 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800701 ret = ssl_tls13_parse_key_shares_ext(
702 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +0000703 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000704 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +0800706 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000707 }
708
Jerry Yu582dd062022-04-22 21:59:01 +0800709 if( ret < 0 )
710 {
711 MBEDTLS_SSL_DEBUG_RET(
712 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000713 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +0800714 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000715
716 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
717 break;
XiaokangQian88408882022-04-02 10:15:03 +0000718#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000719
720 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
722
723 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800724 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000725 if( ret != 0 )
726 {
727 MBEDTLS_SSL_DEBUG_RET( 1,
728 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
729 return( ret );
730 }
731 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
732 break;
733
734#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
735 case MBEDTLS_TLS_EXT_SIG_ALG:
736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
737
Gabor Mezei078e8032022-04-27 21:17:56 +0200738 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800739 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000740 if( ret != 0 )
741 {
742 MBEDTLS_SSL_DEBUG_MSG( 1,
743 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
744 ret ) );
745 return( ret );
746 }
747 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
748 break;
749#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
750
751 default:
752 MBEDTLS_SSL_DEBUG_MSG( 3,
753 ( "unknown extension found: %ud ( ignoring )",
754 extension_type ) );
755 }
756
757 p += extension_data_len;
758 }
759
760 /* Update checksum with either
761 * - The entire content of the CH message, if no PSK extension is present
762 * - The content up to but excluding the PSK extension, if present.
763 */
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000764 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
XiaokangQianc4b8c992022-04-07 11:31:38 +0000765 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766
767 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000768#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000769 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000770#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000771
XiaokangQian75fe8c72022-06-15 09:42:45 +0000772 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
773}
774
775/* Update the handshake state machine */
776
777static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
778{
779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
780
XiaokangQian7807f9f2022-02-15 10:04:37 +0000781 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000782 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783 */
XiaokangQianb67384d2022-04-19 00:02:38 +0000784 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785 {
786 MBEDTLS_SSL_DEBUG_MSG(
787 1,
788 ( "ClientHello message misses mandatory extensions." ) );
789 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
790 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
791 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
792 }
793
XiaokangQianfb665a82022-06-15 03:57:21 +0000794 /*
795 * Server certificate selection
796 */
797 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
798 {
799 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
800 return( ret );
801 }
802#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
803 ssl->handshake->sni_name = NULL;
804 ssl->handshake->sni_name_len = 0;
805#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000806
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
808 if( ret != 0 )
809 {
810 MBEDTLS_SSL_DEBUG_RET( 1,
811 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
812 return( ret );
813 }
814
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815 return( 0 );
816
817}
818
819/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000820 * Main entry point from the state machine; orchestrates the otherfunctions.
821 */
822
823static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
824{
825
XiaokangQian08037552022-04-20 07:16:41 +0000826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +0000827 unsigned char* buf = NULL;
828 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +0800829 int parse_client_hello_ret;
830
XiaokangQianed582dd2022-04-13 08:21:05 +0000831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
832
XiaokangQianed582dd2022-04-13 08:21:05 +0000833 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
834 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
835 &buf, &buflen ) );
836
837 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
838 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +0800839 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
840 * only SSL_CLIENT_HELLO_OK or
841 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
842 * stage as negative error codes are handled
843 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +0800844
XiaokangQiancfd925f2022-04-14 07:10:37 +0000845 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +0800846
Jerry Yu4ca91402022-05-09 15:50:57 +0800847 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
848 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
849 else
850 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +0000851
852cleanup:
853
854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
855 return( ret );
856}
857
858/*
Jerry Yu1c3e6882022-04-20 21:23:40 +0800859 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +0800860 */
Jerry Yuf4b27e42022-03-30 17:32:21 +0800861static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
862{
Jerry Yu637a3f12022-04-20 21:37:58 +0800863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
864 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +0800865 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +0800866 if( ssl->conf->f_rng == NULL )
867 {
868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
869 return( MBEDTLS_ERR_SSL_NO_RNG );
870 }
871
Jerry Yu637a3f12022-04-20 21:37:58 +0800872 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800873 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
874 {
875 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
876 return( ret );
877 }
878
Jerry Yu637a3f12022-04-20 21:37:58 +0800879 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800880 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
881
882#if defined(MBEDTLS_HAVE_TIME)
883 ssl->session_negotiate->start = time( NULL );
884#endif /* MBEDTLS_HAVE_TIME */
885
886 return( ret );
887}
888
Jerry Yu3bf2c642022-03-30 22:02:12 +0800889/*
Jerry Yue74e04a2022-04-21 09:23:16 +0800890 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +0800891 *
892 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +0800893 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800894 * } SupportedVersions;
895 */
Jerry Yue74e04a2022-04-21 09:23:16 +0800896static int ssl_tls13_write_server_hello_supported_versions_ext(
897 mbedtls_ssl_context *ssl,
898 unsigned char *buf,
899 unsigned char *end,
900 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800901{
Jerry Yu3bf2c642022-03-30 22:02:12 +0800902 *out_len = 0;
903
Jerry Yu955ddd72022-04-22 22:27:33 +0800904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800905
906 /* Check if we have space to write the extension:
907 * - extension_type (2 bytes)
908 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +0800909 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800910 */
Jerry Yu349a6132022-04-14 20:52:56 +0800911 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800912
Jerry Yu349a6132022-04-14 20:52:56 +0800913 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800914
Jerry Yu349a6132022-04-14 20:52:56 +0800915 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800916
Jerry Yu349a6132022-04-14 20:52:56 +0800917 mbedtls_ssl_write_version( buf + 4,
918 ssl->conf->transport,
919 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800920
921 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
922 ssl->tls_version ) );
923
Jerry Yu349a6132022-04-14 20:52:56 +0800924 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800925
926 return( 0 );
927}
928
Jerry Yud9436a12022-04-20 22:28:09 +0800929
Jerry Yu3bf2c642022-03-30 22:02:12 +0800930
931/* Generate and export a single key share. For hybrid KEMs, this can
932 * be called multiple times with the different components of the hybrid. */
Jerry Yu955ddd72022-04-22 22:27:33 +0800933static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
934 uint16_t named_group,
935 unsigned char *buf,
936 unsigned char *end,
937 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800938{
939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +0800940
941 *out_len = 0;
942
Jerry Yu89e103c2022-03-30 22:43:29 +0800943#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800944 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
945 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800946 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
947 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800948 if( ret != 0 )
949 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800950 MBEDTLS_SSL_DEBUG_RET(
951 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
952 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800953 return( ret );
954 }
Jerry Yu3bf2c642022-03-30 22:02:12 +0800955 }
Jerry Yu89e103c2022-03-30 22:43:29 +0800956 else
957#endif /* MBEDTLS_ECDH_C */
958 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800959 {
960 }
961 else
962 {
Jerry Yu955ddd72022-04-22 22:27:33 +0800963 ((void) ssl);
964 ((void) named_group);
965 ((void) buf);
966 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +0800967 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
968 }
969
970 return( ret );
971}
972
973/*
974 * ssl_tls13_write_key_share_ext
975 *
976 * Structure of key_share extension in ServerHello:
977 *
Jerry Yu1c3e6882022-04-20 21:23:40 +0800978 * struct {
979 * NamedGroup group;
980 * opaque key_exchange<1..2^16-1>;
981 * } KeyShareEntry;
982 * struct {
983 * KeyShareEntry server_share;
984 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800985 */
986static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
987 unsigned char *buf,
988 unsigned char *end,
989 size_t *out_len )
990{
Jerry Yu955ddd72022-04-22 22:27:33 +0800991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800992 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +0800993 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800994 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800995 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800996
997 *out_len = 0;
998
999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1000
1001 /* Check if we have space for header and length fields:
1002 * - extension_type (2 bytes)
1003 * - extension_data_length (2 bytes)
1004 * - group (2 bytes)
1005 * - key_exchange_length (2 bytes)
1006 */
1007 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001008 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1009 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001010 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001011
Jerry Yu3bf2c642022-03-30 22:02:12 +08001012 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1013 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001014 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001015 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001016 if( ret != 0 )
1017 return( ret );
1018 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001019
Jerry Yu955ddd72022-04-22 22:27:33 +08001020 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001021
Jerry Yu57d48412022-04-20 21:50:42 +08001022 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001023
Jerry Yu57d48412022-04-20 21:50:42 +08001024 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001025
Jerry Yu3bf2c642022-03-30 22:02:12 +08001026 return( 0 );
1027}
Jerry Yud9436a12022-04-20 22:28:09 +08001028
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001029static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1030 unsigned char *buf,
1031 unsigned char *end,
1032 size_t *out_len )
1033{
1034 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1035 /* key_share Extension
1036 *
1037 * struct {
1038 * select (Handshake.msg_type) {
1039 * ...
1040 * case hello_retry_request:
1041 * NamedGroup selected_group;
1042 * ...
1043 * };
1044 * } KeyShare;
1045 */
1046
1047 *out_len = 0;
1048
Jerry Yub0ac10b2022-05-05 11:10:08 +08001049 /*
1050 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1051 * of the HRR is then to transmit a cookie to force the client to demonstrate
1052 * reachability at their apparent network address (primarily useful for DTLS).
1053 */
1054 if( ! mbedtls_ssl_tls13_some_ephemeral_enabled( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001055 return( 0 );
1056
1057 /* We should only send the key_share extension if the client's initial
1058 * key share was not acceptable. */
1059 if( ssl->handshake->offered_group_id != 0 )
1060 {
1061 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1062 return( 0 );
1063 }
1064
1065 if( selected_group == 0 )
1066 {
1067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1068 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1069 }
1070
Jerry Yub0ac10b2022-05-05 11:10:08 +08001071 /* Check if we have enough space:
1072 * - extension_type (2 bytes)
1073 * - extension_data_length (2 bytes)
1074 * - selected_group (2 bytes)
1075 */
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001076 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 6 );
1077
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001078 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001079 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001080 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1081
1082 MBEDTLS_SSL_DEBUG_MSG( 3,
1083 ( "HRR selected_group: %s (%x)",
1084 mbedtls_ssl_named_group_to_str( selected_group ),
1085 selected_group ) );
1086
1087 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001088
Jerry Yub0ac10b2022-05-05 11:10:08 +08001089 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001090}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001091
1092/*
1093 * Structure of ServerHello message:
1094 *
1095 * struct {
1096 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1097 * Random random;
1098 * opaque legacy_session_id_echo<0..32>;
1099 * CipherSuite cipher_suite;
1100 * uint8 legacy_compression_method = 0;
1101 * Extension extensions<6..2^16-1>;
1102 * } ServerHello;
1103 */
1104static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001105 unsigned char *buf,
1106 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001107 size_t *out_len,
1108 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001109{
Jerry Yu955ddd72022-04-22 22:27:33 +08001110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001111 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001112 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001113 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001114
1115 *out_len = 0;
1116
Jerry Yucfc04b32022-04-21 09:31:58 +08001117 /* ...
1118 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1119 * ...
1120 * with ProtocolVersion defined as:
1121 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001122 */
1123 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1124 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1125 p += 2;
1126
Jerry Yu1c3e6882022-04-20 21:23:40 +08001127 /* ...
1128 * Random random;
1129 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001130 * with Random defined as:
1131 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001132 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001133 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001134 if( is_hrr )
1135 {
1136 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001137 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001138 }
1139 else
1140 {
1141 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001142 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001143 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001144 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001145 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1146 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1147
Jerry Yucfc04b32022-04-21 09:31:58 +08001148 /* ...
1149 * opaque legacy_session_id_echo<0..32>;
1150 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001151 */
1152 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1153 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1154 if( ssl->session_negotiate->id_len > 0 )
1155 {
1156 memcpy( p, &ssl->session_negotiate->id[0],
1157 ssl->session_negotiate->id_len );
1158 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001159
Jerry Yu3bf2c642022-03-30 22:02:12 +08001160 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1161 ssl->session_negotiate->id_len );
1162 }
1163
Jerry Yucfc04b32022-04-21 09:31:58 +08001164 /* ...
1165 * CipherSuite cipher_suite;
1166 * ...
1167 * with CipherSuite defined as:
1168 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001169 */
1170 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1171 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1172 p += 2;
1173 MBEDTLS_SSL_DEBUG_MSG( 3,
1174 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1175 mbedtls_ssl_get_ciphersuite_name(
1176 ssl->session_negotiate->ciphersuite ),
1177 ssl->session_negotiate->ciphersuite ) );
1178
Jerry Yucfc04b32022-04-21 09:31:58 +08001179 /* ...
1180 * uint8 legacy_compression_method = 0;
1181 * ...
1182 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001183 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1184 *p++ = 0x0;
1185
Jerry Yucfc04b32022-04-21 09:31:58 +08001186 /* ...
1187 * Extension extensions<6..2^16-1>;
1188 * ...
1189 * struct {
1190 * ExtensionType extension_type; (2 bytes)
1191 * opaque extension_data<0..2^16-1>;
1192 * } Extension;
1193 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001194 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001195 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001196 p += 2;
1197
Jerry Yue74e04a2022-04-21 09:23:16 +08001198 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001199 ssl, p, end, &output_len ) ) != 0 )
1200 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001201 MBEDTLS_SSL_DEBUG_RET(
1202 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001203 return( ret );
1204 }
1205 p += output_len;
1206
Jerry Yu3bf2c642022-03-30 22:02:12 +08001207 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1208 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001209 if( is_hrr )
1210 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1211 else
1212 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001213 if( ret != 0 )
1214 return( ret );
1215 p += output_len;
1216 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001217
Jerry Yud9436a12022-04-20 22:28:09 +08001218 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001219
Jerry Yud9436a12022-04-20 22:28:09 +08001220 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1221 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001222
Jerry Yud9436a12022-04-20 22:28:09 +08001223 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001224
Jerry Yud9436a12022-04-20 22:28:09 +08001225 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001226
1227 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001228}
1229
Jerry Yue110d252022-05-05 10:19:22 +08001230static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1231{
1232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001233 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001234 if( ret != 0 )
1235 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001236 MBEDTLS_SSL_DEBUG_RET( 1,
1237 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001238 ret );
1239 return( ret );
1240 }
1241
Jerry Yue110d252022-05-05 10:19:22 +08001242 return( ret );
1243}
1244
Jerry Yu5b64ae92022-03-30 17:15:02 +08001245static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1246{
Jerry Yu637a3f12022-04-20 21:37:58 +08001247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001248 unsigned char *buf;
1249 size_t buf_len, msg_len;
1250
1251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1252
Jerry Yuf4b27e42022-03-30 17:32:21 +08001253 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1254
Jerry Yu3bf2c642022-03-30 22:02:12 +08001255 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001256 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1257
Jerry Yu3bf2c642022-03-30 22:02:12 +08001258 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1259 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001260 &msg_len,
1261 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001262
Jerry Yu3bf2c642022-03-30 22:02:12 +08001263 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001264 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1265
Jerry Yu3bf2c642022-03-30 22:02:12 +08001266 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001267 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001268
Jerry Yue110d252022-05-05 10:19:22 +08001269 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1270
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001271#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1272 /* The server sends a dummy change_cipher_spec record immediately
1273 * after its first handshake message. This may either be after
1274 * a ServerHello or a HelloRetryRequest.
1275 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001276 mbedtls_ssl_handshake_set_state(
1277 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001278#else
Jerry Yu637a3f12022-04-20 21:37:58 +08001279 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001280#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08001281
Jerry Yuf4b27e42022-03-30 17:32:21 +08001282cleanup:
1283
1284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1285 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001286}
1287
Jerry Yu23d1a252022-05-12 18:08:59 +08001288
1289/*
1290 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1291 */
1292static int ssl_tls13_write_hello_retry_request_coordinate(
1293 mbedtls_ssl_context *ssl )
1294{
1295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1296 if( ssl->handshake->hello_retry_request_count > 0 )
1297 {
1298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1299 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1300 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1301 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1302 }
1303
1304 /*
1305 * Create stateless transcript hash for HRR
1306 */
1307 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1308 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1309 if( ret != 0 )
1310 {
1311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1312 return( ret );
1313 }
1314 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1315
1316 return( 0 );
1317}
1318
1319static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1320{
1321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1322 unsigned char *buf;
1323 size_t buf_len, msg_len;
1324
1325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1326
1327 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_hello_retry_request_coordinate( ssl ) );
1328
1329 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1330 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1331 &buf, &buf_len ) );
1332
1333 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1334 buf + buf_len,
1335 &msg_len,
1336 1 ) );
1337 mbedtls_ssl_add_hs_msg_to_checksum(
1338 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1339
1340
1341 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1342 msg_len ) );
1343
1344 ssl->handshake->hello_retry_request_count++;
1345
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001346#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1347 /* The server sends a dummy change_cipher_spec record immediately
1348 * after its first handshake message. This may either be after
1349 * a ServerHello or a HelloRetryRequest.
1350 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001351 mbedtls_ssl_handshake_set_state(
1352 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REUEST );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001353#else
Jerry Yu23d1a252022-05-12 18:08:59 +08001354 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001355#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08001356
1357cleanup:
1358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1359 return( ret );
1360}
1361
Jerry Yu5b64ae92022-03-30 17:15:02 +08001362/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001363 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1364 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001365
1366/*
1367 * struct {
1368 * Extension extensions<0..2 ^ 16 - 1>;
1369 * } EncryptedExtensions;
1370 *
1371 */
1372static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1373 unsigned char *buf,
1374 unsigned char *end,
1375 size_t *out_len )
1376{
Jerry Yu4d3841a2022-04-16 12:37:19 +08001377 unsigned char *p = buf;
1378 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001379 unsigned char *p_extensions_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001380
Jerry Yu4d3841a2022-04-16 12:37:19 +08001381 *out_len = 0;
1382
1383 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001384 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001385 p += 2;
1386
Jerry Yu8937eb42022-05-03 12:12:14 +08001387 ((void) ssl);
Jerry Yu4d3841a2022-04-16 12:37:19 +08001388
Jerry Yu8937eb42022-05-03 12:12:14 +08001389 extensions_len = ( p - p_extensions_len ) - 2;
1390 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
1391
1392 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001393
1394 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
1395
1396 return( 0 );
1397}
1398
1399static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
1400{
1401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1402 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08001403 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001404
Gabor Mezei54719122022-06-28 11:34:56 +02001405 mbedtls_ssl_set_outbound_transform( ssl,
1406 ssl->handshake->transform_handshake );
1407 MBEDTLS_SSL_DEBUG_MSG(
1408 3, ( "switching to handshake transform for outbound data" ) );
1409
Jerry Yuab452cc2022-04-28 15:27:08 +08001410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001411
Jerry Yu4d3841a2022-04-16 12:37:19 +08001412 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1413 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
1414
1415 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
1416 ssl, buf, buf + buf_len, &msg_len ) );
1417
1418 mbedtls_ssl_add_hs_msg_to_checksum(
1419 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
1420
Jerry Yu4d3841a2022-04-16 12:37:19 +08001421 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1422 ssl, buf_len, msg_len ) );
1423
Jerry Yu8937eb42022-05-03 12:12:14 +08001424#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1425 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1426 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1427 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00001428 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08001429#else
1430 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1431#endif
1432
Jerry Yu4d3841a2022-04-16 12:37:19 +08001433cleanup:
1434
Jerry Yuab452cc2022-04-28 15:27:08 +08001435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001436 return( ret );
1437}
1438
XiaokangQiancec9ae62022-05-06 07:28:50 +00001439#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00001440#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
1441#define SSL_CERTIFICATE_REQUEST_SKIP 1
1442/* Coordination:
1443 * Check whether a CertificateRequest message should be written.
1444 * Returns a negative code on failure, or
1445 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
1446 * - SSL_CERTIFICATE_REQUEST_SKIP
1447 * indicating if the writing of the CertificateRequest
1448 * should be skipped or not.
1449 */
1450static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
1451{
1452 int authmode;
1453
XiaokangQian40a35232022-05-07 09:02:40 +00001454#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1455 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
1456 authmode = ssl->handshake->sni_authmode;
1457 else
1458#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00001459 authmode = ssl->conf->authmode;
1460
1461 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
1462 return( SSL_CERTIFICATE_REQUEST_SKIP );
1463
XiaokangQianc3017f62022-05-13 05:55:41 +00001464 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00001465
XiaokangQiana987e1d2022-05-07 01:25:58 +00001466 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
1467}
1468
XiaokangQiancec9ae62022-05-06 07:28:50 +00001469/*
1470 * struct {
1471 * opaque certificate_request_context<0..2^8-1>;
1472 * Extension extensions<2..2^16-1>;
1473 * } CertificateRequest;
1474 *
1475 */
1476static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
1477 unsigned char *buf,
1478 const unsigned char *end,
1479 size_t *out_len )
1480{
1481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1482 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00001483 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00001484 unsigned char *p_extensions_len;
1485
1486 *out_len = 0;
1487
1488 /* Check if we have enough space:
1489 * - certificate_request_context (1 byte)
1490 * - extensions length (2 bytes)
1491 */
1492 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
1493
1494 /*
1495 * Write certificate_request_context
1496 */
1497 /*
1498 * We use a zero length context for the normal handshake
1499 * messages. For post-authentication handshake messages
1500 * this request context would be set to a non-zero value.
1501 */
1502 *p++ = 0x0;
1503
1504 /*
1505 * Write extensions
1506 */
1507 /* The extensions must contain the signature_algorithms. */
1508 p_extensions_len = p;
1509 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00001510 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001511 if( ret != 0 )
1512 return( ret );
1513
XiaokangQianec6efb92022-05-06 09:53:10 +00001514 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00001515 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001516
1517 *out_len = p - buf;
1518
1519 return( 0 );
1520}
1521
1522static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
1523{
1524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1525
1526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1527
1528 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1529
1530 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
1531 {
1532 unsigned char *buf;
1533 size_t buf_len, msg_len;
1534
1535 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1536 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
1537
1538 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
1539 ssl, buf, buf + buf_len, &msg_len ) );
1540
1541 mbedtls_ssl_add_hs_msg_to_checksum(
1542 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
1543
1544 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1545 ssl, buf_len, msg_len ) );
1546 }
1547 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
1548 {
1549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1550 ret = 0;
1551 }
1552 else
1553 {
1554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1555 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1556 goto cleanup;
1557 }
1558
1559 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1560cleanup:
1561
1562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1563 return( ret );
1564}
XiaokangQiancec9ae62022-05-06 07:28:50 +00001565
Jerry Yu4d3841a2022-04-16 12:37:19 +08001566/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001567 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08001568 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001569static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001570{
Jerry Yu5a26f302022-05-10 20:46:40 +08001571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00001572
1573#if defined(MBEDTLS_X509_CRT_PARSE_C)
1574 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
1575 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08001576 {
Jerry Yub89125b2022-05-13 15:45:49 +08001577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08001578 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1579 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001580 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08001581 }
XiaokangQianfb665a82022-06-15 03:57:21 +00001582#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08001583
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001584 ret = mbedtls_ssl_tls13_write_certificate( ssl );
1585 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08001586 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08001587 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1588 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001589}
1590
1591/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001592 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08001593 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001594static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001595{
Jerry Yu4ff9e142022-04-16 14:57:49 +08001596 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001597 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08001598 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08001599 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1600 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001601}
Jerry Yu5a26f302022-05-10 20:46:40 +08001602#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001603
1604/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001605 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001606 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001607static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001608{
Jerry Yud6e253d2022-05-18 13:59:24 +08001609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001610
1611 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1612 if( ret != 0 )
1613 return( ret );
1614
Jerry Yue3d67cb2022-05-19 15:33:10 +08001615 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1616 if( ret != 0 )
1617 {
1618 MBEDTLS_SSL_PEND_FATAL_ALERT(
1619 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1620 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1621 return( ret );
1622 }
XiaokangQianc3017f62022-05-13 05:55:41 +00001623
1624 if( ssl->handshake->certificate_request_sent )
XiaokangQian189ded22022-05-10 08:12:17 +00001625 {
1626 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
1627
1628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1629 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
1630 }
1631 else
1632 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001633 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001634}
1635
1636/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001637 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001638 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001639static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001640{
Jerry Yud6e253d2022-05-18 13:59:24 +08001641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1642
XiaokangQianc3017f62022-05-13 05:55:41 +00001643 if( ! ssl->handshake->certificate_request_sent )
XiaokangQianaca90482022-05-19 07:19:31 +00001644 {
1645 MBEDTLS_SSL_DEBUG_MSG( 1,
1646 ( "Switch to handshake traffic keys for inbound traffic" ) );
XiaokangQianc3017f62022-05-13 05:55:41 +00001647 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQianaca90482022-05-19 07:19:31 +00001648 }
Jerry Yuff226982022-04-16 16:52:57 +08001649 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
1650 if( ret != 0 )
1651 return( ret );
1652
Jerry Yue3d67cb2022-05-19 15:33:10 +08001653 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1654 if( ret != 0 )
1655 {
1656 MBEDTLS_SSL_DEBUG_RET( 1,
1657 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1658 }
1659
Jerry Yu03ed50b2022-04-16 17:13:30 +08001660 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08001661 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001662}
1663
1664/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001665 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08001666 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001667static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001668{
Jerry Yu03ed50b2022-04-16 17:13:30 +08001669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1670
Jerry Yu03ed50b2022-04-16 17:13:30 +08001671 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1672 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1673 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001674}
1675
1676/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001677 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00001678 */
Jerry Yu27561932021-08-27 17:07:38 +08001679int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08001680{
XiaokangQian08037552022-04-20 07:16:41 +00001681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001682
1683 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
1684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1685
Jerry Yue3b34122021-09-28 17:53:35 +08001686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
1687 mbedtls_ssl_states_str( ssl->state ),
1688 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08001689
XiaokangQian7807f9f2022-02-15 10:04:37 +00001690 switch( ssl->state )
1691 {
1692 /* start state */
1693 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00001695 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001696 break;
1697
XiaokangQian7807f9f2022-02-15 10:04:37 +00001698 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00001699 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001700 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00001701 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08001702 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001703
Jerry Yuf41553b2022-05-09 22:20:30 +08001704 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
1705 ret = ssl_tls13_write_hello_retry_request( ssl );
1706 if( ret != 0 )
1707 {
1708 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
1709 return( ret );
1710 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001711 break;
1712
Jerry Yu5b64ae92022-03-30 17:15:02 +08001713 case MBEDTLS_SSL_SERVER_HELLO:
1714 ret = ssl_tls13_write_server_hello( ssl );
1715 break;
1716
Xiaofei Baicba64af2022-02-15 10:00:56 +00001717 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08001718 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001719 if( ret != 0 )
1720 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08001721 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
1722 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001723 }
Jerry Yu48330562022-05-06 21:35:44 +08001724 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08001725
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001726#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1727 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00001728 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001729 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001730
Jerry Yu83da34e2022-04-16 13:59:52 +08001731 case MBEDTLS_SSL_SERVER_CERTIFICATE:
1732 ret = ssl_tls13_write_server_certificate( ssl );
1733 break;
1734
1735 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
1736 ret = ssl_tls13_write_certificate_verify( ssl );
1737 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08001738#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001739
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001740 /*
1741 * Injection of dummy-CCS's for middlebox compatibility
1742 */
1743#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1744 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REUEST:
1745 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1746 if( ret == 0 )
1747 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1748 break;
1749
1750 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
1751 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1752 if( ret == 0 )
1753 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1754 break;
1755#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1756
Jerry Yu69dd8d42022-04-16 12:51:26 +08001757 case MBEDTLS_SSL_SERVER_FINISHED:
1758 ret = ssl_tls13_write_server_finished( ssl );
1759 break;
1760
1761 case MBEDTLS_SSL_CLIENT_FINISHED:
1762 ret = ssl_tls13_process_client_finished( ssl );
1763 break;
1764
Jerry Yu69dd8d42022-04-16 12:51:26 +08001765 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
1766 ret = ssl_tls13_handshake_wrapup( ssl );
1767 break;
1768
XiaokangQian6b916b12022-04-25 07:29:34 +00001769 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1770 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02001771 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00001772 {
Ronald Cron209cae92022-06-07 10:30:19 +02001773 if( ssl->session_negotiate->peer_cert != NULL )
1774 {
1775 mbedtls_ssl_handshake_set_state(
1776 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1777 }
1778 else
1779 mbedtls_ssl_handshake_set_state(
1780 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
XiaokangQian6b916b12022-04-25 07:29:34 +00001781 }
1782 break;
1783
1784 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1785 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1786 if( ret == 0 )
1787 {
1788 mbedtls_ssl_handshake_set_state(
1789 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1790 }
1791 break;
1792
XiaokangQian7807f9f2022-02-15 10:04:37 +00001793 default:
1794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00001795 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001796 }
1797
1798 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08001799}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001800
Jerry Yufb4b6472022-01-27 15:03:26 +08001801#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */