blob: b7b25576e7fb26c8eec1b0eaf4715643165a8642 [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 Yuaebaaaf2022-06-24 13:14:36 +0800392 MBEDTLS_SSL_DEBUG_MSG(
393 3, ( "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 Yuaebaaaf2022-06-24 13:14:36 +0800401 MBEDTLS_SSL_DEBUG_MSG(
402 3, ( "ssl_tls13_pick_key_cert:"
403 "selected signature algorithm %s [%04x]",
404 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
405 *sig_alg ) );
XiaokangQianfb665a82022-06-15 03:57:21 +0000406 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000407 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000408 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000409 return( 0 );
410 }
XiaokangQian81802f42022-06-10 13:25:22 +0000411 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000412 }
413
Jerry Yuf55886a2022-06-19 11:48:56 +0800414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ssl_tls13_pick_key_cert: "
415 "No signature algorithm found" ) );
XiaokangQian23c5be62022-06-07 02:04:34 +0000416 return( -1 );
417}
XiaokangQian81802f42022-06-10 13:25:22 +0000418#endif /* MBEDTLS_X509_CRT_PARSE_C &&
419 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000420
XiaokangQian4080a7f2022-04-11 09:55:18 +0000421/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000422 *
423 * STATE HANDLING: ClientHello
424 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000425 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000426 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000427 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000428 *
429 * In this case, the server progresses to sending its ServerHello.
430 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000431 * 2) The ClientHello was well-formed but didn't match the server's
432 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000433 *
434 * For example, the client might not have offered a key share which
435 * the server supports, or the server might require a cookie.
436 *
437 * In this case, the server sends a HelloRetryRequest.
438 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000439 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000440 *
441 * In this case, we abort the handshake.
442 *
443 */
444
445/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000446 * Structure of this message:
447 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000448 * uint16 ProtocolVersion;
449 * opaque Random[32];
450 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000451 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000452 * struct {
453 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
454 * Random random;
455 * opaque legacy_session_id<0..32>;
456 * CipherSuite cipher_suites<2..2^16-2>;
457 * opaque legacy_compression_methods<1..2^8-1>;
458 * Extension extensions<8..2^16-1>;
459 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000460 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000461
462#define SSL_CLIENT_HELLO_OK 0
463#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
464
XiaokangQian4080a7f2022-04-11 09:55:18 +0000465static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
466 const unsigned char *buf,
467 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000468{
XiaokangQianb67384d2022-04-19 00:02:38 +0000469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
470 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000471 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000472 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000473 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000474 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000475 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000476 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800477 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000478
XiaokangQian7807f9f2022-02-15 10:04:37 +0000479 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000480
481 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
482
483 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000484 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000485 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000486 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000487 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000488 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000489 * .. . .. ciphersuite list length ( 2 bytes )
490 * .. . .. ciphersuite list
491 * .. . .. compression alg. list length ( 1 byte )
492 * .. . .. compression alg. list
493 * .. . .. extensions length ( 2 bytes, optional )
494 * .. . .. extensions ( optional )
495 */
496
XiaokangQianb67384d2022-04-19 00:02:38 +0000497 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400498 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000499 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
500 * read at least up to session id length without worrying.
501 */
502 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
503
504 /* ...
505 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
506 * ...
507 * with ProtocolVersion defined as:
508 * uint16 ProtocolVersion;
509 */
XiaokangQiande333912022-04-20 08:49:42 +0000510 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
511 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000512 {
513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
514 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
515 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000516 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000517 }
518 p += 2;
519
520 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000521 * Only support TLS 1.3 currently, temporarily set the version.
522 */
XiaokangQiande333912022-04-20 08:49:42 +0000523 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000524
Jerry Yuc1be19f2022-04-23 16:11:39 +0800525 /* ...
526 * Random random;
527 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000528 * with Random defined as:
529 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000530 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000531 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +0000532 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533
XiaokangQian08037552022-04-20 07:16:41 +0000534 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
535 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000536
Jerry Yuc1be19f2022-04-23 16:11:39 +0800537 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000538 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800539 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000540 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000541 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000542 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000543
XiaokangQianb67384d2022-04-19 00:02:38 +0000544 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000545 {
546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
547 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
548 }
549
XiaokangQian4080a7f2022-04-11 09:55:18 +0000550 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000551 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +0000552 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000553 /*
554 * Check we have enough data for the legacy session identifier
555 * and the ciphersuite list length.
556 */
557 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000558
XiaokangQianed582dd2022-04-13 08:21:05 +0000559 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000560 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000561
XiaokangQian7807f9f2022-02-15 10:04:37 +0000562 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
563 p += 2;
564
XiaokangQianb67384d2022-04-19 00:02:38 +0000565 /* Check we have enough data for the ciphersuite list, the legacy
566 * compression methods and the length of the extensions.
567 */
568 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000569
Jerry Yuc1be19f2022-04-23 16:11:39 +0800570 /* ...
XiaokangQian08037552022-04-20 07:16:41 +0000571 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800572 * ...
XiaokangQian08037552022-04-20 07:16:41 +0000573 * with CipherSuite defined as:
574 * uint8 CipherSuite[2];
575 */
XiaokangQian060d8672022-04-21 09:24:56 +0000576 cipher_suites = p;
577 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000578 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
579 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +0000580 /*
581 * Search for a matching ciphersuite
582 */
XiaokangQian318dc762022-04-20 09:43:51 +0000583 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +0000584 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +0000585 {
XiaokangQiane8ff3502022-04-22 02:34:40 +0000586 uint16_t cipher_suite;
587 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
588 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
589 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +0000590 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +0000591 * Check whether this ciphersuite is valid and offered.
592 */
XiaokangQian08037552022-04-20 07:16:41 +0000593 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800594 ssl, ciphersuite_info, ssl->tls_version,
595 ssl->tls_version ) != 0 ) ||
596 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
597 {
XiaokangQian08037552022-04-20 07:16:41 +0000598 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800599 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000600
XiaokangQian08037552022-04-20 07:16:41 +0000601 ssl->session_negotiate->ciphersuite = cipher_suite;
602 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +0000603 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +0000604
XiaokangQian08037552022-04-20 07:16:41 +0000605 break;
XiaokangQian17f974c2022-04-19 09:57:41 +0000606
XiaokangQian17f974c2022-04-19 09:57:41 +0000607 }
608
Jerry Yuc1be19f2022-04-23 16:11:39 +0800609 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +0000610 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000611 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
612 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
613 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +0000614 }
XiaokangQian17f974c2022-04-19 09:57:41 +0000615
616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
617 ciphersuite_info->name ) );
618
XiaokangQian060d8672022-04-21 09:24:56 +0000619 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800620
XiaokangQian4080a7f2022-04-11 09:55:18 +0000621 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000622 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000623 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000624 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000625 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000626 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
628 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
629 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
630 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000631 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000632 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000633
Jerry Yuc1be19f2022-04-23 16:11:39 +0800634 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000635 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800636 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000637 * with Extension defined as:
638 * struct {
639 * ExtensionType extension_type;
640 * opaque extension_data<0..2^16-1>;
641 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000642 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000643 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000644 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000645 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +0000646 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000647
XiaokangQian4080a7f2022-04-11 09:55:18 +0000648 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000649
650 while( p < extensions_end )
651 {
652 unsigned int extension_type;
653 size_t extension_data_len;
654 const unsigned char *extension_data_end;
655
XiaokangQian318dc762022-04-20 09:43:51 +0000656 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000657 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
658 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
659 p += 4;
660
661 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
662 extension_data_end = p + extension_data_len;
663
664 switch( extension_type )
665 {
XiaokangQian40a35232022-05-07 09:02:40 +0000666#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
667 case MBEDTLS_TLS_EXT_SERVERNAME:
668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +0000669 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
670 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +0000671 if( ret != 0 )
672 {
673 MBEDTLS_SSL_DEBUG_RET(
674 1, "mbedtls_ssl_parse_servername_ext", ret );
675 return( ret );
676 }
677 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
678 break;
679#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
680
XiaokangQianb67384d2022-04-19 00:02:38 +0000681#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000682 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
684
685 /* Supported Groups Extension
686 *
687 * When sent by the client, the "supported_groups" extension
688 * indicates the named groups which the client supports,
689 * ordered from most preferred to least preferred.
690 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800691 ret = ssl_tls13_parse_supported_groups_ext(
692 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000693 if( ret != 0 )
694 {
695 MBEDTLS_SSL_DEBUG_RET( 1,
696 "mbedtls_ssl_parse_supported_groups_ext", ret );
697 return( ret );
698 }
699
700 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
701 break;
XiaokangQianb67384d2022-04-19 00:02:38 +0000702#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000703
XiaokangQian88408882022-04-02 10:15:03 +0000704#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000705 case MBEDTLS_TLS_EXT_KEY_SHARE:
706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
707
708 /*
709 * Key Share Extension
710 *
711 * When sent by the client, the "key_share" extension
712 * contains the endpoint's cryptographic parameters for
713 * ECDHE/DHE key establishment methods.
714 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800715 ret = ssl_tls13_parse_key_shares_ext(
716 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +0000717 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000718 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +0800720 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000721 }
722
Jerry Yu582dd062022-04-22 21:59:01 +0800723 if( ret < 0 )
724 {
725 MBEDTLS_SSL_DEBUG_RET(
726 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000727 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +0800728 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000729
730 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
731 break;
XiaokangQian88408882022-04-02 10:15:03 +0000732#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000733
734 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
736
737 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800738 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000739 if( ret != 0 )
740 {
741 MBEDTLS_SSL_DEBUG_RET( 1,
742 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
743 return( ret );
744 }
745 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
746 break;
747
XiaokangQianacb39922022-06-17 10:18:48 +0000748#if defined(MBEDTLS_SSL_ALPN)
749 case MBEDTLS_TLS_EXT_ALPN:
750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
751
752 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
753 if( ret != 0 )
754 {
755 MBEDTLS_SSL_DEBUG_RET(
756 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
757 return( ret );
758 }
759 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
760 break;
761#endif /* MBEDTLS_SSL_ALPN */
762
XiaokangQian7807f9f2022-02-15 10:04:37 +0000763#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
764 case MBEDTLS_TLS_EXT_SIG_ALG:
765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
766
Gabor Mezei078e8032022-04-27 21:17:56 +0200767 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800768 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769 if( ret != 0 )
770 {
771 MBEDTLS_SSL_DEBUG_MSG( 1,
772 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
773 ret ) );
774 return( ret );
775 }
776 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
777 break;
778#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
779
780 default:
781 MBEDTLS_SSL_DEBUG_MSG( 3,
782 ( "unknown extension found: %ud ( ignoring )",
783 extension_type ) );
784 }
785
786 p += extension_data_len;
787 }
788
789 /* Update checksum with either
790 * - The entire content of the CH message, if no PSK extension is present
791 * - The content up to but excluding the PSK extension, if present.
792 */
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000793 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
XiaokangQianc4b8c992022-04-07 11:31:38 +0000794 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795
796 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000797#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000798 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000799#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800
XiaokangQian75fe8c72022-06-15 09:42:45 +0000801 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
802}
803
804/* Update the handshake state machine */
805
806static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
807{
808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
809
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000811 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812 */
XiaokangQianb67384d2022-04-19 00:02:38 +0000813 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814 {
815 MBEDTLS_SSL_DEBUG_MSG(
816 1,
817 ( "ClientHello message misses mandatory extensions." ) );
818 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
819 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
820 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
821 }
822
XiaokangQianfb665a82022-06-15 03:57:21 +0000823 /*
824 * Server certificate selection
825 */
826 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
827 {
828 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
829 return( ret );
830 }
831#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
832 ssl->handshake->sni_name = NULL;
833 ssl->handshake->sni_name_len = 0;
834#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
837 if( ret != 0 )
838 {
839 MBEDTLS_SSL_DEBUG_RET( 1,
840 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
841 return( ret );
842 }
843
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844 return( 0 );
845
846}
847
848/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000849 * Main entry point from the state machine; orchestrates the otherfunctions.
850 */
851
852static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
853{
854
XiaokangQian08037552022-04-20 07:16:41 +0000855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +0000856 unsigned char* buf = NULL;
857 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +0800858 int parse_client_hello_ret;
859
XiaokangQianed582dd2022-04-13 08:21:05 +0000860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
861
XiaokangQianed582dd2022-04-13 08:21:05 +0000862 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
863 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
864 &buf, &buflen ) );
865
866 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
867 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +0800868 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
869 * only SSL_CLIENT_HELLO_OK or
870 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
871 * stage as negative error codes are handled
872 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +0800873
XiaokangQiancfd925f2022-04-14 07:10:37 +0000874 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +0800875
Jerry Yu4ca91402022-05-09 15:50:57 +0800876 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
877 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
878 else
879 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +0000880
881cleanup:
882
883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
884 return( ret );
885}
886
887/*
Jerry Yu1c3e6882022-04-20 21:23:40 +0800888 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +0800889 */
Jerry Yuf4b27e42022-03-30 17:32:21 +0800890static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
891{
Jerry Yu637a3f12022-04-20 21:37:58 +0800892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
893 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +0800894 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +0800895 if( ssl->conf->f_rng == NULL )
896 {
897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
898 return( MBEDTLS_ERR_SSL_NO_RNG );
899 }
900
Jerry Yu637a3f12022-04-20 21:37:58 +0800901 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800902 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
903 {
904 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
905 return( ret );
906 }
907
Jerry Yu637a3f12022-04-20 21:37:58 +0800908 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800909 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
910
911#if defined(MBEDTLS_HAVE_TIME)
912 ssl->session_negotiate->start = time( NULL );
913#endif /* MBEDTLS_HAVE_TIME */
914
915 return( ret );
916}
917
Jerry Yu3bf2c642022-03-30 22:02:12 +0800918/*
Jerry Yue74e04a2022-04-21 09:23:16 +0800919 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +0800920 *
921 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +0800922 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800923 * } SupportedVersions;
924 */
Jerry Yue74e04a2022-04-21 09:23:16 +0800925static int ssl_tls13_write_server_hello_supported_versions_ext(
926 mbedtls_ssl_context *ssl,
927 unsigned char *buf,
928 unsigned char *end,
929 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800930{
Jerry Yu3bf2c642022-03-30 22:02:12 +0800931 *out_len = 0;
932
Jerry Yu955ddd72022-04-22 22:27:33 +0800933 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800934
935 /* Check if we have space to write the extension:
936 * - extension_type (2 bytes)
937 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +0800938 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800939 */
Jerry Yu349a6132022-04-14 20:52:56 +0800940 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800941
Jerry Yu349a6132022-04-14 20:52:56 +0800942 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800943
Jerry Yu349a6132022-04-14 20:52:56 +0800944 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800945
Jerry Yu349a6132022-04-14 20:52:56 +0800946 mbedtls_ssl_write_version( buf + 4,
947 ssl->conf->transport,
948 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800949
950 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
951 ssl->tls_version ) );
952
Jerry Yu349a6132022-04-14 20:52:56 +0800953 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800954
955 return( 0 );
956}
957
Jerry Yud9436a12022-04-20 22:28:09 +0800958
Jerry Yu3bf2c642022-03-30 22:02:12 +0800959
960/* Generate and export a single key share. For hybrid KEMs, this can
961 * be called multiple times with the different components of the hybrid. */
Jerry Yu955ddd72022-04-22 22:27:33 +0800962static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
963 uint16_t named_group,
964 unsigned char *buf,
965 unsigned char *end,
966 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800967{
968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +0800969
970 *out_len = 0;
971
Jerry Yu89e103c2022-03-30 22:43:29 +0800972#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800973 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
974 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800975 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
976 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800977 if( ret != 0 )
978 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800979 MBEDTLS_SSL_DEBUG_RET(
980 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
981 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800982 return( ret );
983 }
Jerry Yu3bf2c642022-03-30 22:02:12 +0800984 }
Jerry Yu89e103c2022-03-30 22:43:29 +0800985 else
986#endif /* MBEDTLS_ECDH_C */
987 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800988 {
989 }
990 else
991 {
Jerry Yu955ddd72022-04-22 22:27:33 +0800992 ((void) ssl);
993 ((void) named_group);
994 ((void) buf);
995 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +0800996 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
997 }
998
999 return( ret );
1000}
1001
1002/*
1003 * ssl_tls13_write_key_share_ext
1004 *
1005 * Structure of key_share extension in ServerHello:
1006 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001007 * struct {
1008 * NamedGroup group;
1009 * opaque key_exchange<1..2^16-1>;
1010 * } KeyShareEntry;
1011 * struct {
1012 * KeyShareEntry server_share;
1013 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001014 */
1015static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
1016 unsigned char *buf,
1017 unsigned char *end,
1018 size_t *out_len )
1019{
Jerry Yu955ddd72022-04-22 22:27:33 +08001020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001021 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001022 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001023 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001024 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001025
1026 *out_len = 0;
1027
1028 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1029
1030 /* Check if we have space for header and length fields:
1031 * - extension_type (2 bytes)
1032 * - extension_data_length (2 bytes)
1033 * - group (2 bytes)
1034 * - key_exchange_length (2 bytes)
1035 */
1036 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001037 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1038 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001039 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001040
Jerry Yu3bf2c642022-03-30 22:02:12 +08001041 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1042 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001043 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001044 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001045 if( ret != 0 )
1046 return( ret );
1047 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001048
Jerry Yu955ddd72022-04-22 22:27:33 +08001049 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001050
Jerry Yu57d48412022-04-20 21:50:42 +08001051 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001052
Jerry Yu57d48412022-04-20 21:50:42 +08001053 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001054
Jerry Yu3bf2c642022-03-30 22:02:12 +08001055 return( 0 );
1056}
Jerry Yud9436a12022-04-20 22:28:09 +08001057
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001058static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1059 unsigned char *buf,
1060 unsigned char *end,
1061 size_t *out_len )
1062{
1063 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1064 /* key_share Extension
1065 *
1066 * struct {
1067 * select (Handshake.msg_type) {
1068 * ...
1069 * case hello_retry_request:
1070 * NamedGroup selected_group;
1071 * ...
1072 * };
1073 * } KeyShare;
1074 */
1075
1076 *out_len = 0;
1077
Jerry Yub0ac10b2022-05-05 11:10:08 +08001078 /*
1079 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1080 * of the HRR is then to transmit a cookie to force the client to demonstrate
1081 * reachability at their apparent network address (primarily useful for DTLS).
1082 */
1083 if( ! mbedtls_ssl_tls13_some_ephemeral_enabled( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001084 return( 0 );
1085
1086 /* We should only send the key_share extension if the client's initial
1087 * key share was not acceptable. */
1088 if( ssl->handshake->offered_group_id != 0 )
1089 {
1090 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1091 return( 0 );
1092 }
1093
1094 if( selected_group == 0 )
1095 {
1096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1097 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1098 }
1099
Jerry Yub0ac10b2022-05-05 11:10:08 +08001100 /* Check if we have enough space:
1101 * - extension_type (2 bytes)
1102 * - extension_data_length (2 bytes)
1103 * - selected_group (2 bytes)
1104 */
Ronald Cron154d1b62022-06-01 15:33:26 +02001105 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001106
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001107 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001108 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001109 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1110
1111 MBEDTLS_SSL_DEBUG_MSG( 3,
1112 ( "HRR selected_group: %s (%x)",
1113 mbedtls_ssl_named_group_to_str( selected_group ),
1114 selected_group ) );
1115
1116 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001117
Jerry Yub0ac10b2022-05-05 11:10:08 +08001118 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001119}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001120
1121/*
1122 * Structure of ServerHello message:
1123 *
1124 * struct {
1125 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1126 * Random random;
1127 * opaque legacy_session_id_echo<0..32>;
1128 * CipherSuite cipher_suite;
1129 * uint8 legacy_compression_method = 0;
1130 * Extension extensions<6..2^16-1>;
1131 * } ServerHello;
1132 */
1133static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001134 unsigned char *buf,
1135 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001136 size_t *out_len,
1137 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001138{
Jerry Yu955ddd72022-04-22 22:27:33 +08001139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001140 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001141 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001142 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001143
1144 *out_len = 0;
1145
Jerry Yucfc04b32022-04-21 09:31:58 +08001146 /* ...
1147 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1148 * ...
1149 * with ProtocolVersion defined as:
1150 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001151 */
1152 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1153 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1154 p += 2;
1155
Jerry Yu1c3e6882022-04-20 21:23:40 +08001156 /* ...
1157 * Random random;
1158 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001159 * with Random defined as:
1160 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001161 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001162 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001163 if( is_hrr )
1164 {
1165 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001166 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001167 }
1168 else
1169 {
1170 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001171 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001172 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001173 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001174 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1175 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1176
Jerry Yucfc04b32022-04-21 09:31:58 +08001177 /* ...
1178 * opaque legacy_session_id_echo<0..32>;
1179 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001180 */
1181 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1182 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1183 if( ssl->session_negotiate->id_len > 0 )
1184 {
1185 memcpy( p, &ssl->session_negotiate->id[0],
1186 ssl->session_negotiate->id_len );
1187 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001188
Jerry Yu3bf2c642022-03-30 22:02:12 +08001189 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1190 ssl->session_negotiate->id_len );
1191 }
1192
Jerry Yucfc04b32022-04-21 09:31:58 +08001193 /* ...
1194 * CipherSuite cipher_suite;
1195 * ...
1196 * with CipherSuite defined as:
1197 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001198 */
1199 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1200 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1201 p += 2;
1202 MBEDTLS_SSL_DEBUG_MSG( 3,
1203 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1204 mbedtls_ssl_get_ciphersuite_name(
1205 ssl->session_negotiate->ciphersuite ),
1206 ssl->session_negotiate->ciphersuite ) );
1207
Jerry Yucfc04b32022-04-21 09:31:58 +08001208 /* ...
1209 * uint8 legacy_compression_method = 0;
1210 * ...
1211 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001212 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1213 *p++ = 0x0;
1214
Jerry Yucfc04b32022-04-21 09:31:58 +08001215 /* ...
1216 * Extension extensions<6..2^16-1>;
1217 * ...
1218 * struct {
1219 * ExtensionType extension_type; (2 bytes)
1220 * opaque extension_data<0..2^16-1>;
1221 * } Extension;
1222 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001223 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001224 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001225 p += 2;
1226
Jerry Yue74e04a2022-04-21 09:23:16 +08001227 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001228 ssl, p, end, &output_len ) ) != 0 )
1229 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001230 MBEDTLS_SSL_DEBUG_RET(
1231 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001232 return( ret );
1233 }
1234 p += output_len;
1235
Jerry Yu3bf2c642022-03-30 22:02:12 +08001236 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1237 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001238 if( is_hrr )
1239 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1240 else
1241 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001242 if( ret != 0 )
1243 return( ret );
1244 p += output_len;
1245 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001246
Jerry Yud9436a12022-04-20 22:28:09 +08001247 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001248
Jerry Yud9436a12022-04-20 22:28:09 +08001249 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1250 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001251
Jerry Yud9436a12022-04-20 22:28:09 +08001252 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001253
Jerry Yud9436a12022-04-20 22:28:09 +08001254 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001255
1256 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001257}
1258
Jerry Yue110d252022-05-05 10:19:22 +08001259static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1260{
1261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001262 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001263 if( ret != 0 )
1264 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001265 MBEDTLS_SSL_DEBUG_RET( 1,
1266 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001267 ret );
1268 return( ret );
1269 }
1270
Jerry Yue110d252022-05-05 10:19:22 +08001271 return( ret );
1272}
1273
Jerry Yu5b64ae92022-03-30 17:15:02 +08001274static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1275{
Jerry Yu637a3f12022-04-20 21:37:58 +08001276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001277 unsigned char *buf;
1278 size_t buf_len, msg_len;
1279
1280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1281
Jerry Yuf4b27e42022-03-30 17:32:21 +08001282 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1283
Jerry Yu3bf2c642022-03-30 22:02:12 +08001284 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001285 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1286
Jerry Yu3bf2c642022-03-30 22:02:12 +08001287 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1288 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001289 &msg_len,
1290 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001291
Jerry Yu3bf2c642022-03-30 22:02:12 +08001292 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001293 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1294
Jerry Yu3bf2c642022-03-30 22:02:12 +08001295 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001296 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001297
Jerry Yue110d252022-05-05 10:19:22 +08001298 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1299
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001300#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1301 /* The server sends a dummy change_cipher_spec record immediately
1302 * after its first handshake message. This may either be after
1303 * a ServerHello or a HelloRetryRequest.
1304 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001305 mbedtls_ssl_handshake_set_state(
1306 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001307#else
Jerry Yu637a3f12022-04-20 21:37:58 +08001308 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001309#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08001310
Jerry Yuf4b27e42022-03-30 17:32:21 +08001311cleanup:
1312
1313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1314 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001315}
1316
Jerry Yu23d1a252022-05-12 18:08:59 +08001317
1318/*
1319 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1320 */
Ronald Cron7b840462022-06-01 17:05:53 +02001321static int ssl_tls13_prepare_hello_retry_request( mbedtls_ssl_context *ssl )
Jerry Yu23d1a252022-05-12 18:08:59 +08001322{
1323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1324 if( ssl->handshake->hello_retry_request_count > 0 )
1325 {
1326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1327 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1328 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1329 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1330 }
1331
1332 /*
1333 * Create stateless transcript hash for HRR
1334 */
1335 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1336 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1337 if( ret != 0 )
1338 {
1339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1340 return( ret );
1341 }
1342 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1343
1344 return( 0 );
1345}
1346
1347static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1348{
1349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1350 unsigned char *buf;
1351 size_t buf_len, msg_len;
1352
1353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1354
Ronald Cron7b840462022-06-01 17:05:53 +02001355 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_hello_retry_request( ssl ) );
Jerry Yu23d1a252022-05-12 18:08:59 +08001356
1357 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1358 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1359 &buf, &buf_len ) );
1360
1361 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1362 buf + buf_len,
1363 &msg_len,
1364 1 ) );
1365 mbedtls_ssl_add_hs_msg_to_checksum(
1366 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1367
1368
1369 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1370 msg_len ) );
1371
1372 ssl->handshake->hello_retry_request_count++;
1373
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001374#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1375 /* The server sends a dummy change_cipher_spec record immediately
1376 * after its first handshake message. This may either be after
1377 * a ServerHello or a HelloRetryRequest.
1378 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001379 mbedtls_ssl_handshake_set_state(
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001380 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001381#else
Jerry Yu23d1a252022-05-12 18:08:59 +08001382 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001383#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08001384
1385cleanup:
1386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1387 return( ret );
1388}
1389
Jerry Yu5b64ae92022-03-30 17:15:02 +08001390/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001391 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1392 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001393
1394/*
1395 * struct {
1396 * Extension extensions<0..2 ^ 16 - 1>;
1397 * } EncryptedExtensions;
1398 *
1399 */
1400static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1401 unsigned char *buf,
1402 unsigned char *end,
1403 size_t *out_len )
1404{
XiaokangQianacb39922022-06-17 10:18:48 +00001405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001406 unsigned char *p = buf;
1407 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001408 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001409 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001410
Jerry Yu4d3841a2022-04-16 12:37:19 +08001411 *out_len = 0;
1412
1413 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001414 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001415 p += 2;
1416
Jerry Yu8937eb42022-05-03 12:12:14 +08001417 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00001418 ((void) ret);
1419 ((void) output_len);
1420
1421#if defined(MBEDTLS_SSL_ALPN)
1422 ret = mbedtls_ssl_write_alpn_ext( ssl, p, end, &output_len );
1423 if( ret != 0 )
1424 return( ret );
XiaokangQian95d5f542022-06-24 02:29:26 +00001425 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001426#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001427
Jerry Yu8937eb42022-05-03 12:12:14 +08001428 extensions_len = ( p - p_extensions_len ) - 2;
1429 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
1430
1431 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001432
1433 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
1434
1435 return( 0 );
1436}
1437
1438static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
1439{
1440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1441 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08001442 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001443
Gabor Mezei54719122022-06-28 11:34:56 +02001444 mbedtls_ssl_set_outbound_transform( ssl,
1445 ssl->handshake->transform_handshake );
1446 MBEDTLS_SSL_DEBUG_MSG(
1447 3, ( "switching to handshake transform for outbound data" ) );
1448
Jerry Yuab452cc2022-04-28 15:27:08 +08001449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001450
Jerry Yu4d3841a2022-04-16 12:37:19 +08001451 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1452 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
1453
1454 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
1455 ssl, buf, buf + buf_len, &msg_len ) );
1456
1457 mbedtls_ssl_add_hs_msg_to_checksum(
1458 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
1459
Jerry Yu4d3841a2022-04-16 12:37:19 +08001460 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1461 ssl, buf_len, msg_len ) );
1462
Jerry Yu8937eb42022-05-03 12:12:14 +08001463#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1464 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1465 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1466 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00001467 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08001468#else
1469 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1470#endif
1471
Jerry Yu4d3841a2022-04-16 12:37:19 +08001472cleanup:
1473
Jerry Yuab452cc2022-04-28 15:27:08 +08001474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001475 return( ret );
1476}
1477
XiaokangQiancec9ae62022-05-06 07:28:50 +00001478#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00001479#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
1480#define SSL_CERTIFICATE_REQUEST_SKIP 1
1481/* Coordination:
1482 * Check whether a CertificateRequest message should be written.
1483 * Returns a negative code on failure, or
1484 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
1485 * - SSL_CERTIFICATE_REQUEST_SKIP
1486 * indicating if the writing of the CertificateRequest
1487 * should be skipped or not.
1488 */
1489static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
1490{
1491 int authmode;
1492
XiaokangQian40a35232022-05-07 09:02:40 +00001493#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1494 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
1495 authmode = ssl->handshake->sni_authmode;
1496 else
1497#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00001498 authmode = ssl->conf->authmode;
1499
1500 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
1501 return( SSL_CERTIFICATE_REQUEST_SKIP );
1502
XiaokangQianc3017f62022-05-13 05:55:41 +00001503 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00001504
XiaokangQiana987e1d2022-05-07 01:25:58 +00001505 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
1506}
1507
XiaokangQiancec9ae62022-05-06 07:28:50 +00001508/*
1509 * struct {
1510 * opaque certificate_request_context<0..2^8-1>;
1511 * Extension extensions<2..2^16-1>;
1512 * } CertificateRequest;
1513 *
1514 */
1515static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
1516 unsigned char *buf,
1517 const unsigned char *end,
1518 size_t *out_len )
1519{
1520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1521 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00001522 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00001523 unsigned char *p_extensions_len;
1524
1525 *out_len = 0;
1526
1527 /* Check if we have enough space:
1528 * - certificate_request_context (1 byte)
1529 * - extensions length (2 bytes)
1530 */
1531 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
1532
1533 /*
1534 * Write certificate_request_context
1535 */
1536 /*
1537 * We use a zero length context for the normal handshake
1538 * messages. For post-authentication handshake messages
1539 * this request context would be set to a non-zero value.
1540 */
1541 *p++ = 0x0;
1542
1543 /*
1544 * Write extensions
1545 */
1546 /* The extensions must contain the signature_algorithms. */
1547 p_extensions_len = p;
1548 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00001549 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001550 if( ret != 0 )
1551 return( ret );
1552
XiaokangQianec6efb92022-05-06 09:53:10 +00001553 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00001554 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001555
1556 *out_len = p - buf;
1557
1558 return( 0 );
1559}
1560
1561static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
1562{
1563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1564
1565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1566
1567 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1568
1569 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
1570 {
1571 unsigned char *buf;
1572 size_t buf_len, msg_len;
1573
1574 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1575 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
1576
1577 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
1578 ssl, buf, buf + buf_len, &msg_len ) );
1579
1580 mbedtls_ssl_add_hs_msg_to_checksum(
1581 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
1582
1583 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1584 ssl, buf_len, msg_len ) );
1585 }
1586 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
1587 {
1588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1589 ret = 0;
1590 }
1591 else
1592 {
1593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1594 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1595 goto cleanup;
1596 }
1597
1598 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1599cleanup:
1600
1601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1602 return( ret );
1603}
XiaokangQiancec9ae62022-05-06 07:28:50 +00001604
Jerry Yu4d3841a2022-04-16 12:37:19 +08001605/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001606 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08001607 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001608static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001609{
Jerry Yu5a26f302022-05-10 20:46:40 +08001610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00001611
1612#if defined(MBEDTLS_X509_CRT_PARSE_C)
1613 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
1614 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08001615 {
Jerry Yub89125b2022-05-13 15:45:49 +08001616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08001617 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1618 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001619 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08001620 }
XiaokangQianfb665a82022-06-15 03:57:21 +00001621#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08001622
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001623 ret = mbedtls_ssl_tls13_write_certificate( ssl );
1624 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08001625 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08001626 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1627 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001628}
1629
1630/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001631 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08001632 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001633static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001634{
Jerry Yu4ff9e142022-04-16 14:57:49 +08001635 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001636 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08001637 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08001638 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1639 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001640}
Jerry Yu5a26f302022-05-10 20:46:40 +08001641#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001642
1643/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001644 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001645 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001646static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001647{
Jerry Yud6e253d2022-05-18 13:59:24 +08001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001649
1650 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1651 if( ret != 0 )
1652 return( ret );
1653
Jerry Yue3d67cb2022-05-19 15:33:10 +08001654 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1655 if( ret != 0 )
1656 {
1657 MBEDTLS_SSL_PEND_FATAL_ALERT(
1658 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1659 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1660 return( ret );
1661 }
XiaokangQianc3017f62022-05-13 05:55:41 +00001662
Ronald Cron63dc4632022-05-31 14:41:53 +02001663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1664 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQian189ded22022-05-10 08:12:17 +00001665
Ronald Cron63dc4632022-05-31 14:41:53 +02001666 if( ssl->handshake->certificate_request_sent )
1667 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
XiaokangQian189ded22022-05-10 08:12:17 +00001668 else
Ronald Cron19385882022-06-15 16:26:13 +02001669 {
1670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate" ) );
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
XiaokangQian189ded22022-05-10 08:12:17 +00001672 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001673 }
Ronald Cron63dc4632022-05-31 14:41:53 +02001674
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001675 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001676}
1677
1678/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001679 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001680 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001681static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001682{
Jerry Yud6e253d2022-05-18 13:59:24 +08001683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1684
Jerry Yuff226982022-04-16 16:52:57 +08001685 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
1686 if( ret != 0 )
1687 return( ret );
1688
Jerry Yue3d67cb2022-05-19 15:33:10 +08001689 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1690 if( ret != 0 )
1691 {
1692 MBEDTLS_SSL_DEBUG_RET( 1,
1693 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1694 }
1695
Jerry Yu03ed50b2022-04-16 17:13:30 +08001696 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08001697 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001698}
1699
1700/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001701 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08001702 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001703static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001704{
Jerry Yu03ed50b2022-04-16 17:13:30 +08001705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1706
Jerry Yu03ed50b2022-04-16 17:13:30 +08001707 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1708 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1709 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001710}
1711
1712/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001713 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00001714 */
Jerry Yu27561932021-08-27 17:07:38 +08001715int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08001716{
XiaokangQian08037552022-04-20 07:16:41 +00001717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001718
1719 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
1720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1721
Jerry Yue3b34122021-09-28 17:53:35 +08001722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
1723 mbedtls_ssl_states_str( ssl->state ),
1724 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08001725
XiaokangQian7807f9f2022-02-15 10:04:37 +00001726 switch( ssl->state )
1727 {
1728 /* start state */
1729 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001730 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00001731 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001732 break;
1733
XiaokangQian7807f9f2022-02-15 10:04:37 +00001734 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00001735 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001736 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00001737 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08001738 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001739
Jerry Yuf41553b2022-05-09 22:20:30 +08001740 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
1741 ret = ssl_tls13_write_hello_retry_request( ssl );
1742 if( ret != 0 )
1743 {
1744 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
1745 return( ret );
1746 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001747 break;
1748
Jerry Yu5b64ae92022-03-30 17:15:02 +08001749 case MBEDTLS_SSL_SERVER_HELLO:
1750 ret = ssl_tls13_write_server_hello( ssl );
1751 break;
1752
Xiaofei Baicba64af2022-02-15 10:00:56 +00001753 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08001754 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001755 if( ret != 0 )
1756 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08001757 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
1758 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001759 }
Jerry Yu48330562022-05-06 21:35:44 +08001760 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08001761
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001762#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1763 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00001764 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001765 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001766
Jerry Yu83da34e2022-04-16 13:59:52 +08001767 case MBEDTLS_SSL_SERVER_CERTIFICATE:
1768 ret = ssl_tls13_write_server_certificate( ssl );
1769 break;
1770
1771 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
1772 ret = ssl_tls13_write_certificate_verify( ssl );
1773 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08001774#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001775
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001776 /*
1777 * Injection of dummy-CCS's for middlebox compatibility
1778 */
1779#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001780 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001781 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1782 if( ret == 0 )
1783 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1784 break;
1785
1786 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
1787 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1788 if( ret == 0 )
1789 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1790 break;
1791#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1792
Jerry Yu69dd8d42022-04-16 12:51:26 +08001793 case MBEDTLS_SSL_SERVER_FINISHED:
1794 ret = ssl_tls13_write_server_finished( ssl );
1795 break;
1796
1797 case MBEDTLS_SSL_CLIENT_FINISHED:
1798 ret = ssl_tls13_process_client_finished( ssl );
1799 break;
1800
Jerry Yu69dd8d42022-04-16 12:51:26 +08001801 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
1802 ret = ssl_tls13_handshake_wrapup( ssl );
1803 break;
1804
XiaokangQian6b916b12022-04-25 07:29:34 +00001805 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1806 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02001807 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00001808 {
Ronald Cron209cae92022-06-07 10:30:19 +02001809 if( ssl->session_negotiate->peer_cert != NULL )
1810 {
1811 mbedtls_ssl_handshake_set_state(
1812 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1813 }
1814 else
Ronald Cron19385882022-06-15 16:26:13 +02001815 {
1816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
Ronald Cron209cae92022-06-07 10:30:19 +02001817 mbedtls_ssl_handshake_set_state(
1818 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001819 }
XiaokangQian6b916b12022-04-25 07:29:34 +00001820 }
1821 break;
1822
1823 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1824 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1825 if( ret == 0 )
1826 {
1827 mbedtls_ssl_handshake_set_state(
1828 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1829 }
1830 break;
1831
XiaokangQian7807f9f2022-02-15 10:04:37 +00001832 default:
1833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00001834 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001835 }
1836
1837 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08001838}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001839
Jerry Yufb4b6472022-01-27 15:03:26 +08001840#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */