blob: 784ab2d516f6ff8601725903d96402b55c60cd5f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
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)
Robert Cragie136884c2015-10-02 13:34:31 +0100410 ssl->handshake->ecjpake_ctx.point_format = p[0];
411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100413 return( 0 );
414 }
415
416 list_size--;
417 p++;
418 }
419
420 return( 0 );
421}
Robert Cragieae8535d2015-10-06 17:11:18 +0100422#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
423 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100424
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200425#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
426static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
427 const unsigned char *buf,
428 size_t len )
429{
Janos Follath865b3eb2019-12-16 11:46:15 +0000430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200431
432 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
433 {
434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
435 return( 0 );
436 }
437
438 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
439 buf, len ) ) != 0 )
440 {
441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
443 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200444 return( ret );
445 }
446
447 /* Only mark the extension as OK when we're sure it is */
448 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
449
450 return( 0 );
451}
452#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
455static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200456 const unsigned char *buf,
457 size_t len )
458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200462 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
463 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200465 }
466
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200467 ssl->session_negotiate->mfl_code = buf[0];
468
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200469 return( 0 );
470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200472
Hanno Beckera0e20d02019-05-15 14:03:01 +0100473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100474static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
475 const unsigned char *buf,
476 size_t len )
477{
478 size_t peer_cid_len;
479
480 /* CID extension only makes sense in DTLS */
481 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
482 {
483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
485 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
486 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
487 }
488
489 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100490 * Quoting draft-ietf-tls-dtls-connection-id-05
491 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100492 *
493 * struct {
494 * opaque cid<0..2^8-1>;
495 * } ConnectionId;
496 */
497
498 if( len < 1 )
499 {
500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
501 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
502 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
503 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
504 }
505
506 peer_cid_len = *buf++;
507 len--;
508
509 if( len != peer_cid_len )
510 {
511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
512 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
513 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
514 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
515 }
516
517 /* Ignore CID if the user has disabled its use. */
518 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
519 {
520 /* Leave ssl->handshake->cid_in_use in its default
521 * value of MBEDTLS_SSL_CID_DISABLED. */
522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
523 return( 0 );
524 }
525
526 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
527 {
528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
530 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
531 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
532 }
533
Hanno Becker08556bf2019-05-03 12:43:44 +0100534 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100535 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
536 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
537
538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
539 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
540
Hanno Becker89dcc882019-04-26 13:56:39 +0100541 return( 0 );
542}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100543#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
546static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200547 const unsigned char *buf,
548 size_t len )
549{
550 if( len != 0 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
554 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200556 }
557
558 ((void) buf);
559
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200560 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200562
563 return( 0 );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
568static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100569 const unsigned char *buf,
570 size_t len )
571{
572 if( len != 0 )
573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200575 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
576 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100578 }
579
580 ((void) buf);
581
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200582 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
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
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200607 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200611 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200612
613 return( 0 );
614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
618static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200619 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200620 size_t len )
621{
Janos Follath865b3eb2019-12-16 11:46:15 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200623 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200624
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200625 mbedtls_ssl_session_init( &session );
626
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200627 if( ssl->conf->f_ticket_parse == NULL ||
628 ssl->conf->f_ticket_write == NULL )
629 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200630 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200631 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200632
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200633 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200634 ssl->handshake->new_session_ticket = 1;
635
Paul Elliottd48d5c62021-01-07 14:47:05 +0000636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200637
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200638 if( len == 0 )
639 return( 0 );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_SSL_RENEGOTIATION)
642 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200645 return( 0 );
646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200648
649 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200650 * Failures are ok: just ignore the ticket and proceed.
651 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200652 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
653 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200654 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200655 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200656
657 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
659 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
661 else
662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
663
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200666
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200667 /*
668 * Keep the session ID sent by the client, since we MUST send it back to
669 * inform them we're accepting the ticket (RFC 5077 section 3.4)
670 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200671 session.id_len = ssl->session_negotiate->id_len;
672 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200673
674 mbedtls_ssl_session_free( ssl->session_negotiate );
675 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
676
677 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500678 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200683
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200684 /* Don't send a new ticket after all, this one is OK */
685 ssl->handshake->new_session_ticket = 0;
686
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200687 return( 0 );
688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_ALPN)
692static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200693 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200694{
Paul Bakker14b16c62014-05-28 11:33:54 +0200695 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200696 const unsigned char *theirs, *start, *end;
697 const char **ours;
698
699 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200700 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200701 return( 0 );
702
703 /*
704 * opaque ProtocolName<1..2^8-1>;
705 *
706 * struct {
707 * ProtocolName protocol_name_list<2..2^16-1>
708 * } ProtocolNameList;
709 */
710
711 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
712 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200713 {
714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200717 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718
719 list_len = ( buf[0] << 8 ) | buf[1];
720 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200721 {
722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200725 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200726
727 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100728 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200729 */
730 start = buf + 2;
731 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100732 for( theirs = start; theirs != end; theirs += cur_len )
733 {
734 cur_len = *theirs++;
735
736 /* Current identifier must fit in list */
737 if( cur_len > (size_t)( end - theirs ) )
738 {
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
742 }
743
744 /* Empty strings MUST NOT be included */
745 if( cur_len == 0 )
746 {
747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
748 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
749 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
750 }
751 }
752
753 /*
754 * Use our order of preference
755 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200756 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200757 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200758 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200759 for( theirs = start; theirs != end; theirs += cur_len )
760 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200761 cur_len = *theirs++;
762
Paul Bakker14b16c62014-05-28 11:33:54 +0200763 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200764 memcmp( theirs, *ours, cur_len ) == 0 )
765 {
766 ssl->alpn_chosen = *ours;
767 return( 0 );
768 }
769 }
770 }
771
772 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
774 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
775 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200778
Johan Pascalb62bb512015-12-03 21:56:45 +0100779#if defined(MBEDTLS_SSL_DTLS_SRTP)
780static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300781 const unsigned char *buf,
782 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100783{
Johan Pascal43f94902020-09-22 12:25:52 +0200784 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100785 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200786 size_t profile_length;
787 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200788 /*! 2 bytes for profile length and 1 byte for mki len */
789 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100790
791 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100792 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
793 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
794 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200795 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100796 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200797 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100798
799 /* RFC5764 section 4.1.1
800 * uint8 SRTPProtectionProfile[2];
801 *
802 * struct {
803 * SRTPProtectionProfiles SRTPProtectionProfiles;
804 * opaque srtp_mki<0..255>;
805 * } UseSRTPData;
806
807 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100808 */
809
Ron Eldoref72faf2018-07-12 11:54:20 +0300810 /*
811 * Min length is 5: at least one protection profile(2 bytes)
812 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200813 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200814 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300815 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200816 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200817 {
818 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
819 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100820 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200821 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100822
Johan Pascal43f94902020-09-22 12:25:52 +0200823 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200824
Ron Eldoref72faf2018-07-12 11:54:20 +0300825 /* first 2 bytes are protection profile length(in bytes) */
826 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200827 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200828
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200829 /* The profile length cannot be bigger than input buffer size - lengths fields */
830 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200831 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200832 {
833 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
834 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
835 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
836 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300837 /*
838 * parse the extension list values are defined in
839 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
840 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200841 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100842 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200843 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200844 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100845
Johan Pascal43f94902020-09-22 12:25:52 +0200846 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300847 {
Johan Pascal43f94902020-09-22 12:25:52 +0200848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
849 mbedtls_ssl_get_srtp_profile_as_string(
850 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300851 }
Johan Pascal85269572020-08-25 10:01:54 +0200852 else
853 {
854 continue;
855 }
Ron Eldor591f1622018-01-22 12:30:04 +0200856 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200857 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200858 {
859 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
860 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200861 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
863 mbedtls_ssl_get_srtp_profile_as_string(
864 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200865 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100866 }
867 }
Johan Pascal43f94902020-09-22 12:25:52 +0200868 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200869 break;
870 }
Johan Pascal042d4562020-08-25 12:14:02 +0200871 buf += profile_length; /* buf points to the mki length */
872 mki_length = *buf;
873 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200874
Johan Pascal042d4562020-08-25 12:14:02 +0200875 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
876 mki_length + profile_length + size_of_lengths != len )
877 {
878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
879 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
880 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
881 }
882
883 /* Parse the mki only if present and mki is supported locally */
884 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
885 mki_length > 0 )
886 {
887 ssl->dtls_srtp_info.mki_len = mki_length;
888
Johan Pascal5ef72d22020-10-28 17:05:47 +0100889 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300890
Ron Eldoref72faf2018-07-12 11:54:20 +0300891 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
892 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100893 }
894
Johan Pascal042d4562020-08-25 12:14:02 +0200895 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100896}
897#endif /* MBEDTLS_SSL_DTLS_SRTP */
898
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100899/*
900 * Auxiliary functions for ServerHello parsing and related actions
901 */
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100904/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100905 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#if defined(MBEDTLS_ECDSA_C)
908static int ssl_check_key_curve( mbedtls_pk_context *pk,
909 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 const mbedtls_ecp_curve_info **crv = curves;
912 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100913
914 while( *crv != NULL )
915 {
916 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100917 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100918 crv++;
919 }
920
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100921 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100924
925/*
926 * Try picking a certificate for this ciphersuite,
927 * return 0 on success and -1 on failure.
928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929static int ssl_pick_cert( mbedtls_ssl_context *ssl,
930 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100933 mbedtls_pk_type_t pk_alg =
934 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200935 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100938 if( ssl->handshake->sni_key_cert != NULL )
939 list = ssl->handshake->sni_key_cert;
940 else
941#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200942 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100945 return( 0 );
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000948
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200949 if( list == NULL )
950 {
951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
952 return( -1 );
953 }
954
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100955 for( cur = list; cur != NULL; cur = cur->next )
956 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400957 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000959 cur->cert );
960
Gilles Peskinee198df52018-01-05 21:17:45 +0100961 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100964 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000965 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100966
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200967 /*
968 * This avoids sending the client a cert it'll reject based on
969 * keyUsage or other extensions.
970 *
971 * It also allows the user to provision different certificates for
972 * different uses based on keyUsage, eg if they want to avoid signing
973 * and decrypting with the same RSA key.
974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100976 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000979 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200980 continue;
981 }
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#if defined(MBEDTLS_ECDSA_C)
984 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200985 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100988 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000989 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100990#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100991
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100992 /*
993 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
994 * present them a SHA-higher cert rather than failing if it's the only
995 * one we got that satisfies the other conditions.
996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
998 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100999 {
1000 if( fallback == NULL )
1001 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001004 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001005 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001006 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001007 }
1008
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001009 /* If we get there, we got a winner */
1010 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001011 }
1012
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001013 if( cur == NULL )
1014 cur = fallback;
1015
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001016 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001017 if( cur != NULL )
1018 {
1019 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001021 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001022 return( 0 );
1023 }
1024
1025 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001028
1029/*
1030 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1031 * Sets ciphersuite_info only if the suite matches.
1032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1034 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001035{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001037
Hanno Becker7e5437a2017-04-28 17:15:26 +01001038#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001039 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001040 mbedtls_pk_type_t sig_type;
1041#endif
1042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001044 if( suite_info == NULL )
1045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001048 }
1049
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +00001051 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001052
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001053 if( suite_info->min_minor_ver > ssl->minor_ver ||
1054 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001057 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001058 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001063 return( 0 );
1064#endif
1065
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001066#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001067 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001071 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001072 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001073#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001074
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001075#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1076 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001077 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001078 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1080 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001081 return( 0 );
1082 }
1083#endif
1084
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1087 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001088 ( ssl->handshake->curves == NULL ||
1089 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001092 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001093 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001094 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001095#endif
1096
Gilles Peskineeccd8882020-03-10 12:19:08 +01001097#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001098 /* If the ciphersuite requires a pre-shared key and we don't
1099 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001101 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001104 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001105 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001106#endif
1107
Hanno Becker7e5437a2017-04-28 17:15:26 +01001108#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001109 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001110 /* If the ciphersuite requires signing, check whether
1111 * a suitable hash algorithm is present. */
1112 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1113 {
1114 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1115 if( sig_type != MBEDTLS_PK_NONE &&
1116 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1117 {
1118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001119 "for signature algorithm %u", (unsigned) sig_type ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001120 return( 0 );
1121 }
1122 }
1123
1124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001125 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001128 /*
1129 * Final check: if ciphersuite requires us to have a
1130 * certificate/key of a particular type:
1131 * - select the appropriate certificate if we have one, or
1132 * - try the next ciphersuite if we don't
1133 * This must be done last since we modify the key_cert list.
1134 */
1135 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001138 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001139 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001140 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001141#endif
1142
1143 *ciphersuite_info = suite_info;
1144 return( 0 );
1145}
1146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1148static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001149{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001150 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001151 unsigned int i, j;
1152 size_t n;
1153 unsigned int ciph_len, sess_len, chal_len;
1154 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001155 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_SSL_RENEGOTIATION)
1161 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001164 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1165 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001169
1170 buf = ssl->in_hdr;
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001175 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001177 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001179 buf[3], buf[4] ) );
1180
1181 /*
1182 * SSLv2 Client Hello
1183 *
1184 * Record layer:
1185 * 0 . 1 message length
1186 *
1187 * SSL layer:
1188 * 2 . 2 message type
1189 * 3 . 4 protocol version
1190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1192 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1195 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001196 }
1197
1198 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1199
1200 if( n < 17 || n > 512 )
1201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1203 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001204 }
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001207 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1208 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001209
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001210 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001213 " [%d:%d] < [%d:%d]",
1214 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001215 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1218 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1219 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001220 }
1221
Paul Bakker2fbefde2013-06-29 16:01:15 +02001222 ssl->handshake->max_major_ver = buf[3];
1223 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001228 return( ret );
1229 }
1230
1231 ssl->handshake->update_checksum( ssl, buf + 2, n );
1232
1233 buf = ssl->in_msg;
1234 n = ssl->in_left - 5;
1235
1236 /*
1237 * 0 . 1 ciphersuitelist length
1238 * 2 . 3 session id length
1239 * 4 . 5 challenge length
1240 * 6 . .. ciphersuitelist
1241 * .. . .. session id
1242 * .. . .. challenge
1243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001245
1246 ciph_len = ( buf[0] << 8 ) | buf[1];
1247 sess_len = ( buf[2] << 8 ) | buf[3];
1248 chal_len = ( buf[4] << 8 ) | buf[5];
1249
Paul Elliott9f352112020-12-09 14:55:45 +00001250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %u, sess_len: %u, chal_len: %u",
Paul Bakker78a8c712013-03-06 17:01:52 +01001251 ciph_len, sess_len, chal_len ) );
1252
1253 /*
1254 * Make sure each parameter length is valid
1255 */
1256 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1259 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001260 }
1261
1262 if( sess_len > 32 )
1263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1265 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001266 }
1267
1268 if( chal_len < 8 || chal_len > 32 )
1269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1271 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001272 }
1273
1274 if( n != 6 + ciph_len + sess_len + chal_len )
1275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1277 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001278 }
1279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001281 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001283 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001285 buf + 6 + ciph_len + sess_len, chal_len );
1286
1287 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001288 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001289 memset( ssl->session_negotiate->id, 0,
1290 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001291 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001292
1293 p += sess_len;
1294 memset( ssl->handshake->randbytes, 0, 64 );
1295 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1296
1297 /*
1298 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1299 */
1300 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1305#if defined(MBEDTLS_SSL_RENEGOTIATION)
1306 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001309 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001310
Gilles Peskinec94f7352017-05-10 16:37:56 +02001311 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1312 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#endif /* MBEDTLS_SSL_RENEGOTIATION */
1316 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001317 break;
1318 }
1319 }
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001322 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1323 {
1324 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1326 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001329
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001330 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1335 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001338 }
1339
1340 break;
1341 }
1342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001344
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001345 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001346 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001347 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001349 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001350 for( i = 0; ciphersuites[i] != 0; i++ )
1351#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001352 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001353 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001354#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001355 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001356 if( p[0] != 0 ||
1357 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1358 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1359 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001360
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001361 got_common_suite = 1;
1362
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001363 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1364 &ciphersuite_info ) ) != 0 )
1365 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001366
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001367 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001368 goto have_ciphersuite_v2;
1369 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001370
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001371 if( got_common_suite )
1372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001374 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001376 }
1377 else
1378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1380 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001381 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001382
1383have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001385
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001386 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001387 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001388
1389 /*
1390 * SSLv2 Client Hello relevant renegotiation security checks
1391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001393 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001396 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1397 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001399 }
1400
1401 ssl->in_left = 0;
1402 ssl->state++;
1403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001405
1406 return( 0 );
1407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001409
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001410/* This function doesn't alert on errors that happen early during
1411 ClientHello parsing because they might indicate that the client is
1412 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001414{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001415 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001416 size_t i, j;
1417 size_t ciph_offset, comp_offset, ext_offset;
1418 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001420 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001421#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001422 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001424 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001425#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001426 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001427 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001429 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Hanno Becker7e5437a2017-04-28 17:15:26 +01001431 /* If there is no signature-algorithm extension present,
1432 * we need to fall back to the default values for allowed
1433 * signature-hash pairs. */
1434#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001435 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001436 int sig_hash_alg_ext_present = 0;
1437#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001438 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001443read_record_header:
1444#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001445 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001447 * otherwise read it ourselves manually in order to support SSLv2
1448 * ClientHello, which doesn't use the same record layer format.
1449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_SSL_RENEGOTIATION)
1451 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001452#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001455 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001456 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001458 return( ret );
1459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 }
1461
1462 buf = ssl->in_hdr;
1463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001467#endif
1468 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001469 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001470#endif
1471
Hanno Becker5903de42019-05-03 14:46:38 +01001472 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001473
Paul Bakkerec636f32012-09-09 19:17:02 +00001474 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001475 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001476 *
1477 * Record layer:
1478 * 0 . 0 message type
1479 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001480 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001481 * 3 . 4 message length
1482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001484 buf[0] ) );
1485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1489 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001493 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001496 buf[1], buf[2] ) );
1497
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001498 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001499
1500 /* According to RFC 5246 Appendix E.1, the version here is typically
1501 * "{03,00}, the lowest version number supported by the client, [or] the
1502 * value of ClientHello.client_version", so the only meaningful check here
1503 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1507 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001510 /* For DTLS if this is the initial handshake, remember the client sequence
1511 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SSL_RENEGOTIATION)
1515 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001516#endif
1517 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001518 {
1519 /* Epoch should be 0 for initial handshakes */
1520 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1523 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001524 }
1525
Hanno Becker19859472018-08-06 09:40:20 +01001526 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1529 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001532 ssl->next_record_offset = 0;
1533 ssl->in_left = 0;
1534 goto read_record_header;
1535 }
1536
1537 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001539#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001540 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001542
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001543 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_SSL_RENEGOTIATION)
1546 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001549 msg_len = ssl->in_hslen;
1550 }
1551 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001552#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001553 {
Angus Grattond8213d02016-05-25 20:56:48 +10001554 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1557 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001558 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001559
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001560 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001561 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001564 return( ret );
1565 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001566
1567 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001570 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001571 else
1572#endif
1573 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001574 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001575
1576 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001579
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001580 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001581
1582 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001583 * Handshake layer:
1584 * 0 . 0 handshake type
1585 * 1 . 3 handshake length
1586 * 4 . 5 DTLS only: message seqence number
1587 * 6 . 8 DTLS only: fragment offset
1588 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1593 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001594 }
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1601 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001602 }
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001605 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1606
1607 /* We don't support fragmentation of ClientHello (yet?) */
1608 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1612 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001613 }
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001617 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001618 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001619 * Copy the client's handshake message_seq on initial handshakes,
1620 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_SSL_RENEGOTIATION)
1623 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001624 {
1625 /* This couldn't be done in ssl_prepare_handshake_record() */
1626 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1627 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001628
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001629 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001632 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001633 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001635 }
1636
1637 ssl->handshake->in_msg_seq++;
1638 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001639 else
1640#endif
1641 {
1642 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1643 ssl->in_msg[5];
1644 ssl->handshake->out_msg_seq = cli_msg_seq;
1645 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1646 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001647
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001648 /*
1649 * For now we don't support fragmentation, so make sure
1650 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001651 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001652 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1653 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1656 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001657 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 buf += mbedtls_ssl_hs_hdr_len( ssl );
1662 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001663
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001664 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001665 * ClientHello layer:
1666 * 0 . 1 protocol version
1667 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1668 * 34 . 35 session id length (1 byte)
1669 * 35 . 34+x session id
1670 * 35+x . 35+x DTLS only: cookie length (1 byte)
1671 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001672 * .. . .. ciphersuite list length (2 bytes)
1673 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001674 * .. . .. compression alg. list length (1 byte)
1675 * .. . .. compression alg. list
1676 * .. . .. extensions length (2 bytes, optional)
1677 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001678 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001679
1680 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001681 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001682 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1683 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001684 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001685 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1688 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001689 }
1690
1691 /*
1692 * Check and save the protocol version
1693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001697 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001698
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001699 ssl->handshake->max_major_ver = ssl->major_ver;
1700 ssl->handshake->max_minor_ver = ssl->minor_ver;
1701
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001702 if( ssl->major_ver < ssl->conf->min_major_ver ||
1703 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001706 " [%d:%d] < [%d:%d]",
1707 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001708 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1710 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001712 }
1713
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001714 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001715 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001716 ssl->major_ver = ssl->conf->max_major_ver;
1717 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001718 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001719 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1720 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001721
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001722 /*
1723 * Save client random (inc. Unix time)
1724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001726
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001727 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001728
1729 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001730 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001731 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001732 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001733
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001734 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001735 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001738 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1739 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001741 }
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001744
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001745 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001746 memset( ssl->session_negotiate->id, 0,
1747 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001748 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001749 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001750
1751 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001752 * Check the cookie length and content
1753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001756 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001757 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001758 cookie_len = buf[cookie_offset];
1759
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001760 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001763 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1764 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001766 }
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001769 buf + cookie_offset + 1, cookie_len );
1770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001772 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_SSL_RENEGOTIATION)
1774 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001775#endif
1776 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001777 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001778 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001779 buf + cookie_offset + 1, cookie_len,
1780 ssl->cli_id, ssl->cli_id_len ) != 0 )
1781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001783 ssl->handshake->verify_cookie_len = 1;
1784 }
1785 else
1786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001788 ssl->handshake->verify_cookie_len = 0;
1789 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001790 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001791 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001793 {
1794 /* We know we didn't send a cookie, so it should be empty */
1795 if( cookie_len != 0 )
1796 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001797 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1799 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001800 }
1801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001803 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001804
1805 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001806 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001807 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001808 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001809 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001810 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001812 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001813
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001814 ciph_len = ( buf[ciph_offset + 0] << 8 )
1815 | ( buf[ciph_offset + 1] );
1816
1817 if( ciph_len < 2 ||
1818 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1819 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001822 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001825 }
1826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001828 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001829
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001830 /*
1831 * Check the compression algorithms length and pick one
1832 */
1833 comp_offset = ciph_offset + 2 + ciph_len;
1834
1835 comp_len = buf[comp_offset];
1836
1837 if( comp_len < 1 ||
1838 comp_len > 16 ||
1839 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001842 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1843 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001845 }
1846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001848 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1851#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001852 for( i = 0; i < comp_len; ++i )
1853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001857 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 }
1859 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001860#endif
1861
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001862 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001864 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001866#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001867
Janos Follathc6dab2b2016-05-23 14:27:02 +01001868 /* Do not parse the extensions if the protocol is SSLv3 */
1869#if defined(MBEDTLS_SSL_PROTO_SSL3)
1870 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1871 {
1872#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001873 /*
1874 * Check the extension length
1875 */
1876 ext_offset = comp_offset + 1 + comp_len;
1877 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001878 {
Simon Butcher584a5472016-05-23 16:24:52 +01001879 if( msg_len < ext_offset + 2 )
1880 {
1881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001882 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1883 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001884 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1885 }
1886
1887 ext_len = ( buf[ext_offset + 0] << 8 )
1888 | ( buf[ext_offset + 1] );
1889
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001890 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1894 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001895 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1896 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001897 }
Simon Butcher584a5472016-05-23 16:24:52 +01001898 else
1899 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001900
Simon Butcher584a5472016-05-23 16:24:52 +01001901 ext = buf + ext_offset + 2;
1902 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001903
Simon Butcher584a5472016-05-23 16:24:52 +01001904 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001905 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001906 unsigned int ext_id;
1907 unsigned int ext_size;
1908 if ( ext_len < 4 ) {
1909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1910 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1911 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1912 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1913 }
1914 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1915 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001916
Simon Butcher584a5472016-05-23 16:24:52 +01001917 if( ext_size + 4 > ext_len )
1918 {
1919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001920 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001922 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1923 }
1924 switch( ext_id )
1925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001927 case MBEDTLS_TLS_EXT_SERVERNAME:
1928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1929 if( ssl->conf->f_sni == NULL )
1930 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001931
Simon Butcher584a5472016-05-23 16:24:52 +01001932 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1933 if( ret != 0 )
1934 return( ret );
1935 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001937
Simon Butcher584a5472016-05-23 16:24:52 +01001938 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001941 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001942#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001943
Simon Butcher584a5472016-05-23 16:24:52 +01001944 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1945 if( ret != 0 )
1946 return( ret );
1947 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001950 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001951 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1953
Simon Butcher584a5472016-05-23 16:24:52 +01001954 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1955 if( ret != 0 )
1956 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001957
1958 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001959 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001961 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001962
Robert Cragie136884c2015-10-02 13:34:31 +01001963#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001964 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001965 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001967
Simon Butcher584a5472016-05-23 16:24:52 +01001968 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1969 if( ret != 0 )
1970 return( ret );
1971 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001972
Simon Butcher584a5472016-05-23 16:24:52 +01001973 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1975 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001976
Simon Butcher584a5472016-05-23 16:24:52 +01001977 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1978 if( ret != 0 )
1979 return( ret );
1980 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001981#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1982 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001983
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001984#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001985 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001987
Simon Butcher584a5472016-05-23 16:24:52 +01001988 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1989 if( ret != 0 )
1990 return( ret );
1991 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001992#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001995 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001997
Simon Butcher584a5472016-05-23 16:24:52 +01001998 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1999 if( ret != 0 )
2000 return( ret );
2001 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002005 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
2006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002007
Simon Butcher584a5472016-05-23 16:24:52 +01002008 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
2009 if( ret != 0 )
2010 return( ret );
2011 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002013
Hanno Beckera0e20d02019-05-15 14:03:01 +01002014#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01002015 case MBEDTLS_TLS_EXT_CID:
2016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2017
2018 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2019 if( ret != 0 )
2020 return( ret );
2021 break;
2022#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002025 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002027
Simon Butcher584a5472016-05-23 16:24:52 +01002028 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2029 if( ret != 0 )
2030 return( ret );
2031 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01002035 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002037
Simon Butcher584a5472016-05-23 16:24:52 +01002038 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
2039 if( ret != 0 )
2040 return( ret );
2041 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01002045 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002047
Simon Butcher584a5472016-05-23 16:24:52 +01002048 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2049 if( ret != 0 )
2050 return( ret );
2051 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002055 case MBEDTLS_TLS_EXT_ALPN:
2056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002057
Simon Butcher584a5472016-05-23 16:24:52 +01002058 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2059 if( ret != 0 )
2060 return( ret );
2061 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002063
Johan Pascalb62bb512015-12-03 21:56:45 +01002064#if defined(MBEDTLS_SSL_DTLS_SRTP)
2065 case MBEDTLS_TLS_EXT_USE_SRTP:
2066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02002067
Johan Pascalc3ccd982020-10-28 17:18:18 +01002068 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2069 if( ret != 0 )
2070 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01002071 break;
2072#endif /* MBEDTLS_SSL_DTLS_SRTP */
2073
Simon Butcher584a5472016-05-23 16:24:52 +01002074 default:
Paul Elliott9f352112020-12-09 14:55:45 +00002075 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01002076 ext_id ) );
2077 }
2078
2079 ext_len -= 4 + ext_size;
2080 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00002081 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002082#if defined(MBEDTLS_SSL_PROTO_SSL3)
2083 }
2084#endif
2085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002087 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
2090 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002091 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002093
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002094 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002095 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2099 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002102 }
2103
2104 break;
2105 }
2106 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002108
Hanno Becker7e5437a2017-04-28 17:15:26 +01002109#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002110 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002111
2112 /*
2113 * Try to fall back to default hash SHA1 if the client
2114 * hasn't provided any preferred signature-hash combinations.
2115 */
2116 if( sig_hash_alg_ext_present == 0 )
2117 {
2118 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2119
2120 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2121 md_default = MBEDTLS_MD_NONE;
2122
2123 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2124 }
2125
2126#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002127 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002128
Paul Bakker48916f92012-09-16 19:57:18 +00002129 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002130 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2131 */
2132 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2137#if defined(MBEDTLS_SSL_RENEGOTIATION)
2138 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002139 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2141 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002142 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2143 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002145 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002148 break;
2149 }
2150 }
2151
2152 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002153 * Renegotiation security checks
2154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002156 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002159 handshake_failure = 1;
2160 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161#if defined(MBEDTLS_SSL_RENEGOTIATION)
2162 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2163 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002164 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002167 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002168 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2170 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002171 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002174 handshake_failure = 1;
2175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2177 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002178 renegotiation_info_seen == 1 )
2179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002181 handshake_failure = 1;
2182 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002184
2185 if( handshake_failure == 1 )
2186 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002187 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2188 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002190 }
Paul Bakker380da532012-04-18 16:10:25 +00002191
Paul Bakker41c83d32013-03-20 14:39:14 +01002192 /*
2193 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002194 * (At the end because we need information from the EC-based extensions
2195 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002196 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002197 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002198 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002199 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002201 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002202 for( i = 0; ciphersuites[i] != 0; i++ )
2203#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002204 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002205 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002206#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002207 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002208 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2209 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2210 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002211
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002212 got_common_suite = 1;
2213
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002214 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2215 &ciphersuite_info ) ) != 0 )
2216 return( ret );
2217
2218 if( ciphersuite_info != NULL )
2219 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002220 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002221
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002222 if( got_common_suite )
2223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002225 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002226 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2227 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002229 }
2230 else
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002233 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2234 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002236 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002237
2238have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002240
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002241 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002242 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002243
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 ssl->state++;
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002247 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002249#endif
2250
Hanno Becker7e5437a2017-04-28 17:15:26 +01002251 /* Debugging-only output for testsuite */
2252#if defined(MBEDTLS_DEBUG_C) && \
2253 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002254 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002255 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2256 {
2257 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2258 if( sig_alg != MBEDTLS_PK_NONE )
2259 {
2260 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2261 sig_alg );
2262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2263 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2264 }
2265 else
2266 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00002268 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002269 }
2270 }
2271#endif
2272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
2275 return( 0 );
2276}
2277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2279static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002280 unsigned char *buf,
2281 size_t *olen )
2282{
2283 unsigned char *p = buf;
2284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002286 {
2287 *olen = 0;
2288 return;
2289 }
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2294 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002295
2296 *p++ = 0x00;
2297 *p++ = 0x00;
2298
2299 *olen = 4;
2300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002302
Hanno Beckera0e20d02019-05-15 14:03:01 +01002303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002304static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2305 unsigned char *buf,
2306 size_t *olen )
2307{
2308 unsigned char *p = buf;
2309 size_t ext_len;
2310 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2311
2312 *olen = 0;
2313
2314 /* Skip writing the extension if we don't want to use it or if
2315 * the client hasn't offered it. */
2316 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2317 return;
2318
2319 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2320 * which is at most 255, so the increment cannot overflow. */
2321 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2322 {
2323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2324 return;
2325 }
2326
2327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2328
2329 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002330 * Quoting draft-ietf-tls-dtls-connection-id-05
2331 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002332 *
2333 * struct {
2334 * opaque cid<0..2^8-1>;
2335 * } ConnectionId;
2336 */
2337
2338 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2339 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2340 ext_len = (size_t) ssl->own_cid_len + 1;
2341 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2342 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2343
2344 *p++ = (uint8_t) ssl->own_cid_len;
2345 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2346
2347 *olen = ssl->own_cid_len + 5;
2348}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002349#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2352static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002353 unsigned char *buf,
2354 size_t *olen )
2355{
2356 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2358 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002359
Hanno Becker27b34d52017-10-20 14:24:51 +01002360 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002362 {
2363 *olen = 0;
2364 return;
2365 }
2366
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002367 /*
2368 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2369 * from a client and then selects a stream or Authenticated Encryption
2370 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2371 * encrypt-then-MAC response extension back to the client."
2372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002374 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2376 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002377 {
2378 *olen = 0;
2379 return;
2380 }
2381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2385 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002386
2387 *p++ = 0x00;
2388 *p++ = 0x00;
2389
2390 *olen = 4;
2391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2395static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002396 unsigned char *buf,
2397 size_t *olen )
2398{
2399 unsigned char *p = buf;
2400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2402 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002403 {
2404 *olen = 0;
2405 return;
2406 }
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002409 "extension" ) );
2410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2412 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002413
2414 *p++ = 0x00;
2415 *p++ = 0x00;
2416
2417 *olen = 4;
2418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2422static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002423 unsigned char *buf,
2424 size_t *olen )
2425{
2426 unsigned char *p = buf;
2427
2428 if( ssl->handshake->new_session_ticket == 0 )
2429 {
2430 *olen = 0;
2431 return;
2432 }
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2437 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002438
2439 *p++ = 0x00;
2440 *p++ = 0x00;
2441
2442 *olen = 4;
2443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002447 unsigned char *buf,
2448 size_t *olen )
2449{
2450 unsigned char *p = buf;
2451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002453 {
2454 *olen = 0;
2455 return;
2456 }
2457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2461 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_SSL_RENEGOTIATION)
2464 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002465 {
2466 *p++ = 0x00;
2467 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2468 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002469
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002470 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2471 p += ssl->verify_data_len;
2472 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2473 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002474 }
2475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002477 {
2478 *p++ = 0x00;
2479 *p++ = 0x01;
2480 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002481 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002482
2483 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002484}
2485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2487static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002488 unsigned char *buf,
2489 size_t *olen )
2490{
2491 unsigned char *p = buf;
2492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002494 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002495 *olen = 0;
2496 return;
2497 }
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2502 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002503
2504 *p++ = 0x00;
2505 *p++ = 1;
2506
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002507 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002508
2509 *olen = 5;
2510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002512
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002513#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002514 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002516 unsigned char *buf,
2517 size_t *olen )
2518{
2519 unsigned char *p = buf;
2520 ((void) ssl);
2521
Paul Bakker677377f2013-10-28 12:54:26 +01002522 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002524 {
2525 *olen = 0;
2526 return;
2527 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2532 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002533
2534 *p++ = 0x00;
2535 *p++ = 2;
2536
2537 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002539
2540 *olen = 6;
2541}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002542#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002543
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002544#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2545static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2546 unsigned char *buf,
2547 size_t *olen )
2548{
Janos Follath865b3eb2019-12-16 11:46:15 +00002549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002550 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002551 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002552 size_t kkpp_len;
2553
2554 *olen = 0;
2555
2556 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002557 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002558 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2559 return;
2560
2561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2562
2563 if( end - p < 4 )
2564 {
2565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2566 return;
2567 }
2568
2569 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2570 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2571
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002572 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2573 p + 2, end - p - 2, &kkpp_len,
2574 ssl->conf->f_rng, ssl->conf->p_rng );
2575 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002576 {
2577 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2578 return;
2579 }
2580
2581 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2582 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2583
2584 *olen = kkpp_len + 4;
2585}
2586#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_ALPN )
2589static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002590 unsigned char *buf, size_t *olen )
2591{
2592 if( ssl->alpn_chosen == NULL )
2593 {
2594 *olen = 0;
2595 return;
2596 }
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002599
2600 /*
2601 * 0 . 1 ext identifier
2602 * 2 . 3 ext length
2603 * 4 . 5 protocol list length
2604 * 6 . 6 protocol name length
2605 * 7 . 7+n protocol name
2606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2608 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002609
2610 *olen = 7 + strlen( ssl->alpn_chosen );
2611
2612 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2613 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2614
2615 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2616 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2617
2618 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2619
2620 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002623
Ron Eldor3adb9922017-12-21 10:15:08 +02002624#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002625static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002626 unsigned char *buf,
2627 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002628{
Ron Eldor75870ec2018-12-06 17:31:55 +02002629 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002630 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002631 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2632
2633 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002634
Johan Pascalc3ccd982020-10-28 17:18:18 +01002635 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2636 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002637 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002638 return;
2639 }
2640
2641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2642
Johan Pascald576fdb2020-09-22 10:39:53 +02002643 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002644 {
2645 mki_len = ssl->dtls_srtp_info.mki_len;
2646 }
2647
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002648 /* The extension total size is 9 bytes :
2649 * - 2 bytes for the extension tag
2650 * - 2 bytes for the total size
2651 * - 2 bytes for the protection profile length
2652 * - 2 bytes for the protection profile
2653 * - 1 byte for the mki length
2654 * + the actual mki length
2655 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002656 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002657 {
2658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2659 return;
2660 }
2661
Johan Pascalb62bb512015-12-03 21:56:45 +01002662 /* extension */
2663 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2664 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002665 /*
2666 * total length 5 and mki value: only one profile(2 bytes)
2667 * and length(2 bytes) and srtp_mki )
2668 */
Ron Eldor591f1622018-01-22 12:30:04 +02002669 ext_len = 5 + mki_len;
2670 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2671 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002672
2673 /* protection profile length: 2 */
2674 buf[4] = 0x00;
2675 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002676 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002677 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002678 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002679 {
2680 buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF );
2681 buf[7] = (unsigned char)( profile_value & 0xFF );
2682 }
2683 else
2684 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002686 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002687 }
2688
Ron Eldor591f1622018-01-22 12:30:04 +02002689 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002690 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002691
Ron Eldor591f1622018-01-22 12:30:04 +02002692 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002693}
2694#endif /* MBEDTLS_SSL_DTLS_SRTP */
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2697static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002698{
Janos Follath865b3eb2019-12-16 11:46:15 +00002699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002700 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002701 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002704
2705 /*
2706 * struct {
2707 * ProtocolVersion server_version;
2708 * opaque cookie<0..2^8-1>;
2709 * } HelloVerifyRequest;
2710 */
2711
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002712 /* The RFC is not clear on this point, but sending the actual negotiated
2713 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002715 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002717 p += 2;
2718
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002719 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002720 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2723 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002724 }
2725
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002726 /* Skip length byte until we know the length */
2727 cookie_len_byte = p++;
2728
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002729 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002730 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002731 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002734 return( ret );
2735 }
2736
2737 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002740
2741 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2743 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002746
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002747 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002748 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002750 return( ret );
2751 }
2752
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002753#if defined(MBEDTLS_SSL_PROTO_DTLS)
2754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2755 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2756 {
2757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2758 return( ret );
2759 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002760#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002763
2764 return( 0 );
2765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002767
Hanno Becker64ce9742021-04-15 08:19:40 +01002768static void ssl_check_id_based_session_resumption( mbedtls_ssl_context *ssl )
2769{
2770 int ret;
2771 mbedtls_ssl_session session_tmp = { 0 };
2772 mbedtls_ssl_session * const session = ssl->session_negotiate;
2773
2774 /* Resume is 0 by default, see ssl_handshake_init().
2775 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2776 if( ssl->handshake->resume == 1 )
2777 return;
2778 if( ssl->session_negotiate->id_len == 0 )
2779 return;
2780 if( ssl->conf->f_get_cache == NULL )
2781 return;
2782#if defined(MBEDTLS_SSL_RENEGOTIATION)
2783 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2784 return;
2785#endif
2786
Hanno Becker64ce9742021-04-15 08:19:40 +01002787 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
Hanno Beckerccdaf6e2021-04-15 09:26:17 +01002788 session->id,
2789 session->id_len,
Hanno Becker64ce9742021-04-15 08:19:40 +01002790 &session_tmp );
2791 if( ret != 0 )
2792 goto exit;
2793
2794 if( session->ciphersuite != session_tmp.ciphersuite ||
2795 session->compression != session_tmp.compression )
2796 {
2797 /* Mismatch between cached and negotiated session */
2798 goto exit;
2799 }
2800
2801 /* Move semantics */
2802 /* Zeroization of session_tmp happens at the end of the function. */
2803 mbedtls_ssl_session_free( session );
2804 *session = session_tmp;
2805
2806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2807 ssl->handshake->resume = 1;
2808
2809exit:
2810
2811 mbedtls_platform_zeroize( &session_tmp, sizeof( session_tmp ) );
2812}
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002815{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002817 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002818#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002820 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 unsigned char *buf, *p;
2822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002827 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002831
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002832 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002835
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002836 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2839 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002840 }
2841
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 /*
2843 * 0 . 0 handshake type
2844 * 1 . 3 handshake length
2845 * 4 . 5 protocol version
2846 * 6 . 9 UNIX time()
2847 * 10 . 37 random bytes
2848 */
2849 buf = ssl->out_msg;
2850 p = buf + 4;
2851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002853 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002854 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002857 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002860 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 *p++ = (unsigned char)( t >> 24 );
2862 *p++ = (unsigned char)( t >> 16 );
2863 *p++ = (unsigned char)( t >> 8 );
2864 *p++ = (unsigned char)( t );
2865
Paul Elliottd48d5c62021-01-07 14:47:05 +00002866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2867 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002868#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002869 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002870 return( ret );
2871
2872 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002875 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002876 return( ret );
2877
2878 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakker48916f92012-09-16 19:57:18 +00002880 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Hanno Becker64ce9742021-04-15 08:19:40 +01002884 ssl_check_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002886 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002887 {
2888 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002889 * New session, create a new session id,
2890 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 ssl->state++;
2893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002895 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002896#endif
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002899 if( ssl->handshake->new_session_ticket != 0 )
2900 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002901 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002902 memset( ssl->session_negotiate->id, 0, 32 );
2903 }
2904 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002906 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002907 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002908 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002909 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002910 return( ret );
2911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 }
2913 else
2914 {
2915 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002916 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002918 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002924 return( ret );
2925 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 }
2927
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002928 /*
2929 * 38 . 38 session id length
2930 * 39 . 38+n session id
2931 * 39+n . 40+n chosen ciphersuite
2932 * 41+n . 41+n chosen compression alg.
2933 * 42+n . 43+n extensions length
2934 * 44+n . 43+n+m extensions
2935 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002936 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2937 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2938 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Paul Elliottd48d5c62021-01-07 14:47:05 +00002940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002943 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Paul Bakker48916f92012-09-16 19:57:18 +00002945 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2946 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2947 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2950 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002952 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002953
Janos Follathc6dab2b2016-05-23 14:27:02 +01002954 /* Do not write the extensions if the protocol is SSLv3 */
2955#if defined(MBEDTLS_SSL_PROTO_SSL3)
2956 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2957 {
2958#endif
2959
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002960 /*
2961 * First write extensions, then the total length
2962 */
2963 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2964 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002967 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2968 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002969#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002972 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2973 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002974#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002975
Hanno Beckera0e20d02019-05-15 14:03:01 +01002976#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002977 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2978 ext_len += olen;
2979#endif
2980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002982 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2983 ext_len += olen;
2984#endif
2985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002987 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2988 ext_len += olen;
2989#endif
2990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002992 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2993 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002994#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002995
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002996#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002997 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002998 if ( mbedtls_ssl_ciphersuite_uses_ec(
2999 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
3000 {
3001 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
3002 ext_len += olen;
3003 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02003004#endif
3005
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02003006#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3007 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
3008 ext_len += olen;
3009#endif
3010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02003012 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
3013 ext_len += olen;
3014#endif
3015
Johan Pascalb62bb512015-12-03 21:56:45 +01003016#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01003017 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
3018 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01003019#endif
3020
Paul Elliottd48d5c62021-01-07 14:47:05 +00003021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
3022 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003023
Paul Bakkera7036632014-04-30 10:15:38 +02003024 if( ext_len > 0 )
3025 {
3026 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
3027 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
3028 p += ext_len;
3029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Janos Follathc6dab2b2016-05-23 14:27:02 +01003031#if defined(MBEDTLS_SSL_PROTO_SSL3)
3032 }
3033#endif
3034
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3037 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003039 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
3043 return( ret );
3044}
3045
Gilles Peskineeccd8882020-03-10 12:19:08 +01003046#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003048{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003049 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003050 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003053
Hanno Becker77adddc2019-02-07 12:32:43 +00003054 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003057 ssl->state++;
3058 return( 0 );
3059 }
3060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003063}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003064#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003068 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003069 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03003070 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003071 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10003073 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003075 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
3079 ssl->state++;
3080
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003081#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3082 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3083 authmode = ssl->handshake->sni_authmode;
3084 else
3085#endif
3086 authmode = ssl->conf->authmode;
3087
Hanno Becker77adddc2019-02-07 12:32:43 +00003088 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003089 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
Johan Pascal4f099262020-09-22 10:59:26 +02003091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3092 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 }
3094
3095 /*
3096 * 0 . 0 handshake type
3097 * 1 . 3 handshake length
3098 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003099 * 5 .. m-1 cert types
3100 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003101 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 * n .. n+1 length of all DNs
3103 * n+2 .. n+3 length of DN 1
3104 * n+4 .. ... Distinguished Name #1
3105 * ... .. ... length of DN 2, etc.
3106 */
3107 buf = ssl->out_msg;
3108 p = buf + 4;
3109
3110 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003111 * Supported certificate types
3112 *
3113 * ClientCertificateType certificate_types<1..2^8-1>;
3114 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003116 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118#if defined(MBEDTLS_RSA_C)
3119 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121#if defined(MBEDTLS_ECDSA_C)
3122 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003123#endif
3124
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003125 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003126 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003127
Paul Bakker577e0062013-08-28 11:57:20 +02003128 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003130 /*
3131 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003132 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003133 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3134 *
3135 * struct {
3136 * HashAlgorithm hash;
3137 * SignatureAlgorithm signature;
3138 * } SignatureAndHashAlgorithm;
3139 *
3140 * enum { (255) } HashAlgorithm;
3141 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003144 {
Simon Butcher99000142016-10-13 17:21:01 +01003145 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003146
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003147 /*
3148 * Supported signature algorithms
3149 */
Simon Butcher99000142016-10-13 17:21:01 +01003150 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3151 {
3152 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3153
3154 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3155 continue;
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003158 p[2 + sa_len++] = hash;
3159 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003162 p[2 + sa_len++] = hash;
3163 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003164#endif
Simon Butcher99000142016-10-13 17:21:01 +01003165 }
Paul Bakker926af752012-11-23 13:38:07 +01003166
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003167 p[0] = (unsigned char)( sa_len >> 8 );
3168 p[1] = (unsigned char)( sa_len );
3169 sa_len += 2;
3170 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003174 /*
3175 * DistinguishedName certificate_authorities<0..2^16-1>;
3176 * opaque DistinguishedName<1..2^16-1>;
3177 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003179
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003180 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003181
3182 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003184 /* NOTE: If trusted certificates are provisioned
3185 * via a CA callback (configured through
3186 * `mbedtls_ssl_conf_ca_cb()`, then the
3187 * CertificateRequest is currently left empty. */
3188
Janos Follath088ce432017-04-10 12:42:31 +01003189#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3190 if( ssl->handshake->sni_ca_chain != NULL )
3191 crt = ssl->handshake->sni_ca_chain;
3192 else
3193#endif
3194 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003195
Janos Follath088ce432017-04-10 12:42:31 +01003196 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003197 {
irwirc9bc3002020-04-01 13:46:36 +03003198 /* It follows from RFC 5280 A.1 that this length
3199 * can be represented in at most 11 bits. */
3200 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003201
irwirc9bc3002020-04-01 13:46:36 +03003202 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003203 {
3204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3205 break;
3206 }
3207
3208 *p++ = (unsigned char)( dn_size >> 8 );
3209 *p++ = (unsigned char)( dn_size );
3210 memcpy( p, crt->subject_raw.p, dn_size );
3211 p += dn_size;
3212
3213 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3214
3215 total_dn_size += 2 + dn_size;
3216 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003217 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 }
3219
Paul Bakker926af752012-11-23 13:38:07 +01003220 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3222 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003223 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3224 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003226 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003229
3230 return( ret );
3231}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003232#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3235 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3236static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003237{
Janos Follath865b3eb2019-12-16 11:46:15 +00003238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3243 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003244 }
3245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3247 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3248 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003251 return( ret );
3252 }
3253
3254 return( 0 );
3255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3257 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003258
Gilles Peskineeccd8882020-03-10 12:19:08 +01003259#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003260 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003261static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003262 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003263{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003264 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3265 * signature length which will be added in ssl_write_server_key_exchange
3266 * after the call to ssl_prepare_server_key_exchange.
3267 * ssl_write_server_key_exchange also takes care of incrementing
3268 * ssl->out_msglen. */
3269 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003270 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003271 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003272 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003273 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003274 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3275 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003276 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003277 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003278 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003279 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003280 return( ret );
3281}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003282#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003283 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003284
Gilles Peskined3eb0612018-01-08 17:07:44 +01003285/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003286 * calculating the signature if any, but excluding formatting the
3287 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003288static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3289 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003292 ssl->handshake->ciphersuite_info;
3293
Gilles Peskineeccd8882020-03-10 12:19:08 +01003294#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3295#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003296 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003297#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3298#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003299
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003300 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003301#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003302 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003303#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003304
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003305 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003307 /*
3308 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003309 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003310 *
3311 */
3312
3313 /*
3314 * - ECJPAKE key exchanges
3315 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003316#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3317 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3318 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003320 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003321
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003322 ret = mbedtls_ecjpake_write_round_two(
3323 &ssl->handshake->ecjpake_ctx,
3324 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003325 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003326 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003327 if( ret != 0 )
3328 {
3329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3330 return( ret );
3331 }
3332
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003333 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003334 }
3335#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3336
Hanno Becker1aa267c2017-04-28 17:08:27 +01003337 /*
3338 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3339 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3340 * we use empty support identity hints here.
3341 **/
3342#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3344 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3345 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003346 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003347 ssl->out_msg[ssl->out_msglen++] = 0x00;
3348 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3351 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003352
Hanno Becker7e5437a2017-04-28 17:15:26 +01003353 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003354 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003355 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003356#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003357 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003358 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003360 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003361
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003362 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3363 {
3364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3366 }
3367
Paul Bakker41c83d32013-03-20 14:39:14 +01003368 /*
3369 * Ephemeral DH parameters:
3370 *
3371 * struct {
3372 * opaque dh_p<1..2^16-1>;
3373 * opaque dh_g<1..2^16-1>;
3374 * opaque dh_Ys<1..2^16-1>;
3375 * } ServerDHParams;
3376 */
Hanno Beckerab740562017-10-04 13:15:37 +01003377 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3378 &ssl->conf->dhm_P,
3379 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003380 {
Hanno Beckerab740562017-10-04 13:15:37 +01003381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003382 return( ret );
3383 }
Paul Bakker48916f92012-09-16 19:57:18 +00003384
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003385 if( ( ret = mbedtls_dhm_make_params(
3386 &ssl->handshake->dhm_ctx,
3387 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3388 ssl->out_msg + ssl->out_msglen, &len,
3389 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003392 return( ret );
3393 }
3394
Gilles Peskineeccd8882020-03-10 12:19:08 +01003395#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003396 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003397#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003399 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3402 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3403 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3404 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003405 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003406#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003407
Hanno Becker1aa267c2017-04-28 17:08:27 +01003408 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003409 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003410 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003411#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003412 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003414 /*
3415 * Ephemeral ECDH parameters:
3416 *
3417 * struct {
3418 * ECParameters curve_params;
3419 * ECPoint public;
3420 * } ServerECDHParams;
3421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003425 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003426
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003427 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003428 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003429 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3430 if( (*curve)->grp_id == *gid )
3431 goto curve_matching_done;
3432
3433curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003434 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3437 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003438 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003441
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003442 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3443 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003444 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003446 return( ret );
3447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003448
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003449 if( ( ret = mbedtls_ecdh_make_params(
3450 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003451 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003452 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003453 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003456 return( ret );
3457 }
3458
Gilles Peskineeccd8882020-03-10 12:19:08 +01003459#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003460 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003461#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003462
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003463 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003464
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003465 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3466 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003467 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003468#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003469
Hanno Becker1aa267c2017-04-28 17:08:27 +01003470 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003471 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003472 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003473 * exchange parameters, compute and add the signature here.
3474 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003475 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003476#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003477 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003478 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003479 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003480 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003481 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003483
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003484 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003485 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003486 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003487 * to choose appropriate hash.
3488 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3489 * (RFC 4492, Sec. 5.4)
3490 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003491 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003492
3493 mbedtls_md_type_t md_alg;
3494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003496 mbedtls_pk_type_t sig_alg =
3497 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003499 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003500 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3501 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003502 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003503 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3504 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003507 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003508 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003510 }
3511 }
Paul Bakker577e0062013-08-28 11:57:20 +02003512 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3514#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3515 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003516 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003517 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003518 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003520 }
3521 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003522#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3523 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003524 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003525 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003527 }
3528
Paul Elliott3891caf2020-12-17 18:42:40 +00003529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003530
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003531 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003532 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3535 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3536 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003537 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003538 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003539 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3540 dig_signed,
3541 dig_signed_len );
3542 if( ret != 0 )
3543 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003544 }
3545 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3547 MBEDTLS_SSL_PROTO_TLS1_1 */
3548#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3549 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3550 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003551 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003552 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003553 dig_signed,
3554 dig_signed_len,
3555 md_alg );
3556 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003557 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003558 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003559 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3561 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003565 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003566
Gilles Peskineebd652f2018-01-05 21:18:59 +01003567 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003568
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003569 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003570 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3573 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003574 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003575 /*
3576 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003577 * explicitly through a prefix to the signature.
3578 *
3579 * struct {
3580 * HashAlgorithm hash;
3581 * SignatureAlgorithm signature;
3582 * } SignatureAndHashAlgorithm;
3583 *
3584 * struct {
3585 * SignatureAndHashAlgorithm algorithm;
3586 * opaque signature<0..2^16-1>;
3587 * } DigitallySigned;
3588 *
3589 */
3590
Gilles Peskine1004c192018-01-08 16:59:14 +01003591 ssl->out_msg[ssl->out_msglen++] =
3592 mbedtls_ssl_hash_from_md_alg( md_alg );
3593 ssl->out_msg[ssl->out_msglen++] =
3594 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003597
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003598#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003599 if( ssl->conf->f_async_sign_start != NULL )
3600 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003601 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003602 mbedtls_ssl_own_cert( ssl ),
3603 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003604 switch( ret )
3605 {
3606 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3607 /* act as if f_async_sign was null */
3608 break;
3609 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003610 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003611 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003612 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003613 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003614 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3615 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003616 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003617 return( ret );
3618 }
3619 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003620#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003621
3622 if( mbedtls_ssl_own_key( ssl ) == NULL )
3623 {
3624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3625 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3626 }
3627
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003628 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3629 * signature length which will be added in ssl_write_server_key_exchange
3630 * after the call to ssl_prepare_server_key_exchange.
3631 * ssl_write_server_key_exchange also takes care of incrementing
3632 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003633 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3634 md_alg, hash, hashlen,
3635 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003636 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003637 ssl->conf->f_rng,
3638 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003641 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003642 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003643 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003644#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003645
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003646 return( 0 );
3647}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003648
Gilles Peskined3eb0612018-01-08 17:07:44 +01003649/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003650 * that do not include a ServerKeyExchange message, do nothing. Either
3651 * way, if successful, move on to the next step in the SSL state
3652 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003653static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3654{
Janos Follath865b3eb2019-12-16 11:46:15 +00003655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003656 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003657#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003658 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003659 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003660#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003661
Gilles Peskined3eb0612018-01-08 17:07:44 +01003662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3663
Gilles Peskineeccd8882020-03-10 12:19:08 +01003664#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003665 /* Extract static ECDH parameters and abort if ServerKeyExchange
3666 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003667 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3668 {
3669 /* For suites involving ECDH, extract DH parameters
3670 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003671#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003672 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3673 {
3674 ssl_get_ecdh_params_from_cert( ssl );
3675 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003676#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003677
3678 /* Key exchanges not involving ephemeral keys don't use
3679 * ServerKeyExchange, so end here. */
3680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3681 ssl->state++;
3682 return( 0 );
3683 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003684#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003685
Gilles Peskineeccd8882020-03-10 12:19:08 +01003686#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003687 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003688 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003689 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003690 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003691 {
3692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3693 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003694 }
3695 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003696#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003697 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003698 {
3699 /* ServerKeyExchange is needed. Prepare the message. */
3700 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003701 }
3702
3703 if( ret != 0 )
3704 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003705 /* If we're starting to write a new message, set ssl->out_msglen
3706 * to 0. But if we're resuming after an asynchronous message,
3707 * out_msglen is the amount of data written so far and mst be
3708 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003709 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3711 else
3712 ssl->out_msglen = 0;
3713 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003714 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003715
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003716 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003717 * ssl_prepare_server_key_exchange already wrote the signature
3718 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003719#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003720 if( signature_len != 0 )
3721 {
3722 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3723 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3724
3725 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3726 ssl->out_msg + ssl->out_msglen,
3727 signature_len );
3728
3729 /* Skip over the already-written signature */
3730 ssl->out_msglen += signature_len;
3731 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003732#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003733
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003734 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3736 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
3738 ssl->state++;
3739
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003740 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 return( ret );
3744 }
3745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748}
3749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003751{
Janos Follath865b3eb2019-12-16 11:46:15 +00003752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
3756 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3758 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
3760 ssl->state++;
3761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003765#endif
3766
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003767 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 return( ret );
3771 }
3772
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003773#if defined(MBEDTLS_SSL_PROTO_DTLS)
3774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3775 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3776 {
3777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3778 return( ret );
3779 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003780#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003783
3784 return( 0 );
3785}
3786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3788 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3789static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003790 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003793 size_t n;
3794
3795 /*
3796 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3797 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003798 if( *p + 2 > end )
3799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3801 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003802 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003803
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003804 n = ( (*p)[0] << 8 ) | (*p)[1];
3805 *p += 2;
3806
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003807 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3810 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003811 }
3812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3816 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003817 }
3818
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003819 *p += n;
3820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003822
Paul Bakker70df2fb2013-04-17 17:19:09 +02003823 return( ret );
3824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3826 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3829 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003830
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003831#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003832static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3833 unsigned char *peer_pms,
3834 size_t *peer_pmslen,
3835 size_t peer_pmssize )
3836{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003837 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003838 peer_pms, peer_pmslen, peer_pmssize );
3839 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3840 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003841 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003842 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003843 }
3844 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3845 return( ret );
3846}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003847#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003848
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003849static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3850 const unsigned char *p,
3851 const unsigned char *end,
3852 unsigned char *peer_pms,
3853 size_t *peer_pmslen,
3854 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003855{
Janos Follath865b3eb2019-12-16 11:46:15 +00003856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003857 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3858 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3859 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003860
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003861#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003862 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003863 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003864 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003865 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3867 return( ssl_resume_decrypt_pms( ssl,
3868 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003869 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003870#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003871
3872 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003873 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3876 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3877 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003878 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003879 if ( p + 2 > end ) {
3880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3881 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3882 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003883 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3884 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3887 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003888 }
3889 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003890#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003891
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003892 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3895 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003896 }
3897
Gilles Peskine422ccab2018-01-11 18:29:01 +01003898 /*
3899 * Decrypt the premaster secret
3900 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003901#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003902 if( ssl->conf->f_async_decrypt_start != NULL )
3903 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003904 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003905 mbedtls_ssl_own_cert( ssl ),
3906 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003907 switch( ret )
3908 {
3909 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3910 /* act as if f_async_decrypt_start was null */
3911 break;
3912 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003913 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003914 return( ssl_resume_decrypt_pms( ssl,
3915 peer_pms,
3916 peer_pmslen,
3917 peer_pmssize ) );
3918 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003919 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003920 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3921 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003922 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003923 return( ret );
3924 }
3925 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003926#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003927
Gilles Peskine422ccab2018-01-11 18:29:01 +01003928 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3929 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3931 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3932 }
3933
3934 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003935 peer_pms, peer_pmslen, peer_pmssize,
3936 ssl->conf->f_rng, ssl->conf->p_rng );
3937 return( ret );
3938}
3939
3940static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3941 const unsigned char *p,
3942 const unsigned char *end,
3943 size_t pms_offset )
3944{
Janos Follath865b3eb2019-12-16 11:46:15 +00003945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003946 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3947 unsigned char ver[2];
3948 unsigned char fake_pms[48], peer_pms[48];
3949 unsigned char mask;
3950 size_t i, peer_pmslen;
3951 unsigned int diff;
3952
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003953 /* In case of a failure in decryption, the decryption may write less than
3954 * 2 bytes of output, but we always read the first two bytes. It doesn't
3955 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003956 * ret being nonzero, and we only care whether diff is 0.
3957 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3958 * also makes memory analyzers happy (don't access uninitialized memory,
3959 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003960 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003961 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003962
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003963 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3964 peer_pms,
3965 &peer_pmslen,
3966 sizeof( peer_pms ) );
3967
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003968#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003969 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3970 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003971#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003974 ssl->handshake->max_minor_ver,
3975 ssl->conf->transport, ver );
3976
3977 /* Avoid data-dependent branches while checking for invalid
3978 * padding, to protect against timing-based Bleichenbacher-type
3979 * attacks. */
3980 diff = (unsigned int) ret;
3981 diff |= peer_pmslen ^ 48;
3982 diff |= peer_pms[0] ^ ver[0];
3983 diff |= peer_pms[1] ^ ver[1];
3984
3985 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3986 /* MSVC has a warning about unary minus on unsigned, but this is
3987 * well-defined and precisely what we want to do here */
3988#if defined(_MSC_VER)
3989#pragma warning( push )
3990#pragma warning( disable : 4146 )
3991#endif
3992 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3993#if defined(_MSC_VER)
3994#pragma warning( pop )
3995#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003996
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003997 /*
3998 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3999 * must not cause the connection to end immediately; instead, send a
4000 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01004001 * To protect against timing-based variants of the attack, we must
4002 * not have any branch that depends on whether the decryption was
4003 * successful. In particular, always generate the fake premaster secret,
4004 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004005 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004006 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004007 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01004008 {
Gilles Peskinee1416382018-04-26 10:23:21 +02004009 /* It's ok to abort on an RNG failure, since this does not reveal
4010 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004011 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01004012 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004013
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01004014#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02004015 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004017#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02004018
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004019 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
4020 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
4021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004024 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004025 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004026
Gilles Peskine422ccab2018-01-11 18:29:01 +01004027 /* Set pms to either the true or the fake PMS, without
4028 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004029 for( i = 0; i < ssl->handshake->pmslen; i++ )
4030 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
4031
4032 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4035 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02004036
Gilles Peskineeccd8882020-03-10 12:19:08 +01004037#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004039 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004040{
Paul Bakker6db455e2013-09-18 17:29:31 +02004041 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03004042 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004043
Hanno Becker845b9462018-10-26 12:07:29 +01004044 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4047 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004048 }
4049
4050 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004051 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02004052 */
Hanno Becker83c9f492017-06-26 13:52:14 +01004053 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4056 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004057 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004058
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004059 n = ( (*p)[0] << 8 ) | (*p)[1];
4060 *p += 2;
4061
irwir6527bd62019-09-21 18:51:25 +03004062 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4065 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004066 }
4067
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004068 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004069 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004070 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004072 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004073 else
Paul Bakker6db455e2013-09-18 17:29:31 +02004074 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01004075 /* Identity is not a big secret since clients send it in the clear,
4076 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004077 if( n != ssl->conf->psk_identity_len ||
4078 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02004079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004081 }
4082 }
4083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004084 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02004087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4088 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004090 }
4091
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004092 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004093
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004094 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004095}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004096#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004099{
Janos Follath865b3eb2019-12-16 11:46:15 +00004100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004102 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004103
Hanno Beckere694c3e2017-12-27 21:34:08 +00004104 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004107
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004108#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004109 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4110 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4111 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4112 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004113 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004114 {
4115 /* We've already read a record and there is an asynchronous
4116 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004117 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4119 }
4120 else
4121#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004122 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004125 return( ret );
4126 }
4127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004129 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4134 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004135 }
4136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4140 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004141 }
4142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4144 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004145 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004146 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004149 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004150 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004151
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004152 if( p != end )
4153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4155 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004156 }
4157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004159 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004160 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004161 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004162 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4165 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004166 }
4167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004169 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4172#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4173 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4174 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4175 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4176 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4177 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4178 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4179 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004182 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4185 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004186 }
4187
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004188 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4189 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004192 &ssl->handshake->pmslen,
4193 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004195 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4198 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004199 }
4200
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004201 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4202 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004203 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4206 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4207 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4208 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4209#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4210 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004211 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004212 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004215 return( ret );
4216 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004217
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004218 if( p != end )
4219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4221 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004222 }
4223
Hanno Becker845b9462018-10-26 12:07:29 +01004224#if defined(MBEDTLS_USE_PSA_CRYPTO)
4225 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4226 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004227 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4229 else
4230#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004232 ciphersuite_info->key_exchange ) ) != 0 )
4233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004235 return( ret );
4236 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004238 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4240#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4241 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004242 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004243#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004244 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004245 {
4246 /* There is an asynchronous operation in progress to
4247 * decrypt the encrypted premaster secret, so skip
4248 * directly to resuming this operation. */
4249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4250 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4251 * won't actually use it, but maintain p anyway for robustness. */
4252 p += ssl->conf->psk_identity_len + 2;
4253 }
4254 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004255#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004256 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004259 return( ret );
4260 }
4261
Hanno Becker845b9462018-10-26 12:07:29 +01004262#if defined(MBEDTLS_USE_PSA_CRYPTO)
4263 /* Opaque PSKs are currently only supported for PSK-only. */
4264 if( ssl_use_opaque_psk( ssl ) == 1 )
4265 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4266#endif
4267
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004268 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004271 return( ret );
4272 }
4273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004275 ciphersuite_info->key_exchange ) ) != 0 )
4276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004278 return( ret );
4279 }
4280 }
4281 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4283#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4284 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004285 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004286 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004289 return( ret );
4290 }
4291 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004294 return( ret );
4295 }
4296
Hanno Becker845b9462018-10-26 12:07:29 +01004297#if defined(MBEDTLS_USE_PSA_CRYPTO)
4298 /* Opaque PSKs are currently only supported for PSK-only. */
4299 if( ssl_use_opaque_psk( ssl ) == 1 )
4300 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4301#endif
4302
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004303 if( p != end )
4304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4306 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004307 }
4308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004310 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004313 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004314 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004315 }
4316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4318#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4319 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004320 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004321 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004324 return( ret );
4325 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004328 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4331 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004332 }
4333
Hanno Becker845b9462018-10-26 12:07:29 +01004334#if defined(MBEDTLS_USE_PSA_CRYPTO)
4335 /* Opaque PSKs are currently only supported for PSK-only. */
4336 if( ssl_use_opaque_psk( ssl ) == 1 )
4337 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4338#endif
4339
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004340 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4341 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004344 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004347 return( ret );
4348 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004349 }
4350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4352#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4353 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004354 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004355 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004358 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 }
4360 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004363#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4364 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4365 {
4366 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4367 p, end - p );
4368 if( ret != 0 )
4369 {
4370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4371 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4372 }
4373
4374 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4375 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4376 ssl->conf->f_rng, ssl->conf->p_rng );
4377 if( ret != 0 )
4378 {
4379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4380 return( ret );
4381 }
4382 }
4383 else
4384#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004393 return( ret );
4394 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004395
Paul Bakker5121ce52009-01-03 21:22:43 +00004396 ssl->state++;
4397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004399
4400 return( 0 );
4401}
4402
Gilles Peskineeccd8882020-03-10 12:19:08 +01004403#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004405{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004406 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004407 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004410
Hanno Becker77adddc2019-02-07 12:32:43 +00004411 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004414 ssl->state++;
4415 return( 0 );
4416 }
4417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004420}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004421#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004425 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004426 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004427 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004428 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4430 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004433 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004434 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004435 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004438
Hanno Becker2a831a42019-02-07 13:17:25 +00004439 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004442 ssl->state++;
4443 return( 0 );
4444 }
4445
Hanno Becker2a831a42019-02-07 13:17:25 +00004446#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4447 if( ssl->session_negotiate->peer_cert == NULL )
4448 {
4449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4450 ssl->state++;
4451 return( 0 );
4452 }
4453#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4454 if( ssl->session_negotiate->peer_cert_digest == NULL )
4455 {
4456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4457 ssl->state++;
4458 return( 0 );
4459 }
4460#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4461
Simon Butcher99000142016-10-13 17:21:01 +01004462 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004463 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004464 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004465 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004466 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 return( ret );
4468 }
4469
4470 ssl->state++;
4471
Simon Butcher99000142016-10-13 17:21:01 +01004472 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4474 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4477 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 }
4479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004481
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004482#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4483 peer_pk = &ssl->handshake->peer_pubkey;
4484#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4485 if( ssl->session_negotiate->peer_cert == NULL )
4486 {
4487 /* Should never happen */
4488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4489 }
4490 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4491#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4492
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004493 /*
4494 * struct {
4495 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4496 * opaque signature<0..2^16-1>;
4497 * } DigitallySigned;
4498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4500 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4501 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004504 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004505
4506 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004507 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004508 {
4509 hash_start += 16;
4510 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004512 }
Paul Bakker926af752012-11-23 13:38:07 +01004513 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004514 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4516 MBEDTLS_SSL_PROTO_TLS1_1 */
4517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4518 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004519 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004520 if( i + 2 > ssl->in_hslen )
4521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4523 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004524 }
4525
Paul Bakker5121ce52009-01-03 21:22:43 +00004526 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004527 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004528 */
Simon Butcher99000142016-10-13 17:21:01 +01004529 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4530
4531 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004534 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004536 }
4537
Simon Butcher99000142016-10-13 17:21:01 +01004538#if !defined(MBEDTLS_MD_SHA1)
4539 if( MBEDTLS_MD_SHA1 == md_alg )
4540 hash_start += 16;
4541#endif
Paul Bakker926af752012-11-23 13:38:07 +01004542
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004543 /* Info from md_alg will be used instead */
4544 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004545
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004546 i++;
4547
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004548 /*
4549 * Signature
4550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4552 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004555 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004557 }
4558
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004559 /*
4560 * Check the certificate's key type matches the signature alg
4561 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004562 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4565 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004566 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004567
4568 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004569 }
4570 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004575 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004576
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004577 if( i + 2 > ssl->in_hslen )
4578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4580 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004581 }
4582
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004583 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4584 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004585
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004586 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4589 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 }
4591
Simon Butcher99000142016-10-13 17:21:01 +01004592 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004593 {
4594 size_t dummy_hlen;
4595 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4596 }
Simon Butcher99000142016-10-13 17:21:01 +01004597
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004598 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004599 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004600 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004603 return( ret );
4604 }
4605
Simon Butcher99000142016-10-13 17:21:01 +01004606 mbedtls_ssl_update_handshake_status( ssl );
4607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004609
Paul Bakkered27a042013-04-18 22:46:23 +02004610 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004611}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004612#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4615static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004616{
Janos Follath865b3eb2019-12-16 11:46:15 +00004617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004618 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004619 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4624 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004625
4626 /*
4627 * struct {
4628 * uint32 ticket_lifetime_hint;
4629 * opaque ticket<0..2^16-1>;
4630 * } NewSessionTicket;
4631 *
4632 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4633 * 8 . 9 ticket_len (n)
4634 * 10 . 9+n ticket content
4635 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004636
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004637 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004638 ssl->session_negotiate,
4639 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004640 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004641 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004642 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004644 tlen = 0;
4645 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004646
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004647 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4648 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4649 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4650 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4651
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004652 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4653 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004654
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004655 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004656
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004657 /*
4658 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4659 * ChangeCipherSpec share the same state.
4660 */
4661 ssl->handshake->new_session_ticket = 0;
4662
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004663 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004664 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004666 return( ret );
4667 }
4668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004670
4671 return( 0 );
4672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004673#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004674
Paul Bakker5121ce52009-01-03 21:22:43 +00004675/*
Paul Bakker1961b702013-01-25 14:49:24 +01004676 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004678int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004679{
4680 int ret = 0;
4681
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004682 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004688 return( ret );
4689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004691 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004693 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004694 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004695 return( ret );
4696 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004697#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004698
Paul Bakker1961b702013-01-25 14:49:24 +01004699 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 case MBEDTLS_SSL_HELLO_REQUEST:
4702 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 break;
4704
Paul Bakker1961b702013-01-25 14:49:24 +01004705 /*
4706 * <== ClientHello
4707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004709 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004710 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712#if defined(MBEDTLS_SSL_PROTO_DTLS)
4713 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4714 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004715#endif
4716
Paul Bakker1961b702013-01-25 14:49:24 +01004717 /*
4718 * ==> ServerHello
4719 * Certificate
4720 * ( ServerKeyExchange )
4721 * ( CertificateRequest )
4722 * ServerHelloDone
4723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004725 ret = ssl_write_server_hello( ssl );
4726 break;
4727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4729 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004730 break;
4731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004733 ret = ssl_write_server_key_exchange( ssl );
4734 break;
4735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004737 ret = ssl_write_certificate_request( ssl );
4738 break;
4739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004741 ret = ssl_write_server_hello_done( ssl );
4742 break;
4743
4744 /*
4745 * <== ( Certificate/Alert )
4746 * ClientKeyExchange
4747 * ( CertificateVerify )
4748 * ChangeCipherSpec
4749 * Finished
4750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4752 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004753 break;
4754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004756 ret = ssl_parse_client_key_exchange( ssl );
4757 break;
4758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004760 ret = ssl_parse_certificate_verify( ssl );
4761 break;
4762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4764 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004765 break;
4766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 case MBEDTLS_SSL_CLIENT_FINISHED:
4768 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004769 break;
4770
4771 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004772 * ==> ( NewSessionTicket )
4773 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004774 * Finished
4775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4777#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004778 if( ssl->handshake->new_session_ticket != 0 )
4779 ret = ssl_write_new_session_ticket( ssl );
4780 else
Paul Bakkera503a632013-08-14 13:48:06 +02004781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004783 break;
4784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 case MBEDTLS_SSL_SERVER_FINISHED:
4786 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004787 break;
4788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 case MBEDTLS_SSL_FLUSH_BUFFERS:
4790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4791 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004792 break;
4793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4795 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004796 break;
4797
4798 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004801 }
4802
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 return( ret );
4804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#endif /* MBEDTLS_SSL_SRV_C */