blob: 0b56d0e0a49fb6d180072c297d7ef165d963bc80 [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 */
XiaokangQianacb39922022-06-17 10:18:48 +0000306#if defined ( MBEDTLS_SSL_ALPN )
307 MBEDTLS_SSL_DEBUG_MSG( 3,
308 ( "- ALPN_EXTENSION ( %s )",
309 ( ( ssl->handshake->extensions_present
310 & MBEDTLS_SSL_EXT_ALPN ) > 0 ) ?
311 "TRUE" : "FALSE" ) );
312#endif /* MBEDTLS_SSL_ALPN */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000313}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000314#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000315
XiaokangQian4080a7f2022-04-11 09:55:18 +0000316static int ssl_tls13_client_hello_has_exts( mbedtls_ssl_context *ssl,
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000317 int exts_mask )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000318{
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000319 int masked = ssl->handshake->extensions_present & exts_mask;
320 return( masked == exts_mask );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000321}
322
XiaokangQianb67384d2022-04-19 00:02:38 +0000323static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
324 mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000325{
XiaokangQian4080a7f2022-04-11 09:55:18 +0000326 return( ssl_tls13_client_hello_has_exts( ssl,
XiaokangQian7807f9f2022-02-15 10:04:37 +0000327 MBEDTLS_SSL_EXT_SUPPORTED_GROUPS |
328 MBEDTLS_SSL_EXT_KEY_SHARE |
329 MBEDTLS_SSL_EXT_SIG_ALG ) );
330}
331
XiaokangQianb67384d2022-04-19 00:02:38 +0000332static int ssl_tls13_check_ephemeral_key_exchange( mbedtls_ssl_context *ssl )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000333{
334 if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
335 return( 0 );
336
XiaokangQianb67384d2022-04-19 00:02:38 +0000337 if( !ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000338 return( 0 );
339
XiaokangQianb67384d2022-04-19 00:02:38 +0000340 ssl->handshake->tls13_kex_modes =
341 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000342 return( 1 );
343}
344
XiaokangQian81802f42022-06-10 13:25:22 +0000345#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
346 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQian23c5be62022-06-07 02:04:34 +0000347/*
XiaokangQianfb665a82022-06-15 03:57:21 +0000348 * Pick best ( private key, certificate chain ) pair based on the signature
349 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +0000350 */
XiaokangQian07aad072022-06-14 05:35:09 +0000351static int ssl_tls13_pick_key_cert( mbedtls_ssl_context *ssl )
XiaokangQian23c5be62022-06-07 02:04:34 +0000352{
XiaokangQianfb665a82022-06-15 03:57:21 +0000353 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +0000354 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQianfb665a82022-06-15 03:57:21 +0000355 uint16_t key_sig_alg;
XiaokangQian23c5be62022-06-07 02:04:34 +0000356
357#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
358 if( ssl->handshake->sni_key_cert != NULL )
XiaokangQianfb665a82022-06-15 03:57:21 +0000359 key_cert_list = ssl->handshake->sni_key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000360 else
361#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianfb665a82022-06-15 03:57:21 +0000362 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000363
XiaokangQianfb665a82022-06-15 03:57:21 +0000364 if( key_cert_list == NULL )
XiaokangQian23c5be62022-06-07 02:04:34 +0000365 {
366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
367 return( -1 );
368 }
369
XiaokangQian81802f42022-06-10 13:25:22 +0000370 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
XiaokangQian23c5be62022-06-07 02:04:34 +0000371 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000372 for( key_cert = key_cert_list; key_cert != NULL;
373 key_cert = key_cert->next )
XiaokangQian23c5be62022-06-07 02:04:34 +0000374 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000375 int ret;
376 MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
377 key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000378
379 /*
380 * This avoids sending the client a cert it'll reject based on
381 * keyUsage or other extensions.
382 */
383 if( mbedtls_x509_crt_check_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000384 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +0000385 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000386 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
387 MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
XiaokangQian81802f42022-06-10 13:25:22 +0000388 {
389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
XiaokangQianfb665a82022-06-15 03:57:21 +0000390 "(extended) key usage extension" ) );
XiaokangQian81802f42022-06-10 13:25:22 +0000391 continue;
392 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000393
XiaokangQianfb665a82022-06-15 03:57:21 +0000394 ret = mbedtls_ssl_tls13_get_sig_alg_from_pk(
395 ssl, &key_cert->cert->pk, &key_sig_alg );
396 if( ret != 0 )
397 continue;
398 if( *sig_alg == key_sig_alg )
XiaokangQian81802f42022-06-10 13:25:22 +0000399 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000400 ssl->handshake->key_cert = key_cert;
401 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000402 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000403 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000404 return( 0 );
405 }
XiaokangQian81802f42022-06-10 13:25:22 +0000406 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000407 }
408
409 return( -1 );
410}
XiaokangQian81802f42022-06-10 13:25:22 +0000411#endif /* MBEDTLS_X509_CRT_PARSE_C &&
412 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000413
XiaokangQian4080a7f2022-04-11 09:55:18 +0000414/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000415 *
416 * STATE HANDLING: ClientHello
417 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000418 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000419 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000420 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000421 *
422 * In this case, the server progresses to sending its ServerHello.
423 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000424 * 2) The ClientHello was well-formed but didn't match the server's
425 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000426 *
427 * For example, the client might not have offered a key share which
428 * the server supports, or the server might require a cookie.
429 *
430 * In this case, the server sends a HelloRetryRequest.
431 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000432 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000433 *
434 * In this case, we abort the handshake.
435 *
436 */
437
438/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000439 * Structure of this message:
440 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000441 * uint16 ProtocolVersion;
442 * opaque Random[32];
443 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000444 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000445 * struct {
446 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
447 * Random random;
448 * opaque legacy_session_id<0..32>;
449 * CipherSuite cipher_suites<2..2^16-2>;
450 * opaque legacy_compression_methods<1..2^8-1>;
451 * Extension extensions<8..2^16-1>;
452 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000453 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000454
455#define SSL_CLIENT_HELLO_OK 0
456#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
457
XiaokangQian4080a7f2022-04-11 09:55:18 +0000458static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
459 const unsigned char *buf,
460 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000461{
XiaokangQianb67384d2022-04-19 00:02:38 +0000462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
463 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000464 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000465 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000466 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000467 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000468 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000469 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800470 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000471
XiaokangQian7807f9f2022-02-15 10:04:37 +0000472 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000473
474 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
475
476 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000477 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000478 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000479 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000480 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000481 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000482 * .. . .. ciphersuite list length ( 2 bytes )
483 * .. . .. ciphersuite list
484 * .. . .. compression alg. list length ( 1 byte )
485 * .. . .. compression alg. list
486 * .. . .. extensions length ( 2 bytes, optional )
487 * .. . .. extensions ( optional )
488 */
489
XiaokangQianb67384d2022-04-19 00:02:38 +0000490 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400491 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000492 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
493 * read at least up to session id length without worrying.
494 */
495 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
496
497 /* ...
498 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
499 * ...
500 * with ProtocolVersion defined as:
501 * uint16 ProtocolVersion;
502 */
XiaokangQiande333912022-04-20 08:49:42 +0000503 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
504 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000505 {
506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
507 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
508 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000509 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000510 }
511 p += 2;
512
513 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000514 * Only support TLS 1.3 currently, temporarily set the version.
515 */
XiaokangQiande333912022-04-20 08:49:42 +0000516 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000517
Jerry Yuc1be19f2022-04-23 16:11:39 +0800518 /* ...
519 * Random random;
520 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000521 * with Random defined as:
522 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000523 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000524 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +0000525 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000526
XiaokangQian08037552022-04-20 07:16:41 +0000527 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
528 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000529
Jerry Yuc1be19f2022-04-23 16:11:39 +0800530 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000531 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800532 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000534 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000535 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000536
XiaokangQianb67384d2022-04-19 00:02:38 +0000537 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000538 {
539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
540 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
541 }
542
XiaokangQian4080a7f2022-04-11 09:55:18 +0000543 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000544 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +0000545 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000546 /*
547 * Check we have enough data for the legacy session identifier
548 * and the ciphersuite list length.
549 */
550 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000551
XiaokangQianed582dd2022-04-13 08:21:05 +0000552 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000553 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000554
XiaokangQian7807f9f2022-02-15 10:04:37 +0000555 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
556 p += 2;
557
XiaokangQianb67384d2022-04-19 00:02:38 +0000558 /* Check we have enough data for the ciphersuite list, the legacy
559 * compression methods and the length of the extensions.
560 */
561 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000562
Jerry Yuc1be19f2022-04-23 16:11:39 +0800563 /* ...
XiaokangQian08037552022-04-20 07:16:41 +0000564 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800565 * ...
XiaokangQian08037552022-04-20 07:16:41 +0000566 * with CipherSuite defined as:
567 * uint8 CipherSuite[2];
568 */
XiaokangQian060d8672022-04-21 09:24:56 +0000569 cipher_suites = p;
570 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000571 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
572 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +0000573 /*
574 * Search for a matching ciphersuite
575 */
XiaokangQian318dc762022-04-20 09:43:51 +0000576 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +0000577 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +0000578 {
XiaokangQiane8ff3502022-04-22 02:34:40 +0000579 uint16_t cipher_suite;
580 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
581 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
582 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +0000583 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +0000584 * Check whether this ciphersuite is valid and offered.
585 */
XiaokangQian08037552022-04-20 07:16:41 +0000586 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800587 ssl, ciphersuite_info, ssl->tls_version,
588 ssl->tls_version ) != 0 ) ||
589 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
590 {
XiaokangQian08037552022-04-20 07:16:41 +0000591 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800592 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000593
XiaokangQian08037552022-04-20 07:16:41 +0000594 ssl->session_negotiate->ciphersuite = cipher_suite;
595 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +0000596 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +0000597
XiaokangQian08037552022-04-20 07:16:41 +0000598 break;
XiaokangQian17f974c2022-04-19 09:57:41 +0000599
XiaokangQian17f974c2022-04-19 09:57:41 +0000600 }
601
Jerry Yuc1be19f2022-04-23 16:11:39 +0800602 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +0000603 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000604 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
605 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
606 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +0000607 }
XiaokangQian17f974c2022-04-19 09:57:41 +0000608
609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
610 ciphersuite_info->name ) );
611
XiaokangQian060d8672022-04-21 09:24:56 +0000612 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800613
XiaokangQian4080a7f2022-04-11 09:55:18 +0000614 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000615 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000616 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000617 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000618 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000619 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
621 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
622 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
623 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000624 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000625 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000626
Jerry Yuc1be19f2022-04-23 16:11:39 +0800627 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000628 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800629 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000630 * with Extension defined as:
631 * struct {
632 * ExtensionType extension_type;
633 * opaque extension_data<0..2^16-1>;
634 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000635 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000636 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000637 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000638 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +0000639 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000640
XiaokangQian4080a7f2022-04-11 09:55:18 +0000641 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000642
643 while( p < extensions_end )
644 {
645 unsigned int extension_type;
646 size_t extension_data_len;
647 const unsigned char *extension_data_end;
648
XiaokangQian318dc762022-04-20 09:43:51 +0000649 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000650 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
651 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
652 p += 4;
653
654 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
655 extension_data_end = p + extension_data_len;
656
657 switch( extension_type )
658 {
XiaokangQian40a35232022-05-07 09:02:40 +0000659#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
660 case MBEDTLS_TLS_EXT_SERVERNAME:
661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +0000662 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
663 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +0000664 if( ret != 0 )
665 {
666 MBEDTLS_SSL_DEBUG_RET(
667 1, "mbedtls_ssl_parse_servername_ext", ret );
668 return( ret );
669 }
670 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
671 break;
672#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
673
XiaokangQianb67384d2022-04-19 00:02:38 +0000674#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000675 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
677
678 /* Supported Groups Extension
679 *
680 * When sent by the client, the "supported_groups" extension
681 * indicates the named groups which the client supports,
682 * ordered from most preferred to least preferred.
683 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800684 ret = ssl_tls13_parse_supported_groups_ext(
685 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000686 if( ret != 0 )
687 {
688 MBEDTLS_SSL_DEBUG_RET( 1,
689 "mbedtls_ssl_parse_supported_groups_ext", ret );
690 return( ret );
691 }
692
693 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
694 break;
XiaokangQianb67384d2022-04-19 00:02:38 +0000695#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000696
XiaokangQian88408882022-04-02 10:15:03 +0000697#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000698 case MBEDTLS_TLS_EXT_KEY_SHARE:
699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
700
701 /*
702 * Key Share Extension
703 *
704 * When sent by the client, the "key_share" extension
705 * contains the endpoint's cryptographic parameters for
706 * ECDHE/DHE key establishment methods.
707 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800708 ret = ssl_tls13_parse_key_shares_ext(
709 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +0000710 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000711 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +0800713 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000714 }
715
Jerry Yu582dd062022-04-22 21:59:01 +0800716 if( ret < 0 )
717 {
718 MBEDTLS_SSL_DEBUG_RET(
719 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000720 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +0800721 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000722
723 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
724 break;
XiaokangQian88408882022-04-02 10:15:03 +0000725#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000726
727 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
729
730 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800731 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000732 if( ret != 0 )
733 {
734 MBEDTLS_SSL_DEBUG_RET( 1,
735 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
736 return( ret );
737 }
738 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
739 break;
740
XiaokangQianacb39922022-06-17 10:18:48 +0000741#if defined(MBEDTLS_SSL_ALPN)
742 case MBEDTLS_TLS_EXT_ALPN:
743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
744
745 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
746 if( ret != 0 )
747 {
748 MBEDTLS_SSL_DEBUG_RET(
749 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
750 return( ret );
751 }
752 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
753 break;
754#endif /* MBEDTLS_SSL_ALPN */
755
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
757 case MBEDTLS_TLS_EXT_SIG_ALG:
758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
759
Gabor Mezei078e8032022-04-27 21:17:56 +0200760 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800761 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762 if( ret != 0 )
763 {
764 MBEDTLS_SSL_DEBUG_MSG( 1,
765 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
766 ret ) );
767 return( ret );
768 }
769 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
770 break;
771#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
772
773 default:
774 MBEDTLS_SSL_DEBUG_MSG( 3,
775 ( "unknown extension found: %ud ( ignoring )",
776 extension_type ) );
777 }
778
779 p += extension_data_len;
780 }
781
782 /* Update checksum with either
783 * - The entire content of the CH message, if no PSK extension is present
784 * - The content up to but excluding the PSK extension, if present.
785 */
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000786 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
XiaokangQianc4b8c992022-04-07 11:31:38 +0000787 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
789 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000790#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000791 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000792#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793
XiaokangQian75fe8c72022-06-15 09:42:45 +0000794 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
795}
796
797/* Update the handshake state machine */
798
799static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
800{
801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
802
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000804 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +0000805 */
XiaokangQianb67384d2022-04-19 00:02:38 +0000806 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 {
808 MBEDTLS_SSL_DEBUG_MSG(
809 1,
810 ( "ClientHello message misses mandatory extensions." ) );
811 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
812 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
813 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
814 }
815
XiaokangQianfb665a82022-06-15 03:57:21 +0000816 /*
817 * Server certificate selection
818 */
819 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
820 {
821 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
822 return( ret );
823 }
824#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
825 ssl->handshake->sni_name = NULL;
826 ssl->handshake->sni_name_len = 0;
827#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000828
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
830 if( ret != 0 )
831 {
832 MBEDTLS_SSL_DEBUG_RET( 1,
833 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
834 return( ret );
835 }
836
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837 return( 0 );
838
839}
840
841/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000842 * Main entry point from the state machine; orchestrates the otherfunctions.
843 */
844
845static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
846{
847
XiaokangQian08037552022-04-20 07:16:41 +0000848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +0000849 unsigned char* buf = NULL;
850 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +0800851 int parse_client_hello_ret;
852
XiaokangQianed582dd2022-04-13 08:21:05 +0000853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
854
XiaokangQianed582dd2022-04-13 08:21:05 +0000855 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
856 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
857 &buf, &buflen ) );
858
859 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
860 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +0800861 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
862 * only SSL_CLIENT_HELLO_OK or
863 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
864 * stage as negative error codes are handled
865 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +0800866
XiaokangQiancfd925f2022-04-14 07:10:37 +0000867 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +0800868
Jerry Yu4ca91402022-05-09 15:50:57 +0800869 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
870 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
871 else
872 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +0000873
874cleanup:
875
876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
877 return( ret );
878}
879
880/*
Jerry Yu1c3e6882022-04-20 21:23:40 +0800881 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +0800882 */
Jerry Yuf4b27e42022-03-30 17:32:21 +0800883static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
884{
Jerry Yu637a3f12022-04-20 21:37:58 +0800885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
886 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +0800887 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +0800888 if( ssl->conf->f_rng == NULL )
889 {
890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
891 return( MBEDTLS_ERR_SSL_NO_RNG );
892 }
893
Jerry Yu637a3f12022-04-20 21:37:58 +0800894 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800895 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
896 {
897 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
898 return( ret );
899 }
900
Jerry Yu637a3f12022-04-20 21:37:58 +0800901 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800902 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
903
904#if defined(MBEDTLS_HAVE_TIME)
905 ssl->session_negotiate->start = time( NULL );
906#endif /* MBEDTLS_HAVE_TIME */
907
908 return( ret );
909}
910
Jerry Yu3bf2c642022-03-30 22:02:12 +0800911/*
Jerry Yue74e04a2022-04-21 09:23:16 +0800912 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +0800913 *
914 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +0800915 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800916 * } SupportedVersions;
917 */
Jerry Yue74e04a2022-04-21 09:23:16 +0800918static int ssl_tls13_write_server_hello_supported_versions_ext(
919 mbedtls_ssl_context *ssl,
920 unsigned char *buf,
921 unsigned char *end,
922 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800923{
Jerry Yu3bf2c642022-03-30 22:02:12 +0800924 *out_len = 0;
925
Jerry Yu955ddd72022-04-22 22:27:33 +0800926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800927
928 /* Check if we have space to write the extension:
929 * - extension_type (2 bytes)
930 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +0800931 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800932 */
Jerry Yu349a6132022-04-14 20:52:56 +0800933 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800934
Jerry Yu349a6132022-04-14 20:52:56 +0800935 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800936
Jerry Yu349a6132022-04-14 20:52:56 +0800937 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800938
Jerry Yu349a6132022-04-14 20:52:56 +0800939 mbedtls_ssl_write_version( buf + 4,
940 ssl->conf->transport,
941 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800942
943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
944 ssl->tls_version ) );
945
Jerry Yu349a6132022-04-14 20:52:56 +0800946 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800947
948 return( 0 );
949}
950
Jerry Yud9436a12022-04-20 22:28:09 +0800951
Jerry Yu3bf2c642022-03-30 22:02:12 +0800952
953/* Generate and export a single key share. For hybrid KEMs, this can
954 * be called multiple times with the different components of the hybrid. */
Jerry Yu955ddd72022-04-22 22:27:33 +0800955static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
956 uint16_t named_group,
957 unsigned char *buf,
958 unsigned char *end,
959 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800960{
961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +0800962
963 *out_len = 0;
964
Jerry Yu89e103c2022-03-30 22:43:29 +0800965#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800966 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
967 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800968 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
969 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800970 if( ret != 0 )
971 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800972 MBEDTLS_SSL_DEBUG_RET(
973 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
974 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800975 return( ret );
976 }
Jerry Yu3bf2c642022-03-30 22:02:12 +0800977 }
Jerry Yu89e103c2022-03-30 22:43:29 +0800978 else
979#endif /* MBEDTLS_ECDH_C */
980 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800981 {
982 }
983 else
984 {
Jerry Yu955ddd72022-04-22 22:27:33 +0800985 ((void) ssl);
986 ((void) named_group);
987 ((void) buf);
988 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +0800989 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
990 }
991
992 return( ret );
993}
994
995/*
996 * ssl_tls13_write_key_share_ext
997 *
998 * Structure of key_share extension in ServerHello:
999 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001000 * struct {
1001 * NamedGroup group;
1002 * opaque key_exchange<1..2^16-1>;
1003 * } KeyShareEntry;
1004 * struct {
1005 * KeyShareEntry server_share;
1006 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001007 */
1008static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
1009 unsigned char *buf,
1010 unsigned char *end,
1011 size_t *out_len )
1012{
Jerry Yu955ddd72022-04-22 22:27:33 +08001013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001014 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001015 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001016 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001017 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001018
1019 *out_len = 0;
1020
1021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1022
1023 /* Check if we have space for header and length fields:
1024 * - extension_type (2 bytes)
1025 * - extension_data_length (2 bytes)
1026 * - group (2 bytes)
1027 * - key_exchange_length (2 bytes)
1028 */
1029 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001030 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1031 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001032 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001033
Jerry Yu3bf2c642022-03-30 22:02:12 +08001034 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1035 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001036 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001037 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001038 if( ret != 0 )
1039 return( ret );
1040 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001041
Jerry Yu955ddd72022-04-22 22:27:33 +08001042 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001043
Jerry Yu57d48412022-04-20 21:50:42 +08001044 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001045
Jerry Yu57d48412022-04-20 21:50:42 +08001046 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001047
Jerry Yu3bf2c642022-03-30 22:02:12 +08001048 return( 0 );
1049}
Jerry Yud9436a12022-04-20 22:28:09 +08001050
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001051static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1052 unsigned char *buf,
1053 unsigned char *end,
1054 size_t *out_len )
1055{
1056 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1057 /* key_share Extension
1058 *
1059 * struct {
1060 * select (Handshake.msg_type) {
1061 * ...
1062 * case hello_retry_request:
1063 * NamedGroup selected_group;
1064 * ...
1065 * };
1066 * } KeyShare;
1067 */
1068
1069 *out_len = 0;
1070
Jerry Yub0ac10b2022-05-05 11:10:08 +08001071 /*
1072 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1073 * of the HRR is then to transmit a cookie to force the client to demonstrate
1074 * reachability at their apparent network address (primarily useful for DTLS).
1075 */
1076 if( ! mbedtls_ssl_tls13_some_ephemeral_enabled( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001077 return( 0 );
1078
1079 /* We should only send the key_share extension if the client's initial
1080 * key share was not acceptable. */
1081 if( ssl->handshake->offered_group_id != 0 )
1082 {
1083 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1084 return( 0 );
1085 }
1086
1087 if( selected_group == 0 )
1088 {
1089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1090 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1091 }
1092
Jerry Yub0ac10b2022-05-05 11:10:08 +08001093 /* Check if we have enough space:
1094 * - extension_type (2 bytes)
1095 * - extension_data_length (2 bytes)
1096 * - selected_group (2 bytes)
1097 */
Ronald Cron154d1b62022-06-01 15:33:26 +02001098 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001099
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001100 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001101 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001102 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1103
1104 MBEDTLS_SSL_DEBUG_MSG( 3,
1105 ( "HRR selected_group: %s (%x)",
1106 mbedtls_ssl_named_group_to_str( selected_group ),
1107 selected_group ) );
1108
1109 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001110
Jerry Yub0ac10b2022-05-05 11:10:08 +08001111 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001112}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001113
1114/*
1115 * Structure of ServerHello message:
1116 *
1117 * struct {
1118 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1119 * Random random;
1120 * opaque legacy_session_id_echo<0..32>;
1121 * CipherSuite cipher_suite;
1122 * uint8 legacy_compression_method = 0;
1123 * Extension extensions<6..2^16-1>;
1124 * } ServerHello;
1125 */
1126static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001127 unsigned char *buf,
1128 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001129 size_t *out_len,
1130 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001131{
Jerry Yu955ddd72022-04-22 22:27:33 +08001132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001133 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001134 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001135 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001136
1137 *out_len = 0;
1138
Jerry Yucfc04b32022-04-21 09:31:58 +08001139 /* ...
1140 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1141 * ...
1142 * with ProtocolVersion defined as:
1143 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001144 */
1145 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1146 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1147 p += 2;
1148
Jerry Yu1c3e6882022-04-20 21:23:40 +08001149 /* ...
1150 * Random random;
1151 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001152 * with Random defined as:
1153 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001154 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001155 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001156 if( is_hrr )
1157 {
1158 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001159 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001160 }
1161 else
1162 {
1163 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001164 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001165 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001166 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001167 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1168 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1169
Jerry Yucfc04b32022-04-21 09:31:58 +08001170 /* ...
1171 * opaque legacy_session_id_echo<0..32>;
1172 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001173 */
1174 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1175 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1176 if( ssl->session_negotiate->id_len > 0 )
1177 {
1178 memcpy( p, &ssl->session_negotiate->id[0],
1179 ssl->session_negotiate->id_len );
1180 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001181
Jerry Yu3bf2c642022-03-30 22:02:12 +08001182 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1183 ssl->session_negotiate->id_len );
1184 }
1185
Jerry Yucfc04b32022-04-21 09:31:58 +08001186 /* ...
1187 * CipherSuite cipher_suite;
1188 * ...
1189 * with CipherSuite defined as:
1190 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001191 */
1192 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1193 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1194 p += 2;
1195 MBEDTLS_SSL_DEBUG_MSG( 3,
1196 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1197 mbedtls_ssl_get_ciphersuite_name(
1198 ssl->session_negotiate->ciphersuite ),
1199 ssl->session_negotiate->ciphersuite ) );
1200
Jerry Yucfc04b32022-04-21 09:31:58 +08001201 /* ...
1202 * uint8 legacy_compression_method = 0;
1203 * ...
1204 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001205 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1206 *p++ = 0x0;
1207
Jerry Yucfc04b32022-04-21 09:31:58 +08001208 /* ...
1209 * Extension extensions<6..2^16-1>;
1210 * ...
1211 * struct {
1212 * ExtensionType extension_type; (2 bytes)
1213 * opaque extension_data<0..2^16-1>;
1214 * } Extension;
1215 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001216 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001217 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001218 p += 2;
1219
Jerry Yue74e04a2022-04-21 09:23:16 +08001220 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001221 ssl, p, end, &output_len ) ) != 0 )
1222 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001223 MBEDTLS_SSL_DEBUG_RET(
1224 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001225 return( ret );
1226 }
1227 p += output_len;
1228
Jerry Yu3bf2c642022-03-30 22:02:12 +08001229 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1230 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001231 if( is_hrr )
1232 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1233 else
1234 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001235 if( ret != 0 )
1236 return( ret );
1237 p += output_len;
1238 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001239
Jerry Yud9436a12022-04-20 22:28:09 +08001240 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001241
Jerry Yud9436a12022-04-20 22:28:09 +08001242 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1243 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001244
Jerry Yud9436a12022-04-20 22:28:09 +08001245 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001246
Jerry Yud9436a12022-04-20 22:28:09 +08001247 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001248
1249 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001250}
1251
Jerry Yue110d252022-05-05 10:19:22 +08001252static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1253{
1254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001255 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001256 if( ret != 0 )
1257 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001258 MBEDTLS_SSL_DEBUG_RET( 1,
1259 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001260 ret );
1261 return( ret );
1262 }
1263
1264 mbedtls_ssl_set_outbound_transform( ssl,
1265 ssl->handshake->transform_handshake );
1266 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf86eb752022-05-06 11:16:55 +08001267 3, ( "switching to handshake transform for outbound data" ) );
Jerry Yue110d252022-05-05 10:19:22 +08001268
1269 return( ret );
1270}
1271
Jerry Yu5b64ae92022-03-30 17:15:02 +08001272static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1273{
Jerry Yu637a3f12022-04-20 21:37:58 +08001274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001275 unsigned char *buf;
1276 size_t buf_len, msg_len;
1277
1278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1279
Jerry Yuf4b27e42022-03-30 17:32:21 +08001280 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1281
Jerry Yu3bf2c642022-03-30 22:02:12 +08001282 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001283 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1284
Jerry Yu3bf2c642022-03-30 22:02:12 +08001285 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1286 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001287 &msg_len,
1288 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001289
Jerry Yu3bf2c642022-03-30 22:02:12 +08001290 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001291 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1292
Jerry Yu3bf2c642022-03-30 22:02:12 +08001293 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001294 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001295
Jerry Yue110d252022-05-05 10:19:22 +08001296 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1297
Jerry Yu637a3f12022-04-20 21:37:58 +08001298 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Jerry Yue110d252022-05-05 10:19:22 +08001299
Jerry Yuf4b27e42022-03-30 17:32:21 +08001300cleanup:
1301
1302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1303 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001304}
1305
Jerry Yu23d1a252022-05-12 18:08:59 +08001306
1307/*
1308 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1309 */
1310static int ssl_tls13_write_hello_retry_request_coordinate(
1311 mbedtls_ssl_context *ssl )
1312{
1313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1314 if( ssl->handshake->hello_retry_request_count > 0 )
1315 {
1316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1317 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1318 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1319 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1320 }
1321
1322 /*
1323 * Create stateless transcript hash for HRR
1324 */
1325 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1326 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1327 if( ret != 0 )
1328 {
1329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1330 return( ret );
1331 }
1332 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1333
1334 return( 0 );
1335}
1336
1337static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1338{
1339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1340 unsigned char *buf;
1341 size_t buf_len, msg_len;
1342
1343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1344
1345 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_hello_retry_request_coordinate( ssl ) );
1346
1347 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1348 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1349 &buf, &buf_len ) );
1350
1351 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1352 buf + buf_len,
1353 &msg_len,
1354 1 ) );
1355 mbedtls_ssl_add_hs_msg_to_checksum(
1356 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1357
1358
1359 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1360 msg_len ) );
1361
1362 ssl->handshake->hello_retry_request_count++;
1363
1364 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1365
1366cleanup:
1367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1368 return( ret );
1369}
1370
Jerry Yu5b64ae92022-03-30 17:15:02 +08001371/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001372 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1373 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001374
1375/*
1376 * struct {
1377 * Extension extensions<0..2 ^ 16 - 1>;
1378 * } EncryptedExtensions;
1379 *
1380 */
1381static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1382 unsigned char *buf,
1383 unsigned char *end,
1384 size_t *out_len )
1385{
XiaokangQianacb39922022-06-17 10:18:48 +00001386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001387 unsigned char *p = buf;
1388 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001389 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001390 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001391
Jerry Yu4d3841a2022-04-16 12:37:19 +08001392 *out_len = 0;
1393
1394 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001395 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001396 p += 2;
1397
Jerry Yu8937eb42022-05-03 12:12:14 +08001398 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00001399 ((void) ret);
1400 ((void) output_len);
1401
1402#if defined(MBEDTLS_SSL_ALPN)
1403 ret = mbedtls_ssl_write_alpn_ext( ssl, p, end, &output_len );
1404 if( ret != 0 )
1405 return( ret );
XiaokangQian95d5f542022-06-24 02:29:26 +00001406 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001407#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001408
Jerry Yu8937eb42022-05-03 12:12:14 +08001409 extensions_len = ( p - p_extensions_len ) - 2;
1410 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
1411
1412 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001413
1414 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
1415
1416 return( 0 );
1417}
1418
1419static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
1420{
1421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1422 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08001423 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001424
Jerry Yuab452cc2022-04-28 15:27:08 +08001425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001426
Jerry Yu4d3841a2022-04-16 12:37:19 +08001427 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1428 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
1429
1430 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
1431 ssl, buf, buf + buf_len, &msg_len ) );
1432
1433 mbedtls_ssl_add_hs_msg_to_checksum(
1434 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
1435
Jerry Yu4d3841a2022-04-16 12:37:19 +08001436 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1437 ssl, buf_len, msg_len ) );
1438
Jerry Yu8937eb42022-05-03 12:12:14 +08001439#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1440 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1441 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1442 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00001443 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08001444#else
1445 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1446#endif
1447
Jerry Yu4d3841a2022-04-16 12:37:19 +08001448cleanup:
1449
Jerry Yuab452cc2022-04-28 15:27:08 +08001450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001451 return( ret );
1452}
1453
XiaokangQiancec9ae62022-05-06 07:28:50 +00001454#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00001455#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
1456#define SSL_CERTIFICATE_REQUEST_SKIP 1
1457/* Coordination:
1458 * Check whether a CertificateRequest message should be written.
1459 * Returns a negative code on failure, or
1460 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
1461 * - SSL_CERTIFICATE_REQUEST_SKIP
1462 * indicating if the writing of the CertificateRequest
1463 * should be skipped or not.
1464 */
1465static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
1466{
1467 int authmode;
1468
XiaokangQian40a35232022-05-07 09:02:40 +00001469#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1470 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
1471 authmode = ssl->handshake->sni_authmode;
1472 else
1473#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00001474 authmode = ssl->conf->authmode;
1475
1476 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
1477 return( SSL_CERTIFICATE_REQUEST_SKIP );
1478
XiaokangQianc3017f62022-05-13 05:55:41 +00001479 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00001480
XiaokangQiana987e1d2022-05-07 01:25:58 +00001481 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
1482}
1483
XiaokangQiancec9ae62022-05-06 07:28:50 +00001484/*
1485 * struct {
1486 * opaque certificate_request_context<0..2^8-1>;
1487 * Extension extensions<2..2^16-1>;
1488 * } CertificateRequest;
1489 *
1490 */
1491static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
1492 unsigned char *buf,
1493 const unsigned char *end,
1494 size_t *out_len )
1495{
1496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1497 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00001498 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00001499 unsigned char *p_extensions_len;
1500
1501 *out_len = 0;
1502
1503 /* Check if we have enough space:
1504 * - certificate_request_context (1 byte)
1505 * - extensions length (2 bytes)
1506 */
1507 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
1508
1509 /*
1510 * Write certificate_request_context
1511 */
1512 /*
1513 * We use a zero length context for the normal handshake
1514 * messages. For post-authentication handshake messages
1515 * this request context would be set to a non-zero value.
1516 */
1517 *p++ = 0x0;
1518
1519 /*
1520 * Write extensions
1521 */
1522 /* The extensions must contain the signature_algorithms. */
1523 p_extensions_len = p;
1524 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00001525 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001526 if( ret != 0 )
1527 return( ret );
1528
XiaokangQianec6efb92022-05-06 09:53:10 +00001529 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00001530 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001531
1532 *out_len = p - buf;
1533
1534 return( 0 );
1535}
1536
1537static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
1538{
1539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1540
1541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1542
1543 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1544
1545 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
1546 {
1547 unsigned char *buf;
1548 size_t buf_len, msg_len;
1549
1550 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1551 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
1552
1553 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
1554 ssl, buf, buf + buf_len, &msg_len ) );
1555
1556 mbedtls_ssl_add_hs_msg_to_checksum(
1557 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
1558
1559 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1560 ssl, buf_len, msg_len ) );
1561 }
1562 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
1563 {
1564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1565 ret = 0;
1566 }
1567 else
1568 {
1569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1570 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1571 goto cleanup;
1572 }
1573
1574 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1575cleanup:
1576
1577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1578 return( ret );
1579}
XiaokangQiancec9ae62022-05-06 07:28:50 +00001580
Jerry Yu4d3841a2022-04-16 12:37:19 +08001581/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001582 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08001583 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001584static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001585{
Jerry Yu5a26f302022-05-10 20:46:40 +08001586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00001587
1588#if defined(MBEDTLS_X509_CRT_PARSE_C)
1589 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
1590 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08001591 {
Jerry Yub89125b2022-05-13 15:45:49 +08001592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08001593 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1594 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001595 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08001596 }
XiaokangQianfb665a82022-06-15 03:57:21 +00001597#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08001598
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001599 ret = mbedtls_ssl_tls13_write_certificate( ssl );
1600 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08001601 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08001602 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1603 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001604}
1605
1606/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001607 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08001608 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001609static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001610{
Jerry Yu4ff9e142022-04-16 14:57:49 +08001611 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001612 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08001613 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08001614 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1615 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001616}
Jerry Yu5a26f302022-05-10 20:46:40 +08001617#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001618
1619/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001620 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001621 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001622static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001623{
Jerry Yud6e253d2022-05-18 13:59:24 +08001624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001625
1626 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1627 if( ret != 0 )
1628 return( ret );
1629
Jerry Yue3d67cb2022-05-19 15:33:10 +08001630 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1631 if( ret != 0 )
1632 {
1633 MBEDTLS_SSL_PEND_FATAL_ALERT(
1634 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1635 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1636 return( ret );
1637 }
XiaokangQianc3017f62022-05-13 05:55:41 +00001638
1639 if( ssl->handshake->certificate_request_sent )
XiaokangQian189ded22022-05-10 08:12:17 +00001640 {
1641 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
1642
1643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1644 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
1645 }
1646 else
Ronald Cron19385882022-06-15 16:26:13 +02001647 {
1648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate" ) );
1649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
XiaokangQian189ded22022-05-10 08:12:17 +00001650 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001651 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001652 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001653}
1654
1655/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001656 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001657 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001658static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001659{
Jerry Yud6e253d2022-05-18 13:59:24 +08001660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1661
XiaokangQianc3017f62022-05-13 05:55:41 +00001662 if( ! ssl->handshake->certificate_request_sent )
XiaokangQianaca90482022-05-19 07:19:31 +00001663 {
1664 MBEDTLS_SSL_DEBUG_MSG( 1,
1665 ( "Switch to handshake traffic keys for inbound traffic" ) );
XiaokangQianc3017f62022-05-13 05:55:41 +00001666 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQianaca90482022-05-19 07:19:31 +00001667 }
Jerry Yuff226982022-04-16 16:52:57 +08001668 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
1669 if( ret != 0 )
1670 return( ret );
1671
Jerry Yue3d67cb2022-05-19 15:33:10 +08001672 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1673 if( ret != 0 )
1674 {
1675 MBEDTLS_SSL_DEBUG_RET( 1,
1676 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1677 }
1678
Jerry Yu03ed50b2022-04-16 17:13:30 +08001679 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08001680 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001681}
1682
1683/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001684 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08001685 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001686static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001687{
Jerry Yu03ed50b2022-04-16 17:13:30 +08001688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1689
Jerry Yu03ed50b2022-04-16 17:13:30 +08001690 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1691 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1692 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001693}
1694
1695/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001696 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697 */
Jerry Yu27561932021-08-27 17:07:38 +08001698int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08001699{
XiaokangQian08037552022-04-20 07:16:41 +00001700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001701
1702 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
1703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1704
Jerry Yue3b34122021-09-28 17:53:35 +08001705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
1706 mbedtls_ssl_states_str( ssl->state ),
1707 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08001708
XiaokangQian7807f9f2022-02-15 10:04:37 +00001709 switch( ssl->state )
1710 {
1711 /* start state */
1712 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001713 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00001714 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001715 break;
1716
XiaokangQian7807f9f2022-02-15 10:04:37 +00001717 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00001718 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001719 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00001720 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08001721 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001722
Jerry Yuf41553b2022-05-09 22:20:30 +08001723 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
1724 ret = ssl_tls13_write_hello_retry_request( ssl );
1725 if( ret != 0 )
1726 {
1727 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
1728 return( ret );
1729 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001730 break;
1731
Jerry Yu5b64ae92022-03-30 17:15:02 +08001732 case MBEDTLS_SSL_SERVER_HELLO:
1733 ret = ssl_tls13_write_server_hello( ssl );
1734 break;
1735
Xiaofei Baicba64af2022-02-15 10:00:56 +00001736 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08001737 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001738 if( ret != 0 )
1739 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08001740 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
1741 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001742 }
Jerry Yu48330562022-05-06 21:35:44 +08001743 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08001744
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001745#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1746 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00001747 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001748 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001749
Jerry Yu83da34e2022-04-16 13:59:52 +08001750 case MBEDTLS_SSL_SERVER_CERTIFICATE:
1751 ret = ssl_tls13_write_server_certificate( ssl );
1752 break;
1753
1754 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
1755 ret = ssl_tls13_write_certificate_verify( ssl );
1756 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08001757#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001758
Jerry Yu69dd8d42022-04-16 12:51:26 +08001759 case MBEDTLS_SSL_SERVER_FINISHED:
1760 ret = ssl_tls13_write_server_finished( ssl );
1761 break;
1762
1763 case MBEDTLS_SSL_CLIENT_FINISHED:
1764 ret = ssl_tls13_process_client_finished( ssl );
1765 break;
1766
Jerry Yu69dd8d42022-04-16 12:51:26 +08001767 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
1768 ret = ssl_tls13_handshake_wrapup( ssl );
1769 break;
1770
XiaokangQian6b916b12022-04-25 07:29:34 +00001771 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1772 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02001773 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00001774 {
Ronald Cron209cae92022-06-07 10:30:19 +02001775 if( ssl->session_negotiate->peer_cert != NULL )
1776 {
1777 mbedtls_ssl_handshake_set_state(
1778 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1779 }
1780 else
Ronald Cron19385882022-06-15 16:26:13 +02001781 {
1782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
Ronald Cron209cae92022-06-07 10:30:19 +02001783 mbedtls_ssl_handshake_set_state(
1784 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001785 }
XiaokangQian6b916b12022-04-25 07:29:34 +00001786 }
1787 break;
1788
1789 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1790 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1791 if( ret == 0 )
1792 {
1793 mbedtls_ssl_handshake_set_state(
1794 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1795 }
1796 break;
1797
XiaokangQian7807f9f2022-02-15 10:04:37 +00001798 default:
1799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00001800 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001801 }
1802
1803 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08001804}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001805
Jerry Yufb4b6472022-01-27 15:03:26 +08001806#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */