blob: 29569d176e199650fd83e0250ec0100a305fc627 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS server-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
SimonBd5800b72016-04-26 07:43:27 +010024#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
27#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010030#endif
31
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Chris Jones84a773f2021-03-05 18:38:47 +000033#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050036#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000037
38#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010045#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
49int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020050 const unsigned char *info,
51 size_t ilen )
52{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020053 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020057
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020060
61 memcpy( ssl->cli_id, info, ilen );
62 ssl->cli_id_len = ilen;
63
64 return( 0 );
65}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020066
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020067void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_ssl_cookie_write_t *f_cookie_write,
69 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020070 void *p_cookie )
71{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020072 conf->f_cookie_write = f_cookie_write;
73 conf->f_cookie_check = f_cookie_check;
74 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000080 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000081 size_t len )
82{
Janos Follath865b3eb2019-12-16 11:46:15 +000083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000084 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000085 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010088
Philippe Antoine747fd532018-05-30 09:13:21 +020089 if( len < 2 )
90 {
91 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
92 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
93 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
94 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
95 }
Paul Bakker5701cdc2012-09-27 21:49:42 +000096 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
97 if( servername_list_size + 2 != len )
98 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000103 }
104
105 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200106 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000107 {
108 hostname_len = ( ( p[1] << 8 ) | p[2] );
109 if( hostname_len + 3 > servername_list_size )
110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000115 }
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000118 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200119 ret = ssl->conf->f_sni( ssl->conf->p_sni,
120 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000121 if( ret != 0 )
122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
125 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
126 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000127 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000128 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000129 }
130
131 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000132 p += hostname_len + 3;
133 }
134
135 if( servername_list_size != 0 )
136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200138 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
139 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000141 }
142
143 return( 0 );
144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000146
Gilles Peskineeccd8882020-03-10 12:19:08 +0100147#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100148static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
149{
150 if( conf->f_psk != NULL )
151 return( 1 );
152
153 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
154 return( 0 );
155
156 if( conf->psk != NULL && conf->psk_len != 0 )
157 return( 1 );
158
159#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +0200160 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100161 return( 1 );
162#endif /* MBEDTLS_USE_PSA_CRYPTO */
163
164 return( 0 );
165}
166
167#if defined(MBEDTLS_USE_PSA_CRYPTO)
168static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
169{
170 if( ssl->conf->f_psk != NULL )
171 {
172 /* If we've used a callback to select the PSK,
173 * the static configuration is irrelevant. */
174
Ronald Croncf56a0a2020-08-04 09:51:30 +0200175 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100176 return( 1 );
177
178 return( 0 );
179 }
180
Ronald Croncf56a0a2020-08-04 09:51:30 +0200181 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100182 return( 1 );
183
184 return( 0 );
185}
186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100187#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000190 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000191 size_t len )
192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_RENEGOTIATION)
194 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100195 {
196 /* Check verify-data in constant-time. The length OTOH is no secret */
197 if( len != 1 + ssl->verify_data_len ||
198 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100200 ssl->verify_data_len ) != 0 )
201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200203 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
204 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100206 }
207 }
208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000210 {
211 if( len != 1 || buf[0] != 0x0 )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200214 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
215 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +0000217 }
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000220 }
Paul Bakker48916f92012-09-16 19:57:18 +0000221
222 return( 0 );
223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100226 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100227
228/*
229 * Status of the implementation of signature-algorithms extension:
230 *
231 * Currently, we are only considering the signature-algorithm extension
232 * to pick a ciphersuite which allows us to send the ServerKeyExchange
233 * message with a signature-hash combination that the user allows.
234 *
235 * We do *not* check whether all certificates in our certificate
236 * chain are signed with an allowed signature-hash pair.
237 * This needs to be done at a later stage.
238 *
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000241 const unsigned char *buf,
242 size_t len )
243{
244 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100245
Paul Bakker23f36802012-09-28 14:15:14 +0000246 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200247 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200248
Hanno Becker7e5437a2017-04-28 17:15:26 +0100249 mbedtls_md_type_t md_cur;
250 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000251
Philippe Antoine747fd532018-05-30 09:13:21 +0200252 if ( len < 2 ) {
253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
255 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
256 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
257 }
Paul Bakker23f36802012-09-28 14:15:14 +0000258 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
259 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200260 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker23f36802012-09-28 14:15:14 +0000266 }
267
Hanno Becker7e5437a2017-04-28 17:15:26 +0100268 /* Currently we only guarantee signing the ServerKeyExchange message according
269 * to the constraints specified in this extension (see above), so it suffices
270 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200271 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100272 * This will change when we also consider certificate signatures,
273 * in which case we will need to remember the whole signature-hash
274 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200275 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100276
277 for( p = buf + 2; p < end; p += 2 )
278 {
279 /* Silently ignore unknown signature or hash algorithms. */
280
281 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
282 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
284 " unknown sig alg encoding %d", p[1] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100285 continue;
286 }
287
288 /* Check if we support the hash the user proposes */
289 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
290 if( md_cur == MBEDTLS_MD_NONE )
291 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
293 " unknown hash alg encoding %d", p[0] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100294 continue;
295 }
296
297 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
298 {
299 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
Paul Elliott9f352112020-12-09 14:55:45 +0000301 " match sig %u and hash %u",
Paul Elliott3891caf2020-12-17 18:42:40 +0000302 (unsigned) sig_cur, (unsigned) md_cur ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100303 }
304 else
305 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
Paul Elliott3891caf2020-12-17 18:42:40 +0000307 "hash alg %u not supported", (unsigned) md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000308 }
Paul Bakker23f36802012-09-28 14:15:14 +0000309 }
310
Paul Bakker23f36802012-09-28 14:15:14 +0000311 return( 0 );
312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100314 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000315
Robert Cragie136884c2015-10-02 13:34:31 +0100316#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100317 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200319 const unsigned char *buf,
320 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100321{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200322 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100323 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100325
Philippe Antoine747fd532018-05-30 09:13:21 +0200326 if ( len < 2 ) {
327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
330 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
331 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100332 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
333 if( list_size + 2 != len ||
334 list_size % 2 != 0 )
335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100340 }
341
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200342 /* Should never happen unless client duplicates the extension */
343 if( ssl->handshake->curves != NULL )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
347 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200349 }
350
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100351 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200352 * and leave room for a final 0 */
353 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( our_size > MBEDTLS_ECP_DP_MAX )
355 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200356
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200357 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200358 {
359 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
360 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200361 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200362 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200363
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200364 ssl->handshake->curves = curves;
365
Paul Bakker41c83d32013-03-20 14:39:14 +0100366 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200367 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200370
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200371 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100372 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200373 *curves++ = curve_info;
374 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100375 }
376
377 list_size -= 2;
378 p += 2;
379 }
380
381 return( 0 );
382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200385 const unsigned char *buf,
386 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100387{
388 size_t list_size;
389 const unsigned char *p;
390
Philippe Antoine747fd532018-05-30 09:13:21 +0200391 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100397 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200398 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100399
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200400 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100401 while( list_size > 0 )
402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
404 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100405 {
Robert Cragie136884c2015-10-02 13:34:31 +0100406#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200407 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100408#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100409#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200410 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
411 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100414 return( 0 );
415 }
416
417 list_size--;
418 p++;
419 }
420
421 return( 0 );
422}
Robert Cragieae8535d2015-10-06 17:11:18 +0100423#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
424 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100425
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200426#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
427static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
428 const unsigned char *buf,
429 size_t len )
430{
Janos Follath865b3eb2019-12-16 11:46:15 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200432
433 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
434 {
435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
436 return( 0 );
437 }
438
439 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
440 buf, len ) ) != 0 )
441 {
442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
444 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200445 return( ret );
446 }
447
448 /* Only mark the extension as OK when we're sure it is */
449 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
450
451 return( 0 );
452}
453#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
456static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200457 const unsigned char *buf,
458 size_t len )
459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200463 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
464 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200466 }
467
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200468 ssl->session_negotiate->mfl_code = buf[0];
469
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200470 return( 0 );
471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200473
Hanno Beckera0e20d02019-05-15 14:03:01 +0100474#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100475static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
476 const unsigned char *buf,
477 size_t len )
478{
479 size_t peer_cid_len;
480
481 /* CID extension only makes sense in DTLS */
482 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
483 {
484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
486 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
487 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
488 }
489
490 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100491 * Quoting draft-ietf-tls-dtls-connection-id-05
492 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100493 *
494 * struct {
495 * opaque cid<0..2^8-1>;
496 * } ConnectionId;
497 */
498
499 if( len < 1 )
500 {
501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
502 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
503 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
504 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
505 }
506
507 peer_cid_len = *buf++;
508 len--;
509
510 if( len != peer_cid_len )
511 {
512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
514 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
515 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
516 }
517
518 /* Ignore CID if the user has disabled its use. */
519 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
520 {
521 /* Leave ssl->handshake->cid_in_use in its default
522 * value of MBEDTLS_SSL_CID_DISABLED. */
523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
524 return( 0 );
525 }
526
527 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
528 {
529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
530 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
531 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
532 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
533 }
534
Hanno Becker08556bf2019-05-03 12:43:44 +0100535 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100536 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
537 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
538
539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
540 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
541
Hanno Becker89dcc882019-04-26 13:56:39 +0100542 return( 0 );
543}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
547static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200548 const unsigned char *buf,
549 size_t len )
550{
551 if( len != 0 )
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200554 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
555 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200557 }
558
559 ((void) buf);
560
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200561 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200563
564 return( 0 );
565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
569static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100570 const unsigned char *buf,
571 size_t len )
572{
573 if( len != 0 )
574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
577 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100579 }
580
581 ((void) buf);
582
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100583 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100586 }
587
588 return( 0 );
589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
593static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200594 const unsigned char *buf,
595 size_t len )
596{
597 if( len != 0 )
598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200600 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
601 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200603 }
604
605 ((void) buf);
606
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100607 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200610 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200611
612 return( 0 );
613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SSL_SESSION_TICKETS)
617static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200618 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200619 size_t len )
620{
Janos Follath865b3eb2019-12-16 11:46:15 +0000621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200622 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200623
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200624 mbedtls_ssl_session_init( &session );
625
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200626 if( ssl->conf->f_ticket_parse == NULL ||
627 ssl->conf->f_ticket_write == NULL )
628 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200629 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200630 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200631
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200632 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200633 ssl->handshake->new_session_ticket = 1;
634
Paul Elliottd48d5c62021-01-07 14:47:05 +0000635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200636
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200637 if( len == 0 )
638 return( 0 );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#if defined(MBEDTLS_SSL_RENEGOTIATION)
641 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200644 return( 0 );
645 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200647
648 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200649 * Failures are ok: just ignore the ticket and proceed.
650 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200651 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
652 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200653 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200654 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200655
656 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
658 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
660 else
661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
662
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200663 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200664 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200666 /*
667 * Keep the session ID sent by the client, since we MUST send it back to
668 * inform them we're accepting the ticket (RFC 5077 section 3.4)
669 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200670 session.id_len = ssl->session_negotiate->id_len;
671 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200672
673 mbedtls_ssl_session_free( ssl->session_negotiate );
674 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
675
676 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500677 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200680
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200682
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200683 /* Don't send a new ticket after all, this one is OK */
684 ssl->handshake->new_session_ticket = 0;
685
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200686 return( 0 );
687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_SSL_ALPN)
691static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200692 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200693{
Paul Bakker14b16c62014-05-28 11:33:54 +0200694 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200695 const unsigned char *theirs, *start, *end;
696 const char **ours;
697
698 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200699 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200700 return( 0 );
701
702 /*
703 * opaque ProtocolName<1..2^8-1>;
704 *
705 * struct {
706 * ProtocolName protocol_name_list<2..2^16-1>
707 * } ProtocolNameList;
708 */
709
710 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
711 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200712 {
713 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
714 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200716 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200717
718 list_len = ( buf[0] << 8 ) | buf[1];
719 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200720 {
721 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
722 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200724 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200725
726 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100727 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200728 */
729 start = buf + 2;
730 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100731 for( theirs = start; theirs != end; theirs += cur_len )
732 {
733 cur_len = *theirs++;
734
735 /* Current identifier must fit in list */
736 if( cur_len > (size_t)( end - theirs ) )
737 {
738 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
739 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
740 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
741 }
742
743 /* Empty strings MUST NOT be included */
744 if( cur_len == 0 )
745 {
746 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
747 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
748 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
749 }
750 }
751
752 /*
753 * Use our order of preference
754 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200755 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200756 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200757 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200758 for( theirs = start; theirs != end; theirs += cur_len )
759 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200760 cur_len = *theirs++;
761
Paul Bakker14b16c62014-05-28 11:33:54 +0200762 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200763 memcmp( theirs, *ours, cur_len ) == 0 )
764 {
765 ssl->alpn_chosen = *ours;
766 return( 0 );
767 }
768 }
769 }
770
771 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
773 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
774 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200777
Johan Pascalb62bb512015-12-03 21:56:45 +0100778#if defined(MBEDTLS_SSL_DTLS_SRTP)
779static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300780 const unsigned char *buf,
781 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100782{
Johan Pascal43f94902020-09-22 12:25:52 +0200783 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100784 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200785 size_t profile_length;
786 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200787 /*! 2 bytes for profile length and 1 byte for mki len */
788 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100789
790 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100791 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
792 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
793 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200794 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100795 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200796 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100797
798 /* RFC5764 section 4.1.1
799 * uint8 SRTPProtectionProfile[2];
800 *
801 * struct {
802 * SRTPProtectionProfiles SRTPProtectionProfiles;
803 * opaque srtp_mki<0..255>;
804 * } UseSRTPData;
805
806 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100807 */
808
Ron Eldoref72faf2018-07-12 11:54:20 +0300809 /*
810 * Min length is 5: at least one protection profile(2 bytes)
811 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200812 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200813 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300814 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200815 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200816 {
817 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
818 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100819 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200820 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100821
Johan Pascal43f94902020-09-22 12:25:52 +0200822 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200823
Ron Eldoref72faf2018-07-12 11:54:20 +0300824 /* first 2 bytes are protection profile length(in bytes) */
825 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200826 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200827
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200828 /* The profile length cannot be bigger than input buffer size - lengths fields */
829 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200830 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200831 {
832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
833 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
834 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
835 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300836 /*
837 * parse the extension list values are defined in
838 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
839 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200840 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100841 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200842 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200843 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100844
Johan Pascal43f94902020-09-22 12:25:52 +0200845 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300846 {
Johan Pascal43f94902020-09-22 12:25:52 +0200847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
848 mbedtls_ssl_get_srtp_profile_as_string(
849 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300850 }
Johan Pascal85269572020-08-25 10:01:54 +0200851 else
852 {
853 continue;
854 }
Ron Eldor591f1622018-01-22 12:30:04 +0200855 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200856 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200857 {
858 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
859 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200860 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
862 mbedtls_ssl_get_srtp_profile_as_string(
863 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200864 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100865 }
866 }
Johan Pascal43f94902020-09-22 12:25:52 +0200867 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200868 break;
869 }
Johan Pascal042d4562020-08-25 12:14:02 +0200870 buf += profile_length; /* buf points to the mki length */
871 mki_length = *buf;
872 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200873
Johan Pascal042d4562020-08-25 12:14:02 +0200874 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
875 mki_length + profile_length + size_of_lengths != len )
876 {
877 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
878 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
879 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
880 }
881
882 /* Parse the mki only if present and mki is supported locally */
883 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
884 mki_length > 0 )
885 {
886 ssl->dtls_srtp_info.mki_len = mki_length;
887
Johan Pascal5ef72d22020-10-28 17:05:47 +0100888 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300889
Ron Eldoref72faf2018-07-12 11:54:20 +0300890 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
891 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100892 }
893
Johan Pascal042d4562020-08-25 12:14:02 +0200894 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100895}
896#endif /* MBEDTLS_SSL_DTLS_SRTP */
897
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100898/*
899 * Auxiliary functions for ServerHello parsing and related actions
900 */
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100903/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100904 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_ECDSA_C)
907static int ssl_check_key_curve( mbedtls_pk_context *pk,
908 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 const mbedtls_ecp_curve_info **crv = curves;
911 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100912
913 while( *crv != NULL )
914 {
915 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100916 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100917 crv++;
918 }
919
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100920 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100923
924/*
925 * Try picking a certificate for this ciphersuite,
926 * return 0 on success and -1 on failure.
927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928static int ssl_pick_cert( mbedtls_ssl_context *ssl,
929 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100932 mbedtls_pk_type_t pk_alg =
933 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200934 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100937 if( ssl->handshake->sni_key_cert != NULL )
938 list = ssl->handshake->sni_key_cert;
939 else
940#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200941 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944 return( 0 );
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000947
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200948 if( list == NULL )
949 {
950 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
951 return( -1 );
952 }
953
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100954 for( cur = list; cur != NULL; cur = cur->next )
955 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400956 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000958 cur->cert );
959
Gilles Peskinee198df52018-01-05 21:17:45 +0100960 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100963 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000964 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100965
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200966 /*
967 * This avoids sending the client a cert it'll reject based on
968 * keyUsage or other extensions.
969 *
970 * It also allows the user to provision different certificates for
971 * different uses based on keyUsage, eg if they want to avoid signing
972 * and decrypting with the same RSA key.
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100975 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000978 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200979 continue;
980 }
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#if defined(MBEDTLS_ECDSA_C)
983 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200984 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100987 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000988 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100989#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100990
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100991 /*
992 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
993 * present them a SHA-higher cert rather than failing if it's the only
994 * one we got that satisfies the other conditions.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
997 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100998 {
999 if( fallback == NULL )
1000 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001003 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001004 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001006 }
1007
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001008 /* If we get there, we got a winner */
1009 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001010 }
1011
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001012 if( cur == NULL )
1013 cur = fallback;
1014
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001015 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001016 if( cur != NULL )
1017 {
1018 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001020 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001021 return( 0 );
1022 }
1023
1024 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001027
1028/*
1029 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1030 * Sets ciphersuite_info only if the suite matches.
1031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1033 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001036
Hanno Becker7e5437a2017-04-28 17:15:26 +01001037#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001038 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001039 mbedtls_pk_type_t sig_type;
1040#endif
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001043 if( suite_info == NULL )
1044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001047 }
1048
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +00001050 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001051
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001052 if( suite_info->min_minor_ver > ssl->minor_ver ||
1053 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001056 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001057 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001062 return( 0 );
1063#endif
1064
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001065#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1066 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001067 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001068 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1070 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001071 return( 0 );
1072 }
1073#endif
1074
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1077 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001078 ( ssl->handshake->curves == NULL ||
1079 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001082 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001083 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001084 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001085#endif
1086
Gilles Peskineeccd8882020-03-10 12:19:08 +01001087#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001088 /* If the ciphersuite requires a pre-shared key and we don't
1089 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001091 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001094 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001095 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001096#endif
1097
Hanno Becker7e5437a2017-04-28 17:15:26 +01001098#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001099 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001100 /* If the ciphersuite requires signing, check whether
1101 * a suitable hash algorithm is present. */
1102 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1103 {
1104 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1105 if( sig_type != MBEDTLS_PK_NONE &&
1106 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1107 {
1108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001109 "for signature algorithm %u", (unsigned) sig_type ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001110 return( 0 );
1111 }
1112 }
1113
1114#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001115 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001118 /*
1119 * Final check: if ciphersuite requires us to have a
1120 * certificate/key of a particular type:
1121 * - select the appropriate certificate if we have one, or
1122 * - try the next ciphersuite if we don't
1123 * This must be done last since we modify the key_cert list.
1124 */
1125 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001128 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001129 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001130 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001131#endif
1132
1133 *ciphersuite_info = suite_info;
1134 return( 0 );
1135}
1136
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001137/* This function doesn't alert on errors that happen early during
1138 ClientHello parsing because they might indicate that the client is
1139 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001141{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001142 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001143 size_t i, j;
1144 size_t ciph_offset, comp_offset, ext_offset;
1145 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001147 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001148#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001149 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001151 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001152#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001153 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001154 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001156 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Hanno Becker7e5437a2017-04-28 17:15:26 +01001158 /* If there is no signature-algorithm extension present,
1159 * we need to fall back to the default values for allowed
1160 * signature-hash pairs. */
1161#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001162 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001163 int sig_hash_alg_ext_present = 0;
1164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001165 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001170read_record_header:
1171#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001172 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001174 * otherwise read it ourselves manually in order to support SSLv2
1175 * ClientHello, which doesn't use the same record layer format.
1176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_SSL_RENEGOTIATION)
1178 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001182 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001183 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001185 return( ret );
1186 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
1188
1189 buf = ssl->in_hdr;
1190
Hanno Becker5903de42019-05-03 14:46:38 +01001191 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001192
Paul Bakkerec636f32012-09-09 19:17:02 +00001193 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001194 * TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001195 *
1196 * Record layer:
1197 * 0 . 0 message type
1198 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001199 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001200 * 3 . 4 message length
1201 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001203 buf[0] ) );
1204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1208 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001209 }
1210
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001212 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1213
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001215 buf[1], buf[2] ) );
1216
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001217 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001218
1219 /* According to RFC 5246 Appendix E.1, the version here is typically
1220 * "{03,00}, the lowest version number supported by the client, [or] the
1221 * value of ClientHello.client_version", so the only meaningful check here
1222 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1226 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001228
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001229 /* For DTLS if this is the initial handshake, remember the client sequence
1230 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001232 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_SSL_RENEGOTIATION)
1234 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001235#endif
1236 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001237 {
1238 /* Epoch should be 0 for initial handshakes */
1239 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1242 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001243 }
1244
Hanno Becker19859472018-08-06 09:40:20 +01001245 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1248 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001251 ssl->next_record_offset = 0;
1252 ssl->in_left = 0;
1253 goto read_record_header;
1254 }
1255
1256 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001258#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001261
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001262 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_RENEGOTIATION)
1265 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001268 msg_len = ssl->in_hslen;
1269 }
1270 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001271#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001272 {
Angus Grattond8213d02016-05-25 20:56:48 +10001273 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1276 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001277 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001278
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001279 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001280 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001283 return( ret );
1284 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001285
1286 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001289 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001290 else
1291#endif
1292 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001293 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001294
1295 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001298
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001299 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001300
1301 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001302 * Handshake layer:
1303 * 0 . 0 handshake type
1304 * 1 . 3 handshake length
1305 * 4 . 5 DTLS only: message seqence number
1306 * 6 . 8 DTLS only: fragment offset
1307 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1312 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001313 }
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1320 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001321 }
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001324 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1325
1326 /* We don't support fragmentation of ClientHello (yet?) */
1327 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1331 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001332 }
1333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001335 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001336 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001337 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001338 * Copy the client's handshake message_seq on initial handshakes,
1339 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_SSL_RENEGOTIATION)
1342 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001343 {
1344 /* This couldn't be done in ssl_prepare_handshake_record() */
1345 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1346 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001347
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001348 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001351 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001352 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001354 }
1355
1356 ssl->handshake->in_msg_seq++;
1357 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001358 else
1359#endif
1360 {
1361 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1362 ssl->in_msg[5];
1363 ssl->handshake->out_msg_seq = cli_msg_seq;
1364 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1365 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001366
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001367 /*
1368 * For now we don't support fragmentation, so make sure
1369 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001370 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001371 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1372 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1375 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001376 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 buf += mbedtls_ssl_hs_hdr_len( ssl );
1381 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001382
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001383 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001384 * ClientHello layer:
1385 * 0 . 1 protocol version
1386 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1387 * 34 . 35 session id length (1 byte)
1388 * 35 . 34+x session id
1389 * 35+x . 35+x DTLS only: cookie length (1 byte)
1390 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001391 * .. . .. ciphersuite list length (2 bytes)
1392 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001393 * .. . .. compression alg. list length (1 byte)
1394 * .. . .. compression alg. list
1395 * .. . .. extensions length (2 bytes, optional)
1396 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001397 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001398
1399 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001400 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001401 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1402 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001403 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001404 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1407 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001408 }
1409
1410 /*
1411 * Check and save the protocol version
1412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001416 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001417
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001418 ssl->handshake->max_major_ver = ssl->major_ver;
1419 ssl->handshake->max_minor_ver = ssl->minor_ver;
1420
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001421 if( ssl->major_ver < ssl->conf->min_major_ver ||
1422 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001425 " [%d:%d] < [%d:%d]",
1426 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001427 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1429 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001431 }
1432
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001433 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001434 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001435 ssl->major_ver = ssl->conf->max_major_ver;
1436 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001437 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001438 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1439 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001440
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001441 /*
1442 * Save client random (inc. Unix time)
1443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001445
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001446 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001447
1448 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001449 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001450 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001451 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001452
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001453 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001454 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001457 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1458 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001460 }
1461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001463
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001464 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001465 memset( ssl->session_negotiate->id, 0,
1466 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001467 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001468 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001469
1470 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001471 * Check the cookie length and content
1472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001474 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001475 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001476 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001477 cookie_len = buf[cookie_offset];
1478
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001479 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001482 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1483 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001485 }
1486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001488 buf + cookie_offset + 1, cookie_len );
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001491 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_RENEGOTIATION)
1493 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001494#endif
1495 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001496 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001497 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001498 buf + cookie_offset + 1, cookie_len,
1499 ssl->cli_id, ssl->cli_id_len ) != 0 )
1500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001502 ssl->handshake->verify_cookie_len = 1;
1503 }
1504 else
1505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001507 ssl->handshake->verify_cookie_len = 0;
1508 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001509 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001510 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001512 {
1513 /* We know we didn't send a cookie, so it should be empty */
1514 if( cookie_len != 0 )
1515 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001516 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1518 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001519 }
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001522 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001523
1524 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001525 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001526 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001527 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001528 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001529 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001531 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001532
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001533 ciph_len = ( buf[ciph_offset + 0] << 8 )
1534 | ( buf[ciph_offset + 1] );
1535
1536 if( ciph_len < 2 ||
1537 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1538 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1542 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001544 }
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001547 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001548
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001549 /*
1550 * Check the compression algorithms length and pick one
1551 */
1552 comp_offset = ciph_offset + 2 + ciph_len;
1553
1554 comp_len = buf[comp_offset];
1555
1556 if( comp_len < 1 ||
1557 comp_len > 16 ||
1558 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1562 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001564 }
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001567 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001570 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001572 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001574#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001575 /*
1576 * Check the extension length
1577 */
1578 ext_offset = comp_offset + 1 + comp_len;
1579 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001580 {
Simon Butcher584a5472016-05-23 16:24:52 +01001581 if( msg_len < ext_offset + 2 )
1582 {
1583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001584 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1585 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001586 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1587 }
1588
1589 ext_len = ( buf[ext_offset + 0] << 8 )
1590 | ( buf[ext_offset + 1] );
1591
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001592 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001593 {
1594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001595 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1596 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001597 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1598 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001599 }
Simon Butcher584a5472016-05-23 16:24:52 +01001600 else
1601 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001602
Simon Butcher584a5472016-05-23 16:24:52 +01001603 ext = buf + ext_offset + 2;
1604 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001605
Simon Butcher584a5472016-05-23 16:24:52 +01001606 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001607 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001608 unsigned int ext_id;
1609 unsigned int ext_size;
1610 if ( ext_len < 4 ) {
1611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1613 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1614 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1615 }
1616 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1617 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001618
Simon Butcher584a5472016-05-23 16:24:52 +01001619 if( ext_size + 4 > ext_len )
1620 {
1621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001622 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1623 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001624 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1625 }
1626 switch( ext_id )
1627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001629 case MBEDTLS_TLS_EXT_SERVERNAME:
1630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1631 if( ssl->conf->f_sni == NULL )
1632 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001633
Simon Butcher584a5472016-05-23 16:24:52 +01001634 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1635 if( ret != 0 )
1636 return( ret );
1637 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001639
Simon Butcher584a5472016-05-23 16:24:52 +01001640 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001643 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001644#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001645
Simon Butcher584a5472016-05-23 16:24:52 +01001646 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1647 if( ret != 0 )
1648 return( ret );
1649 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001652 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001653 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1655
Simon Butcher584a5472016-05-23 16:24:52 +01001656 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1657 if( ret != 0 )
1658 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001659
1660 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001661 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001663 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001664
Robert Cragie136884c2015-10-02 13:34:31 +01001665#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001666 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001667 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001669
Simon Butcher584a5472016-05-23 16:24:52 +01001670 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1671 if( ret != 0 )
1672 return( ret );
1673 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001674
Simon Butcher584a5472016-05-23 16:24:52 +01001675 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1677 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001678
Simon Butcher584a5472016-05-23 16:24:52 +01001679 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1680 if( ret != 0 )
1681 return( ret );
1682 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001683#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1684 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001685
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001686#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001687 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1688 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001689
Simon Butcher584a5472016-05-23 16:24:52 +01001690 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1691 if( ret != 0 )
1692 return( ret );
1693 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001694#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001697 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001699
Simon Butcher584a5472016-05-23 16:24:52 +01001700 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1701 if( ret != 0 )
1702 return( ret );
1703 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001707 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001709
Simon Butcher584a5472016-05-23 16:24:52 +01001710 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1711 if( ret != 0 )
1712 return( ret );
1713 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001715
Hanno Beckera0e20d02019-05-15 14:03:01 +01001716#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001717 case MBEDTLS_TLS_EXT_CID:
1718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1719
1720 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1721 if( ret != 0 )
1722 return( ret );
1723 break;
1724#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001727 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001729
Simon Butcher584a5472016-05-23 16:24:52 +01001730 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1731 if( ret != 0 )
1732 return( ret );
1733 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001737 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001739
Simon Butcher584a5472016-05-23 16:24:52 +01001740 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1741 if( ret != 0 )
1742 return( ret );
1743 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001747 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001749
Simon Butcher584a5472016-05-23 16:24:52 +01001750 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1751 if( ret != 0 )
1752 return( ret );
1753 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001757 case MBEDTLS_TLS_EXT_ALPN:
1758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001759
Simon Butcher584a5472016-05-23 16:24:52 +01001760 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1761 if( ret != 0 )
1762 return( ret );
1763 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001765
Johan Pascalb62bb512015-12-03 21:56:45 +01001766#if defined(MBEDTLS_SSL_DTLS_SRTP)
1767 case MBEDTLS_TLS_EXT_USE_SRTP:
1768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02001769
Johan Pascalc3ccd982020-10-28 17:18:18 +01001770 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
1771 if( ret != 0 )
1772 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001773 break;
1774#endif /* MBEDTLS_SSL_DTLS_SRTP */
1775
Simon Butcher584a5472016-05-23 16:24:52 +01001776 default:
Paul Elliott9f352112020-12-09 14:55:45 +00001777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01001778 ext_id ) );
1779 }
1780
1781 ext_len -= 4 + ext_size;
1782 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00001783 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01001784
Hanno Becker7e5437a2017-04-28 17:15:26 +01001785#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001786 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001787
1788 /*
1789 * Try to fall back to default hash SHA1 if the client
1790 * hasn't provided any preferred signature-hash combinations.
1791 */
1792 if( sig_hash_alg_ext_present == 0 )
1793 {
1794 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
1795
1796 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
1797 md_default = MBEDTLS_MD_NONE;
1798
1799 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1800 }
1801
1802#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001803 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001804
Paul Bakker48916f92012-09-16 19:57:18 +00001805 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001806 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1807 */
1808 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1813#if defined(MBEDTLS_SSL_RENEGOTIATION)
1814 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001815 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1817 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001818 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1819 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001821 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001824 break;
1825 }
1826 }
1827
1828 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001829 * Renegotiation security checks
1830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001832 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001835 handshake_failure = 1;
1836 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#if defined(MBEDTLS_SSL_RENEGOTIATION)
1838 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1839 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001840 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001843 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1846 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001847 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001850 handshake_failure = 1;
1851 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1853 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001854 renegotiation_info_seen == 1 )
1855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001857 handshake_failure = 1;
1858 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001860
1861 if( handshake_failure == 1 )
1862 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02001863 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1864 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001866 }
Paul Bakker380da532012-04-18 16:10:25 +00001867
Paul Bakker41c83d32013-03-20 14:39:14 +01001868 /*
1869 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001870 * (At the end because we need information from the EC-based extensions
1871 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001872 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001873 got_common_suite = 0;
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001874 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001875 ciphersuite_info = NULL;
TRodziewicz8476f2f2021-06-02 14:34:47 +02001876
TRodziewicz3946f792021-06-14 12:11:18 +02001877 if (ssl->conf->respect_cli_pref == MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT)
TRodziewicz8476f2f2021-06-02 14:34:47 +02001878 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001879 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001880 for( i = 0; ciphersuites[i] != 0; i++ )
1881 {
1882 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1883 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1884 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001885
TRodziewicz8476f2f2021-06-02 14:34:47 +02001886 got_common_suite = 1;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001887
TRodziewicz8476f2f2021-06-02 14:34:47 +02001888 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1889 &ciphersuite_info ) ) != 0 )
1890 return( ret );
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001891
TRodziewicz8476f2f2021-06-02 14:34:47 +02001892 if( ciphersuite_info != NULL )
1893 goto have_ciphersuite;
1894 }
1895 } else {
1896 for( i = 0; ciphersuites[i] != 0; i++ )
1897 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
1898 {
1899 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1900 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1901 continue;
1902
1903 got_common_suite = 1;
1904
1905 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1906 &ciphersuite_info ) ) != 0 )
1907 return( ret );
1908
1909 if( ciphersuite_info != NULL )
1910 goto have_ciphersuite;
1911 }
1912 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001913
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001914 if( got_common_suite )
1915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001917 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001918 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1919 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001921 }
1922 else
1923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001925 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1926 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001928 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001929
1930have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001932
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001933 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001934 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001935
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 ssl->state++;
1937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001939 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001941#endif
1942
Hanno Becker7e5437a2017-04-28 17:15:26 +01001943 /* Debugging-only output for testsuite */
1944#if defined(MBEDTLS_DEBUG_C) && \
1945 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001946 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001947 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1948 {
1949 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
1950 if( sig_alg != MBEDTLS_PK_NONE )
1951 {
1952 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1953 sig_alg );
1954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1955 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
1956 }
1957 else
1958 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001960 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001961 }
1962 }
1963#endif
1964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966
1967 return( 0 );
1968}
1969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1971static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001972 unsigned char *buf,
1973 size_t *olen )
1974{
1975 unsigned char *p = buf;
1976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001978 {
1979 *olen = 0;
1980 return;
1981 }
1982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1986 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001987
1988 *p++ = 0x00;
1989 *p++ = 0x00;
1990
1991 *olen = 4;
1992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001994
Hanno Beckera0e20d02019-05-15 14:03:01 +01001995#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01001996static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
1997 unsigned char *buf,
1998 size_t *olen )
1999{
2000 unsigned char *p = buf;
2001 size_t ext_len;
2002 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2003
2004 *olen = 0;
2005
2006 /* Skip writing the extension if we don't want to use it or if
2007 * the client hasn't offered it. */
2008 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2009 return;
2010
2011 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2012 * which is at most 255, so the increment cannot overflow. */
2013 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2014 {
2015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2016 return;
2017 }
2018
2019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2020
2021 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002022 * Quoting draft-ietf-tls-dtls-connection-id-05
2023 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002024 *
2025 * struct {
2026 * opaque cid<0..2^8-1>;
2027 * } ConnectionId;
2028 */
2029
2030 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2031 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2032 ext_len = (size_t) ssl->own_cid_len + 1;
2033 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2034 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2035
2036 *p++ = (uint8_t) ssl->own_cid_len;
2037 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2038
2039 *olen = ssl->own_cid_len + 5;
2040}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002041#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2044static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002045 unsigned char *buf,
2046 size_t *olen )
2047{
2048 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2050 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002051
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002052 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002053 {
2054 *olen = 0;
2055 return;
2056 }
2057
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002058 /*
2059 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2060 * from a client and then selects a stream or Authenticated Encryption
2061 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2062 * encrypt-then-MAC response extension back to the client."
2063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002065 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2067 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002068 {
2069 *olen = 0;
2070 return;
2071 }
2072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2076 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002077
2078 *p++ = 0x00;
2079 *p++ = 0x00;
2080
2081 *olen = 4;
2082}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2086static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002087 unsigned char *buf,
2088 size_t *olen )
2089{
2090 unsigned char *p = buf;
2091
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002092 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002093 {
2094 *olen = 0;
2095 return;
2096 }
2097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002099 "extension" ) );
2100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2102 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002103
2104 *p++ = 0x00;
2105 *p++ = 0x00;
2106
2107 *olen = 4;
2108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2112static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002113 unsigned char *buf,
2114 size_t *olen )
2115{
2116 unsigned char *p = buf;
2117
2118 if( ssl->handshake->new_session_ticket == 0 )
2119 {
2120 *olen = 0;
2121 return;
2122 }
2123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2127 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002128
2129 *p++ = 0x00;
2130 *p++ = 0x00;
2131
2132 *olen = 4;
2133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002137 unsigned char *buf,
2138 size_t *olen )
2139{
2140 unsigned char *p = buf;
2141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002143 {
2144 *olen = 0;
2145 return;
2146 }
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2151 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_SSL_RENEGOTIATION)
2154 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002155 {
2156 *p++ = 0x00;
2157 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2158 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002159
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002160 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2161 p += ssl->verify_data_len;
2162 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2163 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002164 }
2165 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002167 {
2168 *p++ = 0x00;
2169 *p++ = 0x01;
2170 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002171 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002172
2173 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002174}
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2177static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002178 unsigned char *buf,
2179 size_t *olen )
2180{
2181 unsigned char *p = buf;
2182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002184 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002185 *olen = 0;
2186 return;
2187 }
2188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2192 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002193
2194 *p++ = 0x00;
2195 *p++ = 1;
2196
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002197 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002198
2199 *olen = 5;
2200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002202
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002203#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002204 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002206 unsigned char *buf,
2207 size_t *olen )
2208{
2209 unsigned char *p = buf;
2210 ((void) ssl);
2211
Paul Bakker677377f2013-10-28 12:54:26 +01002212 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002214 {
2215 *olen = 0;
2216 return;
2217 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2222 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002223
2224 *p++ = 0x00;
2225 *p++ = 2;
2226
2227 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002229
2230 *olen = 6;
2231}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002232#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002233
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002234#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2235static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2236 unsigned char *buf,
2237 size_t *olen )
2238{
Janos Follath865b3eb2019-12-16 11:46:15 +00002239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002240 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002241 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002242 size_t kkpp_len;
2243
2244 *olen = 0;
2245
2246 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002247 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002248 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2249 return;
2250
2251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2252
2253 if( end - p < 4 )
2254 {
2255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2256 return;
2257 }
2258
2259 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2260 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2261
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002262 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2263 p + 2, end - p - 2, &kkpp_len,
2264 ssl->conf->f_rng, ssl->conf->p_rng );
2265 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002266 {
2267 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2268 return;
2269 }
2270
2271 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2272 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2273
2274 *olen = kkpp_len + 4;
2275}
2276#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_SSL_ALPN )
2279static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002280 unsigned char *buf, size_t *olen )
2281{
2282 if( ssl->alpn_chosen == NULL )
2283 {
2284 *olen = 0;
2285 return;
2286 }
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002289
2290 /*
2291 * 0 . 1 ext identifier
2292 * 2 . 3 ext length
2293 * 4 . 5 protocol list length
2294 * 6 . 6 protocol name length
2295 * 7 . 7+n protocol name
2296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2298 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002299
2300 *olen = 7 + strlen( ssl->alpn_chosen );
2301
2302 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2303 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2304
2305 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2306 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2307
2308 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2309
2310 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002313
Ron Eldor3adb9922017-12-21 10:15:08 +02002314#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002315static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002316 unsigned char *buf,
2317 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002318{
Ron Eldor75870ec2018-12-06 17:31:55 +02002319 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002320 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002321 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2322
2323 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002324
Johan Pascalc3ccd982020-10-28 17:18:18 +01002325 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2326 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002327 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002328 return;
2329 }
2330
2331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2332
Johan Pascald576fdb2020-09-22 10:39:53 +02002333 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002334 {
2335 mki_len = ssl->dtls_srtp_info.mki_len;
2336 }
2337
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002338 /* The extension total size is 9 bytes :
2339 * - 2 bytes for the extension tag
2340 * - 2 bytes for the total size
2341 * - 2 bytes for the protection profile length
2342 * - 2 bytes for the protection profile
2343 * - 1 byte for the mki length
2344 * + the actual mki length
2345 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002346 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002347 {
2348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2349 return;
2350 }
2351
Johan Pascalb62bb512015-12-03 21:56:45 +01002352 /* extension */
2353 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2354 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002355 /*
2356 * total length 5 and mki value: only one profile(2 bytes)
2357 * and length(2 bytes) and srtp_mki )
2358 */
Ron Eldor591f1622018-01-22 12:30:04 +02002359 ext_len = 5 + mki_len;
2360 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2361 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002362
2363 /* protection profile length: 2 */
2364 buf[4] = 0x00;
2365 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002366 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002367 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002368 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002369 {
2370 buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF );
2371 buf[7] = (unsigned char)( profile_value & 0xFF );
2372 }
2373 else
2374 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002376 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002377 }
2378
Ron Eldor591f1622018-01-22 12:30:04 +02002379 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002380 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002381
Ron Eldor591f1622018-01-22 12:30:04 +02002382 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002383}
2384#endif /* MBEDTLS_SSL_DTLS_SRTP */
2385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2387static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002388{
Janos Follath865b3eb2019-12-16 11:46:15 +00002389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002390 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002391 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002394
2395 /*
2396 * struct {
2397 * ProtocolVersion server_version;
2398 * opaque cookie<0..2^8-1>;
2399 * } HelloVerifyRequest;
2400 */
2401
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002402 /* The RFC is not clear on this point, but sending the actual negotiated
2403 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002405 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002407 p += 2;
2408
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002409 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002410 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002414 }
2415
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002416 /* Skip length byte until we know the length */
2417 cookie_len_byte = p++;
2418
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002419 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002420 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002421 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002424 return( ret );
2425 }
2426
2427 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002430
2431 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2433 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002436
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002437 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002438 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002440 return( ret );
2441 }
2442
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002443#if defined(MBEDTLS_SSL_PROTO_DTLS)
2444 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2445 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2446 {
2447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2448 return( ret );
2449 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002450#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002453
2454 return( 0 );
2455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002457
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002458static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
Hanno Becker64ce9742021-04-15 08:19:40 +01002459{
2460 int ret;
Hanno Beckera5b1a392021-04-15 16:48:01 +01002461 mbedtls_ssl_session session_tmp;
Hanno Becker64ce9742021-04-15 08:19:40 +01002462 mbedtls_ssl_session * const session = ssl->session_negotiate;
2463
2464 /* Resume is 0 by default, see ssl_handshake_init().
2465 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2466 if( ssl->handshake->resume == 1 )
2467 return;
Hanno Becker7ad77962021-05-13 06:13:34 +01002468 if( session->id_len == 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +01002469 return;
2470 if( ssl->conf->f_get_cache == NULL )
2471 return;
2472#if defined(MBEDTLS_SSL_RENEGOTIATION)
2473 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2474 return;
2475#endif
2476
Hanno Beckera5b1a392021-04-15 16:48:01 +01002477 mbedtls_ssl_session_init( &session_tmp );
2478
Hanno Becker64ce9742021-04-15 08:19:40 +01002479 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
Hanno Beckerccdaf6e2021-04-15 09:26:17 +01002480 session->id,
2481 session->id_len,
Hanno Becker64ce9742021-04-15 08:19:40 +01002482 &session_tmp );
2483 if( ret != 0 )
2484 goto exit;
2485
2486 if( session->ciphersuite != session_tmp.ciphersuite ||
2487 session->compression != session_tmp.compression )
2488 {
2489 /* Mismatch between cached and negotiated session */
2490 goto exit;
2491 }
2492
2493 /* Move semantics */
Hanno Becker64ce9742021-04-15 08:19:40 +01002494 mbedtls_ssl_session_free( session );
2495 *session = session_tmp;
Hanno Beckerfc1f4132021-05-14 14:57:54 +01002496 memset( &session_tmp, 0, sizeof( session_tmp ) );
Hanno Becker64ce9742021-04-15 08:19:40 +01002497
2498 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2499 ssl->handshake->resume = 1;
2500
2501exit:
2502
Hanno Beckerdf564022021-05-13 06:26:37 +01002503 mbedtls_ssl_session_free( &session_tmp );
Hanno Becker64ce9742021-04-15 08:19:40 +01002504}
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002509 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002510#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002512 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 unsigned char *buf, *p;
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002519 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002523
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002524 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002527
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002528 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2531 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002532 }
2533
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 /*
2535 * 0 . 0 handshake type
2536 * 1 . 3 handshake length
2537 * 4 . 5 protocol version
2538 * 6 . 9 UNIX time()
2539 * 10 . 37 random bytes
2540 */
2541 buf = ssl->out_msg;
2542 p = buf + 4;
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002545 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002546 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002549 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002552 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 *p++ = (unsigned char)( t >> 24 );
2554 *p++ = (unsigned char)( t >> 16 );
2555 *p++ = (unsigned char)( t >> 8 );
2556 *p++ = (unsigned char)( t );
2557
Paul Elliottd48d5c62021-01-07 14:47:05 +00002558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2559 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002560#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002561 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002562 return( ret );
2563
2564 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002567 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002568 return( ret );
2569
2570 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Paul Bakker48916f92012-09-16 19:57:18 +00002572 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002576 ssl_handle_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002578 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 {
2580 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002581 * New session, create a new session id,
2582 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 ssl->state++;
2585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002587 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002588#endif
2589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002591 if( ssl->handshake->new_session_ticket != 0 )
2592 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002593 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002594 memset( ssl->session_negotiate->id, 0, 32 );
2595 }
2596 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002598 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002599 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002600 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002601 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002602 return( ret );
2603 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605 else
2606 {
2607 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002608 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002610 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002616 return( ret );
2617 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002620 /*
2621 * 38 . 38 session id length
2622 * 39 . 38+n session id
2623 * 39+n . 40+n chosen ciphersuite
2624 * 41+n . 41+n chosen compression alg.
2625 * 42+n . 43+n extensions length
2626 * 44+n . 43+n+m extensions
2627 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002628 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2629 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2630 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Paul Elliottd48d5c62021-01-07 14:47:05 +00002632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002635 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Paul Bakker48916f92012-09-16 19:57:18 +00002637 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2638 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2639 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2642 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002644 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002645
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002646 /*
2647 * First write extensions, then the total length
2648 */
2649 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2650 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002653 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2654 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002655#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002658 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2659 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002660#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002661
Hanno Beckera0e20d02019-05-15 14:03:01 +01002662#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002663 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2664 ext_len += olen;
2665#endif
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002668 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2669 ext_len += olen;
2670#endif
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002673 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2674 ext_len += olen;
2675#endif
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002678 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2679 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002680#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002681
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002682#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002683 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002684 if ( mbedtls_ssl_ciphersuite_uses_ec(
2685 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2686 {
2687 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2688 ext_len += olen;
2689 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002690#endif
2691
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002692#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2693 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2694 ext_len += olen;
2695#endif
2696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002698 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2699 ext_len += olen;
2700#endif
2701
Johan Pascalb62bb512015-12-03 21:56:45 +01002702#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01002703 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
2704 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002705#endif
2706
Paul Elliottd48d5c62021-01-07 14:47:05 +00002707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2708 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002709
Paul Bakkera7036632014-04-30 10:15:38 +02002710 if( ext_len > 0 )
2711 {
2712 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2713 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2714 p += ext_len;
2715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
2717 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2719 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002721 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
2725 return( ret );
2726}
2727
Gilles Peskineeccd8882020-03-10 12:19:08 +01002728#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002730{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002731 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002732 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002735
Hanno Becker77adddc2019-02-07 12:32:43 +00002736 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002739 ssl->state++;
2740 return( 0 );
2741 }
2742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002745}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002746#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002748{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002750 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002751 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002752 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002753 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002755 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002757 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
2761 ssl->state++;
2762
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002763#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2764 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2765 authmode = ssl->handshake->sni_authmode;
2766 else
2767#endif
2768 authmode = ssl->conf->authmode;
2769
Hanno Becker77adddc2019-02-07 12:32:43 +00002770 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002771 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 {
Johan Pascal4f099262020-09-22 10:59:26 +02002773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2774 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 }
2776
2777 /*
2778 * 0 . 0 handshake type
2779 * 1 . 3 handshake length
2780 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002781 * 5 .. m-1 cert types
2782 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002783 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 * n .. n+1 length of all DNs
2785 * n+2 .. n+3 length of DN 1
2786 * n+4 .. ... Distinguished Name #1
2787 * ... .. ... length of DN 2, etc.
2788 */
2789 buf = ssl->out_msg;
2790 p = buf + 4;
2791
2792 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002793 * Supported certificate types
2794 *
2795 * ClientCertificateType certificate_types<1..2^8-1>;
2796 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002798 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#if defined(MBEDTLS_RSA_C)
2801 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#if defined(MBEDTLS_ECDSA_C)
2804 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002805#endif
2806
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002807 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002808 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002809
Paul Bakker577e0062013-08-28 11:57:20 +02002810 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002812 /*
2813 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002814 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002815 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2816 *
2817 * struct {
2818 * HashAlgorithm hash;
2819 * SignatureAlgorithm signature;
2820 * } SignatureAndHashAlgorithm;
2821 *
2822 * enum { (255) } HashAlgorithm;
2823 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002826 {
Simon Butcher99000142016-10-13 17:21:01 +01002827 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002828
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002829 /*
2830 * Supported signature algorithms
2831 */
Simon Butcher99000142016-10-13 17:21:01 +01002832 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
2833 {
2834 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
2835
2836 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
2837 continue;
2838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002840 p[2 + sa_len++] = hash;
2841 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002842#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002844 p[2 + sa_len++] = hash;
2845 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002846#endif
Simon Butcher99000142016-10-13 17:21:01 +01002847 }
Paul Bakker926af752012-11-23 13:38:07 +01002848
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002849 p[0] = (unsigned char)( sa_len >> 8 );
2850 p[1] = (unsigned char)( sa_len );
2851 sa_len += 2;
2852 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002853 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002856 /*
2857 * DistinguishedName certificate_authorities<0..2^16-1>;
2858 * opaque DistinguishedName<1..2^16-1>;
2859 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002862 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01002863
2864 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00002866 /* NOTE: If trusted certificates are provisioned
2867 * via a CA callback (configured through
2868 * `mbedtls_ssl_conf_ca_cb()`, then the
2869 * CertificateRequest is currently left empty. */
2870
Janos Follath088ce432017-04-10 12:42:31 +01002871#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2872 if( ssl->handshake->sni_ca_chain != NULL )
2873 crt = ssl->handshake->sni_ca_chain;
2874 else
2875#endif
2876 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002877
Janos Follath088ce432017-04-10 12:42:31 +01002878 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002879 {
irwirc9bc3002020-04-01 13:46:36 +03002880 /* It follows from RFC 5280 A.1 that this length
2881 * can be represented in at most 11 bits. */
2882 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01002883
irwirc9bc3002020-04-01 13:46:36 +03002884 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01002885 {
2886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2887 break;
2888 }
2889
2890 *p++ = (unsigned char)( dn_size >> 8 );
2891 *p++ = (unsigned char)( dn_size );
2892 memcpy( p, crt->subject_raw.p, dn_size );
2893 p += dn_size;
2894
2895 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2896
2897 total_dn_size += 2 + dn_size;
2898 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 }
2901
Paul Bakker926af752012-11-23 13:38:07 +01002902 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2904 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002905 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2906 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002908 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
2912 return( ret );
2913}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002914#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2917 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2918static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002919{
Janos Follath865b3eb2019-12-16 11:46:15 +00002920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2925 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002926 }
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2929 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2930 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002933 return( ret );
2934 }
2935
2936 return( 0 );
2937}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2939 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002940
Gilles Peskineeccd8882020-03-10 12:19:08 +01002941#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002942 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002943static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002944 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002945{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002946 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2947 * signature length which will be added in ssl_write_server_key_exchange
2948 * after the call to ssl_prepare_server_key_exchange.
2949 * ssl_write_server_key_exchange also takes care of incrementing
2950 * ssl->out_msglen. */
2951 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10002952 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002953 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02002954 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002955 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002956 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
2957 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002958 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02002959 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002960 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01002961 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002962 return( ret );
2963}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002964#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002965 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002966
Gilles Peskined3eb0612018-01-08 17:07:44 +01002967/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02002968 * calculating the signature if any, but excluding formatting the
2969 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01002970static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
2971 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00002972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002974 ssl->handshake->ciphersuite_info;
2975
Gilles Peskineeccd8882020-03-10 12:19:08 +01002976#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
2977#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01002978 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002979#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
2980#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002981
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002982 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002983#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02002984 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002985#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002986
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002987 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002989 /*
2990 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002991 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002992 *
2993 */
2994
2995 /*
2996 * - ECJPAKE key exchanges
2997 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002998#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2999 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3000 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003002 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003003
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003004 ret = mbedtls_ecjpake_write_round_two(
3005 &ssl->handshake->ecjpake_ctx,
3006 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003007 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003008 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003009 if( ret != 0 )
3010 {
3011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3012 return( ret );
3013 }
3014
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003015 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003016 }
3017#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3018
Hanno Becker1aa267c2017-04-28 17:08:27 +01003019 /*
3020 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3021 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3022 * we use empty support identity hints here.
3023 **/
3024#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3026 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3027 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003028 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003029 ssl->out_msg[ssl->out_msglen++] = 0x00;
3030 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3033 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003034
Hanno Becker7e5437a2017-04-28 17:15:26 +01003035 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003036 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003037 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003038#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003039 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003040 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003042 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003043
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003044 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3045 {
3046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3048 }
3049
Paul Bakker41c83d32013-03-20 14:39:14 +01003050 /*
3051 * Ephemeral DH parameters:
3052 *
3053 * struct {
3054 * opaque dh_p<1..2^16-1>;
3055 * opaque dh_g<1..2^16-1>;
3056 * opaque dh_Ys<1..2^16-1>;
3057 * } ServerDHParams;
3058 */
Hanno Beckerab740562017-10-04 13:15:37 +01003059 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3060 &ssl->conf->dhm_P,
3061 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003062 {
Hanno Beckerab740562017-10-04 13:15:37 +01003063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003064 return( ret );
3065 }
Paul Bakker48916f92012-09-16 19:57:18 +00003066
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003067 if( ( ret = mbedtls_dhm_make_params(
3068 &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003069 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003070 ssl->out_msg + ssl->out_msglen, &len,
3071 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003074 return( ret );
3075 }
3076
Gilles Peskineeccd8882020-03-10 12:19:08 +01003077#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003078 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003079#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003080
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003081 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3084 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3085 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3086 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003087 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003088#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003089
Hanno Becker1aa267c2017-04-28 17:08:27 +01003090 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003091 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003092 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003093#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003094 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003096 /*
3097 * Ephemeral ECDH parameters:
3098 *
3099 * struct {
3100 * ECParameters curve_params;
3101 * ECPoint public;
3102 * } ServerECDHParams;
3103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003107 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003108
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003109 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003110 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003111 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3112 if( (*curve)->grp_id == *gid )
3113 goto curve_matching_done;
3114
3115curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003116 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3119 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003120 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003123
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003124 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3125 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003126 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003128 return( ret );
3129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003131 if( ( ret = mbedtls_ecdh_make_params(
3132 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003133 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003134 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003135 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003138 return( ret );
3139 }
3140
Gilles Peskineeccd8882020-03-10 12:19:08 +01003141#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003142 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003143#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003144
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003145 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003146
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003147 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3148 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003149 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003150#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Hanno Becker1aa267c2017-04-28 17:08:27 +01003152 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003153 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003154 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003155 * exchange parameters, compute and add the signature here.
3156 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003157 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003158#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003159 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003161 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003162 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003163 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003165
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003166 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003167 * 2.1: Choose hash algorithm:
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003168 * For TLS 1.2, obey signature-hash-algorithm extension
3169 * to choose appropriate hash.
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003170 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003171
3172 mbedtls_md_type_t md_alg;
3173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003175 mbedtls_pk_type_t sig_alg =
3176 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003178 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003179 /* For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003180 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003181 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003182 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3183 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003186 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003187 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003189 }
3190 }
Paul Bakker577e0062013-08-28 11:57:20 +02003191 else
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003192 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3194 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003195 }
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003196#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003197
Paul Elliott3891caf2020-12-17 18:42:40 +00003198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003199
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003200 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003201 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003202 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003203#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003205 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003206 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003207 dig_signed,
3208 dig_signed_len,
3209 md_alg );
3210 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003211 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003212 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003213 else
TRodziewicz0f82ec62021-05-12 17:49:18 +02003214#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003218 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003219
Gilles Peskineebd652f2018-01-05 21:18:59 +01003220 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003221
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003222 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003223 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3226 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003227 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003228 /*
3229 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003230 * explicitly through a prefix to the signature.
3231 *
3232 * struct {
3233 * HashAlgorithm hash;
3234 * SignatureAlgorithm signature;
3235 * } SignatureAndHashAlgorithm;
3236 *
3237 * struct {
3238 * SignatureAndHashAlgorithm algorithm;
3239 * opaque signature<0..2^16-1>;
3240 * } DigitallySigned;
3241 *
3242 */
3243
Gilles Peskine1004c192018-01-08 16:59:14 +01003244 ssl->out_msg[ssl->out_msglen++] =
3245 mbedtls_ssl_hash_from_md_alg( md_alg );
3246 ssl->out_msg[ssl->out_msglen++] =
3247 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003248 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003250
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003251#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003252 if( ssl->conf->f_async_sign_start != NULL )
3253 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003254 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003255 mbedtls_ssl_own_cert( ssl ),
3256 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003257 switch( ret )
3258 {
3259 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3260 /* act as if f_async_sign was null */
3261 break;
3262 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003263 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003264 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003265 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003266 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003267 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3268 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003269 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003270 return( ret );
3271 }
3272 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003273#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003274
3275 if( mbedtls_ssl_own_key( ssl ) == NULL )
3276 {
3277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3278 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3279 }
3280
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003281 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3282 * signature length which will be added in ssl_write_server_key_exchange
3283 * after the call to ssl_prepare_server_key_exchange.
3284 * ssl_write_server_key_exchange also takes care of incrementing
3285 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003286 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3287 md_alg, hash, hashlen,
3288 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003289 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003290 ssl->conf->f_rng,
3291 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003294 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003295 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003296 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003297#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003298
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003299 return( 0 );
3300}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003301
Gilles Peskined3eb0612018-01-08 17:07:44 +01003302/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003303 * that do not include a ServerKeyExchange message, do nothing. Either
3304 * way, if successful, move on to the next step in the SSL state
3305 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003306static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3307{
Janos Follath865b3eb2019-12-16 11:46:15 +00003308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003309 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003310#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003311 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003312 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003313#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003314
Gilles Peskined3eb0612018-01-08 17:07:44 +01003315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3316
Gilles Peskineeccd8882020-03-10 12:19:08 +01003317#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003318 /* Extract static ECDH parameters and abort if ServerKeyExchange
3319 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003320 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3321 {
3322 /* For suites involving ECDH, extract DH parameters
3323 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003324#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003325 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3326 {
3327 ssl_get_ecdh_params_from_cert( ssl );
3328 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003329#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003330
3331 /* Key exchanges not involving ephemeral keys don't use
3332 * ServerKeyExchange, so end here. */
3333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3334 ssl->state++;
3335 return( 0 );
3336 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003337#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003338
Gilles Peskineeccd8882020-03-10 12:19:08 +01003339#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003340 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003341 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003342 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003343 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003344 {
3345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3346 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003347 }
3348 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003349#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003350 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003351 {
3352 /* ServerKeyExchange is needed. Prepare the message. */
3353 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003354 }
3355
3356 if( ret != 0 )
3357 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003358 /* If we're starting to write a new message, set ssl->out_msglen
3359 * to 0. But if we're resuming after an asynchronous message,
3360 * out_msglen is the amount of data written so far and mst be
3361 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003362 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3364 else
3365 ssl->out_msglen = 0;
3366 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003367 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003368
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003369 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003370 * ssl_prepare_server_key_exchange already wrote the signature
3371 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003372#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003373 if( signature_len != 0 )
3374 {
3375 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3376 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3377
3378 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3379 ssl->out_msg + ssl->out_msglen,
3380 signature_len );
3381
3382 /* Skip over the already-written signature */
3383 ssl->out_msglen += signature_len;
3384 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003385#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003386
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003387 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3389 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003390
3391 ssl->state++;
3392
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003393 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 return( ret );
3397 }
3398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401}
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003404{
Janos Follath865b3eb2019-12-16 11:46:15 +00003405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
3409 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3411 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
3413 ssl->state++;
3414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003418#endif
3419
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003420 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423 return( ret );
3424 }
3425
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003426#if defined(MBEDTLS_SSL_PROTO_DTLS)
3427 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3428 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3429 {
3430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3431 return( ret );
3432 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003433#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003436
3437 return( 0 );
3438}
3439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3441 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3442static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003443 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003446 size_t n;
3447
3448 /*
3449 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3450 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003451 if( *p + 2 > end )
3452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3454 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003455 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003456
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003457 n = ( (*p)[0] << 8 ) | (*p)[1];
3458 *p += 2;
3459
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003460 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3463 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003464 }
3465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3469 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003470 }
3471
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003472 *p += n;
3473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003475
Paul Bakker70df2fb2013-04-17 17:19:09 +02003476 return( ret );
3477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3479 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3482 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003483
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003484#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003485static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3486 unsigned char *peer_pms,
3487 size_t *peer_pmslen,
3488 size_t peer_pmssize )
3489{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003490 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003491 peer_pms, peer_pmslen, peer_pmssize );
3492 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3493 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003494 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003495 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003496 }
3497 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3498 return( ret );
3499}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003500#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003501
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003502static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3503 const unsigned char *p,
3504 const unsigned char *end,
3505 unsigned char *peer_pms,
3506 size_t *peer_pmslen,
3507 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003508{
Janos Follath865b3eb2019-12-16 11:46:15 +00003509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003510 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3511 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3512 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003513
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003514#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003515 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003516 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003517 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003518 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3520 return( ssl_resume_decrypt_pms( ssl,
3521 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003522 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003523#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003524
3525 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003526 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003527 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003528#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003529 if ( p + 2 > end ) {
3530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3531 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3532 }
3533 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3534 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003535 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3537 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003538 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003539#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003540
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003541 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3544 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003545 }
3546
Gilles Peskine422ccab2018-01-11 18:29:01 +01003547 /*
3548 * Decrypt the premaster secret
3549 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003550#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003551 if( ssl->conf->f_async_decrypt_start != NULL )
3552 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003553 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003554 mbedtls_ssl_own_cert( ssl ),
3555 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003556 switch( ret )
3557 {
3558 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3559 /* act as if f_async_decrypt_start was null */
3560 break;
3561 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003562 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003563 return( ssl_resume_decrypt_pms( ssl,
3564 peer_pms,
3565 peer_pmslen,
3566 peer_pmssize ) );
3567 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003568 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003569 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3570 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003571 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003572 return( ret );
3573 }
3574 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003575#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003576
Gilles Peskine422ccab2018-01-11 18:29:01 +01003577 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3578 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3580 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3581 }
3582
3583 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003584 peer_pms, peer_pmslen, peer_pmssize,
3585 ssl->conf->f_rng, ssl->conf->p_rng );
3586 return( ret );
3587}
3588
3589static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3590 const unsigned char *p,
3591 const unsigned char *end,
3592 size_t pms_offset )
3593{
Janos Follath865b3eb2019-12-16 11:46:15 +00003594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003595 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3596 unsigned char ver[2];
3597 unsigned char fake_pms[48], peer_pms[48];
3598 unsigned char mask;
3599 size_t i, peer_pmslen;
3600 unsigned int diff;
3601
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003602 /* In case of a failure in decryption, the decryption may write less than
3603 * 2 bytes of output, but we always read the first two bytes. It doesn't
3604 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003605 * ret being nonzero, and we only care whether diff is 0.
3606 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3607 * also makes memory analyzers happy (don't access uninitialized memory,
3608 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003609 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003610 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003611
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003612 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3613 peer_pms,
3614 &peer_pmslen,
3615 sizeof( peer_pms ) );
3616
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003617#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003618 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3619 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003620#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003623 ssl->handshake->max_minor_ver,
3624 ssl->conf->transport, ver );
3625
3626 /* Avoid data-dependent branches while checking for invalid
3627 * padding, to protect against timing-based Bleichenbacher-type
3628 * attacks. */
3629 diff = (unsigned int) ret;
3630 diff |= peer_pmslen ^ 48;
3631 diff |= peer_pms[0] ^ ver[0];
3632 diff |= peer_pms[1] ^ ver[1];
3633
3634 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3635 /* MSVC has a warning about unary minus on unsigned, but this is
3636 * well-defined and precisely what we want to do here */
3637#if defined(_MSC_VER)
3638#pragma warning( push )
3639#pragma warning( disable : 4146 )
3640#endif
3641 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3642#if defined(_MSC_VER)
3643#pragma warning( pop )
3644#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003645
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003646 /*
3647 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3648 * must not cause the connection to end immediately; instead, send a
3649 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003650 * To protect against timing-based variants of the attack, we must
3651 * not have any branch that depends on whether the decryption was
3652 * successful. In particular, always generate the fake premaster secret,
3653 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003654 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003655 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003656 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003657 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003658 /* It's ok to abort on an RNG failure, since this does not reveal
3659 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003660 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003661 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003662
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003663#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003664 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003666#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003667
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003668 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3669 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003673 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003674 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003675
Gilles Peskine422ccab2018-01-11 18:29:01 +01003676 /* Set pms to either the true or the fake PMS, without
3677 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003678 for( i = 0; i < ssl->handshake->pmslen; i++ )
3679 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3680
3681 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3684 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003685
Gilles Peskineeccd8882020-03-10 12:19:08 +01003686#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003688 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003689{
Paul Bakker6db455e2013-09-18 17:29:31 +02003690 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003691 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003692
Hanno Becker845b9462018-10-26 12:07:29 +01003693 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3696 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003697 }
3698
3699 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003700 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003701 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003702 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3705 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003706 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003707
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003708 n = ( (*p)[0] << 8 ) | (*p)[1];
3709 *p += 2;
3710
irwir6527bd62019-09-21 18:51:25 +03003711 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3714 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003715 }
3716
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003717 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003718 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003719 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003721 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003722 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003723 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003724 /* Identity is not a big secret since clients send it in the clear,
3725 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003726 if( n != ssl->conf->psk_identity_len ||
3727 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003730 }
3731 }
3732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003736 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3737 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003739 }
3740
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003741 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003742
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003743 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003744}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003745#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003748{
Janos Follath865b3eb2019-12-16 11:46:15 +00003749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003751 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003752
Hanno Beckere694c3e2017-12-27 21:34:08 +00003753 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003756
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003757#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003758 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3759 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3760 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3761 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003762 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003763 {
3764 /* We've already read a record and there is an asynchronous
3765 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003766 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3768 }
3769 else
3770#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01003771 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774 return( ret );
3775 }
3776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003778 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3783 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 }
3785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3789 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003790 }
3791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3793 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003795 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003798 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003800
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003801 if( p != end )
3802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3804 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003805 }
3806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003808 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01003809 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003810 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003811 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3814 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003815 }
3816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003818 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003819 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3821#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3822 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3823 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3824 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3825 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3826 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3827 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3828 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003831 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
3834 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003835 }
3836
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003837 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3838 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003841 &ssl->handshake->pmslen,
3842 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003844 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
3847 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003848 }
3849
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003850 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3851 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003853 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3855 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3856 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3857 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3858#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3859 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003860 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003861 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003864 return( ret );
3865 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003866
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003867 if( p != end )
3868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3870 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003871 }
3872
Hanno Becker845b9462018-10-26 12:07:29 +01003873#if defined(MBEDTLS_USE_PSA_CRYPTO)
3874 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
3875 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00003876 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01003877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
3878 else
3879#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003881 ciphersuite_info->key_exchange ) ) != 0 )
3882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003884 return( ret );
3885 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003886 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3889#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3890 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003891 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003892#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003893 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003894 {
3895 /* There is an asynchronous operation in progress to
3896 * decrypt the encrypted premaster secret, so skip
3897 * directly to resuming this operation. */
3898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
3899 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3900 * won't actually use it, but maintain p anyway for robustness. */
3901 p += ssl->conf->psk_identity_len + 2;
3902 }
3903 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003904#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003905 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003908 return( ret );
3909 }
3910
Hanno Becker845b9462018-10-26 12:07:29 +01003911#if defined(MBEDTLS_USE_PSA_CRYPTO)
3912 /* Opaque PSKs are currently only supported for PSK-only. */
3913 if( ssl_use_opaque_psk( ssl ) == 1 )
3914 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3915#endif
3916
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003917 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003920 return( ret );
3921 }
3922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003924 ciphersuite_info->key_exchange ) ) != 0 )
3925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003927 return( ret );
3928 }
3929 }
3930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3932#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3933 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003934 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003935 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003938 return( ret );
3939 }
3940 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003943 return( ret );
3944 }
3945
Hanno Becker845b9462018-10-26 12:07:29 +01003946#if defined(MBEDTLS_USE_PSA_CRYPTO)
3947 /* Opaque PSKs are currently only supported for PSK-only. */
3948 if( ssl_use_opaque_psk( ssl ) == 1 )
3949 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3950#endif
3951
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003952 if( p != end )
3953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3955 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003956 }
3957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003959 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003962 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003963 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003964 }
3965 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3967#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3968 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003969 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003970 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003973 return( ret );
3974 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003977 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
3980 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003981 }
3982
Hanno Becker845b9462018-10-26 12:07:29 +01003983#if defined(MBEDTLS_USE_PSA_CRYPTO)
3984 /* Opaque PSKs are currently only supported for PSK-only. */
3985 if( ssl_use_opaque_psk( ssl ) == 1 )
3986 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3987#endif
3988
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003989 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3990 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003993 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003996 return( ret );
3997 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003998 }
3999 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4001#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4002 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004003 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004004 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004007 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004008 }
4009 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004012#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4013 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4014 {
4015 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4016 p, end - p );
4017 if( ret != 0 )
4018 {
4019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4020 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4021 }
4022
4023 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4024 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4025 ssl->conf->f_rng, ssl->conf->p_rng );
4026 if( ret != 0 )
4027 {
4028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4029 return( ret );
4030 }
4031 }
4032 else
4033#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004042 return( ret );
4043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004044
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 ssl->state++;
4046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004048
4049 return( 0 );
4050}
4051
Gilles Peskineeccd8882020-03-10 12:19:08 +01004052#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004054{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004055 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004056 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Hanno Becker77adddc2019-02-07 12:32:43 +00004060 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004063 ssl->state++;
4064 return( 0 );
4065 }
4066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004069}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004070#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004074 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004075 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004076 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004077 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4079 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004082 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004083 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004084 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004087
Hanno Becker2a831a42019-02-07 13:17:25 +00004088 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004091 ssl->state++;
4092 return( 0 );
4093 }
4094
Hanno Becker2a831a42019-02-07 13:17:25 +00004095#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4096 if( ssl->session_negotiate->peer_cert == NULL )
4097 {
4098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4099 ssl->state++;
4100 return( 0 );
4101 }
4102#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4103 if( ssl->session_negotiate->peer_cert_digest == NULL )
4104 {
4105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4106 ssl->state++;
4107 return( 0 );
4108 }
4109#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4110
Simon Butcher99000142016-10-13 17:21:01 +01004111 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004112 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004113 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004115 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004116 return( ret );
4117 }
4118
4119 ssl->state++;
4120
Simon Butcher99000142016-10-13 17:21:01 +01004121 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4123 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4126 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 }
4128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004131#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4132 peer_pk = &ssl->handshake->peer_pubkey;
4133#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4134 if( ssl->session_negotiate->peer_cert == NULL )
4135 {
4136 /* Should never happen */
4137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4138 }
4139 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4140#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4141
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004142 /*
4143 * struct {
4144 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4145 * opaque signature<0..2^16-1>;
4146 * } DigitallySigned;
4147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4149 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004150 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004151 if( i + 2 > ssl->in_hslen )
4152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4154 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004155 }
4156
Paul Bakker5121ce52009-01-03 21:22:43 +00004157 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004158 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004159 */
Simon Butcher99000142016-10-13 17:21:01 +01004160 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4161
4162 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004165 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004167 }
4168
Simon Butcher99000142016-10-13 17:21:01 +01004169#if !defined(MBEDTLS_MD_SHA1)
4170 if( MBEDTLS_MD_SHA1 == md_alg )
4171 hash_start += 16;
4172#endif
Paul Bakker926af752012-11-23 13:38:07 +01004173
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004174 /* Info from md_alg will be used instead */
4175 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004176
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004177 i++;
4178
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004179 /*
4180 * Signature
4181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4183 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004186 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004188 }
4189
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004190 /*
4191 * Check the certificate's key type matches the signature alg
4192 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004193 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4196 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004197 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004198
4199 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004200 }
4201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004206 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004207
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004208 if( i + 2 > ssl->in_hslen )
4209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4211 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004212 }
4213
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004214 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4215 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004216
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004217 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4220 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 }
4222
Simon Butcher99000142016-10-13 17:21:01 +01004223 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004224 {
4225 size_t dummy_hlen;
4226 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4227 }
Simon Butcher99000142016-10-13 17:21:01 +01004228
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004229 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004230 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004231 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 return( ret );
4235 }
4236
Simon Butcher99000142016-10-13 17:21:01 +01004237 mbedtls_ssl_update_handshake_status( ssl );
4238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004240
Paul Bakkered27a042013-04-18 22:46:23 +02004241 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004242}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004243#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4246static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004247{
Janos Follath865b3eb2019-12-16 11:46:15 +00004248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004249 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004250 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4255 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004256
4257 /*
4258 * struct {
4259 * uint32 ticket_lifetime_hint;
4260 * opaque ticket<0..2^16-1>;
4261 * } NewSessionTicket;
4262 *
4263 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4264 * 8 . 9 ticket_len (n)
4265 * 10 . 9+n ticket content
4266 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004267
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004268 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004269 ssl->session_negotiate,
4270 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004271 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004272 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004273 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004275 tlen = 0;
4276 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004277
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004278 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4279 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4280 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4281 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4282
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004283 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4284 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004285
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004286 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004287
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004288 /*
4289 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4290 * ChangeCipherSpec share the same state.
4291 */
4292 ssl->handshake->new_session_ticket = 0;
4293
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004294 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004295 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004297 return( ret );
4298 }
4299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004301
4302 return( 0 );
4303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004305
Paul Bakker5121ce52009-01-03 21:22:43 +00004306/*
Paul Bakker1961b702013-01-25 14:49:24 +01004307 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004310{
4311 int ret = 0;
4312
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004313 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004319 return( ret );
4320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004324 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004325 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004326 return( ret );
4327 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004328#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004329
Paul Bakker1961b702013-01-25 14:49:24 +01004330 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 case MBEDTLS_SSL_HELLO_REQUEST:
4333 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 break;
4335
Paul Bakker1961b702013-01-25 14:49:24 +01004336 /*
4337 * <== ClientHello
4338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004340 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004341 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343#if defined(MBEDTLS_SSL_PROTO_DTLS)
4344 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4345 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004346#endif
4347
Paul Bakker1961b702013-01-25 14:49:24 +01004348 /*
4349 * ==> ServerHello
4350 * Certificate
4351 * ( ServerKeyExchange )
4352 * ( CertificateRequest )
4353 * ServerHelloDone
4354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004356 ret = ssl_write_server_hello( ssl );
4357 break;
4358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4360 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004361 break;
4362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004364 ret = ssl_write_server_key_exchange( ssl );
4365 break;
4366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004368 ret = ssl_write_certificate_request( ssl );
4369 break;
4370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004372 ret = ssl_write_server_hello_done( ssl );
4373 break;
4374
4375 /*
4376 * <== ( Certificate/Alert )
4377 * ClientKeyExchange
4378 * ( CertificateVerify )
4379 * ChangeCipherSpec
4380 * Finished
4381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4383 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004384 break;
4385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004387 ret = ssl_parse_client_key_exchange( ssl );
4388 break;
4389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004391 ret = ssl_parse_certificate_verify( ssl );
4392 break;
4393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4395 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004396 break;
4397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 case MBEDTLS_SSL_CLIENT_FINISHED:
4399 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004400 break;
4401
4402 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004403 * ==> ( NewSessionTicket )
4404 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004405 * Finished
4406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4408#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004409 if( ssl->handshake->new_session_ticket != 0 )
4410 ret = ssl_write_new_session_ticket( ssl );
4411 else
Paul Bakkera503a632013-08-14 13:48:06 +02004412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004414 break;
4415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 case MBEDTLS_SSL_SERVER_FINISHED:
4417 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004418 break;
4419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 case MBEDTLS_SSL_FLUSH_BUFFERS:
4421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4422 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004423 break;
4424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4426 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004427 break;
4428
4429 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 }
4433
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 return( ret );
4435}
TRodziewicz8476f2f2021-06-02 14:34:47 +02004436
TRodziewicz3946f792021-06-14 12:11:18 +02004437void mbedtls_ssl_conf_preference_order( mbedtls_ssl_config *conf, int order )
TRodziewicz8476f2f2021-06-02 14:34:47 +02004438{
TRodziewicz3946f792021-06-14 12:11:18 +02004439 conf->respect_cli_pref = order;
TRodziewicz8476f2f2021-06-02 14:34:47 +02004440}
4441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#endif /* MBEDTLS_SSL_SRV_C */