blob: 38908c840c82885e8d0d2c9caf53c733b20b479e [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"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020033#include "mbedtls/ssl_internal.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)
160 if( conf->psk_opaque != 0 )
161 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
175 if( ssl->handshake->psk_opaque != 0 )
176 return( 1 );
177
178 return( 0 );
179 }
180
181 if( ssl->conf->psk_opaque != 0 )
182 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:"
301 " match sig %d and hash %d",
Hanno Becker7e5437a2017-04-28 17:15:26 +0100302 sig_cur, md_cur ) );
303 }
304 else
305 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
307 "hash alg %d not supported", 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", 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{
Ron Eldor3adb9922017-12-21 10:15:08 +0200784 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
Johan Pascalb62bb512015-12-03 21:56:45 +0100785 size_t i,j;
Ron Eldor591f1622018-01-22 12:30:04 +0200786 size_t profile_length;
Ron Eldoref72faf2018-07-12 11:54:20 +0300787 const mbedtls_ssl_srtp_profile_info *profile_info;
Johan Pascalb62bb512015-12-03 21:56:45 +0100788
789 /* If use_srtp is not configured, just ignore the extension */
Ron Eldoref72faf2018-07-12 11:54:20 +0300790 if( ssl->conf->dtls_srtp_profile_list == NULL ||
791 ssl->conf->dtls_srtp_profile_list_len == 0 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100792 return( 0 );
793
794 /* RFC5764 section 4.1.1
795 * uint8 SRTPProtectionProfile[2];
796 *
797 * struct {
798 * SRTPProtectionProfiles SRTPProtectionProfiles;
799 * opaque srtp_mki<0..255>;
800 * } UseSRTPData;
801
802 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100803 */
804
Ron Eldoref72faf2018-07-12 11:54:20 +0300805 /*
806 * Min length is 5: at least one protection profile(2 bytes)
807 * and length(2 bytes) + srtp_mki length(1 byte)
808 */
Johan Pascalb62bb512015-12-03 21:56:45 +0100809 if( len < 5 )
810 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
811
Ron Eldor591f1622018-01-22 12:30:04 +0200812 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
813
Ron Eldoref72faf2018-07-12 11:54:20 +0300814 /* first 2 bytes are protection profile length(in bytes) */
815 profile_length = ( buf[0] << 8 ) | buf[1];
Ron Eldor591f1622018-01-22 12:30:04 +0200816
Ron Eldoref72faf2018-07-12 11:54:20 +0300817 /*
818 * parse the extension list values are defined in
819 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
820 */
821 for( j=0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100822 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300823 /* + 2 to skip the length field */
824 uint16_t protection_profile_value = buf[j + 2] << 8 | buf[j+3];
Johan Pascalb62bb512015-12-03 21:56:45 +0100825
Ron Eldor591f1622018-01-22 12:30:04 +0200826 switch ( protection_profile_value )
827 {
828 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
829 client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
830 break;
831 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
832 client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
833 break;
834 case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
835 client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
836 break;
837 case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
838 client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
839 break;
840 default:
841 client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
842 break;
843 }
Ron Eldorb4655392018-07-05 18:25:39 +0300844 profile_info = mbedtls_ssl_dtls_srtp_profile_info_from_id( client_protection );
845 if( profile_info != NULL )
846 {
847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", profile_info->name ) );
848 }
Ron Eldor591f1622018-01-22 12:30:04 +0200849 /* check if suggested profile is in our list */
850 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
851 {
852 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
853 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200854 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Ron Eldorb4655392018-07-05 18:25:39 +0300855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", profile_info->name ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200856 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100857 }
858 }
Ron Eldor591f1622018-01-22 12:30:04 +0200859 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE )
860 break;
861 }
862 if( ( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) &&
863 ( len > ( profile_length + 2 ) ) )
864 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300865 ssl->dtls_srtp_info.mki_len = buf[profile_length + 2];
Ron Eldor591f1622018-01-22 12:30:04 +0200866 if( ssl->dtls_srtp_info.mki_len > MBEDTLS_DTLS_SRTP_MAX_MKI_LENGTH )
867 {
868 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
869 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
870 ssl->dtls_srtp_info.mki_len = 0;
871 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
872 }
873
Ron Eldoref72faf2018-07-12 11:54:20 +0300874 ssl->dtls_srtp_info.mki_len = buf[profile_length + 2];
Ron Eldor591f1622018-01-22 12:30:04 +0200875 for( i=0; i < ssl->dtls_srtp_info.mki_len; i++ )
876 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300877 ssl->dtls_srtp_info.mki_value[i] = buf[profile_length + 2 + 1 + i];
Ron Eldor591f1622018-01-22 12:30:04 +0200878 }
Ron Eldorb4655392018-07-05 18:25:39 +0300879
Ron Eldoref72faf2018-07-12 11:54:20 +0300880 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
881 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100882 }
883
Ron Eldor591f1622018-01-22 12:30:04 +0200884 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100885}
886#endif /* MBEDTLS_SSL_DTLS_SRTP */
887
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100888/*
889 * Auxiliary functions for ServerHello parsing and related actions
890 */
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100893/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100894 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#if defined(MBEDTLS_ECDSA_C)
897static int ssl_check_key_curve( mbedtls_pk_context *pk,
898 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 const mbedtls_ecp_curve_info **crv = curves;
901 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100902
903 while( *crv != NULL )
904 {
905 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100906 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100907 crv++;
908 }
909
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100910 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100913
914/*
915 * Try picking a certificate for this ciphersuite,
916 * return 0 on success and -1 on failure.
917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918static int ssl_pick_cert( mbedtls_ssl_context *ssl,
919 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100922 mbedtls_pk_type_t pk_alg =
923 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200924 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100927 if( ssl->handshake->sni_key_cert != NULL )
928 list = ssl->handshake->sni_key_cert;
929 else
930#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200931 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100934 return( 0 );
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000937
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200938 if( list == NULL )
939 {
940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
941 return( -1 );
942 }
943
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944 for( cur = list; cur != NULL; cur = cur->next )
945 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400946 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000948 cur->cert );
949
Gilles Peskinee198df52018-01-05 21:17:45 +0100950 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100953 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000954 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100955
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200956 /*
957 * This avoids sending the client a cert it'll reject based on
958 * keyUsage or other extensions.
959 *
960 * It also allows the user to provision different certificates for
961 * different uses based on keyUsage, eg if they want to avoid signing
962 * and decrypting with the same RSA key.
963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100965 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000968 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200969 continue;
970 }
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_ECDSA_C)
973 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200974 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100977 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000978 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100979#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100980
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100981 /*
982 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
983 * present them a SHA-higher cert rather than failing if it's the only
984 * one we got that satisfies the other conditions.
985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
987 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100988 {
989 if( fallback == NULL )
990 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000993 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100994 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000995 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100996 }
997
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100998 /* If we get there, we got a winner */
999 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001000 }
1001
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001002 if( cur == NULL )
1003 cur = fallback;
1004
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001005 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001006 if( cur != NULL )
1007 {
1008 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001010 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001011 return( 0 );
1012 }
1013
1014 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001017
1018/*
1019 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1020 * Sets ciphersuite_info only if the suite matches.
1021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1023 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001026
Hanno Becker7e5437a2017-04-28 17:15:26 +01001027#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001028 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001029 mbedtls_pk_type_t sig_type;
1030#endif
1031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001033 if( suite_info == NULL )
1034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001037 }
1038
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Hanno Beckerecea07d2018-11-07 16:24:35 +00001040 suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001041
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001042 if( suite_info->min_minor_ver > ssl->minor_ver ||
1043 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001046 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001047 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001050 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001052 return( 0 );
1053#endif
1054
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001055#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001056 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001060 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001061 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001062#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001063
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001064#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1065 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001066 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001067 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1069 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001070 return( 0 );
1071 }
1072#endif
1073
1074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1076 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001077 ( ssl->handshake->curves == NULL ||
1078 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001081 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001082 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001083 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001084#endif
1085
Gilles Peskineeccd8882020-03-10 12:19:08 +01001086#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001087 /* If the ciphersuite requires a pre-shared key and we don't
1088 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001090 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
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
Hanno Becker7e5437a2017-04-28 17:15:26 +01001097#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001098 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001099 /* If the ciphersuite requires signing, check whether
1100 * a suitable hash algorithm is present. */
1101 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1102 {
1103 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1104 if( sig_type != MBEDTLS_PK_NONE &&
1105 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1106 {
1107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1108 "for signature algorithm %d", sig_type ) );
1109 return( 0 );
1110 }
1111 }
1112
1113#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001114 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001117 /*
1118 * Final check: if ciphersuite requires us to have a
1119 * certificate/key of a particular type:
1120 * - select the appropriate certificate if we have one, or
1121 * - try the next ciphersuite if we don't
1122 * This must be done last since we modify the key_cert list.
1123 */
1124 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001127 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001128 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001129 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001130#endif
1131
1132 *ciphersuite_info = suite_info;
1133 return( 0 );
1134}
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1137static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001138{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001139 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001140 unsigned int i, j;
1141 size_t n;
1142 unsigned int ciph_len, sess_len, chal_len;
1143 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001144 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_SSL_RENEGOTIATION)
1150 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001153 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1154 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001158
1159 buf = ssl->in_hdr;
1160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001164 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001166 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001168 buf[3], buf[4] ) );
1169
1170 /*
1171 * SSLv2 Client Hello
1172 *
1173 * Record layer:
1174 * 0 . 1 message length
1175 *
1176 * SSL layer:
1177 * 2 . 2 message type
1178 * 3 . 4 protocol version
1179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1181 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1184 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001185 }
1186
1187 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1188
1189 if( n < 17 || n > 512 )
1190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1192 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001193 }
1194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001196 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1197 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001198
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001199 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001202 " [%d:%d] < [%d:%d]",
1203 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001204 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1207 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1208 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001209 }
1210
Paul Bakker2fbefde2013-06-29 16:01:15 +02001211 ssl->handshake->max_major_ver = buf[3];
1212 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001217 return( ret );
1218 }
1219
1220 ssl->handshake->update_checksum( ssl, buf + 2, n );
1221
1222 buf = ssl->in_msg;
1223 n = ssl->in_left - 5;
1224
1225 /*
1226 * 0 . 1 ciphersuitelist length
1227 * 2 . 3 session id length
1228 * 4 . 5 challenge length
1229 * 6 . .. ciphersuitelist
1230 * .. . .. session id
1231 * .. . .. challenge
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001234
1235 ciph_len = ( buf[0] << 8 ) | buf[1];
1236 sess_len = ( buf[2] << 8 ) | buf[3];
1237 chal_len = ( buf[4] << 8 ) | buf[5];
1238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001240 ciph_len, sess_len, chal_len ) );
1241
1242 /*
1243 * Make sure each parameter length is valid
1244 */
1245 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1248 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001249 }
1250
1251 if( sess_len > 32 )
1252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1254 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001255 }
1256
1257 if( chal_len < 8 || chal_len > 32 )
1258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1260 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001261 }
1262
1263 if( n != 6 + ciph_len + sess_len + chal_len )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001267 }
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001270 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001272 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001274 buf + 6 + ciph_len + sess_len, chal_len );
1275
1276 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001277 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001278 memset( ssl->session_negotiate->id, 0,
1279 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001280 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001281
1282 p += sess_len;
1283 memset( ssl->handshake->randbytes, 0, 64 );
1284 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1285
1286 /*
1287 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1288 */
1289 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1294#if defined(MBEDTLS_SSL_RENEGOTIATION)
1295 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001298 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001299
Gilles Peskinec94f7352017-05-10 16:37:56 +02001300 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1301 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_SSL_RENEGOTIATION */
1305 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001306 break;
1307 }
1308 }
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001311 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1312 {
1313 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1315 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001318
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001319 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1324 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001327 }
1328
1329 break;
1330 }
1331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001333
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001334 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001335 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001336 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001338 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001339 for( i = 0; ciphersuites[i] != 0; i++ )
1340#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001341 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001342 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001343#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001344 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001345 if( p[0] != 0 ||
1346 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1347 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1348 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001349
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001350 got_common_suite = 1;
1351
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001352 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1353 &ciphersuite_info ) ) != 0 )
1354 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001355
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001356 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001357 goto have_ciphersuite_v2;
1358 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001359
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001360 if( got_common_suite )
1361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001363 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001365 }
1366 else
1367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1369 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001370 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001371
1372have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001374
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001375 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001376 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001377
1378 /*
1379 * SSLv2 Client Hello relevant renegotiation security checks
1380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001382 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001385 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1386 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001388 }
1389
1390 ssl->in_left = 0;
1391 ssl->state++;
1392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001394
1395 return( 0 );
1396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001398
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001399/* This function doesn't alert on errors that happen early during
1400 ClientHello parsing because they might indicate that the client is
1401 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001403{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001404 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001405 size_t i, j;
1406 size_t ciph_offset, comp_offset, ext_offset;
1407 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001409 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001410#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001411 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001413 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001414#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001415 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001416 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001418 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Hanno Becker7e5437a2017-04-28 17:15:26 +01001420 /* If there is no signature-algorithm extension present,
1421 * we need to fall back to the default values for allowed
1422 * signature-hash pairs. */
1423#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001424 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001425 int sig_hash_alg_ext_present = 0;
1426#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001427 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001432read_record_header:
1433#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001434 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001436 * otherwise read it ourselves manually in order to support SSLv2
1437 * ClientHello, which doesn't use the same record layer format.
1438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_RENEGOTIATION)
1440 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001444 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001445 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001447 return( ret );
1448 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 }
1450
1451 buf = ssl->in_hdr;
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1454#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001455 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001456#endif
1457 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001458 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001459#endif
1460
Hanno Becker5903de42019-05-03 14:46:38 +01001461 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001462
Paul Bakkerec636f32012-09-09 19:17:02 +00001463 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001464 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001465 *
1466 * Record layer:
1467 * 0 . 0 message type
1468 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001469 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001470 * 3 . 4 message length
1471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001473 buf[0] ) );
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1478 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001479 }
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001482 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001485 buf[1], buf[2] ) );
1486
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001487 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001488
1489 /* According to RFC 5246 Appendix E.1, the version here is typically
1490 * "{03,00}, the lowest version number supported by the client, [or] the
1491 * value of ClientHello.client_version", so the only meaningful check here
1492 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1496 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001497 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001499 /* For DTLS if this is the initial handshake, remember the client sequence
1500 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001502 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_SSL_RENEGOTIATION)
1504 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001505#endif
1506 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001507 {
1508 /* Epoch should be 0 for initial handshakes */
1509 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1512 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001513 }
1514
Hanno Becker19859472018-08-06 09:40:20 +01001515 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1518 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001521 ssl->next_record_offset = 0;
1522 ssl->in_left = 0;
1523 goto read_record_header;
1524 }
1525
1526 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001528#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001531
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001532 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_SSL_RENEGOTIATION)
1535 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001538 msg_len = ssl->in_hslen;
1539 }
1540 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001541#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001542 {
Angus Grattond8213d02016-05-25 20:56:48 +10001543 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1546 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001547 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001548
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001549 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001550 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001553 return( ret );
1554 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001555
1556 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001558 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001559 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001560 else
1561#endif
1562 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001563 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001564
1565 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001568
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001569 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001570
1571 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001572 * Handshake layer:
1573 * 0 . 0 handshake type
1574 * 1 . 3 handshake length
1575 * 4 . 5 DTLS only: message seqence number
1576 * 6 . 8 DTLS only: fragment offset
1577 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1582 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001583 }
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1590 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001591 }
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001594 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1595
1596 /* We don't support fragmentation of ClientHello (yet?) */
1597 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
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#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001605 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001606 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001607 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001608 * Copy the client's handshake message_seq on initial handshakes,
1609 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_SSL_RENEGOTIATION)
1612 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001613 {
1614 /* This couldn't be done in ssl_prepare_handshake_record() */
1615 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1616 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001617
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001618 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001621 "%d (expected %d)", cli_msg_seq,
1622 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001624 }
1625
1626 ssl->handshake->in_msg_seq++;
1627 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001628 else
1629#endif
1630 {
1631 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1632 ssl->in_msg[5];
1633 ssl->handshake->out_msg_seq = cli_msg_seq;
1634 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1635 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001636
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001637 /*
1638 * For now we don't support fragmentation, so make sure
1639 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001640 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001641 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1642 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1645 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001646 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 buf += mbedtls_ssl_hs_hdr_len( ssl );
1651 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001652
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001653 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001654 * ClientHello layer:
1655 * 0 . 1 protocol version
1656 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1657 * 34 . 35 session id length (1 byte)
1658 * 35 . 34+x session id
1659 * 35+x . 35+x DTLS only: cookie length (1 byte)
1660 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001661 * .. . .. ciphersuite list length (2 bytes)
1662 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001663 * .. . .. compression alg. list length (1 byte)
1664 * .. . .. compression alg. list
1665 * .. . .. extensions length (2 bytes, optional)
1666 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001667 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001668
1669 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001670 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001671 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1672 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001673 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001674 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1677 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001678 }
1679
1680 /*
1681 * Check and save the protocol version
1682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001686 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001687
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001688 ssl->handshake->max_major_ver = ssl->major_ver;
1689 ssl->handshake->max_minor_ver = ssl->minor_ver;
1690
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001691 if( ssl->major_ver < ssl->conf->min_major_ver ||
1692 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001695 " [%d:%d] < [%d:%d]",
1696 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001697 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1699 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001701 }
1702
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001703 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001704 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001705 ssl->major_ver = ssl->conf->max_major_ver;
1706 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001707 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001708 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1709 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001710
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001711 /*
1712 * Save client random (inc. Unix time)
1713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001715
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001716 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001717
1718 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001719 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001720 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001721 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001722
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001723 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001724 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001727 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1728 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001730 }
1731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001733
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001734 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001735 memset( ssl->session_negotiate->id, 0,
1736 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001737 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001738 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001739
1740 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001741 * Check the cookie length and content
1742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001744 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001745 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001746 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001747 cookie_len = buf[cookie_offset];
1748
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001749 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1753 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001755 }
1756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001758 buf + cookie_offset + 1, cookie_len );
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001761 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#if defined(MBEDTLS_SSL_RENEGOTIATION)
1763 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001764#endif
1765 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001766 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001767 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001768 buf + cookie_offset + 1, cookie_len,
1769 ssl->cli_id, ssl->cli_id_len ) != 0 )
1770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001772 ssl->handshake->verify_cookie_len = 1;
1773 }
1774 else
1775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001777 ssl->handshake->verify_cookie_len = 0;
1778 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001779 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001780 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001782 {
1783 /* We know we didn't send a cookie, so it should be empty */
1784 if( cookie_len != 0 )
1785 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001786 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1788 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001789 }
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001792 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001793
1794 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001795 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001796 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001797 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001798 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001799 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001801 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001802
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001803 ciph_len = ( buf[ciph_offset + 0] << 8 )
1804 | ( buf[ciph_offset + 1] );
1805
1806 if( ciph_len < 2 ||
1807 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1808 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001811 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1812 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001814 }
1815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001817 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001818
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001819 /*
1820 * Check the compression algorithms length and pick one
1821 */
1822 comp_offset = ciph_offset + 2 + ciph_len;
1823
1824 comp_len = buf[comp_offset];
1825
1826 if( comp_len < 1 ||
1827 comp_len > 16 ||
1828 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1832 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001834 }
1835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001837 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1840#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001841 for( i = 0; i < comp_len; ++i )
1842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001846 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 }
1848 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001849#endif
1850
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001851 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001855#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001856
Janos Follathc6dab2b2016-05-23 14:27:02 +01001857 /* Do not parse the extensions if the protocol is SSLv3 */
1858#if defined(MBEDTLS_SSL_PROTO_SSL3)
1859 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1860 {
1861#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001862 /*
1863 * Check the extension length
1864 */
1865 ext_offset = comp_offset + 1 + comp_len;
1866 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001867 {
Simon Butcher584a5472016-05-23 16:24:52 +01001868 if( msg_len < ext_offset + 2 )
1869 {
1870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001871 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1872 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001873 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1874 }
1875
1876 ext_len = ( buf[ext_offset + 0] << 8 )
1877 | ( buf[ext_offset + 1] );
1878
1879 if( ( ext_len > 0 && ext_len < 4 ) ||
1880 msg_len != ext_offset + 2 + ext_len )
1881 {
1882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001883 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1884 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001885 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1886 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001887 }
Simon Butcher584a5472016-05-23 16:24:52 +01001888 else
1889 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001890
Simon Butcher584a5472016-05-23 16:24:52 +01001891 ext = buf + ext_offset + 2;
1892 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001893
Simon Butcher584a5472016-05-23 16:24:52 +01001894 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001895 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001896 unsigned int ext_id;
1897 unsigned int ext_size;
1898 if ( ext_len < 4 ) {
1899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1900 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1901 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1902 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1903 }
1904 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1905 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001906
Simon Butcher584a5472016-05-23 16:24:52 +01001907 if( ext_size + 4 > ext_len )
1908 {
1909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001910 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1911 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001912 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1913 }
1914 switch( ext_id )
1915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001917 case MBEDTLS_TLS_EXT_SERVERNAME:
1918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1919 if( ssl->conf->f_sni == NULL )
1920 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001921
Simon Butcher584a5472016-05-23 16:24:52 +01001922 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1923 if( ret != 0 )
1924 return( ret );
1925 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001927
Simon Butcher584a5472016-05-23 16:24:52 +01001928 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001931 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001932#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001933
Simon Butcher584a5472016-05-23 16:24:52 +01001934 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1935 if( ret != 0 )
1936 return( ret );
1937 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001940 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001941 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1943
Simon Butcher584a5472016-05-23 16:24:52 +01001944 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1945 if( ret != 0 )
1946 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001947
1948 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001949 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001951 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001952
Robert Cragie136884c2015-10-02 13:34:31 +01001953#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001954 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001955 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1956 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001957
Simon Butcher584a5472016-05-23 16:24:52 +01001958 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1959 if( ret != 0 )
1960 return( ret );
1961 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001962
Simon Butcher584a5472016-05-23 16:24:52 +01001963 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1965 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001966
Simon Butcher584a5472016-05-23 16:24:52 +01001967 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1968 if( ret != 0 )
1969 return( ret );
1970 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001971#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1972 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001973
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001974#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001975 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001977
Simon Butcher584a5472016-05-23 16:24:52 +01001978 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1979 if( ret != 0 )
1980 return( ret );
1981 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001982#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001985 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001987
Simon Butcher584a5472016-05-23 16:24:52 +01001988 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1989 if( ret != 0 )
1990 return( ret );
1991 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001995 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001997
Simon Butcher584a5472016-05-23 16:24:52 +01001998 ret = ssl_parse_truncated_hmac_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_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002003
Hanno Beckera0e20d02019-05-15 14:03:01 +01002004#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01002005 case MBEDTLS_TLS_EXT_CID:
2006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2007
2008 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2009 if( ret != 0 )
2010 return( ret );
2011 break;
2012#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002015 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002017
Simon Butcher584a5472016-05-23 16:24:52 +01002018 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2019 if( ret != 0 )
2020 return( ret );
2021 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01002025 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002027
Simon Butcher584a5472016-05-23 16:24:52 +01002028 ret = ssl_parse_extended_ms_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_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01002035 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002037
Simon Butcher584a5472016-05-23 16:24:52 +01002038 ret = ssl_parse_session_ticket_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_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002045 case MBEDTLS_TLS_EXT_ALPN:
2046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002047
Simon Butcher584a5472016-05-23 16:24:52 +01002048 ret = ssl_parse_alpn_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é-Gonnard89e35792014-04-07 12:10:30 +02002053
Johan Pascalb62bb512015-12-03 21:56:45 +01002054#if defined(MBEDTLS_SSL_DTLS_SRTP)
2055 case MBEDTLS_TLS_EXT_USE_SRTP:
2056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2057 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2058 if ( ret != 0 )
2059 return( ret );
2060 break;
2061#endif /* MBEDTLS_SSL_DTLS_SRTP */
2062
Simon Butcher584a5472016-05-23 16:24:52 +01002063 default:
2064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
2065 ext_id ) );
2066 }
2067
2068 ext_len -= 4 + ext_size;
2069 ext += 4 + ext_size;
2070
2071 if( ext_len > 0 && ext_len < 4 )
2072 {
2073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2075 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01002076 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2077 }
Paul Bakker48916f92012-09-16 19:57:18 +00002078 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002079#if defined(MBEDTLS_SSL_PROTO_SSL3)
2080 }
2081#endif
2082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002084 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
2087 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002088 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002090
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002091 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002092 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2096 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002099 }
2100
2101 break;
2102 }
2103 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002105
Hanno Becker7e5437a2017-04-28 17:15:26 +01002106#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002107 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002108
2109 /*
2110 * Try to fall back to default hash SHA1 if the client
2111 * hasn't provided any preferred signature-hash combinations.
2112 */
2113 if( sig_hash_alg_ext_present == 0 )
2114 {
2115 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2116
2117 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2118 md_default = MBEDTLS_MD_NONE;
2119
2120 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2121 }
2122
2123#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002124 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002125
Paul Bakker48916f92012-09-16 19:57:18 +00002126 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002127 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2128 */
2129 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2134#if defined(MBEDTLS_SSL_RENEGOTIATION)
2135 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002136 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2138 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002139 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2140 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002142 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002145 break;
2146 }
2147 }
2148
2149 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002150 * Renegotiation security checks
2151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002153 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002156 handshake_failure = 1;
2157 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158#if defined(MBEDTLS_SSL_RENEGOTIATION)
2159 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2160 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002161 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002164 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002165 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2167 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002168 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002171 handshake_failure = 1;
2172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2174 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002175 renegotiation_info_seen == 1 )
2176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002178 handshake_failure = 1;
2179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002181
2182 if( handshake_failure == 1 )
2183 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002184 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2185 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002187 }
Paul Bakker380da532012-04-18 16:10:25 +00002188
Paul Bakker41c83d32013-03-20 14:39:14 +01002189 /*
2190 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002191 * (At the end because we need information from the EC-based extensions
2192 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002193 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002194 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002195 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002196 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002198 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002199 for( i = 0; ciphersuites[i] != 0; i++ )
2200#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002201 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002202 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002203#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002204 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002205 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2206 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2207 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002208
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002209 got_common_suite = 1;
2210
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002211 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2212 &ciphersuite_info ) ) != 0 )
2213 return( ret );
2214
2215 if( ciphersuite_info != NULL )
2216 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002217 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002218
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002219 if( got_common_suite )
2220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002222 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002223 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2224 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002226 }
2227 else
2228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002230 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2231 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002233 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002234
2235have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002237
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002238 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002239 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002240
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 ssl->state++;
2242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002246#endif
2247
Hanno Becker7e5437a2017-04-28 17:15:26 +01002248 /* Debugging-only output for testsuite */
2249#if defined(MBEDTLS_DEBUG_C) && \
2250 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002251 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002252 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2253 {
2254 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2255 if( sig_alg != MBEDTLS_PK_NONE )
2256 {
2257 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2258 sig_alg );
2259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2260 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2261 }
2262 else
2263 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2265 "%d - should not happen", sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002266 }
2267 }
2268#endif
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
2272 return( 0 );
2273}
2274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2276static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002277 unsigned char *buf,
2278 size_t *olen )
2279{
2280 unsigned char *p = buf;
2281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002283 {
2284 *olen = 0;
2285 return;
2286 }
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2291 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002292
2293 *p++ = 0x00;
2294 *p++ = 0x00;
2295
2296 *olen = 4;
2297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002299
Hanno Beckera0e20d02019-05-15 14:03:01 +01002300#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002301static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2302 unsigned char *buf,
2303 size_t *olen )
2304{
2305 unsigned char *p = buf;
2306 size_t ext_len;
2307 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2308
2309 *olen = 0;
2310
2311 /* Skip writing the extension if we don't want to use it or if
2312 * the client hasn't offered it. */
2313 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2314 return;
2315
2316 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2317 * which is at most 255, so the increment cannot overflow. */
2318 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2319 {
2320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2321 return;
2322 }
2323
2324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2325
2326 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002327 * Quoting draft-ietf-tls-dtls-connection-id-05
2328 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002329 *
2330 * struct {
2331 * opaque cid<0..2^8-1>;
2332 * } ConnectionId;
2333 */
2334
2335 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2336 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2337 ext_len = (size_t) ssl->own_cid_len + 1;
2338 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2339 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2340
2341 *p++ = (uint8_t) ssl->own_cid_len;
2342 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2343
2344 *olen = ssl->own_cid_len + 5;
2345}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002346#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2349static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002350 unsigned char *buf,
2351 size_t *olen )
2352{
2353 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2355 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002356
Hanno Becker27b34d52017-10-20 14:24:51 +01002357 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002359 {
2360 *olen = 0;
2361 return;
2362 }
2363
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002364 /*
2365 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2366 * from a client and then selects a stream or Authenticated Encryption
2367 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2368 * encrypt-then-MAC response extension back to the client."
2369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002371 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2373 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002374 {
2375 *olen = 0;
2376 return;
2377 }
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2382 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002383
2384 *p++ = 0x00;
2385 *p++ = 0x00;
2386
2387 *olen = 4;
2388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2392static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002393 unsigned char *buf,
2394 size_t *olen )
2395{
2396 unsigned char *p = buf;
2397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2399 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002400 {
2401 *olen = 0;
2402 return;
2403 }
2404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002406 "extension" ) );
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2409 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002410
2411 *p++ = 0x00;
2412 *p++ = 0x00;
2413
2414 *olen = 4;
2415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2419static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002420 unsigned char *buf,
2421 size_t *olen )
2422{
2423 unsigned char *p = buf;
2424
2425 if( ssl->handshake->new_session_ticket == 0 )
2426 {
2427 *olen = 0;
2428 return;
2429 }
2430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2434 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002435
2436 *p++ = 0x00;
2437 *p++ = 0x00;
2438
2439 *olen = 4;
2440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002444 unsigned char *buf,
2445 size_t *olen )
2446{
2447 unsigned char *p = buf;
2448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002450 {
2451 *olen = 0;
2452 return;
2453 }
2454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2458 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_SSL_RENEGOTIATION)
2461 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002462 {
2463 *p++ = 0x00;
2464 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2465 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002466
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002467 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2468 p += ssl->verify_data_len;
2469 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2470 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002471 }
2472 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002474 {
2475 *p++ = 0x00;
2476 *p++ = 0x01;
2477 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002478 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002479
2480 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002481}
2482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2484static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002485 unsigned char *buf,
2486 size_t *olen )
2487{
2488 unsigned char *p = buf;
2489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002491 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002492 *olen = 0;
2493 return;
2494 }
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2499 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002500
2501 *p++ = 0x00;
2502 *p++ = 1;
2503
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002504 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002505
2506 *olen = 5;
2507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002509
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002510#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002511 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002513 unsigned char *buf,
2514 size_t *olen )
2515{
2516 unsigned char *p = buf;
2517 ((void) ssl);
2518
Paul Bakker677377f2013-10-28 12:54:26 +01002519 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002521 {
2522 *olen = 0;
2523 return;
2524 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2529 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002530
2531 *p++ = 0x00;
2532 *p++ = 2;
2533
2534 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002536
2537 *olen = 6;
2538}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002539#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002540
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002541#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2542static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2543 unsigned char *buf,
2544 size_t *olen )
2545{
Janos Follath865b3eb2019-12-16 11:46:15 +00002546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002547 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002548 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002549 size_t kkpp_len;
2550
2551 *olen = 0;
2552
2553 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002554 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002555 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2556 return;
2557
2558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2559
2560 if( end - p < 4 )
2561 {
2562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2563 return;
2564 }
2565
2566 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2567 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2568
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002569 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2570 p + 2, end - p - 2, &kkpp_len,
2571 ssl->conf->f_rng, ssl->conf->p_rng );
2572 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002573 {
2574 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2575 return;
2576 }
2577
2578 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2579 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2580
2581 *olen = kkpp_len + 4;
2582}
2583#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#if defined(MBEDTLS_SSL_ALPN )
2586static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002587 unsigned char *buf, size_t *olen )
2588{
2589 if( ssl->alpn_chosen == NULL )
2590 {
2591 *olen = 0;
2592 return;
2593 }
2594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002596
2597 /*
2598 * 0 . 1 ext identifier
2599 * 2 . 3 ext length
2600 * 4 . 5 protocol list length
2601 * 6 . 6 protocol name length
2602 * 7 . 7+n protocol name
2603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2605 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002606
2607 *olen = 7 + strlen( ssl->alpn_chosen );
2608
2609 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2610 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2611
2612 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2613 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2614
2615 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2616
2617 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002620
Ron Eldor3adb9922017-12-21 10:15:08 +02002621#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002622static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002623 unsigned char *buf,
2624 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002625{
Ron Eldor591f1622018-01-22 12:30:04 +02002626 size_t mki_len = 0, ext_len = 0, i;
2627
Ron Eldor3adb9922017-12-21 10:15:08 +02002628 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
Johan Pascalb62bb512015-12-03 21:56:45 +01002629 {
2630 *olen = 0;
2631 return;
2632 }
2633
2634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2635
Ron Eldor591f1622018-01-22 12:30:04 +02002636 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
Ron Eldoref72faf2018-07-12 11:54:20 +03002637 ssl->dtls_srtp_info.mki_len != 0 )
Ron Eldor591f1622018-01-22 12:30:04 +02002638 {
2639 mki_len = ssl->dtls_srtp_info.mki_len;
2640 }
2641
Johan Pascalb62bb512015-12-03 21:56:45 +01002642 /* extension */
2643 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2644 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002645 /*
2646 * total length 5 and mki value: only one profile(2 bytes)
2647 * and length(2 bytes) and srtp_mki )
2648 */
Ron Eldor591f1622018-01-22 12:30:04 +02002649 ext_len = 5 + mki_len;
2650 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2651 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002652
2653 /* protection profile length: 2 */
2654 buf[4] = 0x00;
2655 buf[5] = 0x02;
Ron Eldor3adb9922017-12-21 10:15:08 +02002656 switch (ssl->dtls_srtp_info.chosen_dtls_srtp_profile) {
Johan Pascalb62bb512015-12-03 21:56:45 +01002657 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
Ron Eldoref72faf2018-07-12 11:54:20 +03002658 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE >> 8 )
2659 & 0xFF );
2660 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE )
2661 & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002662 break;
2663 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
Ron Eldoref72faf2018-07-12 11:54:20 +03002664 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE >> 8 )
2665 & 0xFF );
2666 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE )
2667 & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002668 break;
2669 case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
Ron Eldoref72faf2018-07-12 11:54:20 +03002670 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE >> 8 )
2671 & 0xFF );
2672 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE )
2673 & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002674 break;
2675 case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
Ron Eldoref72faf2018-07-12 11:54:20 +03002676 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE >> 8 )
2677 & 0xFF );
2678 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE )
2679 & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002680 break;
2681 default:
2682 *olen = 0;
2683 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002684 }
2685
Ron Eldor591f1622018-01-22 12:30:04 +02002686 buf[8] = mki_len & 0xFF;
2687 for( i=0; i < mki_len; i++ )
2688 {
Ron Eldoref72faf2018-07-12 11:54:20 +03002689 buf[9 + i] = ssl->dtls_srtp_info.mki_value[i];
Ron Eldor591f1622018-01-22 12:30:04 +02002690 }
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002771 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002772#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002774 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 unsigned char *buf, *p;
2776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002781 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002785
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002786 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002789
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002790 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2793 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002794 }
2795
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 /*
2797 * 0 . 0 handshake type
2798 * 1 . 3 handshake length
2799 * 4 . 5 protocol version
2800 * 6 . 9 UNIX time()
2801 * 10 . 37 random bytes
2802 */
2803 buf = ssl->out_msg;
2804 p = buf + 4;
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002807 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002808 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002811 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002814 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 *p++ = (unsigned char)( t >> 24 );
2816 *p++ = (unsigned char)( t >> 16 );
2817 *p++ = (unsigned char)( t >> 8 );
2818 *p++ = (unsigned char)( t );
2819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002821#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002822 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002823 return( ret );
2824
2825 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002828 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002829 return( ret );
2830
2831 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Paul Bakker48916f92012-09-16 19:57:18 +00002833 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
2837 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002838 * Resume is 0 by default, see ssl_handshake_init().
2839 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2840 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002842 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SSL_RENEGOTIATION)
2844 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002845#endif
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002846 ssl->session_negotiate->id_len != 0 &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002847 ssl->conf->f_get_cache != NULL &&
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002848 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002851 ssl->handshake->resume = 1;
2852 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002854 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 {
2856 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002857 * New session, create a new session id,
2858 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 ssl->state++;
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002863 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002864#endif
2865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002867 if( ssl->handshake->new_session_ticket != 0 )
2868 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002869 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002870 memset( ssl->session_negotiate->id, 0, 32 );
2871 }
2872 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002874 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002875 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002876 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002877 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002878 return( ret );
2879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 }
2881 else
2882 {
2883 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002884 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002886 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002892 return( ret );
2893 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 }
2895
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002896 /*
2897 * 38 . 38 session id length
2898 * 39 . 38+n session id
2899 * 39+n . 40+n chosen ciphersuite
2900 * 41+n . 41+n chosen compression alg.
2901 * 42+n . 43+n extensions length
2902 * 44+n . 43+n+m extensions
2903 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002904 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2905 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2906 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2909 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002911 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakker48916f92012-09-16 19:57:18 +00002913 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2914 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2915 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2918 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002920 ssl->session_negotiate->compression ) );
2921
Janos Follathc6dab2b2016-05-23 14:27:02 +01002922 /* Do not write the extensions if the protocol is SSLv3 */
2923#if defined(MBEDTLS_SSL_PROTO_SSL3)
2924 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2925 {
2926#endif
2927
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002928 /*
2929 * First write extensions, then the total length
2930 */
2931 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2932 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002935 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2936 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002937#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002940 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2941 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002942#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002943
Hanno Beckera0e20d02019-05-15 14:03:01 +01002944#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002945 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2946 ext_len += olen;
2947#endif
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002950 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2951 ext_len += olen;
2952#endif
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002955 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2956 ext_len += olen;
2957#endif
2958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002960 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2961 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002962#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002963
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002964#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002965 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002966 if ( mbedtls_ssl_ciphersuite_uses_ec(
2967 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2968 {
2969 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2970 ext_len += olen;
2971 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002972#endif
2973
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002974#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2975 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2976 ext_len += olen;
2977#endif
2978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002980 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2981 ext_len += olen;
2982#endif
2983
Johan Pascalb62bb512015-12-03 21:56:45 +01002984#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002985 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
Johan Pascalb62bb512015-12-03 21:56:45 +01002986 ext_len += olen;
2987#endif
2988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002990
Paul Bakkera7036632014-04-30 10:15:38 +02002991 if( ext_len > 0 )
2992 {
2993 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2994 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2995 p += ext_len;
2996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Janos Follathc6dab2b2016-05-23 14:27:02 +01002998#if defined(MBEDTLS_SSL_PROTO_SSL3)
2999 }
3000#endif
3001
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3004 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003006 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
3010 return( ret );
3011}
3012
Gilles Peskineeccd8882020-03-10 12:19:08 +01003013#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003015{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003016 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003017 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003020
Hanno Becker77adddc2019-02-07 12:32:43 +00003021 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003024 ssl->state++;
3025 return( 0 );
3026 }
3027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003030}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003031#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003035 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003036 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03003037 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003038 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10003040 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003042 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
3046 ssl->state++;
3047
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3049 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3050 authmode = ssl->handshake->sni_authmode;
3051 else
3052#endif
Ron Eldor57cc70e2018-04-02 18:25:16 +03003053#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03003054 /*
3055 * check if we have a chosen srtp protection profile,
3056 * force verify mode to be at least OPTIONAL
3057 */
3058 if ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE &&
3059 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_NONE ) {
Ron Eldor1c399bd2018-07-04 18:45:27 +03003060 authmode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Ron Eldor57cc70e2018-04-02 18:25:16 +03003061 }
Ron Eldoref72faf2018-07-12 11:54:20 +03003062 else
Ron Eldor57cc70e2018-04-02 18:25:16 +03003063#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003064 authmode = ssl->conf->authmode;
3065
Hanno Becker77adddc2019-02-07 12:32:43 +00003066 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003067 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 {
Ron Eldor57cc70e2018-04-02 18:25:16 +03003069#if defined(MBEDTLS_SSL_DTLS_SRTP)
3070 /* check if we have a chosen srtp protection profile */
3071 if ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE ) {
3072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "should not happen" ) );
Ron Eldoref72faf2018-07-12 11:54:20 +03003073 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Ron Eldor57cc70e2018-04-02 18:25:16 +03003074 }
3075 else
3076 {
3077#endif
3078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3079 return( 0 );
3080#if defined(MBEDTLS_SSL_DTLS_SRTP)
3081 }
3082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 }
3084
3085 /*
3086 * 0 . 0 handshake type
3087 * 1 . 3 handshake length
3088 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003089 * 5 .. m-1 cert types
3090 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003091 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 * n .. n+1 length of all DNs
3093 * n+2 .. n+3 length of DN 1
3094 * n+4 .. ... Distinguished Name #1
3095 * ... .. ... length of DN 2, etc.
3096 */
3097 buf = ssl->out_msg;
3098 p = buf + 4;
3099
3100 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003101 * Supported certificate types
3102 *
3103 * ClientCertificateType certificate_types<1..2^8-1>;
3104 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003106 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108#if defined(MBEDTLS_RSA_C)
3109 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111#if defined(MBEDTLS_ECDSA_C)
3112 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003113#endif
3114
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003115 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003116 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003117
Paul Bakker577e0062013-08-28 11:57:20 +02003118 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003120 /*
3121 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003122 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003123 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3124 *
3125 * struct {
3126 * HashAlgorithm hash;
3127 * SignatureAlgorithm signature;
3128 * } SignatureAndHashAlgorithm;
3129 *
3130 * enum { (255) } HashAlgorithm;
3131 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003134 {
Simon Butcher99000142016-10-13 17:21:01 +01003135 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003136
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003137 /*
3138 * Supported signature algorithms
3139 */
Simon Butcher99000142016-10-13 17:21:01 +01003140 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3141 {
3142 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3143
3144 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3145 continue;
3146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003148 p[2 + sa_len++] = hash;
3149 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003152 p[2 + sa_len++] = hash;
3153 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003154#endif
Simon Butcher99000142016-10-13 17:21:01 +01003155 }
Paul Bakker926af752012-11-23 13:38:07 +01003156
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003157 p[0] = (unsigned char)( sa_len >> 8 );
3158 p[1] = (unsigned char)( sa_len );
3159 sa_len += 2;
3160 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003161 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003164 /*
3165 * DistinguishedName certificate_authorities<0..2^16-1>;
3166 * opaque DistinguishedName<1..2^16-1>;
3167 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003168 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003170 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003171
3172 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003174 /* NOTE: If trusted certificates are provisioned
3175 * via a CA callback (configured through
3176 * `mbedtls_ssl_conf_ca_cb()`, then the
3177 * CertificateRequest is currently left empty. */
3178
Janos Follath088ce432017-04-10 12:42:31 +01003179#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3180 if( ssl->handshake->sni_ca_chain != NULL )
3181 crt = ssl->handshake->sni_ca_chain;
3182 else
3183#endif
3184 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003185
Janos Follath088ce432017-04-10 12:42:31 +01003186 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003187 {
irwirc9bc3002020-04-01 13:46:36 +03003188 /* It follows from RFC 5280 A.1 that this length
3189 * can be represented in at most 11 bits. */
3190 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003191
irwirc9bc3002020-04-01 13:46:36 +03003192 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003193 {
3194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3195 break;
3196 }
3197
3198 *p++ = (unsigned char)( dn_size >> 8 );
3199 *p++ = (unsigned char)( dn_size );
3200 memcpy( p, crt->subject_raw.p, dn_size );
3201 p += dn_size;
3202
3203 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3204
3205 total_dn_size += 2 + dn_size;
3206 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 }
3209
Paul Bakker926af752012-11-23 13:38:07 +01003210 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3212 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003213 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3214 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003216 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
3220 return( ret );
3221}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003222#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3225 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3226static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003227{
Janos Follath865b3eb2019-12-16 11:46:15 +00003228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3233 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003234 }
3235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3237 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3238 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003241 return( ret );
3242 }
3243
3244 return( 0 );
3245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3247 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003248
Gilles Peskineeccd8882020-03-10 12:19:08 +01003249#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003250 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003251static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003252 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003253{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003254 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3255 * signature length which will be added in ssl_write_server_key_exchange
3256 * after the call to ssl_prepare_server_key_exchange.
3257 * ssl_write_server_key_exchange also takes care of incrementing
3258 * ssl->out_msglen. */
3259 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003260 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003261 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003262 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003263 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003264 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3265 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003266 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003267 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003268 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003269 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003270 return( ret );
3271}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003272#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003273 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003274
Gilles Peskined3eb0612018-01-08 17:07:44 +01003275/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003276 * calculating the signature if any, but excluding formatting the
3277 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003278static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3279 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003282 ssl->handshake->ciphersuite_info;
3283
Gilles Peskineeccd8882020-03-10 12:19:08 +01003284#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3285#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003286 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003287#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3288#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003289
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003290 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003291#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003292 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003293#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003294
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003295 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003297 /*
3298 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003299 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003300 *
3301 */
3302
3303 /*
3304 * - ECJPAKE key exchanges
3305 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003306#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3307 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3308 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003310 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003311
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003312 ret = mbedtls_ecjpake_write_round_two(
3313 &ssl->handshake->ecjpake_ctx,
3314 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003315 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003316 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003317 if( ret != 0 )
3318 {
3319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3320 return( ret );
3321 }
3322
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003323 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003324 }
3325#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3326
Hanno Becker1aa267c2017-04-28 17:08:27 +01003327 /*
3328 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3329 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3330 * we use empty support identity hints here.
3331 **/
3332#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3334 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3335 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003336 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003337 ssl->out_msg[ssl->out_msglen++] = 0x00;
3338 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3341 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003342
Hanno Becker7e5437a2017-04-28 17:15:26 +01003343 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003344 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003345 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003346#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003347 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003348 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003350 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003351
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003352 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3353 {
3354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3356 }
3357
Paul Bakker41c83d32013-03-20 14:39:14 +01003358 /*
3359 * Ephemeral DH parameters:
3360 *
3361 * struct {
3362 * opaque dh_p<1..2^16-1>;
3363 * opaque dh_g<1..2^16-1>;
3364 * opaque dh_Ys<1..2^16-1>;
3365 * } ServerDHParams;
3366 */
Hanno Beckerab740562017-10-04 13:15:37 +01003367 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3368 &ssl->conf->dhm_P,
3369 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003370 {
Hanno Beckerab740562017-10-04 13:15:37 +01003371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003372 return( ret );
3373 }
Paul Bakker48916f92012-09-16 19:57:18 +00003374
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003375 if( ( ret = mbedtls_dhm_make_params(
3376 &ssl->handshake->dhm_ctx,
3377 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3378 ssl->out_msg + ssl->out_msglen, &len,
3379 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003382 return( ret );
3383 }
3384
Gilles Peskineeccd8882020-03-10 12:19:08 +01003385#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003386 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003387#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003388
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003389 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3392 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3393 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3394 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003395 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003396#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003397
Hanno Becker1aa267c2017-04-28 17:08:27 +01003398 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003399 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003400 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003401#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003402 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003404 /*
3405 * Ephemeral ECDH parameters:
3406 *
3407 * struct {
3408 * ECParameters curve_params;
3409 * ECPoint public;
3410 * } ServerECDHParams;
3411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003415 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003416
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003417 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003418 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003419 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3420 if( (*curve)->grp_id == *gid )
3421 goto curve_matching_done;
3422
3423curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003424 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3427 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003428 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003431
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003432 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3433 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003434 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003436 return( ret );
3437 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003438
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003439 if( ( ret = mbedtls_ecdh_make_params(
3440 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003441 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003442 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003443 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003446 return( ret );
3447 }
3448
Gilles Peskineeccd8882020-03-10 12:19:08 +01003449#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003450 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003451#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003452
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003453 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003454
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003455 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3456 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003457 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003458#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003459
Hanno Becker1aa267c2017-04-28 17:08:27 +01003460 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003461 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003462 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003463 * exchange parameters, compute and add the signature here.
3464 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003465 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003466#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003467 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003468 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003469 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003470 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003471 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003473
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003474 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003475 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003476 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003477 * to choose appropriate hash.
3478 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3479 * (RFC 4492, Sec. 5.4)
3480 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003481 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003482
3483 mbedtls_md_type_t md_alg;
3484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003486 mbedtls_pk_type_t sig_alg =
3487 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003489 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003490 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3491 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003492 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003493 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3494 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003497 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003498 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003500 }
3501 }
Paul Bakker577e0062013-08-28 11:57:20 +02003502 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3504#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3505 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003506 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003507 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003508 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003510 }
3511 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003512#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3513 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003514 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003515 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003517 }
3518
Hanno Becker7e5437a2017-04-28 17:15:26 +01003519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3520
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003521 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003522 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3525 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3526 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003527 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003528 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003529 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3530 dig_signed,
3531 dig_signed_len );
3532 if( ret != 0 )
3533 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003534 }
3535 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3537 MBEDTLS_SSL_PROTO_TLS1_1 */
3538#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3539 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3540 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003541 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003542 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003543 dig_signed,
3544 dig_signed_len,
3545 md_alg );
3546 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003547 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003548 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003549 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3551 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003555 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003556
Gilles Peskineebd652f2018-01-05 21:18:59 +01003557 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003558
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003559 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003560 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3563 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003564 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003565 /*
3566 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003567 * explicitly through a prefix to the signature.
3568 *
3569 * struct {
3570 * HashAlgorithm hash;
3571 * SignatureAlgorithm signature;
3572 * } SignatureAndHashAlgorithm;
3573 *
3574 * struct {
3575 * SignatureAndHashAlgorithm algorithm;
3576 * opaque signature<0..2^16-1>;
3577 * } DigitallySigned;
3578 *
3579 */
3580
Gilles Peskine1004c192018-01-08 16:59:14 +01003581 ssl->out_msg[ssl->out_msglen++] =
3582 mbedtls_ssl_hash_from_md_alg( md_alg );
3583 ssl->out_msg[ssl->out_msglen++] =
3584 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003585 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003587
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003588#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003589 if( ssl->conf->f_async_sign_start != NULL )
3590 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003591 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003592 mbedtls_ssl_own_cert( ssl ),
3593 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003594 switch( ret )
3595 {
3596 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3597 /* act as if f_async_sign was null */
3598 break;
3599 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003600 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003601 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003602 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003603 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003604 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3605 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003606 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003607 return( ret );
3608 }
3609 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003610#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003611
3612 if( mbedtls_ssl_own_key( ssl ) == NULL )
3613 {
3614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3615 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3616 }
3617
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003618 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3619 * signature length which will be added in ssl_write_server_key_exchange
3620 * after the call to ssl_prepare_server_key_exchange.
3621 * ssl_write_server_key_exchange also takes care of incrementing
3622 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003623 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3624 md_alg, hash, hashlen,
3625 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003626 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003627 ssl->conf->f_rng,
3628 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003631 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003632 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003633 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003634#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003635
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003636 return( 0 );
3637}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003638
Gilles Peskined3eb0612018-01-08 17:07:44 +01003639/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003640 * that do not include a ServerKeyExchange message, do nothing. Either
3641 * way, if successful, move on to the next step in the SSL state
3642 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003643static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3644{
Janos Follath865b3eb2019-12-16 11:46:15 +00003645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003646 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003647#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003648 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003649 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003650#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003651
Gilles Peskined3eb0612018-01-08 17:07:44 +01003652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3653
Gilles Peskineeccd8882020-03-10 12:19:08 +01003654#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003655 /* Extract static ECDH parameters and abort if ServerKeyExchange
3656 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003657 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3658 {
3659 /* For suites involving ECDH, extract DH parameters
3660 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003661#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003662 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3663 {
3664 ssl_get_ecdh_params_from_cert( ssl );
3665 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003666#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003667
3668 /* Key exchanges not involving ephemeral keys don't use
3669 * ServerKeyExchange, so end here. */
3670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3671 ssl->state++;
3672 return( 0 );
3673 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003674#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003675
Gilles Peskineeccd8882020-03-10 12:19:08 +01003676#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003677 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003678 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003679 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003680 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003681 {
3682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3683 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003684 }
3685 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003686#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003687 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003688 {
3689 /* ServerKeyExchange is needed. Prepare the message. */
3690 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003691 }
3692
3693 if( ret != 0 )
3694 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003695 /* If we're starting to write a new message, set ssl->out_msglen
3696 * to 0. But if we're resuming after an asynchronous message,
3697 * out_msglen is the amount of data written so far and mst be
3698 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003699 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3701 else
3702 ssl->out_msglen = 0;
3703 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003704 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003705
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003706 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003707 * ssl_prepare_server_key_exchange already wrote the signature
3708 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003709#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003710 if( signature_len != 0 )
3711 {
3712 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3713 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3714
3715 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3716 ssl->out_msg + ssl->out_msglen,
3717 signature_len );
3718
3719 /* Skip over the already-written signature */
3720 ssl->out_msglen += signature_len;
3721 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003722#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003723
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003724 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3726 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
3728 ssl->state++;
3729
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003730 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 return( ret );
3734 }
3735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738}
3739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741{
Janos Follath865b3eb2019-12-16 11:46:15 +00003742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003745
3746 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3748 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003749
3750 ssl->state++;
3751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003753 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003755#endif
3756
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003757 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003760 return( ret );
3761 }
3762
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003763#if defined(MBEDTLS_SSL_PROTO_DTLS)
3764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3765 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3766 {
3767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3768 return( ret );
3769 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003770#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
3774 return( 0 );
3775}
3776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3778 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3779static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003780 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003783 size_t n;
3784
3785 /*
3786 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3787 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003788 if( *p + 2 > end )
3789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3791 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003792 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003793
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003794 n = ( (*p)[0] << 8 ) | (*p)[1];
3795 *p += 2;
3796
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003797 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3800 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003801 }
3802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3806 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003807 }
3808
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003809 *p += n;
3810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003812
Paul Bakker70df2fb2013-04-17 17:19:09 +02003813 return( ret );
3814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3816 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3819 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003820
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003821#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003822static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3823 unsigned char *peer_pms,
3824 size_t *peer_pmslen,
3825 size_t peer_pmssize )
3826{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003827 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003828 peer_pms, peer_pmslen, peer_pmssize );
3829 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3830 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003831 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003832 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003833 }
3834 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3835 return( ret );
3836}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003837#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003838
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003839static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3840 const unsigned char *p,
3841 const unsigned char *end,
3842 unsigned char *peer_pms,
3843 size_t *peer_pmslen,
3844 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003845{
Janos Follath865b3eb2019-12-16 11:46:15 +00003846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003847 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3848 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3849 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003850
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003851#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003852 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003853 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003854 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003855 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3857 return( ssl_resume_decrypt_pms( ssl,
3858 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003859 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003860#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003861
3862 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003863 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3866 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3867 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003868 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003869 if ( p + 2 > end ) {
3870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3871 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3872 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003873 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3874 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3877 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003878 }
3879 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003880#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003881
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003882 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3885 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003886 }
3887
Gilles Peskine422ccab2018-01-11 18:29:01 +01003888 /*
3889 * Decrypt the premaster secret
3890 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003891#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003892 if( ssl->conf->f_async_decrypt_start != NULL )
3893 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003894 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003895 mbedtls_ssl_own_cert( ssl ),
3896 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003897 switch( ret )
3898 {
3899 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3900 /* act as if f_async_decrypt_start was null */
3901 break;
3902 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003903 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003904 return( ssl_resume_decrypt_pms( ssl,
3905 peer_pms,
3906 peer_pmslen,
3907 peer_pmssize ) );
3908 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003909 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003910 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3911 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003912 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003913 return( ret );
3914 }
3915 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003916#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003917
Gilles Peskine422ccab2018-01-11 18:29:01 +01003918 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3919 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3921 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3922 }
3923
3924 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003925 peer_pms, peer_pmslen, peer_pmssize,
3926 ssl->conf->f_rng, ssl->conf->p_rng );
3927 return( ret );
3928}
3929
3930static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3931 const unsigned char *p,
3932 const unsigned char *end,
3933 size_t pms_offset )
3934{
Janos Follath865b3eb2019-12-16 11:46:15 +00003935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003936 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3937 unsigned char ver[2];
3938 unsigned char fake_pms[48], peer_pms[48];
3939 unsigned char mask;
3940 size_t i, peer_pmslen;
3941 unsigned int diff;
3942
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003943 /* In case of a failure in decryption, the decryption may write less than
3944 * 2 bytes of output, but we always read the first two bytes. It doesn't
3945 * matter in the end because diff will be nonzero in that case due to
3946 * peer_pmslen being less than 48, and we only care whether diff is 0.
3947 * But do initialize peer_pms for robustness anyway. This also makes
3948 * memory analyzers happy (don't access uninitialized memory, even
3949 * if it's an unsigned char). */
3950 peer_pms[0] = peer_pms[1] = ~0;
3951
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003952 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3953 peer_pms,
3954 &peer_pmslen,
3955 sizeof( peer_pms ) );
3956
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003957#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003958 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3959 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003960#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003963 ssl->handshake->max_minor_ver,
3964 ssl->conf->transport, ver );
3965
3966 /* Avoid data-dependent branches while checking for invalid
3967 * padding, to protect against timing-based Bleichenbacher-type
3968 * attacks. */
3969 diff = (unsigned int) ret;
3970 diff |= peer_pmslen ^ 48;
3971 diff |= peer_pms[0] ^ ver[0];
3972 diff |= peer_pms[1] ^ ver[1];
3973
3974 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3975 /* MSVC has a warning about unary minus on unsigned, but this is
3976 * well-defined and precisely what we want to do here */
3977#if defined(_MSC_VER)
3978#pragma warning( push )
3979#pragma warning( disable : 4146 )
3980#endif
3981 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3982#if defined(_MSC_VER)
3983#pragma warning( pop )
3984#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003985
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003986 /*
3987 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3988 * must not cause the connection to end immediately; instead, send a
3989 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003990 * To protect against timing-based variants of the attack, we must
3991 * not have any branch that depends on whether the decryption was
3992 * successful. In particular, always generate the fake premaster secret,
3993 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003994 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003995 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003996 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003997 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003998 /* It's ok to abort on an RNG failure, since this does not reveal
3999 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004000 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01004001 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004002
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01004003#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02004004 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004006#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02004007
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004008 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
4009 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
4010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004013 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004014 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004015
Gilles Peskine422ccab2018-01-11 18:29:01 +01004016 /* Set pms to either the true or the fake PMS, without
4017 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004018 for( i = 0; i < ssl->handshake->pmslen; i++ )
4019 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
4020
4021 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4024 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02004025
Gilles Peskineeccd8882020-03-10 12:19:08 +01004026#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004028 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004029{
Paul Bakker6db455e2013-09-18 17:29:31 +02004030 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03004031 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004032
Hanno Becker845b9462018-10-26 12:07:29 +01004033 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4036 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004037 }
4038
4039 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004040 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02004041 */
Hanno Becker83c9f492017-06-26 13:52:14 +01004042 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4045 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004046 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004047
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004048 n = ( (*p)[0] << 8 ) | (*p)[1];
4049 *p += 2;
4050
irwir6527bd62019-09-21 18:51:25 +03004051 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4054 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004055 }
4056
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004057 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004058 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004059 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004061 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004062 else
Paul Bakker6db455e2013-09-18 17:29:31 +02004063 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01004064 /* Identity is not a big secret since clients send it in the clear,
4065 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004066 if( n != ssl->conf->psk_identity_len ||
4067 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02004068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004070 }
4071 }
4072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02004076 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4077 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004079 }
4080
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004081 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004082
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004083 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004084}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004085#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004088{
Janos Follath865b3eb2019-12-16 11:46:15 +00004089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004091 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004092
Hanno Beckere694c3e2017-12-27 21:34:08 +00004093 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004096
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004097#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004098 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4099 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4100 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4101 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004102 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004103 {
4104 /* We've already read a record and there is an asynchronous
4105 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004106 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4108 }
4109 else
4110#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004111 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 return( ret );
4115 }
4116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004118 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4123 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 }
4125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4129 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 }
4131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4133 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004135 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004138 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004139 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004140
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004141 if( p != end )
4142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4144 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004145 }
4146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004148 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004149 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004150 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004151 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4154 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004155 }
4156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004158 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4161#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4162 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4163 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4164 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4165 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4166 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4167 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4168 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004171 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4174 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004175 }
4176
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004177 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4178 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004181 &ssl->handshake->pmslen,
4182 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004184 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4187 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004188 }
4189
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004190 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4191 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004193 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4195 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4196 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4197 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4198#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4199 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004200 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004201 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004204 return( ret );
4205 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004206
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004207 if( p != end )
4208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4210 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004211 }
4212
Hanno Becker845b9462018-10-26 12:07:29 +01004213#if defined(MBEDTLS_USE_PSA_CRYPTO)
4214 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4215 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004216 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4218 else
4219#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004221 ciphersuite_info->key_exchange ) ) != 0 )
4222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004224 return( ret );
4225 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4229#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4230 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004231 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004232#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004233 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004234 {
4235 /* There is an asynchronous operation in progress to
4236 * decrypt the encrypted premaster secret, so skip
4237 * directly to resuming this operation. */
4238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4239 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4240 * won't actually use it, but maintain p anyway for robustness. */
4241 p += ssl->conf->psk_identity_len + 2;
4242 }
4243 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004244#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004245 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004248 return( ret );
4249 }
4250
Hanno Becker845b9462018-10-26 12:07:29 +01004251#if defined(MBEDTLS_USE_PSA_CRYPTO)
4252 /* Opaque PSKs are currently only supported for PSK-only. */
4253 if( ssl_use_opaque_psk( ssl ) == 1 )
4254 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4255#endif
4256
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004257 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004260 return( ret );
4261 }
4262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004264 ciphersuite_info->key_exchange ) ) != 0 )
4265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004267 return( ret );
4268 }
4269 }
4270 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4272#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4273 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004274 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004275 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004278 return( ret );
4279 }
4280 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004283 return( ret );
4284 }
4285
Hanno Becker845b9462018-10-26 12:07:29 +01004286#if defined(MBEDTLS_USE_PSA_CRYPTO)
4287 /* Opaque PSKs are currently only supported for PSK-only. */
4288 if( ssl_use_opaque_psk( ssl ) == 1 )
4289 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4290#endif
4291
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004292 if( p != end )
4293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4295 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004296 }
4297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004299 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004302 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004303 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004304 }
4305 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4307#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4308 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004309 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004310 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004313 return( ret );
4314 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004317 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4320 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004321 }
4322
Hanno Becker845b9462018-10-26 12:07:29 +01004323#if defined(MBEDTLS_USE_PSA_CRYPTO)
4324 /* Opaque PSKs are currently only supported for PSK-only. */
4325 if( ssl_use_opaque_psk( ssl ) == 1 )
4326 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4327#endif
4328
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004329 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4330 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004333 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004336 return( ret );
4337 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004338 }
4339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4341#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4342 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004343 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004344 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004347 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 }
4349 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004352#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4353 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4354 {
4355 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4356 p, end - p );
4357 if( ret != 0 )
4358 {
4359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4360 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4361 }
4362
4363 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4364 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4365 ssl->conf->f_rng, ssl->conf->p_rng );
4366 if( ret != 0 )
4367 {
4368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4369 return( ret );
4370 }
4371 }
4372 else
4373#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004382 return( ret );
4383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004384
Paul Bakker5121ce52009-01-03 21:22:43 +00004385 ssl->state++;
4386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004388
4389 return( 0 );
4390}
4391
Gilles Peskineeccd8882020-03-10 12:19:08 +01004392#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004395 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004396 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004399
Hanno Becker77adddc2019-02-07 12:32:43 +00004400 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004403 ssl->state++;
4404 return( 0 );
4405 }
4406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004409}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004410#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004414 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004415 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004416 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004417 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4419 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004420#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004422 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004423 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004424 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004427
Hanno Becker2a831a42019-02-07 13:17:25 +00004428 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004431 ssl->state++;
4432 return( 0 );
4433 }
4434
Hanno Becker2a831a42019-02-07 13:17:25 +00004435#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4436 if( ssl->session_negotiate->peer_cert == NULL )
4437 {
4438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4439 ssl->state++;
4440 return( 0 );
4441 }
4442#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4443 if( ssl->session_negotiate->peer_cert_digest == NULL )
4444 {
4445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4446 ssl->state++;
4447 return( 0 );
4448 }
4449#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4450
Simon Butcher99000142016-10-13 17:21:01 +01004451 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004452 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004453 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004455 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 return( ret );
4457 }
4458
4459 ssl->state++;
4460
Simon Butcher99000142016-10-13 17:21:01 +01004461 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4463 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4466 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 }
4468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004470
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004471#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4472 peer_pk = &ssl->handshake->peer_pubkey;
4473#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4474 if( ssl->session_negotiate->peer_cert == NULL )
4475 {
4476 /* Should never happen */
4477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4478 }
4479 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4480#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4481
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004482 /*
4483 * struct {
4484 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4485 * opaque signature<0..2^16-1>;
4486 * } DigitallySigned;
4487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4489 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4490 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004493 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004494
4495 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004496 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004497 {
4498 hash_start += 16;
4499 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004501 }
Paul Bakker926af752012-11-23 13:38:07 +01004502 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004503 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4505 MBEDTLS_SSL_PROTO_TLS1_1 */
4506#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4507 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004508 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004509 if( i + 2 > ssl->in_hslen )
4510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4512 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004513 }
4514
Paul Bakker5121ce52009-01-03 21:22:43 +00004515 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004516 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 */
Simon Butcher99000142016-10-13 17:21:01 +01004518 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4519
4520 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004523 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004525 }
4526
Simon Butcher99000142016-10-13 17:21:01 +01004527#if !defined(MBEDTLS_MD_SHA1)
4528 if( MBEDTLS_MD_SHA1 == md_alg )
4529 hash_start += 16;
4530#endif
Paul Bakker926af752012-11-23 13:38:07 +01004531
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004532 /* Info from md_alg will be used instead */
4533 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004534
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004535 i++;
4536
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004537 /*
4538 * Signature
4539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4541 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004544 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004546 }
4547
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004548 /*
4549 * Check the certificate's key type matches the signature alg
4550 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004551 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4554 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004555 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004556
4557 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004558 }
4559 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004564 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004565
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004566 if( i + 2 > ssl->in_hslen )
4567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4569 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004570 }
4571
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004572 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4573 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004574
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004575 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4578 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 }
4580
Simon Butcher99000142016-10-13 17:21:01 +01004581 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004582 {
4583 size_t dummy_hlen;
4584 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4585 }
Simon Butcher99000142016-10-13 17:21:01 +01004586
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004587 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004588 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004589 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004592 return( ret );
4593 }
4594
Simon Butcher99000142016-10-13 17:21:01 +01004595 mbedtls_ssl_update_handshake_status( ssl );
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Paul Bakkered27a042013-04-18 22:46:23 +02004599 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004600}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004601#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4604static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004605{
Janos Follath865b3eb2019-12-16 11:46:15 +00004606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004607 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004608 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4613 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004614
4615 /*
4616 * struct {
4617 * uint32 ticket_lifetime_hint;
4618 * opaque ticket<0..2^16-1>;
4619 * } NewSessionTicket;
4620 *
4621 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4622 * 8 . 9 ticket_len (n)
4623 * 10 . 9+n ticket content
4624 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004625
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004626 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004627 ssl->session_negotiate,
4628 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004629 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004630 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004631 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004633 tlen = 0;
4634 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004635
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004636 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4637 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4638 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4639 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4640
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004641 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4642 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004643
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004644 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004645
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004646 /*
4647 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4648 * ChangeCipherSpec share the same state.
4649 */
4650 ssl->handshake->new_session_ticket = 0;
4651
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004652 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004653 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004655 return( ret );
4656 }
4657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004659
4660 return( 0 );
4661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004663
Paul Bakker5121ce52009-01-03 21:22:43 +00004664/*
Paul Bakker1961b702013-01-25 14:49:24 +01004665 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004668{
4669 int ret = 0;
4670
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004671 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004677 return( ret );
4678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004679#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004682 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004683 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004684 return( ret );
4685 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004686#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004687
Paul Bakker1961b702013-01-25 14:49:24 +01004688 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 case MBEDTLS_SSL_HELLO_REQUEST:
4691 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 break;
4693
Paul Bakker1961b702013-01-25 14:49:24 +01004694 /*
4695 * <== ClientHello
4696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004698 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701#if defined(MBEDTLS_SSL_PROTO_DTLS)
4702 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4703 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004704#endif
4705
Paul Bakker1961b702013-01-25 14:49:24 +01004706 /*
4707 * ==> ServerHello
4708 * Certificate
4709 * ( ServerKeyExchange )
4710 * ( CertificateRequest )
4711 * ServerHelloDone
4712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004714 ret = ssl_write_server_hello( ssl );
4715 break;
4716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4718 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004719 break;
4720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004722 ret = ssl_write_server_key_exchange( ssl );
4723 break;
4724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004726 ret = ssl_write_certificate_request( ssl );
4727 break;
4728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004730 ret = ssl_write_server_hello_done( ssl );
4731 break;
4732
4733 /*
4734 * <== ( Certificate/Alert )
4735 * ClientKeyExchange
4736 * ( CertificateVerify )
4737 * ChangeCipherSpec
4738 * Finished
4739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4741 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004742 break;
4743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004745 ret = ssl_parse_client_key_exchange( ssl );
4746 break;
4747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004749 ret = ssl_parse_certificate_verify( ssl );
4750 break;
4751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4753 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004754 break;
4755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 case MBEDTLS_SSL_CLIENT_FINISHED:
4757 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004758 break;
4759
4760 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004761 * ==> ( NewSessionTicket )
4762 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004763 * Finished
4764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4766#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004767 if( ssl->handshake->new_session_ticket != 0 )
4768 ret = ssl_write_new_session_ticket( ssl );
4769 else
Paul Bakkera503a632013-08-14 13:48:06 +02004770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004772 break;
4773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 case MBEDTLS_SSL_SERVER_FINISHED:
4775 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004776 break;
4777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 case MBEDTLS_SSL_FLUSH_BUFFERS:
4779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4780 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004781 break;
4782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4784 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004785 break;
4786
4787 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 }
4791
Paul Bakker5121ce52009-01-03 21:22:43 +00004792 return( ret );
4793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#endif /* MBEDTLS_SSL_SRV_C */