blob: 0ebad933f2f387794a77dfb3870c70fa4aa75bf2 [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 Yuf249ef72022-06-15 17:23:33 +0800392 MBEDTLS_SSL_DEBUG_MSG( 2,("Try get sig alg %04x",*sig_alg));
393 if( mbedtls_ssl_tls13_sig_alg_is_available_for_pk(
394 ssl, *sig_alg, &key_cert->cert->pk ) )
XiaokangQian81802f42022-06-10 13:25:22 +0000395 {
XiaokangQianfb665a82022-06-15 03:57:21 +0000396 ssl->handshake->key_cert = key_cert;
397 MBEDTLS_SSL_DEBUG_CRT(
XiaokangQian75fe8c72022-06-15 09:42:45 +0000398 3, "selected certificate (chain)",
XiaokangQianfb665a82022-06-15 03:57:21 +0000399 ssl->handshake->key_cert->cert );
XiaokangQian81802f42022-06-10 13:25:22 +0000400 return( 0 );
401 }
XiaokangQian81802f42022-06-10 13:25:22 +0000402 }
XiaokangQian23c5be62022-06-07 02:04:34 +0000403 }
404
Jerry Yuf249ef72022-06-15 17:23:33 +0800405 MBEDTLS_SSL_DEBUG_MSG( 2,("No signature algorithm found"));
XiaokangQian23c5be62022-06-07 02:04:34 +0000406 return( -1 );
407}
XiaokangQian81802f42022-06-10 13:25:22 +0000408#endif /* MBEDTLS_X509_CRT_PARSE_C &&
409 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +0000410
XiaokangQian4080a7f2022-04-11 09:55:18 +0000411/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000412 *
413 * STATE HANDLING: ClientHello
414 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000415 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +0000416 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000417 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000418 *
419 * In this case, the server progresses to sending its ServerHello.
420 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000421 * 2) The ClientHello was well-formed but didn't match the server's
422 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +0000423 *
424 * For example, the client might not have offered a key share which
425 * the server supports, or the server might require a cookie.
426 *
427 * In this case, the server sends a HelloRetryRequest.
428 *
XiaokangQianb67384d2022-04-19 00:02:38 +0000429 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +0000430 *
431 * In this case, we abort the handshake.
432 *
433 */
434
435/*
XiaokangQian4080a7f2022-04-11 09:55:18 +0000436 * Structure of this message:
437 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000438 * uint16 ProtocolVersion;
439 * opaque Random[32];
440 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +0000441 *
XiaokangQiane8ff3502022-04-22 02:34:40 +0000442 * struct {
443 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
444 * Random random;
445 * opaque legacy_session_id<0..32>;
446 * CipherSuite cipher_suites<2..2^16-2>;
447 * opaque legacy_compression_methods<1..2^8-1>;
448 * Extension extensions<8..2^16-1>;
449 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000450 */
XiaokangQianed582dd2022-04-13 08:21:05 +0000451
452#define SSL_CLIENT_HELLO_OK 0
453#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
454
XiaokangQian4080a7f2022-04-11 09:55:18 +0000455static int ssl_tls13_parse_client_hello( mbedtls_ssl_context *ssl,
456 const unsigned char *buf,
457 const unsigned char *end )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000458{
XiaokangQianb67384d2022-04-19 00:02:38 +0000459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
460 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000461 size_t legacy_session_id_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000462 const unsigned char *cipher_suites;
XiaokangQian318dc762022-04-20 09:43:51 +0000463 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +0000464 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +0000465 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000466 const unsigned char *extensions_end;
Jerry Yu49ca9282022-05-05 11:05:22 +0800467 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000468
XiaokangQian7807f9f2022-02-15 10:04:37 +0000469 const mbedtls_ssl_ciphersuite_t* ciphersuite_info;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000470
471 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
472
473 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000474 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +0000475 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +0000476 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +0000477 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000478 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +0000479 * .. . .. ciphersuite list length ( 2 bytes )
480 * .. . .. ciphersuite list
481 * .. . .. compression alg. list length ( 1 byte )
482 * .. . .. compression alg. list
483 * .. . .. extensions length ( 2 bytes, optional )
484 * .. . .. extensions ( optional )
485 */
486
XiaokangQianb67384d2022-04-19 00:02:38 +0000487 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400488 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +0000489 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
490 * read at least up to session id length without worrying.
491 */
492 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 );
493
494 /* ...
495 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
496 * ...
497 * with ProtocolVersion defined as:
498 * uint16 ProtocolVersion;
499 */
XiaokangQiande333912022-04-20 08:49:42 +0000500 if( mbedtls_ssl_read_version( p, ssl->conf->transport ) !=
501 MBEDTLS_SSL_VERSION_TLS1_2 )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000502 {
503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
504 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
505 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQianb67384d2022-04-19 00:02:38 +0000506 return ( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000507 }
508 p += 2;
509
510 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +0000511 * Only support TLS 1.3 currently, temporarily set the version.
512 */
XiaokangQiande333912022-04-20 08:49:42 +0000513 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +0000514
Jerry Yuc1be19f2022-04-23 16:11:39 +0800515 /* ...
516 * Random random;
517 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000518 * with Random defined as:
519 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000520 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000521 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
XiaokangQian08037552022-04-20 07:16:41 +0000522 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000523
XiaokangQian08037552022-04-20 07:16:41 +0000524 memcpy( &ssl->handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
525 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000526
Jerry Yuc1be19f2022-04-23 16:11:39 +0800527 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000528 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800529 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000530 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000531 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +0000532 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000533
XiaokangQianb67384d2022-04-19 00:02:38 +0000534 if( legacy_session_id_len > sizeof( ssl->session_negotiate->id ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000535 {
536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
537 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
538 }
539
XiaokangQian4080a7f2022-04-11 09:55:18 +0000540 ssl->session_negotiate->id_len = legacy_session_id_len;
XiaokangQianed582dd2022-04-13 08:21:05 +0000541 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
XiaokangQiane8ff3502022-04-22 02:34:40 +0000542 p, legacy_session_id_len );
XiaokangQianb67384d2022-04-19 00:02:38 +0000543 /*
544 * Check we have enough data for the legacy session identifier
545 * and the ciphersuite list length.
546 */
547 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_len + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000548
XiaokangQianed582dd2022-04-13 08:21:05 +0000549 memcpy( &ssl->session_negotiate->id[0], p, legacy_session_id_len );
XiaokangQian4080a7f2022-04-11 09:55:18 +0000550 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000551
XiaokangQian7807f9f2022-02-15 10:04:37 +0000552 cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 );
553 p += 2;
554
XiaokangQianb67384d2022-04-19 00:02:38 +0000555 /* Check we have enough data for the ciphersuite list, the legacy
556 * compression methods and the length of the extensions.
557 */
558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len + 2 + 2 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000559
Jerry Yuc1be19f2022-04-23 16:11:39 +0800560 /* ...
XiaokangQian08037552022-04-20 07:16:41 +0000561 * CipherSuite cipher_suites<2..2^16-2>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800562 * ...
XiaokangQian08037552022-04-20 07:16:41 +0000563 * with CipherSuite defined as:
564 * uint8 CipherSuite[2];
565 */
XiaokangQian060d8672022-04-21 09:24:56 +0000566 cipher_suites = p;
567 cipher_suites_end = p + cipher_suites_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000568 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
569 p, cipher_suites_len );
XiaokangQian17f974c2022-04-19 09:57:41 +0000570 /*
571 * Search for a matching ciphersuite
572 */
XiaokangQian318dc762022-04-20 09:43:51 +0000573 int ciphersuite_match = 0;
XiaokangQian060d8672022-04-21 09:24:56 +0000574 for ( ; p < cipher_suites_end; p += 2 )
XiaokangQian17f974c2022-04-19 09:57:41 +0000575 {
XiaokangQiane8ff3502022-04-22 02:34:40 +0000576 uint16_t cipher_suite;
577 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, cipher_suites_end, 2 );
578 cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
579 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
XiaokangQian08037552022-04-20 07:16:41 +0000580 /*
XiaokangQiane8ff3502022-04-22 02:34:40 +0000581 * Check whether this ciphersuite is valid and offered.
582 */
XiaokangQian08037552022-04-20 07:16:41 +0000583 if( ( mbedtls_ssl_validate_ciphersuite(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800584 ssl, ciphersuite_info, ssl->tls_version,
585 ssl->tls_version ) != 0 ) ||
586 ! mbedtls_ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) )
587 {
XiaokangQian08037552022-04-20 07:16:41 +0000588 continue;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800589 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000590
XiaokangQian08037552022-04-20 07:16:41 +0000591 ssl->session_negotiate->ciphersuite = cipher_suite;
592 ssl->handshake->ciphersuite_info = ciphersuite_info;
XiaokangQian318dc762022-04-20 09:43:51 +0000593 ciphersuite_match = 1;
XiaokangQian17f974c2022-04-19 09:57:41 +0000594
XiaokangQian08037552022-04-20 07:16:41 +0000595 break;
XiaokangQian17f974c2022-04-19 09:57:41 +0000596
XiaokangQian17f974c2022-04-19 09:57:41 +0000597 }
598
Jerry Yuc1be19f2022-04-23 16:11:39 +0800599 if( ! ciphersuite_match )
XiaokangQian318dc762022-04-20 09:43:51 +0000600 {
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000601 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
602 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
603 return ( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQian318dc762022-04-20 09:43:51 +0000604 }
XiaokangQian17f974c2022-04-19 09:57:41 +0000605
606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s",
607 ciphersuite_info->name ) );
608
XiaokangQian060d8672022-04-21 09:24:56 +0000609 p = cipher_suites + cipher_suites_len;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800610
XiaokangQian4080a7f2022-04-11 09:55:18 +0000611 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000612 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000613 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +0000614 */
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000615 if( p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000616 {
XiaokangQian4080a7f2022-04-11 09:55:18 +0000617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
618 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
619 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
620 return ( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000621 }
XiaokangQianb67384d2022-04-19 00:02:38 +0000622 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000623
Jerry Yuc1be19f2022-04-23 16:11:39 +0800624 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000625 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800626 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +0000627 * with Extension defined as:
628 * struct {
629 * ExtensionType extension_type;
630 * opaque extension_data<0..2^16-1>;
631 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000632 */
XiaokangQian4080a7f2022-04-11 09:55:18 +0000633 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000634 p += 2;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000635 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
XiaokangQiane8ff3502022-04-22 02:34:40 +0000636 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000637
XiaokangQian4080a7f2022-04-11 09:55:18 +0000638 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, extensions_len );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000639
640 while( p < extensions_end )
641 {
642 unsigned int extension_type;
643 size_t extension_data_len;
644 const unsigned char *extension_data_end;
645
XiaokangQian318dc762022-04-20 09:43:51 +0000646 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000647 extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
648 extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
649 p += 4;
650
651 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
652 extension_data_end = p + extension_data_len;
653
654 switch( extension_type )
655 {
XiaokangQian40a35232022-05-07 09:02:40 +0000656#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
657 case MBEDTLS_TLS_EXT_SERVERNAME:
658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
XiaokangQian9b2b7712022-05-17 02:57:00 +0000659 ret = mbedtls_ssl_parse_server_name_ext( ssl, p,
660 extension_data_end );
XiaokangQian40a35232022-05-07 09:02:40 +0000661 if( ret != 0 )
662 {
663 MBEDTLS_SSL_DEBUG_RET(
664 1, "mbedtls_ssl_parse_servername_ext", ret );
665 return( ret );
666 }
667 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SERVERNAME;
668 break;
669#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
670
XiaokangQianb67384d2022-04-19 00:02:38 +0000671#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000672 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) );
674
675 /* Supported Groups Extension
676 *
677 * When sent by the client, the "supported_groups" extension
678 * indicates the named groups which the client supports,
679 * ordered from most preferred to least preferred.
680 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800681 ret = ssl_tls13_parse_supported_groups_ext(
682 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000683 if( ret != 0 )
684 {
685 MBEDTLS_SSL_DEBUG_RET( 1,
686 "mbedtls_ssl_parse_supported_groups_ext", ret );
687 return( ret );
688 }
689
690 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
691 break;
XiaokangQianb67384d2022-04-19 00:02:38 +0000692#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000693
XiaokangQian88408882022-04-02 10:15:03 +0000694#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000695 case MBEDTLS_TLS_EXT_KEY_SHARE:
696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) );
697
698 /*
699 * Key Share Extension
700 *
701 * When sent by the client, the "key_share" extension
702 * contains the endpoint's cryptographic parameters for
703 * ECDHE/DHE key establishment methods.
704 */
Jerry Yuc1be19f2022-04-23 16:11:39 +0800705 ret = ssl_tls13_parse_key_shares_ext(
706 ssl, p, extension_data_end );
XiaokangQian08037552022-04-20 07:16:41 +0000707 if( ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000708 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "HRR needed " ) );
Jerry Yu49ca9282022-05-05 11:05:22 +0800710 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000711 }
712
Jerry Yu582dd062022-04-22 21:59:01 +0800713 if( ret < 0 )
714 {
715 MBEDTLS_SSL_DEBUG_RET(
716 1, "ssl_tls13_parse_key_shares_ext", ret );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000717 return( ret );
Jerry Yu582dd062022-04-22 21:59:01 +0800718 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000719
720 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
721 break;
XiaokangQian88408882022-04-02 10:15:03 +0000722#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000723
724 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) );
726
727 ret = ssl_tls13_parse_supported_versions_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800728 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000729 if( ret != 0 )
730 {
731 MBEDTLS_SSL_DEBUG_RET( 1,
732 ( "ssl_tls13_parse_supported_versions_ext" ), ret );
733 return( ret );
734 }
735 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS;
736 break;
737
XiaokangQianacb39922022-06-17 10:18:48 +0000738#if defined(MBEDTLS_SSL_ALPN)
739 case MBEDTLS_TLS_EXT_ALPN:
740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
741
742 ret = mbedtls_ssl_parse_alpn_ext( ssl, p, extension_data_end );
743 if( ret != 0 )
744 {
745 MBEDTLS_SSL_DEBUG_RET(
746 1, ( "mbedtls_ssl_parse_alpn_ext" ), ret );
747 return( ret );
748 }
749 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_ALPN;
750 break;
751#endif /* MBEDTLS_SSL_ALPN */
752
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
754 case MBEDTLS_TLS_EXT_SIG_ALG:
755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
756
Gabor Mezei078e8032022-04-27 21:17:56 +0200757 ret = mbedtls_ssl_parse_sig_alg_ext(
Jerry Yuc1be19f2022-04-23 16:11:39 +0800758 ssl, p, extension_data_end );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 if( ret != 0 )
760 {
761 MBEDTLS_SSL_DEBUG_MSG( 1,
762 ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
763 ret ) );
764 return( ret );
765 }
766 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
767 break;
768#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
769
770 default:
771 MBEDTLS_SSL_DEBUG_MSG( 3,
772 ( "unknown extension found: %ud ( ignoring )",
773 extension_type ) );
774 }
775
776 p += extension_data_len;
777 }
778
779 /* Update checksum with either
780 * - The entire content of the CH message, if no PSK extension is present
781 * - The content up to but excluding the PSK extension, if present.
782 */
XiaokangQian3f84d5d2022-04-19 06:36:17 +0000783 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
XiaokangQianc4b8c992022-04-07 11:31:38 +0000784 buf, p - buf );
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785
786 /* List all the extensions we have received */
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000787#if defined(MBEDTLS_DEBUG_C)
XiaokangQian4080a7f2022-04-11 09:55:18 +0000788 ssl_tls13_debug_print_client_hello_exts( ssl );
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000789#endif /* MBEDTLS_DEBUG_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790
XiaokangQian75fe8c72022-06-15 09:42:45 +0000791 return( hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK );
792}
793
794/* Update the handshake state machine */
795
796static int ssl_tls13_postprocess_client_hello( mbedtls_ssl_context* ssl )
797{
798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
799
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800 /*
XiaokangQianb67384d2022-04-19 00:02:38 +0000801 * Here we only support the ephemeral or (EC)DHE key echange mode
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802 */
XiaokangQianb67384d2022-04-19 00:02:38 +0000803 if( !ssl_tls13_check_ephemeral_key_exchange( ssl ) )
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804 {
805 MBEDTLS_SSL_DEBUG_MSG(
806 1,
807 ( "ClientHello message misses mandatory extensions." ) );
808 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION ,
809 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
810 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
811 }
812
XiaokangQianfb665a82022-06-15 03:57:21 +0000813 /*
814 * Server certificate selection
815 */
816 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
817 {
818 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
819 return( ret );
820 }
821#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
822 ssl->handshake->sni_name = NULL;
823 ssl->handshake->sni_name_len = 0;
824#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825
XiaokangQian7807f9f2022-02-15 10:04:37 +0000826 ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl );
827 if( ret != 0 )
828 {
829 MBEDTLS_SSL_DEBUG_RET( 1,
830 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret );
831 return( ret );
832 }
833
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834 return( 0 );
835
836}
837
838/*
XiaokangQianed582dd2022-04-13 08:21:05 +0000839 * Main entry point from the state machine; orchestrates the otherfunctions.
840 */
841
842static int ssl_tls13_process_client_hello( mbedtls_ssl_context *ssl )
843{
844
XiaokangQian08037552022-04-20 07:16:41 +0000845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianed582dd2022-04-13 08:21:05 +0000846 unsigned char* buf = NULL;
847 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +0800848 int parse_client_hello_ret;
849
XiaokangQianed582dd2022-04-13 08:21:05 +0000850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
851
XiaokangQianed582dd2022-04-13 08:21:05 +0000852 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
853 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
854 &buf, &buflen ) );
855
856 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_parse_client_hello( ssl, buf,
857 buf + buflen ) );
Jerry Yuf41553b2022-05-09 22:20:30 +0800858 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
859 * only SSL_CLIENT_HELLO_OK or
860 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
861 * stage as negative error codes are handled
862 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +0800863
XiaokangQiancfd925f2022-04-14 07:10:37 +0000864 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_client_hello( ssl ) );
Jerry Yu582dd062022-04-22 21:59:01 +0800865
Jerry Yu4ca91402022-05-09 15:50:57 +0800866 if( parse_client_hello_ret == SSL_CLIENT_HELLO_OK )
867 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
868 else
869 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST );
XiaokangQianed582dd2022-04-13 08:21:05 +0000870
871cleanup:
872
873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
874 return( ret );
875}
876
877/*
Jerry Yu1c3e6882022-04-20 21:23:40 +0800878 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +0800879 */
Jerry Yuf4b27e42022-03-30 17:32:21 +0800880static int ssl_tls13_prepare_server_hello( mbedtls_ssl_context *ssl )
881{
Jerry Yu637a3f12022-04-20 21:37:58 +0800882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
883 unsigned char *server_randbytes =
Jerry Yu3bf2c642022-03-30 22:02:12 +0800884 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +0800885 if( ssl->conf->f_rng == NULL )
886 {
887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
888 return( MBEDTLS_ERR_SSL_NO_RNG );
889 }
890
Jerry Yu637a3f12022-04-20 21:37:58 +0800891 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800892 MBEDTLS_SERVER_HELLO_RANDOM_LEN ) ) != 0 )
893 {
894 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
895 return( ret );
896 }
897
Jerry Yu637a3f12022-04-20 21:37:58 +0800898 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", server_randbytes,
Jerry Yuf4b27e42022-03-30 17:32:21 +0800899 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
900
901#if defined(MBEDTLS_HAVE_TIME)
902 ssl->session_negotiate->start = time( NULL );
903#endif /* MBEDTLS_HAVE_TIME */
904
905 return( ret );
906}
907
Jerry Yu3bf2c642022-03-30 22:02:12 +0800908/*
Jerry Yue74e04a2022-04-21 09:23:16 +0800909 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +0800910 *
911 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +0800912 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800913 * } SupportedVersions;
914 */
Jerry Yue74e04a2022-04-21 09:23:16 +0800915static int ssl_tls13_write_server_hello_supported_versions_ext(
916 mbedtls_ssl_context *ssl,
917 unsigned char *buf,
918 unsigned char *end,
919 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800920{
Jerry Yu3bf2c642022-03-30 22:02:12 +0800921 *out_len = 0;
922
Jerry Yu955ddd72022-04-22 22:27:33 +0800923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, write selected version" ) );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800924
925 /* Check if we have space to write the extension:
926 * - extension_type (2 bytes)
927 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +0800928 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800929 */
Jerry Yu349a6132022-04-14 20:52:56 +0800930 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800931
Jerry Yu349a6132022-04-14 20:52:56 +0800932 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800933
Jerry Yu349a6132022-04-14 20:52:56 +0800934 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800935
Jerry Yu349a6132022-04-14 20:52:56 +0800936 mbedtls_ssl_write_version( buf + 4,
937 ssl->conf->transport,
938 ssl->tls_version );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800939
940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%04x]",
941 ssl->tls_version ) );
942
Jerry Yu349a6132022-04-14 20:52:56 +0800943 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +0800944
945 return( 0 );
946}
947
Jerry Yud9436a12022-04-20 22:28:09 +0800948
Jerry Yu3bf2c642022-03-30 22:02:12 +0800949
950/* Generate and export a single key share. For hybrid KEMs, this can
951 * be called multiple times with the different components of the hybrid. */
Jerry Yu955ddd72022-04-22 22:27:33 +0800952static int ssl_tls13_generate_and_write_key_share( mbedtls_ssl_context *ssl,
953 uint16_t named_group,
954 unsigned char *buf,
955 unsigned char *end,
956 size_t *out_len )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800957{
958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +0800959
960 *out_len = 0;
961
Jerry Yu89e103c2022-03-30 22:43:29 +0800962#if defined(MBEDTLS_ECDH_C)
Jerry Yu3bf2c642022-03-30 22:02:12 +0800963 if( mbedtls_ssl_tls13_named_group_is_ecdhe( named_group ) )
964 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800965 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
966 ssl, named_group, buf, end, out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800967 if( ret != 0 )
968 {
Jerry Yu89e103c2022-03-30 22:43:29 +0800969 MBEDTLS_SSL_DEBUG_RET(
970 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
971 ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +0800972 return( ret );
973 }
Jerry Yu3bf2c642022-03-30 22:02:12 +0800974 }
Jerry Yu89e103c2022-03-30 22:43:29 +0800975 else
976#endif /* MBEDTLS_ECDH_C */
977 if( 0 /* Other kinds of KEMs */ )
Jerry Yu3bf2c642022-03-30 22:02:12 +0800978 {
979 }
980 else
981 {
Jerry Yu955ddd72022-04-22 22:27:33 +0800982 ((void) ssl);
983 ((void) named_group);
984 ((void) buf);
985 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +0800986 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
987 }
988
989 return( ret );
990}
991
992/*
993 * ssl_tls13_write_key_share_ext
994 *
995 * Structure of key_share extension in ServerHello:
996 *
Jerry Yu1c3e6882022-04-20 21:23:40 +0800997 * struct {
998 * NamedGroup group;
999 * opaque key_exchange<1..2^16-1>;
1000 * } KeyShareEntry;
1001 * struct {
1002 * KeyShareEntry server_share;
1003 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001004 */
1005static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
1006 unsigned char *buf,
1007 unsigned char *end,
1008 size_t *out_len )
1009{
Jerry Yu955ddd72022-04-22 22:27:33 +08001010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001011 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001012 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001013 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001014 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001015
1016 *out_len = 0;
1017
1018 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding key share extension" ) );
1019
1020 /* Check if we have space for header and length fields:
1021 * - extension_type (2 bytes)
1022 * - extension_data_length (2 bytes)
1023 * - group (2 bytes)
1024 * - key_exchange_length (2 bytes)
1025 */
1026 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 8 );
Jerry Yu57d48412022-04-20 21:50:42 +08001027 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, p, 0 );
1028 MBEDTLS_PUT_UINT16_BE( group, server_share, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001029 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001030
Jerry Yu3bf2c642022-03-30 22:02:12 +08001031 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1032 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001033 ret = ssl_tls13_generate_and_write_key_share(
Jerry Yue65d8012022-04-23 10:34:35 +08001034 ssl, group, server_share + 4, end, &key_exchange_length );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001035 if( ret != 0 )
1036 return( ret );
1037 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001038
Jerry Yu955ddd72022-04-22 22:27:33 +08001039 MBEDTLS_PUT_UINT16_BE( key_exchange_length, server_share + 2, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001040
Jerry Yu57d48412022-04-20 21:50:42 +08001041 MBEDTLS_PUT_UINT16_BE( p - server_share, buf, 2 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001042
Jerry Yu57d48412022-04-20 21:50:42 +08001043 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001044
Jerry Yu3bf2c642022-03-30 22:02:12 +08001045 return( 0 );
1046}
Jerry Yud9436a12022-04-20 22:28:09 +08001047
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001048static int ssl_tls13_write_hrr_key_share_ext( mbedtls_ssl_context *ssl,
1049 unsigned char *buf,
1050 unsigned char *end,
1051 size_t *out_len )
1052{
1053 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1054 /* key_share Extension
1055 *
1056 * struct {
1057 * select (Handshake.msg_type) {
1058 * ...
1059 * case hello_retry_request:
1060 * NamedGroup selected_group;
1061 * ...
1062 * };
1063 * } KeyShare;
1064 */
1065
1066 *out_len = 0;
1067
Jerry Yub0ac10b2022-05-05 11:10:08 +08001068 /*
1069 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1070 * of the HRR is then to transmit a cookie to force the client to demonstrate
1071 * reachability at their apparent network address (primarily useful for DTLS).
1072 */
1073 if( ! mbedtls_ssl_tls13_some_ephemeral_enabled( ssl ) )
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001074 return( 0 );
1075
1076 /* We should only send the key_share extension if the client's initial
1077 * key share was not acceptable. */
1078 if( ssl->handshake->offered_group_id != 0 )
1079 {
1080 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Skip key_share extension in HRR" ) );
1081 return( 0 );
1082 }
1083
1084 if( selected_group == 0 )
1085 {
1086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching named group found" ) );
1087 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1088 }
1089
Jerry Yub0ac10b2022-05-05 11:10:08 +08001090 /* Check if we have enough space:
1091 * - extension_type (2 bytes)
1092 * - extension_data_length (2 bytes)
1093 * - selected_group (2 bytes)
1094 */
Ronald Cron154d1b62022-06-01 15:33:26 +02001095 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 6 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001096
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001097 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001098 MBEDTLS_PUT_UINT16_BE( 2, buf, 2 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001099 MBEDTLS_PUT_UINT16_BE( selected_group, buf, 4 );
1100
1101 MBEDTLS_SSL_DEBUG_MSG( 3,
1102 ( "HRR selected_group: %s (%x)",
1103 mbedtls_ssl_named_group_to_str( selected_group ),
1104 selected_group ) );
1105
1106 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001107
Jerry Yub0ac10b2022-05-05 11:10:08 +08001108 return( 0 );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001109}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001110
1111/*
1112 * Structure of ServerHello message:
1113 *
1114 * struct {
1115 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1116 * Random random;
1117 * opaque legacy_session_id_echo<0..32>;
1118 * CipherSuite cipher_suite;
1119 * uint8 legacy_compression_method = 0;
1120 * Extension extensions<6..2^16-1>;
1121 * } ServerHello;
1122 */
1123static int ssl_tls13_write_server_hello_body( mbedtls_ssl_context *ssl,
Jerry Yu56404d72022-03-30 17:36:13 +08001124 unsigned char *buf,
1125 unsigned char *end,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001126 size_t *out_len,
1127 int is_hrr )
Jerry Yu56404d72022-03-30 17:36:13 +08001128{
Jerry Yu955ddd72022-04-22 22:27:33 +08001129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001130 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001131 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001132 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001133
1134 *out_len = 0;
1135
Jerry Yucfc04b32022-04-21 09:31:58 +08001136 /* ...
1137 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1138 * ...
1139 * with ProtocolVersion defined as:
1140 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001141 */
1142 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1143 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
1144 p += 2;
1145
Jerry Yu1c3e6882022-04-20 21:23:40 +08001146 /* ...
1147 * Random random;
1148 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08001149 * with Random defined as:
1150 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08001151 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001152 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001153 if( is_hrr )
1154 {
1155 memcpy( p, mbedtls_ssl_tls13_hello_retry_request_magic,
Jerry Yufbe3e642022-04-25 19:31:51 +08001156 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001157 }
1158 else
1159 {
1160 memcpy( p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
Jerry Yufbe3e642022-04-25 19:31:51 +08001161 MBEDTLS_SERVER_HELLO_RANDOM_LEN );
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001162 }
Jerry Yu955ddd72022-04-22 22:27:33 +08001163 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
Jerry Yu3bf2c642022-03-30 22:02:12 +08001164 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
1165 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1166
Jerry Yucfc04b32022-04-21 09:31:58 +08001167 /* ...
1168 * opaque legacy_session_id_echo<0..32>;
1169 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08001170 */
1171 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + ssl->session_negotiate->id_len );
1172 *p++ = (unsigned char)ssl->session_negotiate->id_len;
1173 if( ssl->session_negotiate->id_len > 0 )
1174 {
1175 memcpy( p, &ssl->session_negotiate->id[0],
1176 ssl->session_negotiate->id_len );
1177 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08001178
Jerry Yu3bf2c642022-03-30 22:02:12 +08001179 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
1180 ssl->session_negotiate->id_len );
1181 }
1182
Jerry Yucfc04b32022-04-21 09:31:58 +08001183 /* ...
1184 * CipherSuite cipher_suite;
1185 * ...
1186 * with CipherSuite defined as:
1187 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08001188 */
1189 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1190 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
1191 p += 2;
1192 MBEDTLS_SSL_DEBUG_MSG( 3,
1193 ( "server hello, chosen ciphersuite: %s ( id=%d )",
1194 mbedtls_ssl_get_ciphersuite_name(
1195 ssl->session_negotiate->ciphersuite ),
1196 ssl->session_negotiate->ciphersuite ) );
1197
Jerry Yucfc04b32022-04-21 09:31:58 +08001198 /* ...
1199 * uint8 legacy_compression_method = 0;
1200 * ...
1201 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001202 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1203 *p++ = 0x0;
1204
Jerry Yucfc04b32022-04-21 09:31:58 +08001205 /* ...
1206 * Extension extensions<6..2^16-1>;
1207 * ...
1208 * struct {
1209 * ExtensionType extension_type; (2 bytes)
1210 * opaque extension_data<0..2^16-1>;
1211 * } Extension;
1212 */
Jerry Yu3bf2c642022-03-30 22:02:12 +08001213 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yud9436a12022-04-20 22:28:09 +08001214 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001215 p += 2;
1216
Jerry Yue74e04a2022-04-21 09:23:16 +08001217 if( ( ret = ssl_tls13_write_server_hello_supported_versions_ext(
Jerry Yu3bf2c642022-03-30 22:02:12 +08001218 ssl, p, end, &output_len ) ) != 0 )
1219 {
Jerry Yu955ddd72022-04-22 22:27:33 +08001220 MBEDTLS_SSL_DEBUG_RET(
1221 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001222 return( ret );
1223 }
1224 p += output_len;
1225
Jerry Yu3bf2c642022-03-30 22:02:12 +08001226 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
1227 {
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001228 if( is_hrr )
1229 ret = ssl_tls13_write_hrr_key_share_ext( ssl, p, end, &output_len );
1230 else
1231 ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001232 if( ret != 0 )
1233 return( ret );
1234 p += output_len;
1235 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001236
Jerry Yud9436a12022-04-20 22:28:09 +08001237 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001238
Jerry Yud9436a12022-04-20 22:28:09 +08001239 MBEDTLS_SSL_DEBUG_BUF( 4, "server hello extensions",
1240 p_extensions_len, p - p_extensions_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001241
Jerry Yud9436a12022-04-20 22:28:09 +08001242 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001243
Jerry Yud9436a12022-04-20 22:28:09 +08001244 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello", buf, *out_len );
Jerry Yu3bf2c642022-03-30 22:02:12 +08001245
1246 return( ret );
Jerry Yu56404d72022-03-30 17:36:13 +08001247}
1248
Jerry Yue110d252022-05-05 10:19:22 +08001249static int ssl_tls13_finalize_write_server_hello( mbedtls_ssl_context *ssl )
1250{
1251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf86eb752022-05-06 11:16:55 +08001252 ret = mbedtls_ssl_tls13_compute_handshake_transform( ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001253 if( ret != 0 )
1254 {
Jerry Yuf86eb752022-05-06 11:16:55 +08001255 MBEDTLS_SSL_DEBUG_RET( 1,
1256 "mbedtls_ssl_tls13_compute_handshake_transform",
Jerry Yue110d252022-05-05 10:19:22 +08001257 ret );
1258 return( ret );
1259 }
1260
Jerry Yue110d252022-05-05 10:19:22 +08001261 return( ret );
1262}
1263
Jerry Yu5b64ae92022-03-30 17:15:02 +08001264static int ssl_tls13_write_server_hello( mbedtls_ssl_context *ssl )
1265{
Jerry Yu637a3f12022-04-20 21:37:58 +08001266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001267 unsigned char *buf;
1268 size_t buf_len, msg_len;
1269
1270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1271
Jerry Yuf4b27e42022-03-30 17:32:21 +08001272 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_server_hello( ssl ) );
1273
Jerry Yu3bf2c642022-03-30 22:02:12 +08001274 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuf4b27e42022-03-30 17:32:21 +08001275 MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len ) );
1276
Jerry Yu3bf2c642022-03-30 22:02:12 +08001277 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1278 buf + buf_len,
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001279 &msg_len,
1280 0 ) );
Jerry Yuf4b27e42022-03-30 17:32:21 +08001281
Jerry Yu3bf2c642022-03-30 22:02:12 +08001282 mbedtls_ssl_add_hs_msg_to_checksum(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001283 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1284
Jerry Yu3bf2c642022-03-30 22:02:12 +08001285 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuf4b27e42022-03-30 17:32:21 +08001286 ssl, buf_len, msg_len ) );
Jerry Yu637a3f12022-04-20 21:37:58 +08001287
Jerry Yue110d252022-05-05 10:19:22 +08001288 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_server_hello( ssl ) );
1289
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001290#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1291 /* The server sends a dummy change_cipher_spec record immediately
1292 * after its first handshake message. This may either be after
1293 * a ServerHello or a HelloRetryRequest.
1294 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001295 mbedtls_ssl_handshake_set_state(
1296 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001297#else
Jerry Yu637a3f12022-04-20 21:37:58 +08001298 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001299#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08001300
Jerry Yuf4b27e42022-03-30 17:32:21 +08001301cleanup:
1302
1303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1304 return( ret );
Jerry Yu5b64ae92022-03-30 17:15:02 +08001305}
1306
Jerry Yu23d1a252022-05-12 18:08:59 +08001307
1308/*
1309 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
1310 */
Ronald Cron7b840462022-06-01 17:05:53 +02001311static int ssl_tls13_prepare_hello_retry_request( mbedtls_ssl_context *ssl )
Jerry Yu23d1a252022-05-12 18:08:59 +08001312{
1313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1314 if( ssl->handshake->hello_retry_request_count > 0 )
1315 {
1316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Too many HRRs" ) );
1317 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1318 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1319 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1320 }
1321
1322 /*
1323 * Create stateless transcript hash for HRR
1324 */
1325 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) );
1326 ret = mbedtls_ssl_reset_transcript_for_hrr( ssl );
1327 if( ret != 0 )
1328 {
1329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", ret );
1330 return( ret );
1331 }
1332 mbedtls_ssl_session_reset_msg_layer( ssl, 0 );
1333
1334 return( 0 );
1335}
1336
1337static int ssl_tls13_write_hello_retry_request( mbedtls_ssl_context *ssl )
1338{
1339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1340 unsigned char *buf;
1341 size_t buf_len, msg_len;
1342
1343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello retry request" ) );
1344
Ronald Cron7b840462022-06-01 17:05:53 +02001345 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_hello_retry_request( ssl ) );
Jerry Yu23d1a252022-05-12 18:08:59 +08001346
1347 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
1348 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1349 &buf, &buf_len ) );
1350
1351 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_server_hello_body( ssl, buf,
1352 buf + buf_len,
1353 &msg_len,
1354 1 ) );
1355 mbedtls_ssl_add_hs_msg_to_checksum(
1356 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len );
1357
1358
1359 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl, buf_len,
1360 msg_len ) );
1361
1362 ssl->handshake->hello_retry_request_count++;
1363
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001364#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1365 /* The server sends a dummy change_cipher_spec record immediately
1366 * after its first handshake message. This may either be after
1367 * a ServerHello or a HelloRetryRequest.
1368 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02001369 mbedtls_ssl_handshake_set_state(
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001370 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001371#else
Jerry Yu23d1a252022-05-12 18:08:59 +08001372 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001373#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08001374
1375cleanup:
1376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello retry request" ) );
1377 return( ret );
1378}
1379
Jerry Yu5b64ae92022-03-30 17:15:02 +08001380/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08001381 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
1382 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001383
1384/*
1385 * struct {
1386 * Extension extensions<0..2 ^ 16 - 1>;
1387 * } EncryptedExtensions;
1388 *
1389 */
1390static int ssl_tls13_write_encrypted_extensions_body( mbedtls_ssl_context *ssl,
1391 unsigned char *buf,
1392 unsigned char *end,
1393 size_t *out_len )
1394{
XiaokangQianacb39922022-06-17 10:18:48 +00001395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001396 unsigned char *p = buf;
1397 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08001398 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001399 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08001400
Jerry Yu4d3841a2022-04-16 12:37:19 +08001401 *out_len = 0;
1402
1403 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerry Yu8937eb42022-05-03 12:12:14 +08001404 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001405 p += 2;
1406
Jerry Yu8937eb42022-05-03 12:12:14 +08001407 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00001408 ((void) ret);
1409 ((void) output_len);
1410
1411#if defined(MBEDTLS_SSL_ALPN)
1412 ret = mbedtls_ssl_write_alpn_ext( ssl, p, end, &output_len );
1413 if( ret != 0 )
1414 return( ret );
XiaokangQian95d5f542022-06-24 02:29:26 +00001415 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00001416#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08001417
Jerry Yu8937eb42022-05-03 12:12:14 +08001418 extensions_len = ( p - p_extensions_len ) - 2;
1419 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
1420
1421 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001422
1423 MBEDTLS_SSL_DEBUG_BUF( 4, "encrypted extensions", buf, *out_len );
1424
1425 return( 0 );
1426}
1427
1428static int ssl_tls13_write_encrypted_extensions( mbedtls_ssl_context *ssl )
1429{
1430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1431 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08001432 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08001433
Gabor Mezei54719122022-06-28 11:34:56 +02001434 mbedtls_ssl_set_outbound_transform( ssl,
1435 ssl->handshake->transform_handshake );
1436 MBEDTLS_SSL_DEBUG_MSG(
1437 3, ( "switching to handshake transform for outbound data" ) );
1438
Jerry Yuab452cc2022-04-28 15:27:08 +08001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001440
Jerry Yu4d3841a2022-04-16 12:37:19 +08001441 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1442 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf, &buf_len ) );
1443
1444 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_encrypted_extensions_body(
1445 ssl, buf, buf + buf_len, &msg_len ) );
1446
1447 mbedtls_ssl_add_hs_msg_to_checksum(
1448 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len );
1449
Jerry Yu4d3841a2022-04-16 12:37:19 +08001450 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1451 ssl, buf_len, msg_len ) );
1452
Jerry Yu8937eb42022-05-03 12:12:14 +08001453#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1454 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
1455 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1456 else
XiaokangQiana987e1d2022-05-07 01:25:58 +00001457 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
Jerry Yu8937eb42022-05-03 12:12:14 +08001458#else
1459 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1460#endif
1461
Jerry Yu4d3841a2022-04-16 12:37:19 +08001462cleanup:
1463
Jerry Yuab452cc2022-04-28 15:27:08 +08001464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write encrypted extensions" ) );
Jerry Yu4d3841a2022-04-16 12:37:19 +08001465 return( ret );
1466}
1467
XiaokangQiancec9ae62022-05-06 07:28:50 +00001468#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00001469#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
1470#define SSL_CERTIFICATE_REQUEST_SKIP 1
1471/* Coordination:
1472 * Check whether a CertificateRequest message should be written.
1473 * Returns a negative code on failure, or
1474 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
1475 * - SSL_CERTIFICATE_REQUEST_SKIP
1476 * indicating if the writing of the CertificateRequest
1477 * should be skipped or not.
1478 */
1479static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
1480{
1481 int authmode;
1482
XiaokangQian40a35232022-05-07 09:02:40 +00001483#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1484 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
1485 authmode = ssl->handshake->sni_authmode;
1486 else
1487#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00001488 authmode = ssl->conf->authmode;
1489
1490 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
1491 return( SSL_CERTIFICATE_REQUEST_SKIP );
1492
XiaokangQianc3017f62022-05-13 05:55:41 +00001493 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00001494
XiaokangQiana987e1d2022-05-07 01:25:58 +00001495 return( SSL_CERTIFICATE_REQUEST_SEND_REQUEST );
1496}
1497
XiaokangQiancec9ae62022-05-06 07:28:50 +00001498/*
1499 * struct {
1500 * opaque certificate_request_context<0..2^8-1>;
1501 * Extension extensions<2..2^16-1>;
1502 * } CertificateRequest;
1503 *
1504 */
1505static int ssl_tls13_write_certificate_request_body( mbedtls_ssl_context *ssl,
1506 unsigned char *buf,
1507 const unsigned char *end,
1508 size_t *out_len )
1509{
1510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1511 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00001512 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00001513 unsigned char *p_extensions_len;
1514
1515 *out_len = 0;
1516
1517 /* Check if we have enough space:
1518 * - certificate_request_context (1 byte)
1519 * - extensions length (2 bytes)
1520 */
1521 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
1522
1523 /*
1524 * Write certificate_request_context
1525 */
1526 /*
1527 * We use a zero length context for the normal handshake
1528 * messages. For post-authentication handshake messages
1529 * this request context would be set to a non-zero value.
1530 */
1531 *p++ = 0x0;
1532
1533 /*
1534 * Write extensions
1535 */
1536 /* The extensions must contain the signature_algorithms. */
1537 p_extensions_len = p;
1538 p += 2;
XiaokangQianec6efb92022-05-06 09:53:10 +00001539 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001540 if( ret != 0 )
1541 return( ret );
1542
XiaokangQianec6efb92022-05-06 09:53:10 +00001543 p += output_len;
XiaokangQianaad9b0a2022-05-09 01:11:21 +00001544 MBEDTLS_PUT_UINT16_BE( p - p_extensions_len - 2, p_extensions_len, 0 );
XiaokangQiancec9ae62022-05-06 07:28:50 +00001545
1546 *out_len = p - buf;
1547
1548 return( 0 );
1549}
1550
1551static int ssl_tls13_write_certificate_request( mbedtls_ssl_context *ssl )
1552{
1553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1554
1555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1556
1557 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
1558
1559 if( ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST )
1560 {
1561 unsigned char *buf;
1562 size_t buf_len, msg_len;
1563
1564 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
1565 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, &buf, &buf_len ) );
1566
1567 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_request_body(
1568 ssl, buf, buf + buf_len, &msg_len ) );
1569
1570 mbedtls_ssl_add_hs_msg_to_checksum(
1571 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len );
1572
1573 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1574 ssl, buf_len, msg_len ) );
1575 }
1576 else if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
1577 {
1578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1579 ret = 0;
1580 }
1581 else
1582 {
1583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1584 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1585 goto cleanup;
1586 }
1587
1588 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
1589cleanup:
1590
1591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1592 return( ret );
1593}
XiaokangQiancec9ae62022-05-06 07:28:50 +00001594
Jerry Yu4d3841a2022-04-16 12:37:19 +08001595/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001596 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08001597 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001598static int ssl_tls13_write_server_certificate( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001599{
Jerry Yu5a26f302022-05-10 20:46:40 +08001600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00001601
1602#if defined(MBEDTLS_X509_CRT_PARSE_C)
1603 if( ( ssl_tls13_pick_key_cert( ssl ) != 0 ) ||
1604 mbedtls_ssl_own_cert( ssl ) == NULL )
Jerry Yu5a26f302022-05-10 20:46:40 +08001605 {
Jerry Yub89125b2022-05-13 15:45:49 +08001606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "No certificate available." ) );
Jerry Yu5a26f302022-05-10 20:46:40 +08001607 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1608 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001609 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu5a26f302022-05-10 20:46:40 +08001610 }
XiaokangQianfb665a82022-06-15 03:57:21 +00001611#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08001612
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001613 ret = mbedtls_ssl_tls13_write_certificate( ssl );
1614 if( ret != 0 )
Jerry Yu1bff7112022-04-16 14:29:11 +08001615 return( ret );
Jerry Yu1bff7112022-04-16 14:29:11 +08001616 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
1617 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001618}
1619
1620/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001621 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08001622 */
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08001623static int ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu83da34e2022-04-16 13:59:52 +08001624{
Jerry Yu4ff9e142022-04-16 14:57:49 +08001625 int ret = mbedtls_ssl_tls13_write_certificate_verify( ssl );
Jerry Yuf1c3c4e2022-05-10 11:36:35 +08001626 if( ret != 0 )
Jerry Yu4ff9e142022-04-16 14:57:49 +08001627 return( ret );
Jerry Yu4ff9e142022-04-16 14:57:49 +08001628 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1629 return( 0 );
Jerry Yu83da34e2022-04-16 13:59:52 +08001630}
Jerry Yu5a26f302022-05-10 20:46:40 +08001631#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001632
1633/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001634 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001635 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001636static int ssl_tls13_write_server_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001637{
Jerry Yud6e253d2022-05-18 13:59:24 +08001638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001639
1640 ret = mbedtls_ssl_tls13_write_finished_message( ssl );
1641 if( ret != 0 )
1642 return( ret );
1643
Jerry Yue3d67cb2022-05-19 15:33:10 +08001644 ret = mbedtls_ssl_tls13_compute_application_transform( ssl );
1645 if( ret != 0 )
1646 {
1647 MBEDTLS_SSL_PEND_FATAL_ALERT(
1648 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1649 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1650 return( ret );
1651 }
XiaokangQianc3017f62022-05-13 05:55:41 +00001652
Ronald Cron63dc4632022-05-31 14:41:53 +02001653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
1654 mbedtls_ssl_set_inbound_transform( ssl, ssl->handshake->transform_handshake );
XiaokangQian189ded22022-05-10 08:12:17 +00001655
Ronald Cron63dc4632022-05-31 14:41:53 +02001656 if( ssl->handshake->certificate_request_sent )
1657 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE );
XiaokangQian189ded22022-05-10 08:12:17 +00001658 else
Ronald Cron19385882022-06-15 16:26:13 +02001659 {
1660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate" ) );
1661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
XiaokangQian189ded22022-05-10 08:12:17 +00001662 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001663 }
Ronald Cron63dc4632022-05-31 14:41:53 +02001664
Jerry Yu27bdc7c2022-04-16 13:33:27 +08001665 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001666}
1667
1668/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001669 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08001670 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001671static int ssl_tls13_process_client_finished( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001672{
Jerry Yud6e253d2022-05-18 13:59:24 +08001673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1674
Jerry Yuff226982022-04-16 16:52:57 +08001675 ret = mbedtls_ssl_tls13_process_finished_message( ssl );
1676 if( ret != 0 )
1677 return( ret );
1678
Jerry Yue3d67cb2022-05-19 15:33:10 +08001679 ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
1680 if( ret != 0 )
1681 {
1682 MBEDTLS_SSL_DEBUG_RET( 1,
1683 "mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
1684 }
1685
Jerry Yu03ed50b2022-04-16 17:13:30 +08001686 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
Jerry Yuff226982022-04-16 16:52:57 +08001687 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001688}
1689
1690/*
Jerry Yud6e253d2022-05-18 13:59:24 +08001691 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08001692 */
Jerry Yu4d8567f2022-04-17 10:57:57 +08001693static int ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
Jerry Yu69dd8d42022-04-16 12:51:26 +08001694{
Jerry Yu03ed50b2022-04-16 17:13:30 +08001695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
1696
Jerry Yu03ed50b2022-04-16 17:13:30 +08001697 mbedtls_ssl_tls13_handshake_wrapup( ssl );
1698 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
1699 return( 0 );
Jerry Yu69dd8d42022-04-16 12:51:26 +08001700}
1701
1702/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00001703 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00001704 */
Jerry Yu27561932021-08-27 17:07:38 +08001705int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl )
Jerry Yub9930e72021-08-06 17:11:51 +08001706{
XiaokangQian08037552022-04-20 07:16:41 +00001707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001708
1709 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
1710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1711
Jerry Yue3b34122021-09-28 17:53:35 +08001712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)",
1713 mbedtls_ssl_states_str( ssl->state ),
1714 ssl->state ) );
Jerry Yu6e81b272021-09-27 11:16:17 +08001715
XiaokangQian7807f9f2022-02-15 10:04:37 +00001716 switch( ssl->state )
1717 {
1718 /* start state */
1719 case MBEDTLS_SSL_HELLO_REQUEST:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001720 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
XiaokangQian08037552022-04-20 07:16:41 +00001721 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001722 break;
1723
XiaokangQian7807f9f2022-02-15 10:04:37 +00001724 case MBEDTLS_SSL_CLIENT_HELLO:
XiaokangQian4080a7f2022-04-11 09:55:18 +00001725 ret = ssl_tls13_process_client_hello( ssl );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001726 if( ret != 0 )
XiaokangQian4080a7f2022-04-11 09:55:18 +00001727 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_process_client_hello", ret );
Jerry Yuf41553b2022-05-09 22:20:30 +08001728 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001729
Jerry Yuf41553b2022-05-09 22:20:30 +08001730 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
1731 ret = ssl_tls13_write_hello_retry_request( ssl );
1732 if( ret != 0 )
1733 {
1734 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_hello_retry_request", ret );
1735 return( ret );
1736 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001737 break;
1738
Jerry Yu5b64ae92022-03-30 17:15:02 +08001739 case MBEDTLS_SSL_SERVER_HELLO:
1740 ret = ssl_tls13_write_server_hello( ssl );
1741 break;
1742
Xiaofei Baicba64af2022-02-15 10:00:56 +00001743 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Jerry Yu4d3841a2022-04-16 12:37:19 +08001744 ret = ssl_tls13_write_encrypted_extensions( ssl );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001745 if( ret != 0 )
1746 {
Jerry Yu4d3841a2022-04-16 12:37:19 +08001747 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_encrypted_extensions", ret );
1748 return( ret );
Xiaofei Baicba64af2022-02-15 10:00:56 +00001749 }
Jerry Yu48330562022-05-06 21:35:44 +08001750 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08001751
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001752#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1753 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Xiaofei Bai5ee73d82022-03-14 02:48:30 +00001754 ret = ssl_tls13_write_certificate_request( ssl );
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001755 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00001756
Jerry Yu83da34e2022-04-16 13:59:52 +08001757 case MBEDTLS_SSL_SERVER_CERTIFICATE:
1758 ret = ssl_tls13_write_server_certificate( ssl );
1759 break;
1760
1761 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
1762 ret = ssl_tls13_write_certificate_verify( ssl );
1763 break;
Jerry Yu5a26f302022-05-10 20:46:40 +08001764#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08001765
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001766 /*
1767 * Injection of dummy-CCS's for middlebox compatibility
1768 */
1769#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02001770 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gabor Mezei7b39bf12022-05-24 16:04:14 +02001771 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1772 if( ret == 0 )
1773 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1774 break;
1775
1776 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
1777 ret = mbedtls_ssl_tls13_write_change_cipher_spec( ssl );
1778 if( ret == 0 )
1779 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
1780 break;
1781#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1782
Jerry Yu69dd8d42022-04-16 12:51:26 +08001783 case MBEDTLS_SSL_SERVER_FINISHED:
1784 ret = ssl_tls13_write_server_finished( ssl );
1785 break;
1786
1787 case MBEDTLS_SSL_CLIENT_FINISHED:
1788 ret = ssl_tls13_process_client_finished( ssl );
1789 break;
1790
Jerry Yu69dd8d42022-04-16 12:51:26 +08001791 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
1792 ret = ssl_tls13_handshake_wrapup( ssl );
1793 break;
1794
XiaokangQian6b916b12022-04-25 07:29:34 +00001795 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
1796 ret = mbedtls_ssl_tls13_process_certificate( ssl );
Ronald Cron209cae92022-06-07 10:30:19 +02001797 if( ret == 0 )
XiaokangQian6b916b12022-04-25 07:29:34 +00001798 {
Ronald Cron209cae92022-06-07 10:30:19 +02001799 if( ssl->session_negotiate->peer_cert != NULL )
1800 {
1801 mbedtls_ssl_handshake_set_state(
1802 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
1803 }
1804 else
Ronald Cron19385882022-06-15 16:26:13 +02001805 {
1806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "skip parse certificate verify" ) );
Ronald Cron209cae92022-06-07 10:30:19 +02001807 mbedtls_ssl_handshake_set_state(
1808 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Ronald Cron19385882022-06-15 16:26:13 +02001809 }
XiaokangQian6b916b12022-04-25 07:29:34 +00001810 }
1811 break;
1812
1813 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
1814 ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
1815 if( ret == 0 )
1816 {
1817 mbedtls_ssl_handshake_set_state(
1818 ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1819 }
1820 break;
1821
XiaokangQian7807f9f2022-02-15 10:04:37 +00001822 default:
1823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
XiaokangQian060d8672022-04-21 09:24:56 +00001824 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian7807f9f2022-02-15 10:04:37 +00001825 }
1826
1827 return( ret );
Jerry Yub9930e72021-08-06 17:11:51 +08001828}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001829
Jerry Yufb4b6472022-01-27 15:03:26 +08001830#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */