blob: ee2ae89bca3c97731399e33f702b417ac8064266 [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,
781 const unsigned char *buf, size_t len )
782{
783 enum mbedtls_DTLS_SRTP_protection_profiles client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
784 size_t i,j;
785 uint16_t profile_length;
786
787 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalbbc057a2016-02-04 22:07:32 +0100788 if( ( ssl->conf->dtls_srtp_profiles_list == NULL ) || ( ssl->conf->dtls_srtp_profiles_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100789 return( 0 );
790
791 /* RFC5764 section 4.1.1
792 * uint8 SRTPProtectionProfile[2];
793 *
794 * struct {
795 * SRTPProtectionProfiles SRTPProtectionProfiles;
796 * opaque srtp_mki<0..255>;
797 * } UseSRTPData;
798
799 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
800 *
801 * Note: srtp_mki is not supported
802 */
803
804 /* Min length is 5 : at least one protection profile(2 bytes) and length(2 bytes) + srtp_mki length(1 byte) */
805 if( len < 5 )
806 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
807
808 /*
809 * Use our order of preference
810 */
811 profile_length = buf[0]<<8|buf[1]; /* first 2 bytes are protection profile length(in bytes) */
Johan Pascalbbc057a2016-02-04 22:07:32 +0100812 for( i=0; i < ssl->conf->dtls_srtp_profiles_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +0100813 {
814 /* parse the extension list values are defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml */
815 for (j=0; j<profile_length; j+=2) { /* parse only the protection profile, srtp_mki is not supported and ignored */
816 uint16_t protection_profile_value = buf[j+2]<<8 | buf[j+3]; /* +2 to skip the length field */
817
818 switch ( protection_profile_value ) {
819 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE:
820 client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80;
821 break;
822 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE:
823 client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32;
824 break;
825 case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE:
826 client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80;
827 break;
828 case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE:
829 client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32;
830 break;
831 default:
832 client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
833 break;
834 }
835
Johan Pascalbbc057a2016-02-04 22:07:32 +0100836 if (client_protection == ssl->conf->dtls_srtp_profiles_list[i]) {
837 ssl->chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profiles_list[i];
Johan Pascalb62bb512015-12-03 21:56:45 +0100838 return 0;
839 }
840 }
841 }
842
843 /* If we get there, no match was found */
844 ssl->chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
845 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
846 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
847 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
848}
849#endif /* MBEDTLS_SSL_DTLS_SRTP */
850
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100851/*
852 * Auxiliary functions for ServerHello parsing and related actions
853 */
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100856/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100857 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_ECDSA_C)
860static int ssl_check_key_curve( mbedtls_pk_context *pk,
861 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 const mbedtls_ecp_curve_info **crv = curves;
864 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100865
866 while( *crv != NULL )
867 {
868 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100869 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100870 crv++;
871 }
872
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100873 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100876
877/*
878 * Try picking a certificate for this ciphersuite,
879 * return 0 on success and -1 on failure.
880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881static int ssl_pick_cert( mbedtls_ssl_context *ssl,
882 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100885 mbedtls_pk_type_t pk_alg =
886 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200887 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100890 if( ssl->handshake->sni_key_cert != NULL )
891 list = ssl->handshake->sni_key_cert;
892 else
893#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200894 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100897 return( 0 );
898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000900
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200901 if( list == NULL )
902 {
903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
904 return( -1 );
905 }
906
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100907 for( cur = list; cur != NULL; cur = cur->next )
908 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400909 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000911 cur->cert );
912
Gilles Peskinee198df52018-01-05 21:17:45 +0100913 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100916 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000917 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100918
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200919 /*
920 * This avoids sending the client a cert it'll reject based on
921 * keyUsage or other extensions.
922 *
923 * It also allows the user to provision different certificates for
924 * different uses based on keyUsage, eg if they want to avoid signing
925 * and decrypting with the same RSA key.
926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100928 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000931 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200932 continue;
933 }
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#if defined(MBEDTLS_ECDSA_C)
936 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200937 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100940 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000941 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100942#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100943
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100944 /*
945 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
946 * present them a SHA-higher cert rather than failing if it's the only
947 * one we got that satisfies the other conditions.
948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
950 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100951 {
952 if( fallback == NULL )
953 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000956 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100957 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000958 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100959 }
960
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100961 /* If we get there, we got a winner */
962 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100963 }
964
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000965 if( cur == NULL )
966 cur = fallback;
967
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200968 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100969 if( cur != NULL )
970 {
971 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000973 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100974 return( 0 );
975 }
976
977 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100980
981/*
982 * Check if a given ciphersuite is suitable for use with our config/keys/etc
983 * Sets ciphersuite_info only if the suite matches.
984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
986 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100987{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100989
Hanno Becker7e5437a2017-04-28 17:15:26 +0100990#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100991 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100992 mbedtls_pk_type_t sig_type;
993#endif
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100996 if( suite_info == NULL )
997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001000 }
1001
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001002 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Hanno Beckerecea07d2018-11-07 16:24:35 +00001003 suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001004
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001005 if( suite_info->min_minor_ver > ssl->minor_ver ||
1006 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001009 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001010 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001013 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001015 return( 0 );
1016#endif
1017
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001018#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001019 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001023 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001024 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001025#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001026
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001027#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1028 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001029 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001030 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001031 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1032 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001033 return( 0 );
1034 }
1035#endif
1036
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1039 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001040 ( ssl->handshake->curves == NULL ||
1041 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001044 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001045 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001046 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001047#endif
1048
Gilles Peskineeccd8882020-03-10 12:19:08 +01001049#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001050 /* If the ciphersuite requires a pre-shared key and we don't
1051 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001053 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
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: no pre-shared key" ) );
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#endif
1059
Hanno Becker7e5437a2017-04-28 17:15:26 +01001060#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001061 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001062 /* If the ciphersuite requires signing, check whether
1063 * a suitable hash algorithm is present. */
1064 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1065 {
1066 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1067 if( sig_type != MBEDTLS_PK_NONE &&
1068 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1069 {
1070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1071 "for signature algorithm %d", sig_type ) );
1072 return( 0 );
1073 }
1074 }
1075
1076#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001077 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001080 /*
1081 * Final check: if ciphersuite requires us to have a
1082 * certificate/key of a particular type:
1083 * - select the appropriate certificate if we have one, or
1084 * - try the next ciphersuite if we don't
1085 * This must be done last since we modify the key_cert list.
1086 */
1087 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001090 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001091 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001092 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001093#endif
1094
1095 *ciphersuite_info = suite_info;
1096 return( 0 );
1097}
1098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1100static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001101{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001102 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001103 unsigned int i, j;
1104 size_t n;
1105 unsigned int ciph_len, sess_len, chal_len;
1106 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001107 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_SSL_RENEGOTIATION)
1113 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001116 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1117 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001119 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001121
1122 buf = ssl->in_hdr;
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001127 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001129 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001131 buf[3], buf[4] ) );
1132
1133 /*
1134 * SSLv2 Client Hello
1135 *
1136 * Record layer:
1137 * 0 . 1 message length
1138 *
1139 * SSL layer:
1140 * 2 . 2 message type
1141 * 3 . 4 protocol version
1142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1144 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1147 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001148 }
1149
1150 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1151
1152 if( n < 17 || n > 512 )
1153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1155 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001156 }
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001159 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1160 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001161
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001162 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001165 " [%d:%d] < [%d:%d]",
1166 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001167 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1170 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1171 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001172 }
1173
Paul Bakker2fbefde2013-06-29 16:01:15 +02001174 ssl->handshake->max_major_ver = buf[3];
1175 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001180 return( ret );
1181 }
1182
1183 ssl->handshake->update_checksum( ssl, buf + 2, n );
1184
1185 buf = ssl->in_msg;
1186 n = ssl->in_left - 5;
1187
1188 /*
1189 * 0 . 1 ciphersuitelist length
1190 * 2 . 3 session id length
1191 * 4 . 5 challenge length
1192 * 6 . .. ciphersuitelist
1193 * .. . .. session id
1194 * .. . .. challenge
1195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001197
1198 ciph_len = ( buf[0] << 8 ) | buf[1];
1199 sess_len = ( buf[2] << 8 ) | buf[3];
1200 chal_len = ( buf[4] << 8 ) | buf[5];
1201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001203 ciph_len, sess_len, chal_len ) );
1204
1205 /*
1206 * Make sure each parameter length is valid
1207 */
1208 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1211 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001212 }
1213
1214 if( sess_len > 32 )
1215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1217 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001218 }
1219
1220 if( chal_len < 8 || chal_len > 32 )
1221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1223 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001224 }
1225
1226 if( n != 6 + ciph_len + sess_len + chal_len )
1227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1229 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001230 }
1231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001233 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001235 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001237 buf + 6 + ciph_len + sess_len, chal_len );
1238
1239 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001240 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001241 memset( ssl->session_negotiate->id, 0,
1242 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001243 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001244
1245 p += sess_len;
1246 memset( ssl->handshake->randbytes, 0, 64 );
1247 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1248
1249 /*
1250 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1251 */
1252 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1257#if defined(MBEDTLS_SSL_RENEGOTIATION)
1258 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001261 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001262
Gilles Peskinec94f7352017-05-10 16:37:56 +02001263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1264 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_SSL_RENEGOTIATION */
1268 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001269 break;
1270 }
1271 }
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001274 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1275 {
1276 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1278 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001281
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001282 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1287 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001290 }
1291
1292 break;
1293 }
1294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001296
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001297 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001298 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001299 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001301 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001302 for( i = 0; ciphersuites[i] != 0; i++ )
1303#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001304 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001305 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001306#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001307 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001308 if( p[0] != 0 ||
1309 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1310 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1311 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001312
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001313 got_common_suite = 1;
1314
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001315 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1316 &ciphersuite_info ) ) != 0 )
1317 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001318
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001319 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001320 goto have_ciphersuite_v2;
1321 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001322
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001323 if( got_common_suite )
1324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001326 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001328 }
1329 else
1330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1332 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001333 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001334
1335have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001337
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001338 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001339 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001340
1341 /*
1342 * SSLv2 Client Hello relevant renegotiation security checks
1343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001345 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001348 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1349 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001351 }
1352
1353 ssl->in_left = 0;
1354 ssl->state++;
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001357
1358 return( 0 );
1359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001361
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001362/* This function doesn't alert on errors that happen early during
1363 ClientHello parsing because they might indicate that the client is
1364 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001367 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001368 size_t i, j;
1369 size_t ciph_offset, comp_offset, ext_offset;
1370 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001372 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001373#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001374 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001376 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001377#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001378 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001379 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001381 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Hanno Becker7e5437a2017-04-28 17:15:26 +01001383 /* If there is no signature-algorithm extension present,
1384 * we need to fall back to the default values for allowed
1385 * signature-hash pairs. */
1386#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001387 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001388 int sig_hash_alg_ext_present = 0;
1389#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001390 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001395read_record_header:
1396#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001397 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001399 * otherwise read it ourselves manually in order to support SSLv2
1400 * ClientHello, which doesn't use the same record layer format.
1401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_SSL_RENEGOTIATION)
1403 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001407 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001408 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001410 return( ret );
1411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 }
1413
1414 buf = ssl->in_hdr;
1415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001419#endif
1420 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001421 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001422#endif
1423
Hanno Becker5903de42019-05-03 14:46:38 +01001424 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001425
Paul Bakkerec636f32012-09-09 19:17:02 +00001426 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001427 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001428 *
1429 * Record layer:
1430 * 0 . 0 message type
1431 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001432 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001433 * 3 . 4 message length
1434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001436 buf[0] ) );
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1441 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001442 }
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001445 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001448 buf[1], buf[2] ) );
1449
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001450 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001451
1452 /* According to RFC 5246 Appendix E.1, the version here is typically
1453 * "{03,00}, the lowest version number supported by the client, [or] the
1454 * value of ClientHello.client_version", so the only meaningful check here
1455 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1459 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001462 /* For DTLS if this is the initial handshake, remember the client sequence
1463 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_SSL_RENEGOTIATION)
1467 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001468#endif
1469 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001470 {
1471 /* Epoch should be 0 for initial handshakes */
1472 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1475 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001476 }
1477
Hanno Becker19859472018-08-06 09:40:20 +01001478 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1481 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001484 ssl->next_record_offset = 0;
1485 ssl->in_left = 0;
1486 goto read_record_header;
1487 }
1488
1489 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001491#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001494
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001495 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_SSL_RENEGOTIATION)
1498 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001501 msg_len = ssl->in_hslen;
1502 }
1503 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001504#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001505 {
Angus Grattond8213d02016-05-25 20:56:48 +10001506 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1509 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001510 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001511
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001512 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001513 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001516 return( ret );
1517 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001518
1519 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001521 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001522 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001523 else
1524#endif
1525 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001526 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001527
1528 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001531
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001532 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001533
1534 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001535 * Handshake layer:
1536 * 0 . 0 handshake type
1537 * 1 . 3 handshake length
1538 * 4 . 5 DTLS only: message seqence number
1539 * 6 . 8 DTLS only: fragment offset
1540 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1545 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001546 }
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1553 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001554 }
1555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001557 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1558
1559 /* We don't support fragmentation of ClientHello (yet?) */
1560 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1564 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001565 }
1566
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 )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001569 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001570 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001571 * Copy the client's handshake message_seq on initial handshakes,
1572 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_SSL_RENEGOTIATION)
1575 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001576 {
1577 /* This couldn't be done in ssl_prepare_handshake_record() */
1578 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1579 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001580
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001581 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001584 "%d (expected %d)", cli_msg_seq,
1585 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001587 }
1588
1589 ssl->handshake->in_msg_seq++;
1590 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001591 else
1592#endif
1593 {
1594 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1595 ssl->in_msg[5];
1596 ssl->handshake->out_msg_seq = cli_msg_seq;
1597 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1598 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001599
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001600 /*
1601 * For now we don't support fragmentation, so make sure
1602 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001603 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001604 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1605 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1608 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001609 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 buf += mbedtls_ssl_hs_hdr_len( ssl );
1614 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001615
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001616 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001617 * ClientHello layer:
1618 * 0 . 1 protocol version
1619 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1620 * 34 . 35 session id length (1 byte)
1621 * 35 . 34+x session id
1622 * 35+x . 35+x DTLS only: cookie length (1 byte)
1623 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001624 * .. . .. ciphersuite list length (2 bytes)
1625 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001626 * .. . .. compression alg. list length (1 byte)
1627 * .. . .. compression alg. list
1628 * .. . .. extensions length (2 bytes, optional)
1629 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001630 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001631
1632 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001633 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001634 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1635 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001636 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001637 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1640 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001641 }
1642
1643 /*
1644 * Check and save the protocol version
1645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001649 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001650
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001651 ssl->handshake->max_major_ver = ssl->major_ver;
1652 ssl->handshake->max_minor_ver = ssl->minor_ver;
1653
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001654 if( ssl->major_ver < ssl->conf->min_major_ver ||
1655 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001658 " [%d:%d] < [%d:%d]",
1659 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001660 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1662 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001664 }
1665
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001666 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001667 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001668 ssl->major_ver = ssl->conf->max_major_ver;
1669 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001670 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001671 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1672 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001673
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001674 /*
1675 * Save client random (inc. Unix time)
1676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001678
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001679 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001680
1681 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001682 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001683 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001684 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001685
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001686 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001687 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001690 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1691 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001693 }
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001696
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001697 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001698 memset( ssl->session_negotiate->id, 0,
1699 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001700 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001701 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001702
1703 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001704 * Check the cookie length and content
1705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001707 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001708 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001709 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001710 cookie_len = buf[cookie_offset];
1711
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001712 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001715 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1716 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001718 }
1719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001721 buf + cookie_offset + 1, cookie_len );
1722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001724 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_SSL_RENEGOTIATION)
1726 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001727#endif
1728 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001729 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001730 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001731 buf + cookie_offset + 1, cookie_len,
1732 ssl->cli_id, ssl->cli_id_len ) != 0 )
1733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001735 ssl->handshake->verify_cookie_len = 1;
1736 }
1737 else
1738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001740 ssl->handshake->verify_cookie_len = 0;
1741 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001742 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001743 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001745 {
1746 /* We know we didn't send a cookie, so it should be empty */
1747 if( cookie_len != 0 )
1748 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001749 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1751 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001752 }
1753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001755 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001756
1757 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001758 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001759 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001760 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001761 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001764 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001765
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001766 ciph_len = ( buf[ciph_offset + 0] << 8 )
1767 | ( buf[ciph_offset + 1] );
1768
1769 if( ciph_len < 2 ||
1770 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1771 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1775 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001777 }
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001780 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001781
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001782 /*
1783 * Check the compression algorithms length and pick one
1784 */
1785 comp_offset = ciph_offset + 2 + ciph_len;
1786
1787 comp_len = buf[comp_offset];
1788
1789 if( comp_len < 1 ||
1790 comp_len > 16 ||
1791 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001794 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1795 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001797 }
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001800 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1803#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001804 for( i = 0; i < comp_len; ++i )
1805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001809 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 }
1811 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001812#endif
1813
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001814 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001816 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001818#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001819
Janos Follathc6dab2b2016-05-23 14:27:02 +01001820 /* Do not parse the extensions if the protocol is SSLv3 */
1821#if defined(MBEDTLS_SSL_PROTO_SSL3)
1822 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1823 {
1824#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001825 /*
1826 * Check the extension length
1827 */
1828 ext_offset = comp_offset + 1 + comp_len;
1829 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001830 {
Simon Butcher584a5472016-05-23 16:24:52 +01001831 if( msg_len < ext_offset + 2 )
1832 {
1833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1835 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001836 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1837 }
1838
1839 ext_len = ( buf[ext_offset + 0] << 8 )
1840 | ( buf[ext_offset + 1] );
1841
1842 if( ( ext_len > 0 && ext_len < 4 ) ||
1843 msg_len != ext_offset + 2 + ext_len )
1844 {
1845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001846 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1847 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001848 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1849 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001850 }
Simon Butcher584a5472016-05-23 16:24:52 +01001851 else
1852 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001853
Simon Butcher584a5472016-05-23 16:24:52 +01001854 ext = buf + ext_offset + 2;
1855 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001856
Simon Butcher584a5472016-05-23 16:24:52 +01001857 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001858 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001859 unsigned int ext_id;
1860 unsigned int ext_size;
1861 if ( ext_len < 4 ) {
1862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1863 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1864 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1865 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1866 }
1867 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1868 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001869
Simon Butcher584a5472016-05-23 16:24:52 +01001870 if( ext_size + 4 > ext_len )
1871 {
1872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001873 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1874 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001875 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1876 }
1877 switch( ext_id )
1878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001880 case MBEDTLS_TLS_EXT_SERVERNAME:
1881 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1882 if( ssl->conf->f_sni == NULL )
1883 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001884
Simon Butcher584a5472016-05-23 16:24:52 +01001885 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1886 if( ret != 0 )
1887 return( ret );
1888 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001890
Simon Butcher584a5472016-05-23 16:24:52 +01001891 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001894 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001895#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001896
Simon Butcher584a5472016-05-23 16:24:52 +01001897 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1898 if( ret != 0 )
1899 return( ret );
1900 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001903 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001904 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1906
Simon Butcher584a5472016-05-23 16:24:52 +01001907 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1908 if( ret != 0 )
1909 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001910
1911 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001912 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001914 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001915
Robert Cragie136884c2015-10-02 13:34:31 +01001916#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001917 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001918 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001920
Simon Butcher584a5472016-05-23 16:24:52 +01001921 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1922 if( ret != 0 )
1923 return( ret );
1924 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001925
Simon Butcher584a5472016-05-23 16:24:52 +01001926 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1928 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001929
Simon Butcher584a5472016-05-23 16:24:52 +01001930 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1931 if( ret != 0 )
1932 return( ret );
1933 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001934#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1935 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001936
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001937#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001938 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001940
Simon Butcher584a5472016-05-23 16:24:52 +01001941 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1942 if( ret != 0 )
1943 return( ret );
1944 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001945#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001948 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001950
Simon Butcher584a5472016-05-23 16:24:52 +01001951 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1952 if( ret != 0 )
1953 return( ret );
1954 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001958 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001960
Simon Butcher584a5472016-05-23 16:24:52 +01001961 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1962 if( ret != 0 )
1963 return( ret );
1964 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001966
Hanno Beckera0e20d02019-05-15 14:03:01 +01001967#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001968 case MBEDTLS_TLS_EXT_CID:
1969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1970
1971 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1972 if( ret != 0 )
1973 return( ret );
1974 break;
1975#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001978 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001980
Simon Butcher584a5472016-05-23 16:24:52 +01001981 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1982 if( ret != 0 )
1983 return( ret );
1984 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001988 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001990
Simon Butcher584a5472016-05-23 16:24:52 +01001991 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1992 if( ret != 0 )
1993 return( ret );
1994 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001998 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002000
Simon Butcher584a5472016-05-23 16:24:52 +01002001 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2002 if( ret != 0 )
2003 return( ret );
2004 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002008 case MBEDTLS_TLS_EXT_ALPN:
2009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002010
Simon Butcher584a5472016-05-23 16:24:52 +01002011 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2012 if( ret != 0 )
2013 return( ret );
2014 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002016
Johan Pascalb62bb512015-12-03 21:56:45 +01002017#if defined(MBEDTLS_SSL_DTLS_SRTP)
2018 case MBEDTLS_TLS_EXT_USE_SRTP:
2019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2020 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2021 if ( ret != 0 )
2022 return( ret );
2023 break;
2024#endif /* MBEDTLS_SSL_DTLS_SRTP */
2025
Simon Butcher584a5472016-05-23 16:24:52 +01002026 default:
2027 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
2028 ext_id ) );
2029 }
2030
2031 ext_len -= 4 + ext_size;
2032 ext += 4 + ext_size;
2033
2034 if( ext_len > 0 && ext_len < 4 )
2035 {
2036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002037 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2038 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01002039 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2040 }
Paul Bakker48916f92012-09-16 19:57:18 +00002041 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002042#if defined(MBEDTLS_SSL_PROTO_SSL3)
2043 }
2044#endif
2045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002047 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
2050 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002051 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002053
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002054 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002055 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2059 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002062 }
2063
2064 break;
2065 }
2066 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002068
Hanno Becker7e5437a2017-04-28 17:15:26 +01002069#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002070 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002071
2072 /*
2073 * Try to fall back to default hash SHA1 if the client
2074 * hasn't provided any preferred signature-hash combinations.
2075 */
2076 if( sig_hash_alg_ext_present == 0 )
2077 {
2078 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2079
2080 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2081 md_default = MBEDTLS_MD_NONE;
2082
2083 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2084 }
2085
2086#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002087 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002088
Paul Bakker48916f92012-09-16 19:57:18 +00002089 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002090 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2091 */
2092 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2097#if defined(MBEDTLS_SSL_RENEGOTIATION)
2098 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002099 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2101 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2103 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002105 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002108 break;
2109 }
2110 }
2111
2112 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002113 * Renegotiation security checks
2114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002116 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002119 handshake_failure = 1;
2120 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#if defined(MBEDTLS_SSL_RENEGOTIATION)
2122 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2123 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002124 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002127 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002128 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2130 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002131 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002134 handshake_failure = 1;
2135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2137 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002138 renegotiation_info_seen == 1 )
2139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002141 handshake_failure = 1;
2142 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002144
2145 if( handshake_failure == 1 )
2146 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002147 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2148 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002150 }
Paul Bakker380da532012-04-18 16:10:25 +00002151
Paul Bakker41c83d32013-03-20 14:39:14 +01002152 /*
2153 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002154 * (At the end because we need information from the EC-based extensions
2155 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002156 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002157 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002158 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002159 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002161 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002162 for( i = 0; ciphersuites[i] != 0; i++ )
2163#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002164 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002165 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002166#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002167 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002168 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2169 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2170 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002171
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002172 got_common_suite = 1;
2173
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002174 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2175 &ciphersuite_info ) ) != 0 )
2176 return( ret );
2177
2178 if( ciphersuite_info != NULL )
2179 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002180 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002181
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002182 if( got_common_suite )
2183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002185 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002186 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2187 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002189 }
2190 else
2191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002193 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2194 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002196 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002197
2198have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002200
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002201 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002202 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002203
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 ssl->state++;
2205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002209#endif
2210
Hanno Becker7e5437a2017-04-28 17:15:26 +01002211 /* Debugging-only output for testsuite */
2212#if defined(MBEDTLS_DEBUG_C) && \
2213 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002214 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002215 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2216 {
2217 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2218 if( sig_alg != MBEDTLS_PK_NONE )
2219 {
2220 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2221 sig_alg );
2222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2223 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2224 }
2225 else
2226 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2228 "%d - should not happen", sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002229 }
2230 }
2231#endif
2232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
2235 return( 0 );
2236}
2237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2239static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002240 unsigned char *buf,
2241 size_t *olen )
2242{
2243 unsigned char *p = buf;
2244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002246 {
2247 *olen = 0;
2248 return;
2249 }
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2254 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002255
2256 *p++ = 0x00;
2257 *p++ = 0x00;
2258
2259 *olen = 4;
2260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002262
Hanno Beckera0e20d02019-05-15 14:03:01 +01002263#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002264static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2265 unsigned char *buf,
2266 size_t *olen )
2267{
2268 unsigned char *p = buf;
2269 size_t ext_len;
2270 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2271
2272 *olen = 0;
2273
2274 /* Skip writing the extension if we don't want to use it or if
2275 * the client hasn't offered it. */
2276 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2277 return;
2278
2279 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2280 * which is at most 255, so the increment cannot overflow. */
2281 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2282 {
2283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2284 return;
2285 }
2286
2287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2288
2289 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002290 * Quoting draft-ietf-tls-dtls-connection-id-05
2291 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002292 *
2293 * struct {
2294 * opaque cid<0..2^8-1>;
2295 * } ConnectionId;
2296 */
2297
2298 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2299 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2300 ext_len = (size_t) ssl->own_cid_len + 1;
2301 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2302 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2303
2304 *p++ = (uint8_t) ssl->own_cid_len;
2305 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2306
2307 *olen = ssl->own_cid_len + 5;
2308}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002309#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2312static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002313 unsigned char *buf,
2314 size_t *olen )
2315{
2316 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2318 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002319
Hanno Becker27b34d52017-10-20 14:24:51 +01002320 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002322 {
2323 *olen = 0;
2324 return;
2325 }
2326
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002327 /*
2328 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2329 * from a client and then selects a stream or Authenticated Encryption
2330 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2331 * encrypt-then-MAC response extension back to the client."
2332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002334 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2336 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002337 {
2338 *olen = 0;
2339 return;
2340 }
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2345 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002346
2347 *p++ = 0x00;
2348 *p++ = 0x00;
2349
2350 *olen = 4;
2351}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2355static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002356 unsigned char *buf,
2357 size_t *olen )
2358{
2359 unsigned char *p = buf;
2360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2362 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002363 {
2364 *olen = 0;
2365 return;
2366 }
2367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002369 "extension" ) );
2370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2372 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002373
2374 *p++ = 0x00;
2375 *p++ = 0x00;
2376
2377 *olen = 4;
2378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2382static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002383 unsigned char *buf,
2384 size_t *olen )
2385{
2386 unsigned char *p = buf;
2387
2388 if( ssl->handshake->new_session_ticket == 0 )
2389 {
2390 *olen = 0;
2391 return;
2392 }
2393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2397 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002398
2399 *p++ = 0x00;
2400 *p++ = 0x00;
2401
2402 *olen = 4;
2403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +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->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002413 {
2414 *olen = 0;
2415 return;
2416 }
2417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2421 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#if defined(MBEDTLS_SSL_RENEGOTIATION)
2424 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002425 {
2426 *p++ = 0x00;
2427 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2428 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002429
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002430 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2431 p += ssl->verify_data_len;
2432 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2433 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002434 }
2435 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002437 {
2438 *p++ = 0x00;
2439 *p++ = 0x01;
2440 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002441 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002442
2443 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002444}
2445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2447static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002448 unsigned char *buf,
2449 size_t *olen )
2450{
2451 unsigned char *p = buf;
2452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002454 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002455 *olen = 0;
2456 return;
2457 }
2458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2462 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002463
2464 *p++ = 0x00;
2465 *p++ = 1;
2466
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002467 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002468
2469 *olen = 5;
2470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002472
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002473#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002474 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002476 unsigned char *buf,
2477 size_t *olen )
2478{
2479 unsigned char *p = buf;
2480 ((void) ssl);
2481
Paul Bakker677377f2013-10-28 12:54:26 +01002482 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002484 {
2485 *olen = 0;
2486 return;
2487 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2492 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002493
2494 *p++ = 0x00;
2495 *p++ = 2;
2496
2497 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002499
2500 *olen = 6;
2501}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002502#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002503
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002504#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2505static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2506 unsigned char *buf,
2507 size_t *olen )
2508{
Janos Follath865b3eb2019-12-16 11:46:15 +00002509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002510 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002511 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002512 size_t kkpp_len;
2513
2514 *olen = 0;
2515
2516 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002517 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002518 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2519 return;
2520
2521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2522
2523 if( end - p < 4 )
2524 {
2525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2526 return;
2527 }
2528
2529 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2530 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2531
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002532 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2533 p + 2, end - p - 2, &kkpp_len,
2534 ssl->conf->f_rng, ssl->conf->p_rng );
2535 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002536 {
2537 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2538 return;
2539 }
2540
2541 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2542 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2543
2544 *olen = kkpp_len + 4;
2545}
2546#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_SSL_ALPN )
2549static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002550 unsigned char *buf, size_t *olen )
2551{
2552 if( ssl->alpn_chosen == NULL )
2553 {
2554 *olen = 0;
2555 return;
2556 }
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002559
2560 /*
2561 * 0 . 1 ext identifier
2562 * 2 . 3 ext length
2563 * 4 . 5 protocol list length
2564 * 6 . 6 protocol name length
2565 * 7 . 7+n protocol name
2566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2568 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002569
2570 *olen = 7 + strlen( ssl->alpn_chosen );
2571
2572 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2573 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2574
2575 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2576 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2577
2578 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2579
2580 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002583
Johan Pascalb62bb512015-12-03 21:56:45 +01002584#if defined(MBEDTLS_SSL_DTLS_SRTP )
2585static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
2586 unsigned char *buf, size_t *olen )
2587{
2588 if( ssl->chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
2589 {
2590 *olen = 0;
2591 return;
2592 }
2593
2594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2595
2596 /* extension */
2597 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2598 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
2599 /* total length (5: only one profile(2 bytes) and length(2bytes) and srtp_mki not supported so zero length(1byte) ) */
2600 buf[2] = 0x00;
2601 buf[3] = 0x05;
2602
2603 /* protection profile length: 2 */
2604 buf[4] = 0x00;
2605 buf[5] = 0x02;
2606 switch (ssl->chosen_dtls_srtp_profile) {
2607 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80:
2608 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE >> 8) & 0xFF );
2609 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE ) & 0xFF );
2610 break;
2611 case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32:
2612 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE >> 8) & 0xFF );
2613 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE ) & 0xFF );
2614 break;
2615 case MBEDTLS_SRTP_NULL_HMAC_SHA1_80:
2616 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE >> 8) & 0xFF );
2617 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE ) & 0xFF );
2618 break;
2619 case MBEDTLS_SRTP_NULL_HMAC_SHA1_32:
2620 buf[6] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE >> 8) & 0xFF );
2621 buf[7] = (unsigned char)( ( MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE ) & 0xFF );
2622 break;
2623 default:
2624 *olen = 0;
2625 return;
2626 break;
2627 }
2628
2629 buf[8] = 0x00; /* unsupported srtp_mki variable length vector set to 0 */
2630
2631 *olen = 9;
2632}
2633#endif /* MBEDTLS_SSL_DTLS_SRTP */
2634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2636static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002637{
Janos Follath865b3eb2019-12-16 11:46:15 +00002638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002639 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002640 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002643
2644 /*
2645 * struct {
2646 * ProtocolVersion server_version;
2647 * opaque cookie<0..2^8-1>;
2648 * } HelloVerifyRequest;
2649 */
2650
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002651 /* The RFC is not clear on this point, but sending the actual negotiated
2652 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002654 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002656 p += 2;
2657
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002658 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002659 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002663 }
2664
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002665 /* Skip length byte until we know the length */
2666 cookie_len_byte = p++;
2667
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002668 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002669 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002670 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002673 return( ret );
2674 }
2675
2676 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002679
2680 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2682 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002685
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002686 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002687 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002689 return( ret );
2690 }
2691
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002692#if defined(MBEDTLS_SSL_PROTO_DTLS)
2693 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2694 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2695 {
2696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2697 return( ret );
2698 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002699#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002702
2703 return( 0 );
2704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002710 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002711#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002713 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 unsigned char *buf, *p;
2715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002720 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002724
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002725 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002726 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002728
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002729 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2732 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002733 }
2734
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 /*
2736 * 0 . 0 handshake type
2737 * 1 . 3 handshake length
2738 * 4 . 5 protocol version
2739 * 6 . 9 UNIX time()
2740 * 10 . 37 random bytes
2741 */
2742 buf = ssl->out_msg;
2743 p = buf + 4;
2744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002746 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002747 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002750 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002753 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 *p++ = (unsigned char)( t >> 24 );
2755 *p++ = (unsigned char)( t >> 16 );
2756 *p++ = (unsigned char)( t >> 8 );
2757 *p++ = (unsigned char)( t );
2758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002760#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002761 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002762 return( ret );
2763
2764 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002767 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002768 return( ret );
2769
2770 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Paul Bakker48916f92012-09-16 19:57:18 +00002772 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
2776 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002777 * Resume is 0 by default, see ssl_handshake_init().
2778 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2779 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002781 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_SSL_RENEGOTIATION)
2783 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002784#endif
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002785 ssl->session_negotiate->id_len != 0 &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002786 ssl->conf->f_get_cache != NULL &&
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002787 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002790 ssl->handshake->resume = 1;
2791 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002793 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 {
2795 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002796 * New session, create a new session id,
2797 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 ssl->state++;
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002802 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002803#endif
2804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002806 if( ssl->handshake->new_session_ticket != 0 )
2807 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002808 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002809 memset( ssl->session_negotiate->id, 0, 32 );
2810 }
2811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002813 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002814 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002815 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002816 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002817 return( ret );
2818 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 }
2820 else
2821 {
2822 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002823 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002825 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002831 return( ret );
2832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 }
2834
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002835 /*
2836 * 38 . 38 session id length
2837 * 39 . 38+n session id
2838 * 39+n . 40+n chosen ciphersuite
2839 * 41+n . 41+n chosen compression alg.
2840 * 42+n . 43+n extensions length
2841 * 44+n . 43+n+m extensions
2842 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002843 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2844 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2845 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2848 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002850 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Paul Bakker48916f92012-09-16 19:57:18 +00002852 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2853 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2854 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2857 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002859 ssl->session_negotiate->compression ) );
2860
Janos Follathc6dab2b2016-05-23 14:27:02 +01002861 /* Do not write the extensions if the protocol is SSLv3 */
2862#if defined(MBEDTLS_SSL_PROTO_SSL3)
2863 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2864 {
2865#endif
2866
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002867 /*
2868 * First write extensions, then the total length
2869 */
2870 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2871 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002874 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2875 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002876#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002879 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2880 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002881#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002882
Hanno Beckera0e20d02019-05-15 14:03:01 +01002883#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002884 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2885 ext_len += olen;
2886#endif
2887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002889 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2890 ext_len += olen;
2891#endif
2892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002894 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2895 ext_len += olen;
2896#endif
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002899 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2900 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002901#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002902
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002903#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002904 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002905 if ( mbedtls_ssl_ciphersuite_uses_ec(
2906 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2907 {
2908 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2909 ext_len += olen;
2910 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002911#endif
2912
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002913#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2914 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2915 ext_len += olen;
2916#endif
2917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002919 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2920 ext_len += olen;
2921#endif
2922
Johan Pascalb62bb512015-12-03 21:56:45 +01002923#if defined(MBEDTLS_SSL_DTLS_SRTP)
2924 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen);
2925 ext_len += olen;
2926#endif
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002929
Paul Bakkera7036632014-04-30 10:15:38 +02002930 if( ext_len > 0 )
2931 {
2932 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2933 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2934 p += ext_len;
2935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Janos Follathc6dab2b2016-05-23 14:27:02 +01002937#if defined(MBEDTLS_SSL_PROTO_SSL3)
2938 }
2939#endif
2940
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2943 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002945 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
2949 return( ret );
2950}
2951
Gilles Peskineeccd8882020-03-10 12:19:08 +01002952#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002954{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002955 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002956 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002959
Hanno Becker77adddc2019-02-07 12:32:43 +00002960 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002963 ssl->state++;
2964 return( 0 );
2965 }
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002969}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002970#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002974 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002975 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002976 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002977 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002978 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002979 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002981 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
2985 ssl->state++;
2986
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002987#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2988 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2989 authmode = ssl->handshake->sni_authmode;
2990 else
2991#endif
2992 authmode = ssl->conf->authmode;
2993
Hanno Becker77adddc2019-02-07 12:32:43 +00002994 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002995 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 return( 0 );
2999 }
3000
3001 /*
3002 * 0 . 0 handshake type
3003 * 1 . 3 handshake length
3004 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003005 * 5 .. m-1 cert types
3006 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003007 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 * n .. n+1 length of all DNs
3009 * n+2 .. n+3 length of DN 1
3010 * n+4 .. ... Distinguished Name #1
3011 * ... .. ... length of DN 2, etc.
3012 */
3013 buf = ssl->out_msg;
3014 p = buf + 4;
3015
3016 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003017 * Supported certificate types
3018 *
3019 * ClientCertificateType certificate_types<1..2^8-1>;
3020 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003022 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024#if defined(MBEDTLS_RSA_C)
3025 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#if defined(MBEDTLS_ECDSA_C)
3028 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003029#endif
3030
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003031 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003032 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003033
Paul Bakker577e0062013-08-28 11:57:20 +02003034 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003036 /*
3037 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003038 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003039 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3040 *
3041 * struct {
3042 * HashAlgorithm hash;
3043 * SignatureAlgorithm signature;
3044 * } SignatureAndHashAlgorithm;
3045 *
3046 * enum { (255) } HashAlgorithm;
3047 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003050 {
Simon Butcher99000142016-10-13 17:21:01 +01003051 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003052
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003053 /*
3054 * Supported signature algorithms
3055 */
Simon Butcher99000142016-10-13 17:21:01 +01003056 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3057 {
3058 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3059
3060 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3061 continue;
3062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003064 p[2 + sa_len++] = hash;
3065 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003068 p[2 + sa_len++] = hash;
3069 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003070#endif
Simon Butcher99000142016-10-13 17:21:01 +01003071 }
Paul Bakker926af752012-11-23 13:38:07 +01003072
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003073 p[0] = (unsigned char)( sa_len >> 8 );
3074 p[1] = (unsigned char)( sa_len );
3075 sa_len += 2;
3076 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003077 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003080 /*
3081 * DistinguishedName certificate_authorities<0..2^16-1>;
3082 * opaque DistinguishedName<1..2^16-1>;
3083 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003086 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003087
3088 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003090 /* NOTE: If trusted certificates are provisioned
3091 * via a CA callback (configured through
3092 * `mbedtls_ssl_conf_ca_cb()`, then the
3093 * CertificateRequest is currently left empty. */
3094
Janos Follath088ce432017-04-10 12:42:31 +01003095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3096 if( ssl->handshake->sni_ca_chain != NULL )
3097 crt = ssl->handshake->sni_ca_chain;
3098 else
3099#endif
3100 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003101
Janos Follath088ce432017-04-10 12:42:31 +01003102 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003103 {
irwirc9bc3002020-04-01 13:46:36 +03003104 /* It follows from RFC 5280 A.1 that this length
3105 * can be represented in at most 11 bits. */
3106 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003107
irwirc9bc3002020-04-01 13:46:36 +03003108 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003109 {
3110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3111 break;
3112 }
3113
3114 *p++ = (unsigned char)( dn_size >> 8 );
3115 *p++ = (unsigned char)( dn_size );
3116 memcpy( p, crt->subject_raw.p, dn_size );
3117 p += dn_size;
3118
3119 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3120
3121 total_dn_size += 2 + dn_size;
3122 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 }
3125
Paul Bakker926af752012-11-23 13:38:07 +01003126 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3128 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003129 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3130 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003132 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
3136 return( ret );
3137}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003138#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3141 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3142static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003143{
Janos Follath865b3eb2019-12-16 11:46:15 +00003144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3149 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003150 }
3151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3153 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3154 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003157 return( ret );
3158 }
3159
3160 return( 0 );
3161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3163 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003164
Gilles Peskineeccd8882020-03-10 12:19:08 +01003165#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003166 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003167static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003168 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003169{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003170 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3171 * signature length which will be added in ssl_write_server_key_exchange
3172 * after the call to ssl_prepare_server_key_exchange.
3173 * ssl_write_server_key_exchange also takes care of incrementing
3174 * ssl->out_msglen. */
3175 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003176 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003177 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003178 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003179 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003180 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3181 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003182 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003183 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003184 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003185 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003186 return( ret );
3187}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003188#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003189 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003190
Gilles Peskined3eb0612018-01-08 17:07:44 +01003191/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003192 * calculating the signature if any, but excluding formatting the
3193 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003194static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3195 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003198 ssl->handshake->ciphersuite_info;
3199
Gilles Peskineeccd8882020-03-10 12:19:08 +01003200#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3201#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003202 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003203#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3204#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003205
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003206 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003207#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003208 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003209#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003210
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003211 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003212
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003213 /*
3214 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003215 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003216 *
3217 */
3218
3219 /*
3220 * - ECJPAKE key exchanges
3221 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003222#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3223 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3224 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003226 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003227
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003228 ret = mbedtls_ecjpake_write_round_two(
3229 &ssl->handshake->ecjpake_ctx,
3230 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003231 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003232 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003233 if( ret != 0 )
3234 {
3235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3236 return( ret );
3237 }
3238
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003239 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003240 }
3241#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3242
Hanno Becker1aa267c2017-04-28 17:08:27 +01003243 /*
3244 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3245 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3246 * we use empty support identity hints here.
3247 **/
3248#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3250 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3251 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003252 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003253 ssl->out_msg[ssl->out_msglen++] = 0x00;
3254 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003255 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3257 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258
Hanno Becker7e5437a2017-04-28 17:15:26 +01003259 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003260 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003261 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003262#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003263 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003264 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003266 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003267
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003268 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3269 {
3270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3272 }
3273
Paul Bakker41c83d32013-03-20 14:39:14 +01003274 /*
3275 * Ephemeral DH parameters:
3276 *
3277 * struct {
3278 * opaque dh_p<1..2^16-1>;
3279 * opaque dh_g<1..2^16-1>;
3280 * opaque dh_Ys<1..2^16-1>;
3281 * } ServerDHParams;
3282 */
Hanno Beckerab740562017-10-04 13:15:37 +01003283 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3284 &ssl->conf->dhm_P,
3285 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003286 {
Hanno Beckerab740562017-10-04 13:15:37 +01003287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003288 return( ret );
3289 }
Paul Bakker48916f92012-09-16 19:57:18 +00003290
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003291 if( ( ret = mbedtls_dhm_make_params(
3292 &ssl->handshake->dhm_ctx,
3293 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3294 ssl->out_msg + ssl->out_msglen, &len,
3295 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003298 return( ret );
3299 }
3300
Gilles Peskineeccd8882020-03-10 12:19:08 +01003301#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003302 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003303#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003304
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003305 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3308 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3309 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3310 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003311 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003312#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003313
Hanno Becker1aa267c2017-04-28 17:08:27 +01003314 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003315 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003316 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003317#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003318 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003320 /*
3321 * Ephemeral ECDH parameters:
3322 *
3323 * struct {
3324 * ECParameters curve_params;
3325 * ECPoint public;
3326 * } ServerECDHParams;
3327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003331 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003332
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003333 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003334 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003335 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3336 if( (*curve)->grp_id == *gid )
3337 goto curve_matching_done;
3338
3339curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003340 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3343 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003344 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003347
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003348 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3349 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003350 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003352 return( ret );
3353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003355 if( ( ret = mbedtls_ecdh_make_params(
3356 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003357 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003358 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003359 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003362 return( ret );
3363 }
3364
Gilles Peskineeccd8882020-03-10 12:19:08 +01003365#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003366 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003367#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003368
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003369 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003370
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003371 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3372 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003373 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003374#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Hanno Becker1aa267c2017-04-28 17:08:27 +01003376 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003377 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003378 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003379 * exchange parameters, compute and add the signature here.
3380 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003381 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003382#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003383 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003384 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003385 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003386 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003387 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003389
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003390 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003391 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003392 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003393 * to choose appropriate hash.
3394 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3395 * (RFC 4492, Sec. 5.4)
3396 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003397 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003398
3399 mbedtls_md_type_t md_alg;
3400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003402 mbedtls_pk_type_t sig_alg =
3403 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003405 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003406 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3407 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003408 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003409 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3410 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003413 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003414 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003416 }
3417 }
Paul Bakker577e0062013-08-28 11:57:20 +02003418 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3420#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3421 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003422 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003423 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003424 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003426 }
3427 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003428#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3429 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003430 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003431 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003433 }
3434
Hanno Becker7e5437a2017-04-28 17:15:26 +01003435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3436
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003437 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003438 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3441 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3442 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003443 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003444 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003445 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3446 dig_signed,
3447 dig_signed_len );
3448 if( ret != 0 )
3449 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003450 }
3451 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3453 MBEDTLS_SSL_PROTO_TLS1_1 */
3454#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3455 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3456 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003457 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003458 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003459 dig_signed,
3460 dig_signed_len,
3461 md_alg );
3462 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003463 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003464 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003465 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3467 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003471 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003472
Gilles Peskineebd652f2018-01-05 21:18:59 +01003473 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003474
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003475 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003476 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3479 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003480 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003481 /*
3482 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003483 * explicitly through a prefix to the signature.
3484 *
3485 * struct {
3486 * HashAlgorithm hash;
3487 * SignatureAlgorithm signature;
3488 * } SignatureAndHashAlgorithm;
3489 *
3490 * struct {
3491 * SignatureAndHashAlgorithm algorithm;
3492 * opaque signature<0..2^16-1>;
3493 * } DigitallySigned;
3494 *
3495 */
3496
Gilles Peskine1004c192018-01-08 16:59:14 +01003497 ssl->out_msg[ssl->out_msglen++] =
3498 mbedtls_ssl_hash_from_md_alg( md_alg );
3499 ssl->out_msg[ssl->out_msglen++] =
3500 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003503
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003504#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003505 if( ssl->conf->f_async_sign_start != NULL )
3506 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003507 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003508 mbedtls_ssl_own_cert( ssl ),
3509 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003510 switch( ret )
3511 {
3512 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3513 /* act as if f_async_sign was null */
3514 break;
3515 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003516 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003517 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003518 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003519 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003520 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3521 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003522 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003523 return( ret );
3524 }
3525 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003526#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003527
3528 if( mbedtls_ssl_own_key( ssl ) == NULL )
3529 {
3530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3531 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3532 }
3533
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003534 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3535 * signature length which will be added in ssl_write_server_key_exchange
3536 * after the call to ssl_prepare_server_key_exchange.
3537 * ssl_write_server_key_exchange also takes care of incrementing
3538 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003539 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3540 md_alg, hash, hashlen,
3541 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003542 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003543 ssl->conf->f_rng,
3544 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003547 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003548 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003549 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003550#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003551
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003552 return( 0 );
3553}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003554
Gilles Peskined3eb0612018-01-08 17:07:44 +01003555/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003556 * that do not include a ServerKeyExchange message, do nothing. Either
3557 * way, if successful, move on to the next step in the SSL state
3558 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003559static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3560{
Janos Follath865b3eb2019-12-16 11:46:15 +00003561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003562 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003563#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003564 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003565 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003566#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003567
Gilles Peskined3eb0612018-01-08 17:07:44 +01003568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3569
Gilles Peskineeccd8882020-03-10 12:19:08 +01003570#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003571 /* Extract static ECDH parameters and abort if ServerKeyExchange
3572 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003573 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3574 {
3575 /* For suites involving ECDH, extract DH parameters
3576 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003577#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003578 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3579 {
3580 ssl_get_ecdh_params_from_cert( ssl );
3581 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003582#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003583
3584 /* Key exchanges not involving ephemeral keys don't use
3585 * ServerKeyExchange, so end here. */
3586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3587 ssl->state++;
3588 return( 0 );
3589 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003590#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003591
Gilles Peskineeccd8882020-03-10 12:19:08 +01003592#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003593 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003594 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003595 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003596 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003597 {
3598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3599 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003600 }
3601 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003602#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003603 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003604 {
3605 /* ServerKeyExchange is needed. Prepare the message. */
3606 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003607 }
3608
3609 if( ret != 0 )
3610 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003611 /* If we're starting to write a new message, set ssl->out_msglen
3612 * to 0. But if we're resuming after an asynchronous message,
3613 * out_msglen is the amount of data written so far and mst be
3614 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003615 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3617 else
3618 ssl->out_msglen = 0;
3619 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003620 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003621
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003622 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003623 * ssl_prepare_server_key_exchange already wrote the signature
3624 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003625#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003626 if( signature_len != 0 )
3627 {
3628 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3629 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3630
3631 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3632 ssl->out_msg + ssl->out_msglen,
3633 signature_len );
3634
3635 /* Skip over the already-written signature */
3636 ssl->out_msglen += signature_len;
3637 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003638#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003639
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003640 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3642 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
3644 ssl->state++;
3645
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003646 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 return( ret );
3650 }
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003654}
3655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003657{
Janos Follath865b3eb2019-12-16 11:46:15 +00003658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
3662 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3664 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003665
3666 ssl->state++;
3667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003671#endif
3672
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003674 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 return( ret );
3677 }
3678
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003679#if defined(MBEDTLS_SSL_PROTO_DTLS)
3680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3681 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3682 {
3683 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3684 return( ret );
3685 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003686#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003689
3690 return( 0 );
3691}
3692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3694 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3695static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003696 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003699 size_t n;
3700
3701 /*
3702 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3703 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003704 if( *p + 2 > end )
3705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3707 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003708 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003709
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003710 n = ( (*p)[0] << 8 ) | (*p)[1];
3711 *p += 2;
3712
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003713 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3716 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003717 }
3718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3722 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003723 }
3724
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003725 *p += n;
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003728
Paul Bakker70df2fb2013-04-17 17:19:09 +02003729 return( ret );
3730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3732 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3735 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003736
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003737#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003738static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3739 unsigned char *peer_pms,
3740 size_t *peer_pmslen,
3741 size_t peer_pmssize )
3742{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003743 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003744 peer_pms, peer_pmslen, peer_pmssize );
3745 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3746 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003747 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003748 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003749 }
3750 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3751 return( ret );
3752}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003753#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003754
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003755static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3756 const unsigned char *p,
3757 const unsigned char *end,
3758 unsigned char *peer_pms,
3759 size_t *peer_pmslen,
3760 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003761{
Janos Follath865b3eb2019-12-16 11:46:15 +00003762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003763 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3764 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3765 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003766
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003767#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003768 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003769 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003770 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003771 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3773 return( ssl_resume_decrypt_pms( ssl,
3774 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003775 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003776#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003777
3778 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003779 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3782 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3783 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003784 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003785 if ( p + 2 > end ) {
3786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3787 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3788 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003789 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3790 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3793 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003794 }
3795 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003796#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003797
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003798 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3801 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003802 }
3803
Gilles Peskine422ccab2018-01-11 18:29:01 +01003804 /*
3805 * Decrypt the premaster secret
3806 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003807#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003808 if( ssl->conf->f_async_decrypt_start != NULL )
3809 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003810 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003811 mbedtls_ssl_own_cert( ssl ),
3812 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003813 switch( ret )
3814 {
3815 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3816 /* act as if f_async_decrypt_start was null */
3817 break;
3818 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003819 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003820 return( ssl_resume_decrypt_pms( ssl,
3821 peer_pms,
3822 peer_pmslen,
3823 peer_pmssize ) );
3824 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003825 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003826 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3827 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003828 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003829 return( ret );
3830 }
3831 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003832#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003833
Gilles Peskine422ccab2018-01-11 18:29:01 +01003834 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3835 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3837 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3838 }
3839
3840 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003841 peer_pms, peer_pmslen, peer_pmssize,
3842 ssl->conf->f_rng, ssl->conf->p_rng );
3843 return( ret );
3844}
3845
3846static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3847 const unsigned char *p,
3848 const unsigned char *end,
3849 size_t pms_offset )
3850{
Janos Follath865b3eb2019-12-16 11:46:15 +00003851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003852 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3853 unsigned char ver[2];
3854 unsigned char fake_pms[48], peer_pms[48];
3855 unsigned char mask;
3856 size_t i, peer_pmslen;
3857 unsigned int diff;
3858
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003859 /* In case of a failure in decryption, the decryption may write less than
3860 * 2 bytes of output, but we always read the first two bytes. It doesn't
3861 * matter in the end because diff will be nonzero in that case due to
3862 * peer_pmslen being less than 48, and we only care whether diff is 0.
3863 * But do initialize peer_pms for robustness anyway. This also makes
3864 * memory analyzers happy (don't access uninitialized memory, even
3865 * if it's an unsigned char). */
3866 peer_pms[0] = peer_pms[1] = ~0;
3867
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003868 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3869 peer_pms,
3870 &peer_pmslen,
3871 sizeof( peer_pms ) );
3872
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003873#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003874 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3875 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003876#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003879 ssl->handshake->max_minor_ver,
3880 ssl->conf->transport, ver );
3881
3882 /* Avoid data-dependent branches while checking for invalid
3883 * padding, to protect against timing-based Bleichenbacher-type
3884 * attacks. */
3885 diff = (unsigned int) ret;
3886 diff |= peer_pmslen ^ 48;
3887 diff |= peer_pms[0] ^ ver[0];
3888 diff |= peer_pms[1] ^ ver[1];
3889
3890 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3891 /* MSVC has a warning about unary minus on unsigned, but this is
3892 * well-defined and precisely what we want to do here */
3893#if defined(_MSC_VER)
3894#pragma warning( push )
3895#pragma warning( disable : 4146 )
3896#endif
3897 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3898#if defined(_MSC_VER)
3899#pragma warning( pop )
3900#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003901
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003902 /*
3903 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3904 * must not cause the connection to end immediately; instead, send a
3905 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003906 * To protect against timing-based variants of the attack, we must
3907 * not have any branch that depends on whether the decryption was
3908 * successful. In particular, always generate the fake premaster secret,
3909 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003910 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003911 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003912 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003913 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003914 /* It's ok to abort on an RNG failure, since this does not reveal
3915 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003916 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003917 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003918
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003919#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003920 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003922#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003923
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003924 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3925 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003929 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003930 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003931
Gilles Peskine422ccab2018-01-11 18:29:01 +01003932 /* Set pms to either the true or the fake PMS, without
3933 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003934 for( i = 0; i < ssl->handshake->pmslen; i++ )
3935 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3936
3937 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3940 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003941
Gilles Peskineeccd8882020-03-10 12:19:08 +01003942#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003944 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003945{
Paul Bakker6db455e2013-09-18 17:29:31 +02003946 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003947 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003948
Hanno Becker845b9462018-10-26 12:07:29 +01003949 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3952 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003953 }
3954
3955 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003956 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003957 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003958 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3961 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003962 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003963
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003964 n = ( (*p)[0] << 8 ) | (*p)[1];
3965 *p += 2;
3966
irwir6527bd62019-09-21 18:51:25 +03003967 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3970 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003971 }
3972
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003973 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003974 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003975 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003977 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003978 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003979 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003980 /* Identity is not a big secret since clients send it in the clear,
3981 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003982 if( n != ssl->conf->psk_identity_len ||
3983 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003986 }
3987 }
3988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003992 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3993 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003995 }
3996
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003997 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003998
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003999 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004000}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004001#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004{
Janos Follath865b3eb2019-12-16 11:46:15 +00004005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004007 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004008
Hanno Beckere694c3e2017-12-27 21:34:08 +00004009 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004012
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004013#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004014 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4015 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4016 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4017 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004018 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004019 {
4020 /* We've already read a record and there is an asynchronous
4021 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004022 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4024 }
4025 else
4026#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004027 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004030 return( ret );
4031 }
4032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004034 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4039 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 }
4041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4045 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 }
4047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4049 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004051 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004054 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004056
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004057 if( p != end )
4058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4060 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004061 }
4062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004064 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004065 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004066 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004067 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4070 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004071 }
4072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004074 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004075 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4077#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4078 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4079 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4080 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4081 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4082 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4083 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4084 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004087 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4090 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004091 }
4092
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004093 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4094 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004097 &ssl->handshake->pmslen,
4098 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004100 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4103 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004104 }
4105
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004106 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4107 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004108 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4111 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4112 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4113 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4114#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4115 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004116 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004117 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004120 return( ret );
4121 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004122
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004123 if( p != end )
4124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4126 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004127 }
4128
Hanno Becker845b9462018-10-26 12:07:29 +01004129#if defined(MBEDTLS_USE_PSA_CRYPTO)
4130 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4131 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004132 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4134 else
4135#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004137 ciphersuite_info->key_exchange ) ) != 0 )
4138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004140 return( ret );
4141 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004142 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4145#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4146 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004147 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004148#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004149 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004150 {
4151 /* There is an asynchronous operation in progress to
4152 * decrypt the encrypted premaster secret, so skip
4153 * directly to resuming this operation. */
4154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4155 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4156 * won't actually use it, but maintain p anyway for robustness. */
4157 p += ssl->conf->psk_identity_len + 2;
4158 }
4159 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004160#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004161 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004164 return( ret );
4165 }
4166
Hanno Becker845b9462018-10-26 12:07:29 +01004167#if defined(MBEDTLS_USE_PSA_CRYPTO)
4168 /* Opaque PSKs are currently only supported for PSK-only. */
4169 if( ssl_use_opaque_psk( ssl ) == 1 )
4170 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4171#endif
4172
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004173 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004176 return( ret );
4177 }
4178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004180 ciphersuite_info->key_exchange ) ) != 0 )
4181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004183 return( ret );
4184 }
4185 }
4186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4188#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4189 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004190 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004191 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004194 return( ret );
4195 }
4196 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004199 return( ret );
4200 }
4201
Hanno Becker845b9462018-10-26 12:07:29 +01004202#if defined(MBEDTLS_USE_PSA_CRYPTO)
4203 /* Opaque PSKs are currently only supported for PSK-only. */
4204 if( ssl_use_opaque_psk( ssl ) == 1 )
4205 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4206#endif
4207
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004208 if( p != end )
4209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4211 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004212 }
4213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004215 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004218 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004219 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004220 }
4221 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4223#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4224 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004225 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004226 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004229 return( ret );
4230 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004233 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4236 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004237 }
4238
Hanno Becker845b9462018-10-26 12:07:29 +01004239#if defined(MBEDTLS_USE_PSA_CRYPTO)
4240 /* Opaque PSKs are currently only supported for PSK-only. */
4241 if( ssl_use_opaque_psk( ssl ) == 1 )
4242 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4243#endif
4244
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004245 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4246 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004249 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004252 return( ret );
4253 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004254 }
4255 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4257#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4258 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004259 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004260 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004263 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 }
4265 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004266 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004268#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4269 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4270 {
4271 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4272 p, end - p );
4273 if( ret != 0 )
4274 {
4275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4276 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4277 }
4278
4279 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4280 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4281 ssl->conf->f_rng, ssl->conf->p_rng );
4282 if( ret != 0 )
4283 {
4284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4285 return( ret );
4286 }
4287 }
4288 else
4289#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004298 return( ret );
4299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004300
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 ssl->state++;
4302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004304
4305 return( 0 );
4306}
4307
Gilles Peskineeccd8882020-03-10 12:19:08 +01004308#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004310{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004311 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004312 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004315
Hanno Becker77adddc2019-02-07 12:32:43 +00004316 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004319 ssl->state++;
4320 return( 0 );
4321 }
4322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004325}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004326#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004328{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004330 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004331 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004332 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004333 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4335 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004338 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004339 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004340 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004343
Hanno Becker2a831a42019-02-07 13:17:25 +00004344 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004347 ssl->state++;
4348 return( 0 );
4349 }
4350
Hanno Becker2a831a42019-02-07 13:17:25 +00004351#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4352 if( ssl->session_negotiate->peer_cert == NULL )
4353 {
4354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4355 ssl->state++;
4356 return( 0 );
4357 }
4358#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4359 if( ssl->session_negotiate->peer_cert_digest == NULL )
4360 {
4361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4362 ssl->state++;
4363 return( 0 );
4364 }
4365#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4366
Simon Butcher99000142016-10-13 17:21:01 +01004367 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004368 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004369 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004371 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 return( ret );
4373 }
4374
4375 ssl->state++;
4376
Simon Butcher99000142016-10-13 17:21:01 +01004377 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4379 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4382 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 }
4384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004387#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4388 peer_pk = &ssl->handshake->peer_pubkey;
4389#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4390 if( ssl->session_negotiate->peer_cert == NULL )
4391 {
4392 /* Should never happen */
4393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4394 }
4395 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4396#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4397
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004398 /*
4399 * struct {
4400 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4401 * opaque signature<0..2^16-1>;
4402 * } DigitallySigned;
4403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4405 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4406 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004409 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004410
4411 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004412 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004413 {
4414 hash_start += 16;
4415 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004417 }
Paul Bakker926af752012-11-23 13:38:07 +01004418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004419 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4421 MBEDTLS_SSL_PROTO_TLS1_1 */
4422#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4423 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004424 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004425 if( i + 2 > ssl->in_hslen )
4426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4428 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004429 }
4430
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004432 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 */
Simon Butcher99000142016-10-13 17:21:01 +01004434 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4435
4436 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004439 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004441 }
4442
Simon Butcher99000142016-10-13 17:21:01 +01004443#if !defined(MBEDTLS_MD_SHA1)
4444 if( MBEDTLS_MD_SHA1 == md_alg )
4445 hash_start += 16;
4446#endif
Paul Bakker926af752012-11-23 13:38:07 +01004447
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004448 /* Info from md_alg will be used instead */
4449 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004450
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004451 i++;
4452
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004453 /*
4454 * Signature
4455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4457 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004460 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004462 }
4463
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004464 /*
4465 * Check the certificate's key type matches the signature alg
4466 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004467 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4470 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004471 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004472
4473 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004474 }
4475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004480 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004481
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004482 if( i + 2 > ssl->in_hslen )
4483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4485 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004486 }
4487
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004488 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4489 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004490
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004491 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4494 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004495 }
4496
Simon Butcher99000142016-10-13 17:21:01 +01004497 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004498 {
4499 size_t dummy_hlen;
4500 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4501 }
Simon Butcher99000142016-10-13 17:21:01 +01004502
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004503 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004504 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004505 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004508 return( ret );
4509 }
4510
Simon Butcher99000142016-10-13 17:21:01 +01004511 mbedtls_ssl_update_handshake_status( ssl );
4512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004514
Paul Bakkered27a042013-04-18 22:46:23 +02004515 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004516}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004517#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4520static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004521{
Janos Follath865b3eb2019-12-16 11:46:15 +00004522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004523 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004524 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4529 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004530
4531 /*
4532 * struct {
4533 * uint32 ticket_lifetime_hint;
4534 * opaque ticket<0..2^16-1>;
4535 * } NewSessionTicket;
4536 *
4537 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4538 * 8 . 9 ticket_len (n)
4539 * 10 . 9+n ticket content
4540 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004541
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004542 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004543 ssl->session_negotiate,
4544 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004545 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004546 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004547 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004549 tlen = 0;
4550 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004551
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004552 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4553 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4554 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4555 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4556
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004557 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4558 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004559
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004560 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004561
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004562 /*
4563 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4564 * ChangeCipherSpec share the same state.
4565 */
4566 ssl->handshake->new_session_ticket = 0;
4567
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004568 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004569 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004571 return( ret );
4572 }
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004575
4576 return( 0 );
4577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004579
Paul Bakker5121ce52009-01-03 21:22:43 +00004580/*
Paul Bakker1961b702013-01-25 14:49:24 +01004581 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004584{
4585 int ret = 0;
4586
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004587 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004593 return( ret );
4594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004598 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004599 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004600 return( ret );
4601 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004602#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004603
Paul Bakker1961b702013-01-25 14:49:24 +01004604 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 case MBEDTLS_SSL_HELLO_REQUEST:
4607 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 break;
4609
Paul Bakker1961b702013-01-25 14:49:24 +01004610 /*
4611 * <== ClientHello
4612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004614 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617#if defined(MBEDTLS_SSL_PROTO_DTLS)
4618 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4619 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004620#endif
4621
Paul Bakker1961b702013-01-25 14:49:24 +01004622 /*
4623 * ==> ServerHello
4624 * Certificate
4625 * ( ServerKeyExchange )
4626 * ( CertificateRequest )
4627 * ServerHelloDone
4628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004630 ret = ssl_write_server_hello( ssl );
4631 break;
4632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4634 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004635 break;
4636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004638 ret = ssl_write_server_key_exchange( ssl );
4639 break;
4640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004642 ret = ssl_write_certificate_request( ssl );
4643 break;
4644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004646 ret = ssl_write_server_hello_done( ssl );
4647 break;
4648
4649 /*
4650 * <== ( Certificate/Alert )
4651 * ClientKeyExchange
4652 * ( CertificateVerify )
4653 * ChangeCipherSpec
4654 * Finished
4655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4657 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004658 break;
4659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004661 ret = ssl_parse_client_key_exchange( ssl );
4662 break;
4663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004665 ret = ssl_parse_certificate_verify( ssl );
4666 break;
4667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4669 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004670 break;
4671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 case MBEDTLS_SSL_CLIENT_FINISHED:
4673 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004674 break;
4675
4676 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004677 * ==> ( NewSessionTicket )
4678 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004679 * Finished
4680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4682#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004683 if( ssl->handshake->new_session_ticket != 0 )
4684 ret = ssl_write_new_session_ticket( ssl );
4685 else
Paul Bakkera503a632013-08-14 13:48:06 +02004686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004688 break;
4689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 case MBEDTLS_SSL_SERVER_FINISHED:
4691 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004692 break;
4693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 case MBEDTLS_SSL_FLUSH_BUFFERS:
4695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4696 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004697 break;
4698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4700 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004701 break;
4702
4703 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 }
4707
Paul Bakker5121ce52009-01-03 21:22:43 +00004708 return( ret );
4709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710#endif /* MBEDTLS_SSL_SRV_C */