blob: 2d751d65646563ab410b10a691bc95cd6a1c8c55 [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;
XiaokangQian23c5be62022-06-07 02:04:34 +0000355
356#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
357 if( ssl->handshake->sni_key_cert != NULL )
XiaokangQianfb665a82022-06-15 03:57:21 +0000358 key_cert_list = ssl->handshake->sni_key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000359 else
360#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQianfb665a82022-06-15 03:57:21 +0000361 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +0000362
XiaokangQianfb665a82022-06-15 03:57:21 +0000363 if( key_cert_list == NULL )
XiaokangQian23c5be62022-06-07 02:04:34 +0000364 {
365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
366 return( -1 );
367 }
368
XiaokangQian81802f42022-06-10 13:25:22 +0000369 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
XiaokangQian23c5be62022-06-07 02:04:34 +0000370 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000371 for( key_cert = key_cert_list; key_cert != NULL;
372 key_cert = key_cert->next )
XiaokangQian23c5be62022-06-07 02:04:34 +0000373 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000374 MBEDTLS_SSL_DEBUG_CRT( 3, "certificate (chain) candidate",
375 key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000376
377 /*
378 * This avoids sending the client a cert it'll reject based on
379 * keyUsage or other extensions.
380 */
381 if( mbedtls_x509_crt_check_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000382 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +0000383 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +0000384 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
385 MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ) ) != 0 )
XiaokangQian81802f42022-06-10 13:25:22 +0000386 {
387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
XiaokangQianfb665a82022-06-15 03:57:21 +0000388 "(extended) key usage extension" ) );
XiaokangQian81802f42022-06-10 13:25:22 +0000389 continue;
390 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000391
Jerry Yucc539102022-06-27 16:27:35 +0800392 MBEDTLS_SSL_DEBUG_MSG( 3,
393 ( "ssl_tls13_pick_key_cert:"
394 "check signature algorithm %s [%04x]",
395 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
396 *sig_alg ) );
Jerry Yufb526692022-06-19 11:22:49 +0800397 if( mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
Jerry Yud099cf02022-06-19 13:47:00 +0800398 *sig_alg, &key_cert->cert->pk ) )
XiaokangQian81802f42022-06-10 13:25:22 +0000399 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000400 ssl->handshake->key_cert = key_cert;
Jerry Yucc539102022-06-27 16:27:35 +0800401 MBEDTLS_SSL_DEBUG_MSG( 3,
402 ( "ssl_tls13_pick_key_cert:"
403 "selected signature algorithm"
404 " %s [%04x]",
405 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
406 *sig_alg ) );
XiaokangQianfb665a82022-06-15 03:57:21 +0000407 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000408 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000409 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000410 return( 0 );
411 }
XiaokangQian81802f42022-06-10 13:25:22 +0000412 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000413 }
414
Jerry Yuf55886a2022-06-19 11:48:56 +0800415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ssl_tls13_pick_key_cert: "
Jerry Yucc539102022-06-27 16:27:35 +0800416 "no suitable signature algorithm found" ) );
XiaokangQian23c5be62022-06-07 02:04:34 +0000417 return( -1 );
418}
XiaokangQian81802f42022-06-10 13:25:22 +0000419#endif /* MBEDTLS_X509_CRT_PARSE_C &&
420 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000421
XiaokangQian4080a7f2022-04-11 09:55:18 +0000422/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000423 *
424 * STATE HANDLING: ClientHello
425 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000426 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000427 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000428 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000429 *
430 * In this case, the server progresses to sending its ServerHello.
431 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000432 * 2) The ClientHello was well-formed but didn't match the server's
433 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000434 *
435 * For example, the client might not have offered a key share which
436 * the server supports, or the server might require a cookie.
437 *
438 * In this case, the server sends a HelloRetryRequest.
439 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000440 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000441 *
442 * In this case, we abort the handshake.
443 *
444 */
445
446/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000447 * Structure of this message:
448 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000449 * uint16 ProtocolVersion;
450 * opaque Random[32];
451 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000452 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000453 * struct {
454 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
455 * Random random;
456 * opaque legacy_session_id<0..32>;
457 * CipherSuite cipher_suites<2..2^16-2>;
458 * opaque legacy_compression_methods<1..2^8-1>;
459 * Extension extensions<8..2^16-1>;
460 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000461 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000462
463#define SSL_CLIENT_HELLO_OK 0
464#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
465
XiaokangQian4080a7f2022-04-11 09:55:18 +0000466static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
467 const unsigned char *buf,
468 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000469{
XiaokangQianb67384d2022-04-19 00:02:38 +0000470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
471 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000472 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000473 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000474 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000475 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000476 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000477 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800478 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000479
XiaokangQian7807f9f2022-02-15 10:04:37 +0000480 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000481
482 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
483
484 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000485 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000486 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000487 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000488 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000489 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000490 * .. . .. ciphersuite list length ( 2 bytes )
491 * .. . .. ciphersuite list
492 * .. . .. compression alg. list length ( 1 byte )
493 * .. . .. compression alg. list
494 * .. . .. extensions length ( 2 bytes, optional )
495 * .. . .. extensions ( optional )
496 */
497
XiaokangQianb67384d2022-04-19 00:02:38 +0000498 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400499 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000500 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
501 * read at least up to session id length without worrying.
502 */
503 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
504
505 /* ...
506 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
507 * ...
508 * with ProtocolVersion defined as:
509 * uint16 ProtocolVersion;
510 */
XiaokangQiande333912022-04-20 08:49:42 +0000511 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
512 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000513 {
514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
515 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
516 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000517 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000518 }
519 p += 2;
520
521 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000522 * Only support TLS 1.3 currently, temporarily set the version.
523 */
XiaokangQiande333912022-04-20 08:49:42 +0000524 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000525
Jerry Yuc1be19f2022-04-23 16:11:39 +0800526 /* ...
527 * Random random;
528 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000529 * with Random defined as:
530 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000531 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000532 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +0000533 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000534
XiaokangQian08037552022-04-20 07:16:41 +0000535 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
536 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000537
Jerry Yuc1be19f2022-04-23 16:11:39 +0800538 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000539 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800540 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000541 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000542 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000543 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000544
XiaokangQianb67384d2022-04-19 00:02:38 +0000545 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000546 {
547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
548 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
549 }
550
XiaokangQian4080a7f2022-04-11 09:55:18 +0000551 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000552 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +0000553 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000554 /*
555 * Check we have enough data for the legacy session identifier
556 * and the ciphersuite list length.
557 */
558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000559
XiaokangQianed582dd2022-04-13 08:21:05 +0000560 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000561 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000562
XiaokangQian7807f9f2022-02-15 10:04:37 +0000563 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
564 p += 2;
565
XiaokangQianb67384d2022-04-19 00:02:38 +0000566 /* Check we have enough data for the ciphersuite list, the legacy
567 * compression methods and the length of the extensions.
568 */
569 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000570
Jerry Yuc1be19f2022-04-23 16:11:39 +0800571 /* ...
XiaokangQian08037552022-04-20 07:16:41 +0000572 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800573 * ...
XiaokangQian08037552022-04-20 07:16:41 +0000574 * with CipherSuite defined as:
575 * uint8 CipherSuite[2];
576 */
XiaokangQian060d8672022-04-21 09:24:56 +0000577 cipher_suites = p;
578 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000579 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
580 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +0000581 /*
582 * Search for a matching ciphersuite
583 */
XiaokangQian318dc762022-04-20 09:43:51 +0000584 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +0000585 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +0000586 {
XiaokangQiane8ff3502022-04-22 02:34:40 +0000587 uint16_t cipher_suite;
588 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
589 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
590 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +0000591 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +0000592 * Check whether this ciphersuite is valid and offered.
593 */
XiaokangQian08037552022-04-20 07:16:41 +0000594 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800595 ssl, ciphersuite_info, ssl->tls_version,
596 ssl->tls_version ) != 0 ) ||
597 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
598 {
XiaokangQian08037552022-04-20 07:16:41 +0000599 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800600 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000601
XiaokangQian08037552022-04-20 07:16:41 +0000602 ssl->session_negotiate->ciphersuite = cipher_suite;
603 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +0000604 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +0000605
XiaokangQian08037552022-04-20 07:16:41 +0000606 break;
XiaokangQian17f974c2022-04-19 09:57:41 +0000607
XiaokangQian17f974c2022-04-19 09:57:41 +0000608 }
609
Jerry Yuc1be19f2022-04-23 16:11:39 +0800610 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +0000611 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000612 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
613 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
614 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +0000615 }
XiaokangQian17f974c2022-04-19 09:57:41 +0000616
617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
618 ciphersuite_info->name ) );
619
XiaokangQian060d8672022-04-21 09:24:56 +0000620 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800621
XiaokangQian4080a7f2022-04-11 09:55:18 +0000622 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000623 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000624 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000625 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000626 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000627 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
629 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
630 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
631 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000632 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000633 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000634
Jerry Yuc1be19f2022-04-23 16:11:39 +0800635 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000636 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800637 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000638 * with Extension defined as:
639 * struct {
640 * ExtensionType extension_type;
641 * opaque extension_data<0..2^16-1>;
642 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000643 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000644 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000645 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000646 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +0000647 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000648
XiaokangQian4080a7f2022-04-11 09:55:18 +0000649 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000650
651 while( p < extensions_end )
652 {
653 unsigned int extension_type;
654 size_t extension_data_len;
655 const unsigned char *extension_data_end;
656
XiaokangQian318dc762022-04-20 09:43:51 +0000657 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000658 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
659 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
660 p += 4;
661
662 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
663 extension_data_end = p + extension_data_len;
664
665 switch( extension_type )
666 {
XiaokangQian40a35232022-05-07 09:02:40 +0000667#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
668 case MBEDTLS_TLS_EXT_SERVERNAME:
669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +0000670 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
671 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +0000672 if( ret != 0 )
673 {
674 MBEDTLS_SSL_DEBUG_RET(
675 1, "mbedtls_ssl_parse_servername_ext", ret );
676 return( ret );
677 }
678 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
679 break;
680#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
681
XiaokangQianb67384d2022-04-19 00:02:38 +0000682#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000683 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
685
686 /* Supported Groups Extension
687 *
688 * When sent by the client, the "supported_groups" extension
689 * indicates the named groups which the client supports,
690 * ordered from most preferred to least preferred.
691 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800692 ret = ssl_tls13_parse_supported_groups_ext(
693 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000694 if( ret != 0 )
695 {
696 MBEDTLS_SSL_DEBUG_RET( 1,
697 "mbedtls_ssl_parse_supported_groups_ext", ret );
698 return( ret );
699 }
700
701 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
702 break;
XiaokangQianb67384d2022-04-19 00:02:38 +0000703#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000704
XiaokangQian88408882022-04-02 10:15:03 +0000705#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000706 case MBEDTLS_TLS_EXT_KEY_SHARE:
707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
708
709 /*
710 * Key Share Extension
711 *
712 * When sent by the client, the "key_share" extension
713 * contains the endpoint's cryptographic parameters for
714 * ECDHE/DHE key establishment methods.
715 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800716 ret = ssl_tls13_parse_key_shares_ext(
717 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +0000718 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000719 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +0800721 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000722 }
723
Jerry Yu582dd062022-04-22 21:59:01 +0800724 if( ret < 0 )
725 {
726 MBEDTLS_SSL_DEBUG_RET(
727 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000728 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +0800729 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000730
731 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
732 break;
XiaokangQian88408882022-04-02 10:15:03 +0000733#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000734
735 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
737
738 ret = ssl_tls13_parse_supported_versions_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_RET( 1,
743 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
744 return( ret );
745 }
746 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
747 break;
748
XiaokangQianacb39922022-06-17 10:18:48 +0000749#if defined(MBEDTLS_SSL_ALPN)
750 case MBEDTLS_TLS_EXT_ALPN:
751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
752
753 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
754 if( ret != 0 )
755 {
756 MBEDTLS_SSL_DEBUG_RET(
757 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
758 return( ret );
759 }
760 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
761 break;
762#endif /* MBEDTLS_SSL_ALPN */
763
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
765 case MBEDTLS_TLS_EXT_SIG_ALG:
766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
767
Gabor Mezei078e8032022-04-27 21:17:56 +0200768 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800769 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000770 if( ret != 0 )
771 {
772 MBEDTLS_SSL_DEBUG_MSG( 1,
773 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
774 ret ) );
775 return( ret );
776 }
777 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
778 break;
779#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
780
781 default:
782 MBEDTLS_SSL_DEBUG_MSG( 3,
783 ( "unknown extension found: %ud ( ignoring )",
784 extension_type ) );
785 }
786
787 p += extension_data_len;
788 }
789
790 /* Update checksum with either
791 * - The entire content of the CH message, if no PSK extension is present
792 * - The content up to but excluding the PSK extension, if present.
793 */
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000794 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
XiaokangQianc4b8c992022-04-07 11:31:38 +0000795 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796
797 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000798#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000799 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000800#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000801
XiaokangQian75fe8c72022-06-15 09:42:45 +0000802 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
803}
804
805/* Update the handshake state machine */
806
807static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
808{
809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
810
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000812 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813 */
XiaokangQianb67384d2022-04-19 00:02:38 +0000814 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815 {
816 MBEDTLS_SSL_DEBUG_MSG(
817 1,
818 ( "ClientHello message misses mandatory extensions." ) );
819 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
820 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
821 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
822 }
823
XiaokangQianfb665a82022-06-15 03:57:21 +0000824 /*
825 * Server certificate selection
826 */
827 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
828 {
829 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
830 return( ret );
831 }
832#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
833 ssl->handshake->sni_name = NULL;
834 ssl->handshake->sni_name_len = 0;
835#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
838 if( ret != 0 )
839 {
840 MBEDTLS_SSL_DEBUG_RET( 1,
841 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
842 return( ret );
843 }
844
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 return( 0 );
846
847}
848
849/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000850 * Main entry point from the state machine; orchestrates the otherfunctions.
851 */
852
853static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
854{
855
XiaokangQian08037552022-04-20 07:16:41 +0000856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +0000857 unsigned char* buf = NULL;
858 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +0800859 int parse_client_hello_ret;
860
XiaokangQianed582dd2022-04-13 08:21:05 +0000861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
862
XiaokangQianed582dd2022-04-13 08:21:05 +0000863 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
864 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
865 &buf, &buflen ) );
866
867 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
868 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +0800869 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
870 * only SSL_CLIENT_HELLO_OK or
871 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
872 * stage as negative error codes are handled
873 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +0800874
XiaokangQiancfd925f2022-04-14 07:10:37 +0000875 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +0800876
Jerry Yu4ca91402022-05-09 15:50:57 +0800877 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
878 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
879 else
880 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +0000881
882cleanup:
883
884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
885 return( ret );
886}
887
888/*
Jerry Yu1c3e6882022-04-20 21:23:40 +0800889 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +0800890 */
Jerry Yuf4b27e42022-03-30 17:32:21 +0800891static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
892{
Jerry Yu637a3f12022-04-20 21:37:58 +0800893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
894 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +0800895 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +0800896 if( ssl->conf->f_rng == NULL )
897 {
898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
899 return( MBEDTLS_ERR_SSL_NO_RNG );
900 }
901
Jerry Yu637a3f12022-04-20 21:37:58 +0800902 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800903 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
904 {
905 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
906 return( ret );
907 }
908
Jerry Yu637a3f12022-04-20 21:37:58 +0800909 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800910 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
911
912#if defined(MBEDTLS_HAVE_TIME)
913 ssl->session_negotiate->start = time( NULL );
914#endif /* MBEDTLS_HAVE_TIME */
915
916 return( ret );
917}
918
Jerry Yu3bf2c642022-03-30 22:02:12 +0800919/*
Jerry Yue74e04a2022-04-21 09:23:16 +0800920 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +0800921 *
922 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +0800923 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800924 * } SupportedVersions;
925 */
Jerry Yue74e04a2022-04-21 09:23:16 +0800926static int ssl_tls13_write_server_hello_supported_versions_ext(
927 mbedtls_ssl_context *ssl,
928 unsigned char *buf,
929 unsigned char *end,
930 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800931{
Jerry Yu3bf2c642022-03-30 22:02:12 +0800932 *out_len = 0;
933
Jerry Yu955ddd72022-04-22 22:27:33 +0800934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800935
936 /* Check if we have space to write the extension:
937 * - extension_type (2 bytes)
938 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +0800939 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800940 */
Jerry Yu349a6132022-04-14 20:52:56 +0800941 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800942
Jerry Yu349a6132022-04-14 20:52:56 +0800943 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800944
Jerry Yu349a6132022-04-14 20:52:56 +0800945 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800946
Jerry Yu349a6132022-04-14 20:52:56 +0800947 mbedtls_ssl_write_version( buf + 4,
948 ssl->conf->transport,
949 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800950
951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
952 ssl->tls_version ) );
953
Jerry Yu349a6132022-04-14 20:52:56 +0800954 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800955
956 return( 0 );
957}
958
Jerry Yud9436a12022-04-20 22:28:09 +0800959
Jerry Yu3bf2c642022-03-30 22:02:12 +0800960
961/* Generate and export a single key share. For hybrid KEMs, this can
962 * be called multiple times with the different components of the hybrid. */
Jerry Yu955ddd72022-04-22 22:27:33 +0800963static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
964 uint16_t named_group,
965 unsigned char *buf,
966 unsigned char *end,
967 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800968{
969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +0800970
971 *out_len = 0;
972
Jerry Yu89e103c2022-03-30 22:43:29 +0800973#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800974 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
975 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800976 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
977 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800978 if( ret != 0 )
979 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800980 MBEDTLS_SSL_DEBUG_RET(
981 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
982 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800983 return( ret );
984 }
Jerry Yu3bf2c642022-03-30 22:02:12 +0800985 }
Jerry Yu89e103c2022-03-30 22:43:29 +0800986 else
987#endif /* MBEDTLS_ECDH_C */
988 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800989 {
990 }
991 else
992 {
Jerry Yu955ddd72022-04-22 22:27:33 +0800993 ((void) ssl);
994 ((void) named_group);
995 ((void) buf);
996 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +0800997 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
998 }
999
1000 return( ret );
1001}
1002
1003/*
1004 * ssl_tls13_write_key_share_ext
1005 *
1006 * Structure of key_share extension in ServerHello:
1007 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001008 * struct {
1009 * NamedGroup group;
1010 * opaque key_exchange<1..2^16-1>;
1011 * } KeyShareEntry;
1012 * struct {
1013 * KeyShareEntry server_share;
1014 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001015 */
1016static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
1017 unsigned char *buf,
1018 unsigned char *end,
1019 size_t *out_len )
1020{
Jerry Yu955ddd72022-04-22 22:27:33 +08001021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001022 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001023 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001024 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001025 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001026
1027 *out_len = 0;
1028
1029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1030
1031 /* Check if we have space for header and length fields:
1032 * - extension_type (2 bytes)
1033 * - extension_data_length (2 bytes)
1034 * - group (2 bytes)
1035 * - key_exchange_length (2 bytes)
1036 */
1037 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001038 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1039 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001040 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001041
Jerry Yu3bf2c642022-03-30 22:02:12 +08001042 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1043 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001044 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001045 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001046 if( ret != 0 )
1047 return( ret );
1048 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001049
Jerry Yu955ddd72022-04-22 22:27:33 +08001050 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001051
Jerry Yu57d48412022-04-20 21:50:42 +08001052 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001053
Jerry Yu57d48412022-04-20 21:50:42 +08001054 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001055
Jerry Yu3bf2c642022-03-30 22:02:12 +08001056 return( 0 );
1057}
Jerry Yud9436a12022-04-20 22:28:09 +08001058
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001059static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1060 unsigned char *buf,
1061 unsigned char *end,
1062 size_t *out_len )
1063{
1064 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1065 /* key_share Extension
1066 *
1067 * struct {
1068 * select (Handshake.msg_type) {
1069 * ...
1070 * case hello_retry_request:
1071 * NamedGroup selected_group;
1072 * ...
1073 * };
1074 * } KeyShare;
1075 */
1076
1077 *out_len = 0;
1078
Jerry Yub0ac10b2022-05-05 11:10:08 +08001079 /*
1080 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1081 * of the HRR is then to transmit a cookie to force the client to demonstrate
1082 * reachability at their apparent network address (primarily useful for DTLS).
1083 */
1084 if( ! mbedtls_ssl_tls13_some_ephemeral_enabled( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001085 return( 0 );
1086
1087 /* We should only send the key_share extension if the client's initial
1088 * key share was not acceptable. */
1089 if( ssl->handshake->offered_group_id != 0 )
1090 {
1091 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1092 return( 0 );
1093 }
1094
1095 if( selected_group == 0 )
1096 {
1097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1098 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1099 }
1100
Jerry Yub0ac10b2022-05-05 11:10:08 +08001101 /* Check if we have enough space:
1102 * - extension_type (2 bytes)
1103 * - extension_data_length (2 bytes)
1104 * - selected_group (2 bytes)
1105 */
Ronald Cron154d1b62022-06-01 15:33:26 +02001106 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001107
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001108 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001109 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001110 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1111
1112 MBEDTLS_SSL_DEBUG_MSG( 3,
1113 ( "HRR selected_group: %s (%x)",
1114 mbedtls_ssl_named_group_to_str( selected_group ),
1115 selected_group ) );
1116
1117 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001118
Jerry Yub0ac10b2022-05-05 11:10:08 +08001119 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001120}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001121
1122/*
1123 * Structure of ServerHello message:
1124 *
1125 * struct {
1126 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1127 * Random random;
1128 * opaque legacy_session_id_echo<0..32>;
1129 * CipherSuite cipher_suite;
1130 * uint8 legacy_compression_method = 0;
1131 * Extension extensions<6..2^16-1>;
1132 * } ServerHello;
1133 */
1134static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001135 unsigned char *buf,
1136 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001137 size_t *out_len,
1138 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001139{
Jerry Yu955ddd72022-04-22 22:27:33 +08001140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001141 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001142 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001143 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001144
1145 *out_len = 0;
1146
Jerry Yucfc04b32022-04-21 09:31:58 +08001147 /* ...
1148 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1149 * ...
1150 * with ProtocolVersion defined as:
1151 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001152 */
1153 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1154 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1155 p += 2;
1156
Jerry Yu1c3e6882022-04-20 21:23:40 +08001157 /* ...
1158 * Random random;
1159 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001160 * with Random defined as:
1161 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001162 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001163 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001164 if( is_hrr )
1165 {
1166 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001167 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001168 }
1169 else
1170 {
1171 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001172 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001173 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001174 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001175 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1176 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1177
Jerry Yucfc04b32022-04-21 09:31:58 +08001178 /* ...
1179 * opaque legacy_session_id_echo<0..32>;
1180 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001181 */
1182 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1183 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1184 if( ssl->session_negotiate->id_len > 0 )
1185 {
1186 memcpy( p, &ssl->session_negotiate->id[0],
1187 ssl->session_negotiate->id_len );
1188 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001189
Jerry Yu3bf2c642022-03-30 22:02:12 +08001190 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1191 ssl->session_negotiate->id_len );
1192 }
1193
Jerry Yucfc04b32022-04-21 09:31:58 +08001194 /* ...
1195 * CipherSuite cipher_suite;
1196 * ...
1197 * with CipherSuite defined as:
1198 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001199 */
1200 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1201 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1202 p += 2;
1203 MBEDTLS_SSL_DEBUG_MSG( 3,
1204 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1205 mbedtls_ssl_get_ciphersuite_name(
1206 ssl->session_negotiate->ciphersuite ),
1207 ssl->session_negotiate->ciphersuite ) );
1208
Jerry Yucfc04b32022-04-21 09:31:58 +08001209 /* ...
1210 * uint8 legacy_compression_method = 0;
1211 * ...
1212 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001213 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1214 *p++ = 0x0;
1215
Jerry Yucfc04b32022-04-21 09:31:58 +08001216 /* ...
1217 * Extension extensions<6..2^16-1>;
1218 * ...
1219 * struct {
1220 * ExtensionType extension_type; (2 bytes)
1221 * opaque extension_data<0..2^16-1>;
1222 * } Extension;
1223 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001224 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001225 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001226 p += 2;
1227
Jerry Yue74e04a2022-04-21 09:23:16 +08001228 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001229 ssl, p, end, &output_len ) ) != 0 )
1230 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001231 MBEDTLS_SSL_DEBUG_RET(
1232 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001233 return( ret );
1234 }
1235 p += output_len;
1236
Jerry Yu3bf2c642022-03-30 22:02:12 +08001237 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1238 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001239 if( is_hrr )
1240 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1241 else
1242 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001243 if( ret != 0 )
1244 return( ret );
1245 p += output_len;
1246 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001247
Jerry Yud9436a12022-04-20 22:28:09 +08001248 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001249
Jerry Yud9436a12022-04-20 22:28:09 +08001250 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1251 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001252
Jerry Yud9436a12022-04-20 22:28:09 +08001253 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001254
Jerry Yud9436a12022-04-20 22:28:09 +08001255 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001256
1257 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001258}
1259
Jerry Yue110d252022-05-05 10:19:22 +08001260static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1261{
1262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001263 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001264 if( ret != 0 )
1265 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001266 MBEDTLS_SSL_DEBUG_RET( 1,
1267 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001268 ret );
1269 return( ret );
1270 }
1271
Jerry Yue110d252022-05-05 10:19:22 +08001272 return( ret );
1273}
1274
Jerry Yu5b64ae92022-03-30 17:15:02 +08001275static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1276{
Jerry Yu637a3f12022-04-20 21:37:58 +08001277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001278 unsigned char *buf;
1279 size_t buf_len, msg_len;
1280
1281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1282
Jerry Yuf4b27e42022-03-30 17:32:21 +08001283 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1284
Jerry Yu3bf2c642022-03-30 22:02:12 +08001285 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001286 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1287
Jerry Yu3bf2c642022-03-30 22:02:12 +08001288 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1289 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001290 &msg_len,
1291 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001292
Jerry Yu3bf2c642022-03-30 22:02:12 +08001293 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001294 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1295
Jerry Yu3bf2c642022-03-30 22:02:12 +08001296 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001297 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001298
Jerry Yue110d252022-05-05 10:19:22 +08001299 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1300
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001301#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1302 /* The server sends a dummy change_cipher_spec record immediately
1303 * after its first handshake message. This may either be after
1304 * a ServerHello or a HelloRetryRequest.
1305 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001306 mbedtls_ssl_handshake_set_state(
1307 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001308#else
Jerry Yu637a3f12022-04-20 21:37:58 +08001309 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001310#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08001311
Jerry Yuf4b27e42022-03-30 17:32:21 +08001312cleanup:
1313
1314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1315 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001316}
1317
Jerry Yu23d1a252022-05-12 18:08:59 +08001318
1319/*
1320 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1321 */
Ronald Cron7b840462022-06-01 17:05:53 +02001322static int ssl_tls13_prepare_hello_retry_request( mbedtls_ssl_context *ssl )
Jerry Yu23d1a252022-05-12 18:08:59 +08001323{
1324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1325 if( ssl->handshake->hello_retry_request_count > 0 )
1326 {
1327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1328 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1329 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1330 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1331 }
1332
1333 /*
1334 * Create stateless transcript hash for HRR
1335 */
1336 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1337 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1338 if( ret != 0 )
1339 {
1340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1341 return( ret );
1342 }
1343 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1344
1345 return( 0 );
1346}
1347
1348static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1349{
1350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1351 unsigned char *buf;
1352 size_t buf_len, msg_len;
1353
1354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1355
Ronald Cron7b840462022-06-01 17:05:53 +02001356 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_hello_retry_request( ssl ) );
Jerry Yu23d1a252022-05-12 18:08:59 +08001357
1358 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1359 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1360 &buf, &buf_len ) );
1361
1362 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1363 buf + buf_len,
1364 &msg_len,
1365 1 ) );
1366 mbedtls_ssl_add_hs_msg_to_checksum(
1367 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1368
1369
1370 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1371 msg_len ) );
1372
1373 ssl->handshake->hello_retry_request_count++;
1374
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001375#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1376 /* The server sends a dummy change_cipher_spec record immediately
1377 * after its first handshake message. This may either be after
1378 * a ServerHello or a HelloRetryRequest.
1379 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001380 mbedtls_ssl_handshake_set_state(
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001381 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001382#else
Jerry Yu23d1a252022-05-12 18:08:59 +08001383 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001384#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08001385
1386cleanup:
1387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1388 return( ret );
1389}
1390
Jerry Yu5b64ae92022-03-30 17:15:02 +08001391/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001392 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1393 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001394
1395/*
1396 * struct {
1397 * Extension extensions<0..2 ^ 16 - 1>;
1398 * } EncryptedExtensions;
1399 *
1400 */
1401static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1402 unsigned char *buf,
1403 unsigned char *end,
1404 size_t *out_len )
1405{
XiaokangQianacb39922022-06-17 10:18:48 +00001406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001407 unsigned char *p = buf;
1408 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001409 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001410 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001411
Jerry Yu4d3841a2022-04-16 12:37:19 +08001412 *out_len = 0;
1413
1414 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001415 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001416 p += 2;
1417
Jerry Yu8937eb42022-05-03 12:12:14 +08001418 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00001419 ((void) ret);
1420 ((void) output_len);
1421
1422#if defined(MBEDTLS_SSL_ALPN)
1423 ret = mbedtls_ssl_write_alpn_ext( ssl, p, end, &output_len );
1424 if( ret != 0 )
1425 return( ret );
XiaokangQian95d5f542022-06-24 02:29:26 +00001426 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001427#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001428
Jerry Yu8937eb42022-05-03 12:12:14 +08001429 extensions_len = ( p - p_extensions_len ) - 2;
1430 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
1431
1432 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001433
1434 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
1435
1436 return( 0 );
1437}
1438
1439static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
1440{
1441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1442 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08001443 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001444
Gabor Mezei54719122022-06-28 11:34:56 +02001445 mbedtls_ssl_set_outbound_transform( ssl,
1446 ssl->handshake->transform_handshake );
1447 MBEDTLS_SSL_DEBUG_MSG(
1448 3, ( "switching to handshake transform for outbound data" ) );
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
Jerry Yu4d3841a2022-04-16 12:37:19 +08001452 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1453 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
1454
1455 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
1456 ssl, buf, buf + buf_len, &msg_len ) );
1457
1458 mbedtls_ssl_add_hs_msg_to_checksum(
1459 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
1460
Jerry Yu4d3841a2022-04-16 12:37:19 +08001461 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1462 ssl, buf_len, msg_len ) );
1463
Jerry Yu8937eb42022-05-03 12:12:14 +08001464#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1465 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1466 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1467 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00001468 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08001469#else
1470 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1471#endif
1472
Jerry Yu4d3841a2022-04-16 12:37:19 +08001473cleanup:
1474
Jerry Yuab452cc2022-04-28 15:27:08 +08001475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001476 return( ret );
1477}
1478
XiaokangQiancec9ae62022-05-06 07:28:50 +00001479#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00001480#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
1481#define SSL_CERTIFICATE_REQUEST_SKIP 1
1482/* Coordination:
1483 * Check whether a CertificateRequest message should be written.
1484 * Returns a negative code on failure, or
1485 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
1486 * - SSL_CERTIFICATE_REQUEST_SKIP
1487 * indicating if the writing of the CertificateRequest
1488 * should be skipped or not.
1489 */
1490static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
1491{
1492 int authmode;
1493
XiaokangQian40a35232022-05-07 09:02:40 +00001494#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1495 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
1496 authmode = ssl->handshake->sni_authmode;
1497 else
1498#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00001499 authmode = ssl->conf->authmode;
1500
1501 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
1502 return( SSL_CERTIFICATE_REQUEST_SKIP );
1503
XiaokangQianc3017f62022-05-13 05:55:41 +00001504 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00001505
XiaokangQiana987e1d2022-05-07 01:25:58 +00001506 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
1507}
1508
XiaokangQiancec9ae62022-05-06 07:28:50 +00001509/*
1510 * struct {
1511 * opaque certificate_request_context<0..2^8-1>;
1512 * Extension extensions<2..2^16-1>;
1513 * } CertificateRequest;
1514 *
1515 */
1516static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
1517 unsigned char *buf,
1518 const unsigned char *end,
1519 size_t *out_len )
1520{
1521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1522 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00001523 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00001524 unsigned char *p_extensions_len;
1525
1526 *out_len = 0;
1527
1528 /* Check if we have enough space:
1529 * - certificate_request_context (1 byte)
1530 * - extensions length (2 bytes)
1531 */
1532 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
1533
1534 /*
1535 * Write certificate_request_context
1536 */
1537 /*
1538 * We use a zero length context for the normal handshake
1539 * messages. For post-authentication handshake messages
1540 * this request context would be set to a non-zero value.
1541 */
1542 *p++ = 0x0;
1543
1544 /*
1545 * Write extensions
1546 */
1547 /* The extensions must contain the signature_algorithms. */
1548 p_extensions_len = p;
1549 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00001550 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001551 if( ret != 0 )
1552 return( ret );
1553
XiaokangQianec6efb92022-05-06 09:53:10 +00001554 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00001555 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001556
1557 *out_len = p - buf;
1558
1559 return( 0 );
1560}
1561
1562static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
1563{
1564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1565
1566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1567
1568 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1569
1570 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
1571 {
1572 unsigned char *buf;
1573 size_t buf_len, msg_len;
1574
1575 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1576 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
1577
1578 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
1579 ssl, buf, buf + buf_len, &msg_len ) );
1580
1581 mbedtls_ssl_add_hs_msg_to_checksum(
1582 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
1583
1584 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1585 ssl, buf_len, msg_len ) );
1586 }
1587 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
1588 {
1589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1590 ret = 0;
1591 }
1592 else
1593 {
1594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1595 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1596 goto cleanup;
1597 }
1598
1599 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1600cleanup:
1601
1602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1603 return( ret );
1604}
XiaokangQiancec9ae62022-05-06 07:28:50 +00001605
Jerry Yu4d3841a2022-04-16 12:37:19 +08001606/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001607 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08001608 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001609static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001610{
Jerry Yu5a26f302022-05-10 20:46:40 +08001611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00001612
1613#if defined(MBEDTLS_X509_CRT_PARSE_C)
1614 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
1615 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08001616 {
Jerry Yub89125b2022-05-13 15:45:49 +08001617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08001618 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1619 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001620 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08001621 }
XiaokangQianfb665a82022-06-15 03:57:21 +00001622#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08001623
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001624 ret = mbedtls_ssl_tls13_write_certificate( ssl );
1625 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08001626 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08001627 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1628 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001629}
1630
1631/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001632 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08001633 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001634static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001635{
Jerry Yu4ff9e142022-04-16 14:57:49 +08001636 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001637 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08001638 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08001639 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1640 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001641}
Jerry Yu5a26f302022-05-10 20:46:40 +08001642#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001643
1644/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001645 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001646 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001647static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001648{
Jerry Yud6e253d2022-05-18 13:59:24 +08001649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001650
1651 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1652 if( ret != 0 )
1653 return( ret );
1654
Jerry Yue3d67cb2022-05-19 15:33:10 +08001655 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1656 if( ret != 0 )
1657 {
1658 MBEDTLS_SSL_PEND_FATAL_ALERT(
1659 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1660 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1661 return( ret );
1662 }
XiaokangQianc3017f62022-05-13 05:55:41 +00001663
Ronald Cron63dc4632022-05-31 14:41:53 +02001664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1665 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQian189ded22022-05-10 08:12:17 +00001666
Ronald Cron63dc4632022-05-31 14:41:53 +02001667 if( ssl->handshake->certificate_request_sent )
1668 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
XiaokangQian189ded22022-05-10 08:12:17 +00001669 else
Ronald Cron19385882022-06-15 16:26:13 +02001670 {
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate" ) );
1672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
XiaokangQian189ded22022-05-10 08:12:17 +00001673 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001674 }
Ronald Cron63dc4632022-05-31 14:41:53 +02001675
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001676 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001677}
1678
1679/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001680 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001681 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001682static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001683{
Jerry Yud6e253d2022-05-18 13:59:24 +08001684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1685
Jerry Yuff226982022-04-16 16:52:57 +08001686 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
1687 if( ret != 0 )
1688 return( ret );
1689
Jerry Yue3d67cb2022-05-19 15:33:10 +08001690 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1691 if( ret != 0 )
1692 {
1693 MBEDTLS_SSL_DEBUG_RET( 1,
1694 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1695 }
1696
Jerry Yu03ed50b2022-04-16 17:13:30 +08001697 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08001698 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001699}
1700
1701/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001702 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08001703 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001704static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001705{
Jerry Yu03ed50b2022-04-16 17:13:30 +08001706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1707
Jerry Yu03ed50b2022-04-16 17:13:30 +08001708 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1709 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1710 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001711}
1712
1713/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001714 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00001715 */
Jerry Yu27561932021-08-27 17:07:38 +08001716int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08001717{
XiaokangQian08037552022-04-20 07:16:41 +00001718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001719
1720 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
1721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1722
Jerry Yue3b34122021-09-28 17:53:35 +08001723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
1724 mbedtls_ssl_states_str( ssl->state ),
1725 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08001726
XiaokangQian7807f9f2022-02-15 10:04:37 +00001727 switch( ssl->state )
1728 {
1729 /* start state */
1730 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001731 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00001732 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001733 break;
1734
XiaokangQian7807f9f2022-02-15 10:04:37 +00001735 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00001736 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001737 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00001738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08001739 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001740
Jerry Yuf41553b2022-05-09 22:20:30 +08001741 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
1742 ret = ssl_tls13_write_hello_retry_request( ssl );
1743 if( ret != 0 )
1744 {
1745 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
1746 return( ret );
1747 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001748 break;
1749
Jerry Yu5b64ae92022-03-30 17:15:02 +08001750 case MBEDTLS_SSL_SERVER_HELLO:
1751 ret = ssl_tls13_write_server_hello( ssl );
1752 break;
1753
Xiaofei Baicba64af2022-02-15 10:00:56 +00001754 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08001755 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001756 if( ret != 0 )
1757 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08001758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
1759 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001760 }
Jerry Yu48330562022-05-06 21:35:44 +08001761 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08001762
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001763#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1764 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00001765 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001766 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001767
Jerry Yu83da34e2022-04-16 13:59:52 +08001768 case MBEDTLS_SSL_SERVER_CERTIFICATE:
1769 ret = ssl_tls13_write_server_certificate( ssl );
1770 break;
1771
1772 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
1773 ret = ssl_tls13_write_certificate_verify( ssl );
1774 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08001775#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001776
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001777 /*
1778 * Injection of dummy-CCS's for middlebox compatibility
1779 */
1780#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001781 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001782 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1783 if( ret == 0 )
1784 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1785 break;
1786
1787 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
1788 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1789 if( ret == 0 )
1790 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1791 break;
1792#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1793
Jerry Yu69dd8d42022-04-16 12:51:26 +08001794 case MBEDTLS_SSL_SERVER_FINISHED:
1795 ret = ssl_tls13_write_server_finished( ssl );
1796 break;
1797
1798 case MBEDTLS_SSL_CLIENT_FINISHED:
1799 ret = ssl_tls13_process_client_finished( ssl );
1800 break;
1801
Jerry Yu69dd8d42022-04-16 12:51:26 +08001802 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
1803 ret = ssl_tls13_handshake_wrapup( ssl );
1804 break;
1805
XiaokangQian6b916b12022-04-25 07:29:34 +00001806 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1807 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02001808 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00001809 {
Ronald Cron209cae92022-06-07 10:30:19 +02001810 if( ssl->session_negotiate->peer_cert != NULL )
1811 {
1812 mbedtls_ssl_handshake_set_state(
1813 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1814 }
1815 else
Ronald Cron19385882022-06-15 16:26:13 +02001816 {
1817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
Ronald Cron209cae92022-06-07 10:30:19 +02001818 mbedtls_ssl_handshake_set_state(
1819 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001820 }
XiaokangQian6b916b12022-04-25 07:29:34 +00001821 }
1822 break;
1823
1824 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1825 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1826 if( ret == 0 )
1827 {
1828 mbedtls_ssl_handshake_set_state(
1829 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1830 }
1831 break;
1832
XiaokangQian7807f9f2022-02-15 10:04:37 +00001833 default:
1834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00001835 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001836 }
1837
1838 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08001839}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001840
Jerry Yufb4b6472022-01-27 15:03:26 +08001841#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */