blob: 007d9e41af21a6d30f4e5cbe60bafa7ff30bd5fe [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{
Johan Pascal43f94902020-09-22 12:25:52 +0200784 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100785 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200786 size_t profile_length;
787 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200788 /*! 2 bytes for profile length and 1 byte for mki len */
789 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100790
791 /* If use_srtp is not configured, just ignore the extension */
Ron Eldoref72faf2018-07-12 11:54:20 +0300792 if( ssl->conf->dtls_srtp_profile_list == NULL ||
793 ssl->conf->dtls_srtp_profile_list_len == 0 )
Johan Pascal85269572020-08-25 10:01:54 +0200794 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100795 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200796 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100797
798 /* RFC5764 section 4.1.1
799 * uint8 SRTPProtectionProfile[2];
800 *
801 * struct {
802 * SRTPProtectionProfiles SRTPProtectionProfiles;
803 * opaque srtp_mki<0..255>;
804 * } UseSRTPData;
805
806 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100807 */
808
Ron Eldoref72faf2018-07-12 11:54:20 +0300809 /*
810 * Min length is 5: at least one protection profile(2 bytes)
811 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200812 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200813 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300814 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200815 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200816 {
817 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
818 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100819 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200820 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100821
Johan Pascal43f94902020-09-22 12:25:52 +0200822 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200823
Ron Eldoref72faf2018-07-12 11:54:20 +0300824 /* first 2 bytes are protection profile length(in bytes) */
825 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200826 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200827
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200828 /* The profile length cannot be bigger than input buffer size - lengths fields */
829 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200830 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200831 {
832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
833 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
834 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
835 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300836 /*
837 * parse the extension list values are defined in
838 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
839 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200840 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100841 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200842 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200843 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100844
Johan Pascal43f94902020-09-22 12:25:52 +0200845 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300846 {
Johan Pascal43f94902020-09-22 12:25:52 +0200847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
848 mbedtls_ssl_get_srtp_profile_as_string(
849 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300850 }
Johan Pascal85269572020-08-25 10:01:54 +0200851 else
852 {
853 continue;
854 }
Ron Eldor591f1622018-01-22 12:30:04 +0200855 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200856 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200857 {
858 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
859 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200860 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
862 mbedtls_ssl_get_srtp_profile_as_string(
863 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200864 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100865 }
866 }
Johan Pascal43f94902020-09-22 12:25:52 +0200867 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200868 break;
869 }
Johan Pascal042d4562020-08-25 12:14:02 +0200870 buf += profile_length; /* buf points to the mki length */
871 mki_length = *buf;
872 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200873
Johan Pascal042d4562020-08-25 12:14:02 +0200874 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
875 mki_length + profile_length + size_of_lengths != len )
876 {
877 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
878 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
879 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
880 }
881
882 /* Parse the mki only if present and mki is supported locally */
883 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
884 mki_length > 0 )
885 {
886 ssl->dtls_srtp_info.mki_len = mki_length;
887
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200888 memcpy(ssl->dtls_srtp_info.mki_value, buf, mki_length);
Ron Eldorb4655392018-07-05 18:25:39 +0300889
Ron Eldoref72faf2018-07-12 11:54:20 +0300890 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
891 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100892 }
893
Johan Pascal042d4562020-08-25 12:14:02 +0200894 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100895}
896#endif /* MBEDTLS_SSL_DTLS_SRTP */
897
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100898/*
899 * Auxiliary functions for ServerHello parsing and related actions
900 */
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100903/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100904 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_ECDSA_C)
907static int ssl_check_key_curve( mbedtls_pk_context *pk,
908 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 const mbedtls_ecp_curve_info **crv = curves;
911 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100912
913 while( *crv != NULL )
914 {
915 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100916 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100917 crv++;
918 }
919
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100920 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100923
924/*
925 * Try picking a certificate for this ciphersuite,
926 * return 0 on success and -1 on failure.
927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928static int ssl_pick_cert( mbedtls_ssl_context *ssl,
929 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100932 mbedtls_pk_type_t pk_alg =
933 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200934 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100937 if( ssl->handshake->sni_key_cert != NULL )
938 list = ssl->handshake->sni_key_cert;
939 else
940#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200941 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944 return( 0 );
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000947
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200948 if( list == NULL )
949 {
950 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
951 return( -1 );
952 }
953
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100954 for( cur = list; cur != NULL; cur = cur->next )
955 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400956 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000958 cur->cert );
959
Gilles Peskinee198df52018-01-05 21:17:45 +0100960 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100963 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000964 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100965
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200966 /*
967 * This avoids sending the client a cert it'll reject based on
968 * keyUsage or other extensions.
969 *
970 * It also allows the user to provision different certificates for
971 * different uses based on keyUsage, eg if they want to avoid signing
972 * and decrypting with the same RSA key.
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100975 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000978 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200979 continue;
980 }
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#if defined(MBEDTLS_ECDSA_C)
983 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200984 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100987 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000988 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100989#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100990
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100991 /*
992 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
993 * present them a SHA-higher cert rather than failing if it's the only
994 * one we got that satisfies the other conditions.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
997 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100998 {
999 if( fallback == NULL )
1000 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001003 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001004 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001006 }
1007
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001008 /* If we get there, we got a winner */
1009 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001010 }
1011
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001012 if( cur == NULL )
1013 cur = fallback;
1014
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001015 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001016 if( cur != NULL )
1017 {
1018 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001020 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001021 return( 0 );
1022 }
1023
1024 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001027
1028/*
1029 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1030 * Sets ciphersuite_info only if the suite matches.
1031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1033 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001036
Hanno Becker7e5437a2017-04-28 17:15:26 +01001037#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001038 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001039 mbedtls_pk_type_t sig_type;
1040#endif
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001043 if( suite_info == NULL )
1044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001047 }
1048
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Hanno Beckerecea07d2018-11-07 16:24:35 +00001050 suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001051
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001052 if( suite_info->min_minor_ver > ssl->minor_ver ||
1053 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001056 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001057 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001062 return( 0 );
1063#endif
1064
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001065#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001066 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001070 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001071 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001072#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001073
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001074#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1075 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001076 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001077 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1079 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001080 return( 0 );
1081 }
1082#endif
1083
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1086 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001087 ( ssl->handshake->curves == NULL ||
1088 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001091 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001092 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001093 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001094#endif
1095
Gilles Peskineeccd8882020-03-10 12:19:08 +01001096#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001097 /* If the ciphersuite requires a pre-shared key and we don't
1098 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001100 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001103 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001104 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001105#endif
1106
Hanno Becker7e5437a2017-04-28 17:15:26 +01001107#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001108 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001109 /* If the ciphersuite requires signing, check whether
1110 * a suitable hash algorithm is present. */
1111 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1112 {
1113 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1114 if( sig_type != MBEDTLS_PK_NONE &&
1115 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1116 {
1117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1118 "for signature algorithm %d", sig_type ) );
1119 return( 0 );
1120 }
1121 }
1122
1123#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001124 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001127 /*
1128 * Final check: if ciphersuite requires us to have a
1129 * certificate/key of a particular type:
1130 * - select the appropriate certificate if we have one, or
1131 * - try the next ciphersuite if we don't
1132 * This must be done last since we modify the key_cert list.
1133 */
1134 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001137 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001138 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001139 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001140#endif
1141
1142 *ciphersuite_info = suite_info;
1143 return( 0 );
1144}
1145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1147static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001148{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001149 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001150 unsigned int i, j;
1151 size_t n;
1152 unsigned int ciph_len, sess_len, chal_len;
1153 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001154 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#if defined(MBEDTLS_SSL_RENEGOTIATION)
1160 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001163 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1164 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001168
1169 buf = ssl->in_hdr;
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001174 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001176 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001178 buf[3], buf[4] ) );
1179
1180 /*
1181 * SSLv2 Client Hello
1182 *
1183 * Record layer:
1184 * 0 . 1 message length
1185 *
1186 * SSL layer:
1187 * 2 . 2 message type
1188 * 3 . 4 protocol version
1189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1191 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1194 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001195 }
1196
1197 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1198
1199 if( n < 17 || n > 512 )
1200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1202 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001203 }
1204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001206 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1207 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001208
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001209 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001212 " [%d:%d] < [%d:%d]",
1213 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001214 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1217 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1218 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001219 }
1220
Paul Bakker2fbefde2013-06-29 16:01:15 +02001221 ssl->handshake->max_major_ver = buf[3];
1222 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001227 return( ret );
1228 }
1229
1230 ssl->handshake->update_checksum( ssl, buf + 2, n );
1231
1232 buf = ssl->in_msg;
1233 n = ssl->in_left - 5;
1234
1235 /*
1236 * 0 . 1 ciphersuitelist length
1237 * 2 . 3 session id length
1238 * 4 . 5 challenge length
1239 * 6 . .. ciphersuitelist
1240 * .. . .. session id
1241 * .. . .. challenge
1242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001244
1245 ciph_len = ( buf[0] << 8 ) | buf[1];
1246 sess_len = ( buf[2] << 8 ) | buf[3];
1247 chal_len = ( buf[4] << 8 ) | buf[5];
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001250 ciph_len, sess_len, chal_len ) );
1251
1252 /*
1253 * Make sure each parameter length is valid
1254 */
1255 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1258 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001259 }
1260
1261 if( sess_len > 32 )
1262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1264 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001265 }
1266
1267 if( chal_len < 8 || chal_len > 32 )
1268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1270 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001271 }
1272
1273 if( n != 6 + ciph_len + sess_len + chal_len )
1274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1276 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001277 }
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001280 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001282 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001284 buf + 6 + ciph_len + sess_len, chal_len );
1285
1286 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001287 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001288 memset( ssl->session_negotiate->id, 0,
1289 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001290 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001291
1292 p += sess_len;
1293 memset( ssl->handshake->randbytes, 0, 64 );
1294 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1295
1296 /*
1297 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1298 */
1299 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1304#if defined(MBEDTLS_SSL_RENEGOTIATION)
1305 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001308 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001309
Gilles Peskinec94f7352017-05-10 16:37:56 +02001310 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1311 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#endif /* MBEDTLS_SSL_RENEGOTIATION */
1315 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001316 break;
1317 }
1318 }
1319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001321 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1322 {
1323 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1325 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001328
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001329 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1334 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001337 }
1338
1339 break;
1340 }
1341 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001343
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001344 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001345 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001346 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001348 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001349 for( i = 0; ciphersuites[i] != 0; i++ )
1350#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001351 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001352 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001353#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001354 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001355 if( p[0] != 0 ||
1356 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1357 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1358 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001359
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001360 got_common_suite = 1;
1361
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001362 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1363 &ciphersuite_info ) ) != 0 )
1364 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001365
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001366 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001367 goto have_ciphersuite_v2;
1368 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001369
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001370 if( got_common_suite )
1371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001373 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001375 }
1376 else
1377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1379 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001380 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001381
1382have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001384
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001385 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001386 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001387
1388 /*
1389 * SSLv2 Client Hello relevant renegotiation security checks
1390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001392 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1396 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001398 }
1399
1400 ssl->in_left = 0;
1401 ssl->state++;
1402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001404
1405 return( 0 );
1406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001408
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001409/* This function doesn't alert on errors that happen early during
1410 ClientHello parsing because they might indicate that the client is
1411 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001413{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001414 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001415 size_t i, j;
1416 size_t ciph_offset, comp_offset, ext_offset;
1417 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001419 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001420#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001421 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001423 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001424#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001425 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001426 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001428 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
Hanno Becker7e5437a2017-04-28 17:15:26 +01001430 /* If there is no signature-algorithm extension present,
1431 * we need to fall back to the default values for allowed
1432 * signature-hash pairs. */
1433#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001434 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001435 int sig_hash_alg_ext_present = 0;
1436#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001437 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001442read_record_header:
1443#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001444 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001446 * otherwise read it ourselves manually in order to support SSLv2
1447 * ClientHello, which doesn't use the same record layer format.
1448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_RENEGOTIATION)
1450 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001451#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001454 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001455 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001457 return( ret );
1458 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 }
1460
1461 buf = ssl->in_hdr;
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001466#endif
1467 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001468 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001469#endif
1470
Hanno Becker5903de42019-05-03 14:46:38 +01001471 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001472
Paul Bakkerec636f32012-09-09 19:17:02 +00001473 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001474 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001475 *
1476 * Record layer:
1477 * 0 . 0 message type
1478 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001479 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001480 * 3 . 4 message length
1481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001483 buf[0] ) );
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1488 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001489 }
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001492 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001495 buf[1], buf[2] ) );
1496
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001497 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001498
1499 /* According to RFC 5246 Appendix E.1, the version here is typically
1500 * "{03,00}, the lowest version number supported by the client, [or] the
1501 * value of ClientHello.client_version", so the only meaningful check here
1502 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1506 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001509 /* For DTLS if this is the initial handshake, remember the client sequence
1510 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_SSL_RENEGOTIATION)
1514 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001515#endif
1516 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001517 {
1518 /* Epoch should be 0 for initial handshakes */
1519 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1522 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001523 }
1524
Hanno Becker19859472018-08-06 09:40:20 +01001525 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1528 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001531 ssl->next_record_offset = 0;
1532 ssl->in_left = 0;
1533 goto read_record_header;
1534 }
1535
1536 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001538#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001541
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001542 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_SSL_RENEGOTIATION)
1545 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001548 msg_len = ssl->in_hslen;
1549 }
1550 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001551#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001552 {
Angus Grattond8213d02016-05-25 20:56:48 +10001553 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1556 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001557 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001558
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001559 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001560 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001563 return( ret );
1564 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001565
1566 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001569 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001570 else
1571#endif
1572 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001573 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001574
1575 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001578
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001579 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001580
1581 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001582 * Handshake layer:
1583 * 0 . 0 handshake type
1584 * 1 . 3 handshake length
1585 * 4 . 5 DTLS only: message seqence number
1586 * 6 . 8 DTLS only: fragment offset
1587 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1592 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001593 }
1594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1600 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001601 }
1602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001604 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1605
1606 /* We don't support fragmentation of ClientHello (yet?) */
1607 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1611 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001612 }
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001616 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001617 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001618 * Copy the client's handshake message_seq on initial handshakes,
1619 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_SSL_RENEGOTIATION)
1622 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001623 {
1624 /* This couldn't be done in ssl_prepare_handshake_record() */
1625 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1626 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001627
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001628 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001631 "%d (expected %d)", cli_msg_seq,
1632 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001634 }
1635
1636 ssl->handshake->in_msg_seq++;
1637 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001638 else
1639#endif
1640 {
1641 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1642 ssl->in_msg[5];
1643 ssl->handshake->out_msg_seq = cli_msg_seq;
1644 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1645 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001646
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001647 /*
1648 * For now we don't support fragmentation, so make sure
1649 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001650 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001651 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1652 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1655 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001656 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 buf += mbedtls_ssl_hs_hdr_len( ssl );
1661 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001662
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001663 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001664 * ClientHello layer:
1665 * 0 . 1 protocol version
1666 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1667 * 34 . 35 session id length (1 byte)
1668 * 35 . 34+x session id
1669 * 35+x . 35+x DTLS only: cookie length (1 byte)
1670 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001671 * .. . .. ciphersuite list length (2 bytes)
1672 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001673 * .. . .. compression alg. list length (1 byte)
1674 * .. . .. compression alg. list
1675 * .. . .. extensions length (2 bytes, optional)
1676 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001677 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001678
1679 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001680 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001681 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1682 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001683 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001684 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1687 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001688 }
1689
1690 /*
1691 * Check and save the protocol version
1692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001696 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001697
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001698 ssl->handshake->max_major_ver = ssl->major_ver;
1699 ssl->handshake->max_minor_ver = ssl->minor_ver;
1700
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001701 if( ssl->major_ver < ssl->conf->min_major_ver ||
1702 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001705 " [%d:%d] < [%d:%d]",
1706 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001707 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1709 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001711 }
1712
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001713 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001714 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001715 ssl->major_ver = ssl->conf->max_major_ver;
1716 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001717 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001718 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1719 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001720
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001721 /*
1722 * Save client random (inc. Unix time)
1723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001725
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001726 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001727
1728 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001729 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001730 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001731 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001732
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001733 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001734 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1738 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001740 }
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001743
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001744 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001745 memset( ssl->session_negotiate->id, 0,
1746 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001747 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001748 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001749
1750 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001751 * Check the cookie length and content
1752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001755 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001756 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001757 cookie_len = buf[cookie_offset];
1758
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001759 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001762 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1763 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001765 }
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001768 buf + cookie_offset + 1, cookie_len );
1769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001771 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_SSL_RENEGOTIATION)
1773 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001774#endif
1775 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001776 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001777 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001778 buf + cookie_offset + 1, cookie_len,
1779 ssl->cli_id, ssl->cli_id_len ) != 0 )
1780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001782 ssl->handshake->verify_cookie_len = 1;
1783 }
1784 else
1785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001787 ssl->handshake->verify_cookie_len = 0;
1788 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001789 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001792 {
1793 /* We know we didn't send a cookie, so it should be empty */
1794 if( cookie_len != 0 )
1795 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001796 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1798 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001802 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001803
1804 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001805 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001806 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001807 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001808 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001811 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001812
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001813 ciph_len = ( buf[ciph_offset + 0] << 8 )
1814 | ( buf[ciph_offset + 1] );
1815
1816 if( ciph_len < 2 ||
1817 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1818 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001821 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1822 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001824 }
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001827 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001828
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001829 /*
1830 * Check the compression algorithms length and pick one
1831 */
1832 comp_offset = ciph_offset + 2 + ciph_len;
1833
1834 comp_len = buf[comp_offset];
1835
1836 if( comp_len < 1 ||
1837 comp_len > 16 ||
1838 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001841 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1842 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001844 }
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001847 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1850#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001851 for( i = 0; i < comp_len; ++i )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001856 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 }
1858 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001859#endif
1860
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001861 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001865#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001866
Janos Follathc6dab2b2016-05-23 14:27:02 +01001867 /* Do not parse the extensions if the protocol is SSLv3 */
1868#if defined(MBEDTLS_SSL_PROTO_SSL3)
1869 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1870 {
1871#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001872 /*
1873 * Check the extension length
1874 */
1875 ext_offset = comp_offset + 1 + comp_len;
1876 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001877 {
Simon Butcher584a5472016-05-23 16:24:52 +01001878 if( msg_len < ext_offset + 2 )
1879 {
1880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001881 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1882 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001883 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1884 }
1885
1886 ext_len = ( buf[ext_offset + 0] << 8 )
1887 | ( buf[ext_offset + 1] );
1888
1889 if( ( ext_len > 0 && ext_len < 4 ) ||
1890 msg_len != ext_offset + 2 + ext_len )
1891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1894 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001895 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1896 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001897 }
Simon Butcher584a5472016-05-23 16:24:52 +01001898 else
1899 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001900
Simon Butcher584a5472016-05-23 16:24:52 +01001901 ext = buf + ext_offset + 2;
1902 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001903
Simon Butcher584a5472016-05-23 16:24:52 +01001904 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001905 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001906 unsigned int ext_id;
1907 unsigned int ext_size;
1908 if ( ext_len < 4 ) {
1909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1910 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1911 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1912 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1913 }
1914 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1915 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001916
Simon Butcher584a5472016-05-23 16:24:52 +01001917 if( ext_size + 4 > ext_len )
1918 {
1919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001920 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001922 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1923 }
1924 switch( ext_id )
1925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001927 case MBEDTLS_TLS_EXT_SERVERNAME:
1928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1929 if( ssl->conf->f_sni == NULL )
1930 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001931
Simon Butcher584a5472016-05-23 16:24:52 +01001932 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1933 if( ret != 0 )
1934 return( ret );
1935 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001937
Simon Butcher584a5472016-05-23 16:24:52 +01001938 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001941 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001942#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001943
Simon Butcher584a5472016-05-23 16:24:52 +01001944 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1945 if( ret != 0 )
1946 return( ret );
1947 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001950 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001951 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1953
Simon Butcher584a5472016-05-23 16:24:52 +01001954 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1955 if( ret != 0 )
1956 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001957
1958 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001959 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001961 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001962
Robert Cragie136884c2015-10-02 13:34:31 +01001963#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001964 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001965 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001967
Simon Butcher584a5472016-05-23 16:24:52 +01001968 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1969 if( ret != 0 )
1970 return( ret );
1971 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001972
Simon Butcher584a5472016-05-23 16:24:52 +01001973 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1975 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001976
Simon Butcher584a5472016-05-23 16:24:52 +01001977 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1978 if( ret != 0 )
1979 return( ret );
1980 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001981#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1982 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001983
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001984#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001985 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001987
Simon Butcher584a5472016-05-23 16:24:52 +01001988 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1989 if( ret != 0 )
1990 return( ret );
1991 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001992#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001995 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001997
Simon Butcher584a5472016-05-23 16:24:52 +01001998 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1999 if( ret != 0 )
2000 return( ret );
2001 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002005 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
2006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002007
Simon Butcher584a5472016-05-23 16:24:52 +01002008 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
2009 if( ret != 0 )
2010 return( ret );
2011 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002013
Hanno Beckera0e20d02019-05-15 14:03:01 +01002014#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01002015 case MBEDTLS_TLS_EXT_CID:
2016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2017
2018 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2019 if( ret != 0 )
2020 return( ret );
2021 break;
2022#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002025 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002027
Simon Butcher584a5472016-05-23 16:24:52 +01002028 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2029 if( ret != 0 )
2030 return( ret );
2031 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01002035 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002037
Simon Butcher584a5472016-05-23 16:24:52 +01002038 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
2039 if( ret != 0 )
2040 return( ret );
2041 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01002045 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002047
Simon Butcher584a5472016-05-23 16:24:52 +01002048 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2049 if( ret != 0 )
2050 return( ret );
2051 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002055 case MBEDTLS_TLS_EXT_ALPN:
2056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002057
Simon Butcher584a5472016-05-23 16:24:52 +01002058 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2059 if( ret != 0 )
2060 return( ret );
2061 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002063
Johan Pascalb62bb512015-12-03 21:56:45 +01002064#if defined(MBEDTLS_SSL_DTLS_SRTP)
2065 case MBEDTLS_TLS_EXT_USE_SRTP:
2066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02002067
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002068 if ( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2069 {
2070 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2071 if ( ret != 0 )
2072 return( ret );
2073 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002074 break;
2075#endif /* MBEDTLS_SSL_DTLS_SRTP */
2076
Simon Butcher584a5472016-05-23 16:24:52 +01002077 default:
2078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
2079 ext_id ) );
2080 }
2081
2082 ext_len -= 4 + ext_size;
2083 ext += 4 + ext_size;
2084
2085 if( ext_len > 0 && ext_len < 4 )
2086 {
2087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002088 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2089 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01002090 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2091 }
Paul Bakker48916f92012-09-16 19:57:18 +00002092 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002093#if defined(MBEDTLS_SSL_PROTO_SSL3)
2094 }
2095#endif
2096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002098 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
2101 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002102 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002104
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002105 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002106 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2110 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002113 }
2114
2115 break;
2116 }
2117 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002119
Hanno Becker7e5437a2017-04-28 17:15:26 +01002120#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002121 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002122
2123 /*
2124 * Try to fall back to default hash SHA1 if the client
2125 * hasn't provided any preferred signature-hash combinations.
2126 */
2127 if( sig_hash_alg_ext_present == 0 )
2128 {
2129 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2130
2131 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2132 md_default = MBEDTLS_MD_NONE;
2133
2134 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2135 }
2136
2137#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002138 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002139
Paul Bakker48916f92012-09-16 19:57:18 +00002140 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002141 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2142 */
2143 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2148#if defined(MBEDTLS_SSL_RENEGOTIATION)
2149 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002150 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2152 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002153 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2154 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002156 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002159 break;
2160 }
2161 }
2162
2163 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002164 * Renegotiation security checks
2165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002167 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002170 handshake_failure = 1;
2171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172#if defined(MBEDTLS_SSL_RENEGOTIATION)
2173 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2174 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002175 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002178 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2181 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002182 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002185 handshake_failure = 1;
2186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2188 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002189 renegotiation_info_seen == 1 )
2190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002192 handshake_failure = 1;
2193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002195
2196 if( handshake_failure == 1 )
2197 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002198 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2199 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002201 }
Paul Bakker380da532012-04-18 16:10:25 +00002202
Paul Bakker41c83d32013-03-20 14:39:14 +01002203 /*
2204 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002205 * (At the end because we need information from the EC-based extensions
2206 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002207 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002208 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002209 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002210 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002212 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002213 for( i = 0; ciphersuites[i] != 0; i++ )
2214#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002215 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002216 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002217#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002218 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002219 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2220 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2221 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002222
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002223 got_common_suite = 1;
2224
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002225 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2226 &ciphersuite_info ) ) != 0 )
2227 return( ret );
2228
2229 if( ciphersuite_info != NULL )
2230 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002231 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002232
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002233 if( got_common_suite )
2234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002236 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2238 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002240 }
2241 else
2242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002244 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2245 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002247 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002248
2249have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002251
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002252 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002253 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002254
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 ssl->state++;
2256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002260#endif
2261
Hanno Becker7e5437a2017-04-28 17:15:26 +01002262 /* Debugging-only output for testsuite */
2263#if defined(MBEDTLS_DEBUG_C) && \
2264 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002265 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002266 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2267 {
2268 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2269 if( sig_alg != MBEDTLS_PK_NONE )
2270 {
2271 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2272 sig_alg );
2273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2274 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2275 }
2276 else
2277 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2279 "%d - should not happen", sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002280 }
2281 }
2282#endif
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
2286 return( 0 );
2287}
2288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2290static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002291 unsigned char *buf,
2292 size_t *olen )
2293{
2294 unsigned char *p = buf;
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002297 {
2298 *olen = 0;
2299 return;
2300 }
2301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2305 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002306
2307 *p++ = 0x00;
2308 *p++ = 0x00;
2309
2310 *olen = 4;
2311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002313
Hanno Beckera0e20d02019-05-15 14:03:01 +01002314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002315static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2316 unsigned char *buf,
2317 size_t *olen )
2318{
2319 unsigned char *p = buf;
2320 size_t ext_len;
2321 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2322
2323 *olen = 0;
2324
2325 /* Skip writing the extension if we don't want to use it or if
2326 * the client hasn't offered it. */
2327 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2328 return;
2329
2330 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2331 * which is at most 255, so the increment cannot overflow. */
2332 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2333 {
2334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2335 return;
2336 }
2337
2338 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2339
2340 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002341 * Quoting draft-ietf-tls-dtls-connection-id-05
2342 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002343 *
2344 * struct {
2345 * opaque cid<0..2^8-1>;
2346 * } ConnectionId;
2347 */
2348
2349 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2350 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2351 ext_len = (size_t) ssl->own_cid_len + 1;
2352 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2353 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2354
2355 *p++ = (uint8_t) ssl->own_cid_len;
2356 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2357
2358 *olen = ssl->own_cid_len + 5;
2359}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002360#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2363static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002364 unsigned char *buf,
2365 size_t *olen )
2366{
2367 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2369 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002370
Hanno Becker27b34d52017-10-20 14:24:51 +01002371 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002373 {
2374 *olen = 0;
2375 return;
2376 }
2377
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002378 /*
2379 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2380 * from a client and then selects a stream or Authenticated Encryption
2381 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2382 * encrypt-then-MAC response extension back to the client."
2383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002385 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2387 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002388 {
2389 *olen = 0;
2390 return;
2391 }
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2396 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002397
2398 *p++ = 0x00;
2399 *p++ = 0x00;
2400
2401 *olen = 4;
2402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2406static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002407 unsigned char *buf,
2408 size_t *olen )
2409{
2410 unsigned char *p = buf;
2411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2413 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002414 {
2415 *olen = 0;
2416 return;
2417 }
2418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002420 "extension" ) );
2421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2423 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002424
2425 *p++ = 0x00;
2426 *p++ = 0x00;
2427
2428 *olen = 4;
2429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2433static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002434 unsigned char *buf,
2435 size_t *olen )
2436{
2437 unsigned char *p = buf;
2438
2439 if( ssl->handshake->new_session_ticket == 0 )
2440 {
2441 *olen = 0;
2442 return;
2443 }
2444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2448 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002449
2450 *p++ = 0x00;
2451 *p++ = 0x00;
2452
2453 *olen = 4;
2454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002458 unsigned char *buf,
2459 size_t *olen )
2460{
2461 unsigned char *p = buf;
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002464 {
2465 *olen = 0;
2466 return;
2467 }
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2472 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_RENEGOTIATION)
2475 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002476 {
2477 *p++ = 0x00;
2478 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2479 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002480
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002481 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2482 p += ssl->verify_data_len;
2483 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2484 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002485 }
2486 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002488 {
2489 *p++ = 0x00;
2490 *p++ = 0x01;
2491 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002492 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002493
2494 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002495}
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2498static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002499 unsigned char *buf,
2500 size_t *olen )
2501{
2502 unsigned char *p = buf;
2503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002505 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002506 *olen = 0;
2507 return;
2508 }
2509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2513 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002514
2515 *p++ = 0x00;
2516 *p++ = 1;
2517
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002518 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002519
2520 *olen = 5;
2521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002523
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002524#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002525 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002527 unsigned char *buf,
2528 size_t *olen )
2529{
2530 unsigned char *p = buf;
2531 ((void) ssl);
2532
Paul Bakker677377f2013-10-28 12:54:26 +01002533 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002535 {
2536 *olen = 0;
2537 return;
2538 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2543 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002544
2545 *p++ = 0x00;
2546 *p++ = 2;
2547
2548 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002550
2551 *olen = 6;
2552}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002553#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002554
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002555#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2556static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2557 unsigned char *buf,
2558 size_t *olen )
2559{
Janos Follath865b3eb2019-12-16 11:46:15 +00002560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002561 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002562 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002563 size_t kkpp_len;
2564
2565 *olen = 0;
2566
2567 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002568 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002569 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2570 return;
2571
2572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2573
2574 if( end - p < 4 )
2575 {
2576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2577 return;
2578 }
2579
2580 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2581 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2582
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002583 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2584 p + 2, end - p - 2, &kkpp_len,
2585 ssl->conf->f_rng, ssl->conf->p_rng );
2586 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002587 {
2588 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2589 return;
2590 }
2591
2592 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2593 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2594
2595 *olen = kkpp_len + 4;
2596}
2597#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#if defined(MBEDTLS_SSL_ALPN )
2600static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002601 unsigned char *buf, size_t *olen )
2602{
2603 if( ssl->alpn_chosen == NULL )
2604 {
2605 *olen = 0;
2606 return;
2607 }
2608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002610
2611 /*
2612 * 0 . 1 ext identifier
2613 * 2 . 3 ext length
2614 * 4 . 5 protocol list length
2615 * 6 . 6 protocol name length
2616 * 7 . 7+n protocol name
2617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2619 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002620
2621 *olen = 7 + strlen( ssl->alpn_chosen );
2622
2623 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2624 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2625
2626 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2627 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2628
2629 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2630
2631 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002634
Ron Eldor3adb9922017-12-21 10:15:08 +02002635#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002636static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002637 unsigned char *buf,
2638 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002639{
Ron Eldor75870ec2018-12-06 17:31:55 +02002640 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002641 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002642 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2643
2644 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002645
Johan Pascal43f94902020-09-22 12:25:52 +02002646 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascalb62bb512015-12-03 21:56:45 +01002647 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002648 return;
2649 }
2650
2651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2652
Johan Pascald576fdb2020-09-22 10:39:53 +02002653 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002654 {
2655 mki_len = ssl->dtls_srtp_info.mki_len;
2656 }
2657
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002658 /* The extension total size is 9 bytes :
2659 * - 2 bytes for the extension tag
2660 * - 2 bytes for the total size
2661 * - 2 bytes for the protection profile length
2662 * - 2 bytes for the protection profile
2663 * - 1 byte for the mki length
2664 * + the actual mki length
2665 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002666 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002667 {
2668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2669 return;
2670 }
2671
Johan Pascalb62bb512015-12-03 21:56:45 +01002672 /* extension */
2673 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2674 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002675 /*
2676 * total length 5 and mki value: only one profile(2 bytes)
2677 * and length(2 bytes) and srtp_mki )
2678 */
Ron Eldor591f1622018-01-22 12:30:04 +02002679 ext_len = 5 + mki_len;
2680 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2681 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002682
2683 /* protection profile length: 2 */
2684 buf[4] = 0x00;
2685 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002686 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002687 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002688 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002689 {
2690 buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF );
2691 buf[7] = (unsigned char)( profile_value & 0xFF );
2692 }
2693 else
2694 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002696 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002697 }
2698
Ron Eldor591f1622018-01-22 12:30:04 +02002699 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002700 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002701
Ron Eldor591f1622018-01-22 12:30:04 +02002702 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002703}
2704#endif /* MBEDTLS_SSL_DTLS_SRTP */
2705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2707static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002708{
Janos Follath865b3eb2019-12-16 11:46:15 +00002709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002710 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002711 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002714
2715 /*
2716 * struct {
2717 * ProtocolVersion server_version;
2718 * opaque cookie<0..2^8-1>;
2719 * } HelloVerifyRequest;
2720 */
2721
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002722 /* The RFC is not clear on this point, but sending the actual negotiated
2723 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002725 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002727 p += 2;
2728
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002729 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002730 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002734 }
2735
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002736 /* Skip length byte until we know the length */
2737 cookie_len_byte = p++;
2738
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002739 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002740 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002741 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002744 return( ret );
2745 }
2746
2747 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002750
2751 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2753 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002756
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002757 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002758 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002760 return( ret );
2761 }
2762
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002763#if defined(MBEDTLS_SSL_PROTO_DTLS)
2764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2765 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2766 {
2767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2768 return( ret );
2769 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002770#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002773
2774 return( 0 );
2775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002781 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002782#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002784 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 unsigned char *buf, *p;
2786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002790 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002791 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002795
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002796 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002799
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002800 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2803 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002804 }
2805
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 /*
2807 * 0 . 0 handshake type
2808 * 1 . 3 handshake length
2809 * 4 . 5 protocol version
2810 * 6 . 9 UNIX time()
2811 * 10 . 37 random bytes
2812 */
2813 buf = ssl->out_msg;
2814 p = buf + 4;
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002817 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002818 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002821 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002824 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 *p++ = (unsigned char)( t >> 24 );
2826 *p++ = (unsigned char)( t >> 16 );
2827 *p++ = (unsigned char)( t >> 8 );
2828 *p++ = (unsigned char)( t );
2829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002831#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002832 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002833 return( ret );
2834
2835 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002838 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002839 return( ret );
2840
2841 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Paul Bakker48916f92012-09-16 19:57:18 +00002843 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
2847 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002848 * Resume is 0 by default, see ssl_handshake_init().
2849 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2850 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002852 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_SSL_RENEGOTIATION)
2854 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002855#endif
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002856 ssl->session_negotiate->id_len != 0 &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002857 ssl->conf->f_get_cache != NULL &&
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002858 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002861 ssl->handshake->resume = 1;
2862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002864 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 {
2866 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002867 * New session, create a new session id,
2868 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 ssl->state++;
2871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002873 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002874#endif
2875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002877 if( ssl->handshake->new_session_ticket != 0 )
2878 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002879 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002880 memset( ssl->session_negotiate->id, 0, 32 );
2881 }
2882 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002884 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002885 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002886 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002887 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002888 return( ret );
2889 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 }
2891 else
2892 {
2893 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002894 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002896 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002902 return( ret );
2903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 }
2905
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002906 /*
2907 * 38 . 38 session id length
2908 * 39 . 38+n session id
2909 * 39+n . 40+n chosen ciphersuite
2910 * 41+n . 41+n chosen compression alg.
2911 * 42+n . 43+n extensions length
2912 * 44+n . 43+n+m extensions
2913 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002914 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2915 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2916 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2919 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002921 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker48916f92012-09-16 19:57:18 +00002923 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2924 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2925 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2928 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002930 ssl->session_negotiate->compression ) );
2931
Janos Follathc6dab2b2016-05-23 14:27:02 +01002932 /* Do not write the extensions if the protocol is SSLv3 */
2933#if defined(MBEDTLS_SSL_PROTO_SSL3)
2934 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2935 {
2936#endif
2937
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002938 /*
2939 * First write extensions, then the total length
2940 */
2941 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2942 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002945 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2946 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002947#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002950 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2951 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002952#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002953
Hanno Beckera0e20d02019-05-15 14:03:01 +01002954#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002955 ssl_write_cid_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_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002960 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2961 ext_len += olen;
2962#endif
2963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002965 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2966 ext_len += olen;
2967#endif
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002970 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2971 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002972#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002973
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002974#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002975 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002976 if ( mbedtls_ssl_ciphersuite_uses_ec(
2977 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2978 {
2979 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2980 ext_len += olen;
2981 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002982#endif
2983
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002984#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2985 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2986 ext_len += olen;
2987#endif
2988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002990 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2991 ext_len += olen;
2992#endif
2993
Johan Pascalb62bb512015-12-03 21:56:45 +01002994#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002995 if ( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2996 {
2997 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
2998 ext_len += olen;
2999 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003000#endif
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003003
Paul Bakkera7036632014-04-30 10:15:38 +02003004 if( ext_len > 0 )
3005 {
3006 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
3007 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
3008 p += ext_len;
3009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
Janos Follathc6dab2b2016-05-23 14:27:02 +01003011#if defined(MBEDTLS_SSL_PROTO_SSL3)
3012 }
3013#endif
3014
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3017 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003019 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
3023 return( ret );
3024}
3025
Gilles Peskineeccd8882020-03-10 12:19:08 +01003026#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003028{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003029 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003030 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033
Hanno Becker77adddc2019-02-07 12:32:43 +00003034 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003037 ssl->state++;
3038 return( 0 );
3039 }
3040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003043}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003044#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003048 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003049 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03003050 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003051 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10003053 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003055 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
3059 ssl->state++;
3060
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003061#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3062 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3063 authmode = ssl->handshake->sni_authmode;
3064 else
3065#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003066 authmode = ssl->conf->authmode;
3067
Hanno Becker77adddc2019-02-07 12:32:43 +00003068 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003069 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 {
Johan Pascal4f099262020-09-22 10:59:26 +02003071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3072 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 }
3074
3075 /*
3076 * 0 . 0 handshake type
3077 * 1 . 3 handshake length
3078 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003079 * 5 .. m-1 cert types
3080 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003081 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 * n .. n+1 length of all DNs
3083 * n+2 .. n+3 length of DN 1
3084 * n+4 .. ... Distinguished Name #1
3085 * ... .. ... length of DN 2, etc.
3086 */
3087 buf = ssl->out_msg;
3088 p = buf + 4;
3089
3090 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003091 * Supported certificate types
3092 *
3093 * ClientCertificateType certificate_types<1..2^8-1>;
3094 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003096 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#if defined(MBEDTLS_RSA_C)
3099 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_ECDSA_C)
3102 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003103#endif
3104
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003105 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003106 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003107
Paul Bakker577e0062013-08-28 11:57:20 +02003108 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003110 /*
3111 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003112 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003113 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3114 *
3115 * struct {
3116 * HashAlgorithm hash;
3117 * SignatureAlgorithm signature;
3118 * } SignatureAndHashAlgorithm;
3119 *
3120 * enum { (255) } HashAlgorithm;
3121 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003124 {
Simon Butcher99000142016-10-13 17:21:01 +01003125 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003126
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003127 /*
3128 * Supported signature algorithms
3129 */
Simon Butcher99000142016-10-13 17:21:01 +01003130 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3131 {
3132 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3133
3134 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3135 continue;
3136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003138 p[2 + sa_len++] = hash;
3139 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003142 p[2 + sa_len++] = hash;
3143 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003144#endif
Simon Butcher99000142016-10-13 17:21:01 +01003145 }
Paul Bakker926af752012-11-23 13:38:07 +01003146
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003147 p[0] = (unsigned char)( sa_len >> 8 );
3148 p[1] = (unsigned char)( sa_len );
3149 sa_len += 2;
3150 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003154 /*
3155 * DistinguishedName certificate_authorities<0..2^16-1>;
3156 * opaque DistinguishedName<1..2^16-1>;
3157 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003160 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003161
3162 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003163 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003164 /* NOTE: If trusted certificates are provisioned
3165 * via a CA callback (configured through
3166 * `mbedtls_ssl_conf_ca_cb()`, then the
3167 * CertificateRequest is currently left empty. */
3168
Janos Follath088ce432017-04-10 12:42:31 +01003169#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3170 if( ssl->handshake->sni_ca_chain != NULL )
3171 crt = ssl->handshake->sni_ca_chain;
3172 else
3173#endif
3174 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003175
Janos Follath088ce432017-04-10 12:42:31 +01003176 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003177 {
irwirc9bc3002020-04-01 13:46:36 +03003178 /* It follows from RFC 5280 A.1 that this length
3179 * can be represented in at most 11 bits. */
3180 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003181
irwirc9bc3002020-04-01 13:46:36 +03003182 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003183 {
3184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3185 break;
3186 }
3187
3188 *p++ = (unsigned char)( dn_size >> 8 );
3189 *p++ = (unsigned char)( dn_size );
3190 memcpy( p, crt->subject_raw.p, dn_size );
3191 p += dn_size;
3192
3193 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3194
3195 total_dn_size += 2 + dn_size;
3196 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 }
3199
Paul Bakker926af752012-11-23 13:38:07 +01003200 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3202 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003203 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3204 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003206 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
3210 return( ret );
3211}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003212#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3215 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3216static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003217{
Janos Follath865b3eb2019-12-16 11:46:15 +00003218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3223 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003224 }
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3227 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3228 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003231 return( ret );
3232 }
3233
3234 return( 0 );
3235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3237 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003238
Gilles Peskineeccd8882020-03-10 12:19:08 +01003239#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003240 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003241static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003242 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003243{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003244 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3245 * signature length which will be added in ssl_write_server_key_exchange
3246 * after the call to ssl_prepare_server_key_exchange.
3247 * ssl_write_server_key_exchange also takes care of incrementing
3248 * ssl->out_msglen. */
3249 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003250 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003251 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003252 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003253 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003254 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3255 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003256 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003257 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003258 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003259 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003260 return( ret );
3261}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003262#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003263 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003264
Gilles Peskined3eb0612018-01-08 17:07:44 +01003265/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003266 * calculating the signature if any, but excluding formatting the
3267 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003268static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3269 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003272 ssl->handshake->ciphersuite_info;
3273
Gilles Peskineeccd8882020-03-10 12:19:08 +01003274#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3275#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003276 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003277#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3278#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003279
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003280 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003281#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003282 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003283#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003284
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003285 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003287 /*
3288 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003289 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003290 *
3291 */
3292
3293 /*
3294 * - ECJPAKE key exchanges
3295 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003296#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3297 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3298 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003300 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003301
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003302 ret = mbedtls_ecjpake_write_round_two(
3303 &ssl->handshake->ecjpake_ctx,
3304 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003305 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003306 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003307 if( ret != 0 )
3308 {
3309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3310 return( ret );
3311 }
3312
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003313 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003314 }
3315#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3316
Hanno Becker1aa267c2017-04-28 17:08:27 +01003317 /*
3318 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3319 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3320 * we use empty support identity hints here.
3321 **/
3322#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3324 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3325 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003326 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003327 ssl->out_msg[ssl->out_msglen++] = 0x00;
3328 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3331 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003332
Hanno Becker7e5437a2017-04-28 17:15:26 +01003333 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003334 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003335 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003336#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003337 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003338 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003340 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003341
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003342 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3343 {
3344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3346 }
3347
Paul Bakker41c83d32013-03-20 14:39:14 +01003348 /*
3349 * Ephemeral DH parameters:
3350 *
3351 * struct {
3352 * opaque dh_p<1..2^16-1>;
3353 * opaque dh_g<1..2^16-1>;
3354 * opaque dh_Ys<1..2^16-1>;
3355 * } ServerDHParams;
3356 */
Hanno Beckerab740562017-10-04 13:15:37 +01003357 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3358 &ssl->conf->dhm_P,
3359 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003360 {
Hanno Beckerab740562017-10-04 13:15:37 +01003361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003362 return( ret );
3363 }
Paul Bakker48916f92012-09-16 19:57:18 +00003364
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003365 if( ( ret = mbedtls_dhm_make_params(
3366 &ssl->handshake->dhm_ctx,
3367 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3368 ssl->out_msg + ssl->out_msglen, &len,
3369 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003372 return( ret );
3373 }
3374
Gilles Peskineeccd8882020-03-10 12:19:08 +01003375#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003376 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003377#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003378
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003379 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3382 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3383 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3384 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003385 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003386#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003387
Hanno Becker1aa267c2017-04-28 17:08:27 +01003388 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003389 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003390 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003391#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003392 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003394 /*
3395 * Ephemeral ECDH parameters:
3396 *
3397 * struct {
3398 * ECParameters curve_params;
3399 * ECPoint public;
3400 * } ServerECDHParams;
3401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003405 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003406
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003407 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003408 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003409 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3410 if( (*curve)->grp_id == *gid )
3411 goto curve_matching_done;
3412
3413curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003414 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3417 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003418 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003421
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003422 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3423 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003424 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003426 return( ret );
3427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003428
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003429 if( ( ret = mbedtls_ecdh_make_params(
3430 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003431 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003432 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003433 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003436 return( ret );
3437 }
3438
Gilles Peskineeccd8882020-03-10 12:19:08 +01003439#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003440 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003441#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003442
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003443 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003444
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003445 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3446 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003447 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003448#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003449
Hanno Becker1aa267c2017-04-28 17:08:27 +01003450 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003451 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003452 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003453 * exchange parameters, compute and add the signature here.
3454 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003455 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003456#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003457 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003458 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003459 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003460 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003461 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003463
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003464 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003465 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003466 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003467 * to choose appropriate hash.
3468 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3469 * (RFC 4492, Sec. 5.4)
3470 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003471 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003472
3473 mbedtls_md_type_t md_alg;
3474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003476 mbedtls_pk_type_t sig_alg =
3477 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003479 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003480 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3481 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003482 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003483 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3484 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003487 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003488 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003490 }
3491 }
Paul Bakker577e0062013-08-28 11:57:20 +02003492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3494#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3495 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003496 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003497 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003498 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003500 }
3501 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003502#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3503 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003504 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003505 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003507 }
3508
Hanno Becker7e5437a2017-04-28 17:15:26 +01003509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3510
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003511 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003512 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3515 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3516 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003517 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003518 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003519 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3520 dig_signed,
3521 dig_signed_len );
3522 if( ret != 0 )
3523 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003524 }
3525 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3527 MBEDTLS_SSL_PROTO_TLS1_1 */
3528#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3529 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3530 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003531 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003532 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003533 dig_signed,
3534 dig_signed_len,
3535 md_alg );
3536 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003537 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003538 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003539 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3541 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003545 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003546
Gilles Peskineebd652f2018-01-05 21:18:59 +01003547 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003548
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003549 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003550 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3553 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003554 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003555 /*
3556 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003557 * explicitly through a prefix to the signature.
3558 *
3559 * struct {
3560 * HashAlgorithm hash;
3561 * SignatureAlgorithm signature;
3562 * } SignatureAndHashAlgorithm;
3563 *
3564 * struct {
3565 * SignatureAndHashAlgorithm algorithm;
3566 * opaque signature<0..2^16-1>;
3567 * } DigitallySigned;
3568 *
3569 */
3570
Gilles Peskine1004c192018-01-08 16:59:14 +01003571 ssl->out_msg[ssl->out_msglen++] =
3572 mbedtls_ssl_hash_from_md_alg( md_alg );
3573 ssl->out_msg[ssl->out_msglen++] =
3574 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003577
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003578#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003579 if( ssl->conf->f_async_sign_start != NULL )
3580 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003581 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003582 mbedtls_ssl_own_cert( ssl ),
3583 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003584 switch( ret )
3585 {
3586 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3587 /* act as if f_async_sign was null */
3588 break;
3589 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003590 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003591 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003592 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003593 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003594 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3595 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003596 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003597 return( ret );
3598 }
3599 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003600#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003601
3602 if( mbedtls_ssl_own_key( ssl ) == NULL )
3603 {
3604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3605 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3606 }
3607
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003608 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3609 * signature length which will be added in ssl_write_server_key_exchange
3610 * after the call to ssl_prepare_server_key_exchange.
3611 * ssl_write_server_key_exchange also takes care of incrementing
3612 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003613 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3614 md_alg, hash, hashlen,
3615 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003616 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003617 ssl->conf->f_rng,
3618 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003621 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003622 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003623 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003624#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003625
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003626 return( 0 );
3627}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003628
Gilles Peskined3eb0612018-01-08 17:07:44 +01003629/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003630 * that do not include a ServerKeyExchange message, do nothing. Either
3631 * way, if successful, move on to the next step in the SSL state
3632 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003633static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3634{
Janos Follath865b3eb2019-12-16 11:46:15 +00003635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003636 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003637#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003638 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003639 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003640#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003641
Gilles Peskined3eb0612018-01-08 17:07:44 +01003642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3643
Gilles Peskineeccd8882020-03-10 12:19:08 +01003644#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003645 /* Extract static ECDH parameters and abort if ServerKeyExchange
3646 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003647 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3648 {
3649 /* For suites involving ECDH, extract DH parameters
3650 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003651#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003652 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3653 {
3654 ssl_get_ecdh_params_from_cert( ssl );
3655 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003656#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003657
3658 /* Key exchanges not involving ephemeral keys don't use
3659 * ServerKeyExchange, so end here. */
3660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3661 ssl->state++;
3662 return( 0 );
3663 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003664#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003665
Gilles Peskineeccd8882020-03-10 12:19:08 +01003666#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003667 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003668 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003669 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003670 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003671 {
3672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3673 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003674 }
3675 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003676#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003677 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003678 {
3679 /* ServerKeyExchange is needed. Prepare the message. */
3680 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003681 }
3682
3683 if( ret != 0 )
3684 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003685 /* If we're starting to write a new message, set ssl->out_msglen
3686 * to 0. But if we're resuming after an asynchronous message,
3687 * out_msglen is the amount of data written so far and mst be
3688 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003689 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3691 else
3692 ssl->out_msglen = 0;
3693 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003694 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003695
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003696 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003697 * ssl_prepare_server_key_exchange already wrote the signature
3698 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003699#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003700 if( signature_len != 0 )
3701 {
3702 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3703 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3704
3705 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3706 ssl->out_msg + ssl->out_msglen,
3707 signature_len );
3708
3709 /* Skip over the already-written signature */
3710 ssl->out_msglen += signature_len;
3711 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003712#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003713
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003714 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3716 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
3718 ssl->state++;
3719
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003720 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003723 return( ret );
3724 }
3725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003728}
3729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731{
Janos Follath865b3eb2019-12-16 11:46:15 +00003732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003735
3736 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3738 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003739
3740 ssl->state++;
3741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003745#endif
3746
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003747 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003748 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 return( ret );
3751 }
3752
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003753#if defined(MBEDTLS_SSL_PROTO_DTLS)
3754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3755 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3756 {
3757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3758 return( ret );
3759 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003760#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003763
3764 return( 0 );
3765}
3766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3768 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3769static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003770 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003773 size_t n;
3774
3775 /*
3776 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3777 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003778 if( *p + 2 > end )
3779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3781 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003782 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003783
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003784 n = ( (*p)[0] << 8 ) | (*p)[1];
3785 *p += 2;
3786
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003787 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3790 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003791 }
3792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3796 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003797 }
3798
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003799 *p += n;
3800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003802
Paul Bakker70df2fb2013-04-17 17:19:09 +02003803 return( ret );
3804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3806 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3809 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003810
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003811#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003812static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3813 unsigned char *peer_pms,
3814 size_t *peer_pmslen,
3815 size_t peer_pmssize )
3816{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003817 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003818 peer_pms, peer_pmslen, peer_pmssize );
3819 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3820 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003821 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003822 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003823 }
3824 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3825 return( ret );
3826}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003827#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003828
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003829static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3830 const unsigned char *p,
3831 const unsigned char *end,
3832 unsigned char *peer_pms,
3833 size_t *peer_pmslen,
3834 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003835{
Janos Follath865b3eb2019-12-16 11:46:15 +00003836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003837 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3838 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3839 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003840
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003841#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003842 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003843 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003844 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003845 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3847 return( ssl_resume_decrypt_pms( ssl,
3848 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003849 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003850#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003851
3852 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003853 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3856 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3857 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003858 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003859 if ( p + 2 > end ) {
3860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3861 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3862 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003863 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3864 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3867 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003868 }
3869 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003870#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003871
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003872 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3875 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003876 }
3877
Gilles Peskine422ccab2018-01-11 18:29:01 +01003878 /*
3879 * Decrypt the premaster secret
3880 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003881#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003882 if( ssl->conf->f_async_decrypt_start != NULL )
3883 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003884 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003885 mbedtls_ssl_own_cert( ssl ),
3886 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003887 switch( ret )
3888 {
3889 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3890 /* act as if f_async_decrypt_start was null */
3891 break;
3892 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003893 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003894 return( ssl_resume_decrypt_pms( ssl,
3895 peer_pms,
3896 peer_pmslen,
3897 peer_pmssize ) );
3898 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003899 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003900 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3901 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003902 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003903 return( ret );
3904 }
3905 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003906#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003907
Gilles Peskine422ccab2018-01-11 18:29:01 +01003908 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3909 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3911 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3912 }
3913
3914 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003915 peer_pms, peer_pmslen, peer_pmssize,
3916 ssl->conf->f_rng, ssl->conf->p_rng );
3917 return( ret );
3918}
3919
3920static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3921 const unsigned char *p,
3922 const unsigned char *end,
3923 size_t pms_offset )
3924{
Janos Follath865b3eb2019-12-16 11:46:15 +00003925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003926 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3927 unsigned char ver[2];
3928 unsigned char fake_pms[48], peer_pms[48];
3929 unsigned char mask;
3930 size_t i, peer_pmslen;
3931 unsigned int diff;
3932
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003933 /* In case of a failure in decryption, the decryption may write less than
3934 * 2 bytes of output, but we always read the first two bytes. It doesn't
3935 * matter in the end because diff will be nonzero in that case due to
3936 * peer_pmslen being less than 48, and we only care whether diff is 0.
3937 * But do initialize peer_pms for robustness anyway. This also makes
3938 * memory analyzers happy (don't access uninitialized memory, even
3939 * if it's an unsigned char). */
3940 peer_pms[0] = peer_pms[1] = ~0;
3941
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003942 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3943 peer_pms,
3944 &peer_pmslen,
3945 sizeof( peer_pms ) );
3946
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003947#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003948 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3949 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003950#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003953 ssl->handshake->max_minor_ver,
3954 ssl->conf->transport, ver );
3955
3956 /* Avoid data-dependent branches while checking for invalid
3957 * padding, to protect against timing-based Bleichenbacher-type
3958 * attacks. */
3959 diff = (unsigned int) ret;
3960 diff |= peer_pmslen ^ 48;
3961 diff |= peer_pms[0] ^ ver[0];
3962 diff |= peer_pms[1] ^ ver[1];
3963
3964 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3965 /* MSVC has a warning about unary minus on unsigned, but this is
3966 * well-defined and precisely what we want to do here */
3967#if defined(_MSC_VER)
3968#pragma warning( push )
3969#pragma warning( disable : 4146 )
3970#endif
3971 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3972#if defined(_MSC_VER)
3973#pragma warning( pop )
3974#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003975
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003976 /*
3977 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3978 * must not cause the connection to end immediately; instead, send a
3979 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003980 * To protect against timing-based variants of the attack, we must
3981 * not have any branch that depends on whether the decryption was
3982 * successful. In particular, always generate the fake premaster secret,
3983 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003984 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003985 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003986 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003987 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003988 /* It's ok to abort on an RNG failure, since this does not reveal
3989 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003990 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003991 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003992
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003993#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003994 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003996#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003997
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003998 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3999 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
4000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004003 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004004 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004005
Gilles Peskine422ccab2018-01-11 18:29:01 +01004006 /* Set pms to either the true or the fake PMS, without
4007 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004008 for( i = 0; i < ssl->handshake->pmslen; i++ )
4009 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
4010
4011 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4014 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02004015
Gilles Peskineeccd8882020-03-10 12:19:08 +01004016#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004018 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004019{
Paul Bakker6db455e2013-09-18 17:29:31 +02004020 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03004021 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004022
Hanno Becker845b9462018-10-26 12:07:29 +01004023 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4026 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004027 }
4028
4029 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004030 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02004031 */
Hanno Becker83c9f492017-06-26 13:52:14 +01004032 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4035 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004036 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004037
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004038 n = ( (*p)[0] << 8 ) | (*p)[1];
4039 *p += 2;
4040
irwir6527bd62019-09-21 18:51:25 +03004041 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4044 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004045 }
4046
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004047 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004048 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004049 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004051 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004052 else
Paul Bakker6db455e2013-09-18 17:29:31 +02004053 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01004054 /* Identity is not a big secret since clients send it in the clear,
4055 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004056 if( n != ssl->conf->psk_identity_len ||
4057 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02004058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004060 }
4061 }
4062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02004066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4067 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004069 }
4070
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004071 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004072
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004073 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004074}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004075#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004078{
Janos Follath865b3eb2019-12-16 11:46:15 +00004079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004081 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004082
Hanno Beckere694c3e2017-12-27 21:34:08 +00004083 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004086
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004087#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004088 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4089 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4090 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4091 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004092 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004093 {
4094 /* We've already read a record and there is an asynchronous
4095 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004096 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4098 }
4099 else
4100#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004101 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 return( ret );
4105 }
4106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004108 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4113 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 }
4115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4119 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004120 }
4121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4123 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004125 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004128 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004129 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004130
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004131 if( p != end )
4132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4134 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004135 }
4136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004138 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004139 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004140 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004141 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4144 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004145 }
4146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004148 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004149 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4151#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4152 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4153 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4154 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4155 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4156 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4157 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4158 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004161 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4164 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004165 }
4166
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004167 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4168 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004171 &ssl->handshake->pmslen,
4172 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004174 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4177 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004178 }
4179
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004180 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4181 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004182 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004183 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4185 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4186 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4187 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4188#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4189 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004190 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004191 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004194 return( ret );
4195 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004196
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004197 if( p != end )
4198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4200 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004201 }
4202
Hanno Becker845b9462018-10-26 12:07:29 +01004203#if defined(MBEDTLS_USE_PSA_CRYPTO)
4204 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4205 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004206 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4208 else
4209#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004211 ciphersuite_info->key_exchange ) ) != 0 )
4212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004214 return( ret );
4215 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004217 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4219#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4220 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004221 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004222#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004223 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004224 {
4225 /* There is an asynchronous operation in progress to
4226 * decrypt the encrypted premaster secret, so skip
4227 * directly to resuming this operation. */
4228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4229 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4230 * won't actually use it, but maintain p anyway for robustness. */
4231 p += ssl->conf->psk_identity_len + 2;
4232 }
4233 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004234#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004235 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004238 return( ret );
4239 }
4240
Hanno Becker845b9462018-10-26 12:07:29 +01004241#if defined(MBEDTLS_USE_PSA_CRYPTO)
4242 /* Opaque PSKs are currently only supported for PSK-only. */
4243 if( ssl_use_opaque_psk( ssl ) == 1 )
4244 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4245#endif
4246
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004247 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004250 return( ret );
4251 }
4252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004254 ciphersuite_info->key_exchange ) ) != 0 )
4255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004257 return( ret );
4258 }
4259 }
4260 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4262#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4263 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004264 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004265 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004268 return( ret );
4269 }
4270 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004273 return( ret );
4274 }
4275
Hanno Becker845b9462018-10-26 12:07:29 +01004276#if defined(MBEDTLS_USE_PSA_CRYPTO)
4277 /* Opaque PSKs are currently only supported for PSK-only. */
4278 if( ssl_use_opaque_psk( ssl ) == 1 )
4279 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4280#endif
4281
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004282 if( p != end )
4283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4285 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004286 }
4287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004289 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004292 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004293 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004294 }
4295 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4297#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4298 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004299 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004300 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004303 return( ret );
4304 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004307 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4310 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004311 }
4312
Hanno Becker845b9462018-10-26 12:07:29 +01004313#if defined(MBEDTLS_USE_PSA_CRYPTO)
4314 /* Opaque PSKs are currently only supported for PSK-only. */
4315 if( ssl_use_opaque_psk( ssl ) == 1 )
4316 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4317#endif
4318
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004319 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4320 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004323 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004326 return( ret );
4327 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004328 }
4329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4331#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4332 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004333 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004334 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004337 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 }
4339 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004340 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004342#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4343 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4344 {
4345 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4346 p, end - p );
4347 if( ret != 0 )
4348 {
4349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4350 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4351 }
4352
4353 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4354 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4355 ssl->conf->f_rng, ssl->conf->p_rng );
4356 if( ret != 0 )
4357 {
4358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4359 return( ret );
4360 }
4361 }
4362 else
4363#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004372 return( ret );
4373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004374
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 ssl->state++;
4376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004378
4379 return( 0 );
4380}
4381
Gilles Peskineeccd8882020-03-10 12:19:08 +01004382#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004384{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004385 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004386 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004389
Hanno Becker77adddc2019-02-07 12:32:43 +00004390 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004393 ssl->state++;
4394 return( 0 );
4395 }
4396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004399}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004400#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004404 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004405 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004406 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004407 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4409 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004412 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004413 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004414 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004417
Hanno Becker2a831a42019-02-07 13:17:25 +00004418 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004421 ssl->state++;
4422 return( 0 );
4423 }
4424
Hanno Becker2a831a42019-02-07 13:17:25 +00004425#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4426 if( ssl->session_negotiate->peer_cert == NULL )
4427 {
4428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4429 ssl->state++;
4430 return( 0 );
4431 }
4432#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4433 if( ssl->session_negotiate->peer_cert_digest == NULL )
4434 {
4435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4436 ssl->state++;
4437 return( 0 );
4438 }
4439#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4440
Simon Butcher99000142016-10-13 17:21:01 +01004441 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004442 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004443 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004445 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 return( ret );
4447 }
4448
4449 ssl->state++;
4450
Simon Butcher99000142016-10-13 17:21:01 +01004451 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4453 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4456 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004457 }
4458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004460
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004461#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4462 peer_pk = &ssl->handshake->peer_pubkey;
4463#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4464 if( ssl->session_negotiate->peer_cert == NULL )
4465 {
4466 /* Should never happen */
4467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4468 }
4469 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4470#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4471
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004472 /*
4473 * struct {
4474 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4475 * opaque signature<0..2^16-1>;
4476 * } DigitallySigned;
4477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4479 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4480 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004483 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004484
4485 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004486 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004487 {
4488 hash_start += 16;
4489 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004491 }
Paul Bakker926af752012-11-23 13:38:07 +01004492 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4495 MBEDTLS_SSL_PROTO_TLS1_1 */
4496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4497 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004498 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004499 if( i + 2 > ssl->in_hslen )
4500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4502 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004503 }
4504
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004506 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004507 */
Simon Butcher99000142016-10-13 17:21:01 +01004508 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4509
4510 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004513 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004515 }
4516
Simon Butcher99000142016-10-13 17:21:01 +01004517#if !defined(MBEDTLS_MD_SHA1)
4518 if( MBEDTLS_MD_SHA1 == md_alg )
4519 hash_start += 16;
4520#endif
Paul Bakker926af752012-11-23 13:38:07 +01004521
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004522 /* Info from md_alg will be used instead */
4523 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004524
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004525 i++;
4526
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004527 /*
4528 * Signature
4529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4531 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004534 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004536 }
4537
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004538 /*
4539 * Check the certificate's key type matches the signature alg
4540 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004541 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
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, ( "sig_alg doesn't match cert key" ) );
4544 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004545 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004546
4547 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004548 }
4549 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004554 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004555
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004556 if( i + 2 > ssl->in_hslen )
4557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4559 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004560 }
4561
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004562 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4563 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004564
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004565 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4568 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 }
4570
Simon Butcher99000142016-10-13 17:21:01 +01004571 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004572 {
4573 size_t dummy_hlen;
4574 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4575 }
Simon Butcher99000142016-10-13 17:21:01 +01004576
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004577 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004578 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004579 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004582 return( ret );
4583 }
4584
Simon Butcher99000142016-10-13 17:21:01 +01004585 mbedtls_ssl_update_handshake_status( ssl );
4586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004588
Paul Bakkered27a042013-04-18 22:46:23 +02004589 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004590}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004591#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4594static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004595{
Janos Follath865b3eb2019-12-16 11:46:15 +00004596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004597 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004598 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4603 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004604
4605 /*
4606 * struct {
4607 * uint32 ticket_lifetime_hint;
4608 * opaque ticket<0..2^16-1>;
4609 * } NewSessionTicket;
4610 *
4611 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4612 * 8 . 9 ticket_len (n)
4613 * 10 . 9+n ticket content
4614 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004615
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004616 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004617 ssl->session_negotiate,
4618 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004619 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004620 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004621 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004623 tlen = 0;
4624 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004625
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004626 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4627 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4628 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4629 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4630
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004631 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4632 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004633
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004634 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004635
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004636 /*
4637 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4638 * ChangeCipherSpec share the same state.
4639 */
4640 ssl->handshake->new_session_ticket = 0;
4641
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004642 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004643 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004645 return( ret );
4646 }
4647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004649
4650 return( 0 );
4651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004653
Paul Bakker5121ce52009-01-03 21:22:43 +00004654/*
Paul Bakker1961b702013-01-25 14:49:24 +01004655 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004658{
4659 int ret = 0;
4660
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004661 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004667 return( ret );
4668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004670 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004672 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004673 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004674 return( ret );
4675 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004676#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004677
Paul Bakker1961b702013-01-25 14:49:24 +01004678 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 case MBEDTLS_SSL_HELLO_REQUEST:
4681 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 break;
4683
Paul Bakker1961b702013-01-25 14:49:24 +01004684 /*
4685 * <== ClientHello
4686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004688 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004689 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691#if defined(MBEDTLS_SSL_PROTO_DTLS)
4692 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4693 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004694#endif
4695
Paul Bakker1961b702013-01-25 14:49:24 +01004696 /*
4697 * ==> ServerHello
4698 * Certificate
4699 * ( ServerKeyExchange )
4700 * ( CertificateRequest )
4701 * ServerHelloDone
4702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004704 ret = ssl_write_server_hello( ssl );
4705 break;
4706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4708 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004709 break;
4710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004712 ret = ssl_write_server_key_exchange( ssl );
4713 break;
4714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004716 ret = ssl_write_certificate_request( ssl );
4717 break;
4718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004720 ret = ssl_write_server_hello_done( ssl );
4721 break;
4722
4723 /*
4724 * <== ( Certificate/Alert )
4725 * ClientKeyExchange
4726 * ( CertificateVerify )
4727 * ChangeCipherSpec
4728 * Finished
4729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4731 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004732 break;
4733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004735 ret = ssl_parse_client_key_exchange( ssl );
4736 break;
4737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004739 ret = ssl_parse_certificate_verify( ssl );
4740 break;
4741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4743 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004744 break;
4745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 case MBEDTLS_SSL_CLIENT_FINISHED:
4747 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004748 break;
4749
4750 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004751 * ==> ( NewSessionTicket )
4752 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004753 * Finished
4754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4756#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004757 if( ssl->handshake->new_session_ticket != 0 )
4758 ret = ssl_write_new_session_ticket( ssl );
4759 else
Paul Bakkera503a632013-08-14 13:48:06 +02004760#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004762 break;
4763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 case MBEDTLS_SSL_SERVER_FINISHED:
4765 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004766 break;
4767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768 case MBEDTLS_SSL_FLUSH_BUFFERS:
4769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4770 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004771 break;
4772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4774 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004775 break;
4776
4777 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 }
4781
Paul Bakker5121ce52009-01-03 21:22:43 +00004782 return( ret );
4783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784#endif /* MBEDTLS_SSL_SRV_C */