blob: 56e0cbf55ddf73ac4cf404c1356304810643a1fd [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
SimonBd5800b72016-04-26 07:43:27 +010024#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
27#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010030#endif
31
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020033#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050036#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000037
38#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010045#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
49int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020050 const unsigned char *info,
51 size_t ilen )
52{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020053 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020057
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020060
61 memcpy( ssl->cli_id, info, ilen );
62 ssl->cli_id_len = ilen;
63
64 return( 0 );
65}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020066
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020067void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_ssl_cookie_write_t *f_cookie_write,
69 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020070 void *p_cookie )
71{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020072 conf->f_cookie_write = f_cookie_write;
73 conf->f_cookie_check = f_cookie_check;
74 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000080 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000081 size_t len )
82{
Janos Follath865b3eb2019-12-16 11:46:15 +000083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000084 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000085 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010088
Philippe Antoine747fd532018-05-30 09:13:21 +020089 if( len < 2 )
90 {
91 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
92 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
93 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
94 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
95 }
Paul Bakker5701cdc2012-09-27 21:49:42 +000096 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
97 if( servername_list_size + 2 != len )
98 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000103 }
104
105 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200106 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000107 {
108 hostname_len = ( ( p[1] << 8 ) | p[2] );
109 if( hostname_len + 3 > servername_list_size )
110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000115 }
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000118 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200119 ret = ssl->conf->f_sni( ssl->conf->p_sni,
120 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000121 if( ret != 0 )
122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
125 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
126 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000127 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000128 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000129 }
130
131 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000132 p += hostname_len + 3;
133 }
134
135 if( servername_list_size != 0 )
136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200138 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
139 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000141 }
142
143 return( 0 );
144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000146
Gilles Peskineeccd8882020-03-10 12:19:08 +0100147#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100148static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
149{
150 if( conf->f_psk != NULL )
151 return( 1 );
152
153 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
154 return( 0 );
155
156 if( conf->psk != NULL && conf->psk_len != 0 )
157 return( 1 );
158
159#if defined(MBEDTLS_USE_PSA_CRYPTO)
160 if( conf->psk_opaque != 0 )
161 return( 1 );
162#endif /* MBEDTLS_USE_PSA_CRYPTO */
163
164 return( 0 );
165}
166
167#if defined(MBEDTLS_USE_PSA_CRYPTO)
168static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
169{
170 if( ssl->conf->f_psk != NULL )
171 {
172 /* If we've used a callback to select the PSK,
173 * the static configuration is irrelevant. */
174
175 if( ssl->handshake->psk_opaque != 0 )
176 return( 1 );
177
178 return( 0 );
179 }
180
181 if( ssl->conf->psk_opaque != 0 )
182 return( 1 );
183
184 return( 0 );
185}
186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100187#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000190 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000191 size_t len )
192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_RENEGOTIATION)
194 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100195 {
196 /* Check verify-data in constant-time. The length OTOH is no secret */
197 if( len != 1 + ssl->verify_data_len ||
198 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100200 ssl->verify_data_len ) != 0 )
201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200203 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
204 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100206 }
207 }
208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000210 {
211 if( len != 1 || buf[0] != 0x0 )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200214 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
215 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +0000217 }
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000220 }
Paul Bakker48916f92012-09-16 19:57:18 +0000221
222 return( 0 );
223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100226 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100227
228/*
229 * Status of the implementation of signature-algorithms extension:
230 *
231 * Currently, we are only considering the signature-algorithm extension
232 * to pick a ciphersuite which allows us to send the ServerKeyExchange
233 * message with a signature-hash combination that the user allows.
234 *
235 * We do *not* check whether all certificates in our certificate
236 * chain are signed with an allowed signature-hash pair.
237 * This needs to be done at a later stage.
238 *
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000241 const unsigned char *buf,
242 size_t len )
243{
244 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100245
Paul Bakker23f36802012-09-28 14:15:14 +0000246 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200247 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200248
Hanno Becker7e5437a2017-04-28 17:15:26 +0100249 mbedtls_md_type_t md_cur;
250 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000251
Philippe Antoine747fd532018-05-30 09:13:21 +0200252 if ( len < 2 ) {
253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
255 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
256 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
257 }
Paul Bakker23f36802012-09-28 14:15:14 +0000258 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
259 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200260 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker23f36802012-09-28 14:15:14 +0000266 }
267
Hanno Becker7e5437a2017-04-28 17:15:26 +0100268 /* Currently we only guarantee signing the ServerKeyExchange message according
269 * to the constraints specified in this extension (see above), so it suffices
270 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200271 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100272 * This will change when we also consider certificate signatures,
273 * in which case we will need to remember the whole signature-hash
274 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200275 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100276
277 for( p = buf + 2; p < end; p += 2 )
278 {
279 /* Silently ignore unknown signature or hash algorithms. */
280
281 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
282 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
284 " unknown sig alg encoding %d", p[1] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100285 continue;
286 }
287
288 /* Check if we support the hash the user proposes */
289 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
290 if( md_cur == MBEDTLS_MD_NONE )
291 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
293 " unknown hash alg encoding %d", p[0] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100294 continue;
295 }
296
297 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
298 {
299 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
301 " match sig %d and hash %d",
Hanno Becker7e5437a2017-04-28 17:15:26 +0100302 sig_cur, md_cur ) );
303 }
304 else
305 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
307 "hash alg %d not supported", md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000308 }
Paul Bakker23f36802012-09-28 14:15:14 +0000309 }
310
Paul Bakker23f36802012-09-28 14:15:14 +0000311 return( 0 );
312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100314 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000315
Robert Cragie136884c2015-10-02 13:34:31 +0100316#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100317 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200319 const unsigned char *buf,
320 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100321{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200322 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100323 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100325
Philippe Antoine747fd532018-05-30 09:13:21 +0200326 if ( len < 2 ) {
327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
330 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
331 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100332 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
333 if( list_size + 2 != len ||
334 list_size % 2 != 0 )
335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100340 }
341
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200342 /* Should never happen unless client duplicates the extension */
343 if( ssl->handshake->curves != NULL )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
347 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200349 }
350
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100351 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200352 * and leave room for a final 0 */
353 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( our_size > MBEDTLS_ECP_DP_MAX )
355 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200356
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200357 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200358 {
359 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
360 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200361 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200362 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200363
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200364 ssl->handshake->curves = curves;
365
Paul Bakker41c83d32013-03-20 14:39:14 +0100366 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200367 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200370
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200371 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100372 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200373 *curves++ = curve_info;
374 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100375 }
376
377 list_size -= 2;
378 p += 2;
379 }
380
381 return( 0 );
382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200385 const unsigned char *buf,
386 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100387{
388 size_t list_size;
389 const unsigned char *p;
390
Philippe Antoine747fd532018-05-30 09:13:21 +0200391 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100397 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200398 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100399
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200400 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100401 while( list_size > 0 )
402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
404 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100405 {
Robert Cragie136884c2015-10-02 13:34:31 +0100406#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200407 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100408#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100409#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Robert Cragie136884c2015-10-02 13:34:31 +0100410 ssl->handshake->ecjpake_ctx.point_format = p[0];
411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100413 return( 0 );
414 }
415
416 list_size--;
417 p++;
418 }
419
420 return( 0 );
421}
Robert Cragieae8535d2015-10-06 17:11:18 +0100422#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
423 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100424
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200425#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
426static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
427 const unsigned char *buf,
428 size_t len )
429{
Janos Follath865b3eb2019-12-16 11:46:15 +0000430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200431
432 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
433 {
434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
435 return( 0 );
436 }
437
438 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
439 buf, len ) ) != 0 )
440 {
441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
443 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200444 return( ret );
445 }
446
447 /* Only mark the extension as OK when we're sure it is */
448 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
449
450 return( 0 );
451}
452#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
455static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200456 const unsigned char *buf,
457 size_t len )
458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200462 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
463 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200465 }
466
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200467 ssl->session_negotiate->mfl_code = buf[0];
468
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200469 return( 0 );
470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200472
Hanno Beckera0e20d02019-05-15 14:03:01 +0100473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100474static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
475 const unsigned char *buf,
476 size_t len )
477{
478 size_t peer_cid_len;
479
480 /* CID extension only makes sense in DTLS */
481 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
482 {
483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
485 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
486 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
487 }
488
489 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100490 * Quoting draft-ietf-tls-dtls-connection-id-05
491 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100492 *
493 * struct {
494 * opaque cid<0..2^8-1>;
495 * } ConnectionId;
496 */
497
498 if( len < 1 )
499 {
500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
501 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
502 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
503 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
504 }
505
506 peer_cid_len = *buf++;
507 len--;
508
509 if( len != peer_cid_len )
510 {
511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
512 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
513 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
514 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
515 }
516
517 /* Ignore CID if the user has disabled its use. */
518 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
519 {
520 /* Leave ssl->handshake->cid_in_use in its default
521 * value of MBEDTLS_SSL_CID_DISABLED. */
522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
523 return( 0 );
524 }
525
526 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
527 {
528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
530 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
531 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
532 }
533
Hanno Becker08556bf2019-05-03 12:43:44 +0100534 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100535 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
536 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
537
538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
539 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
540
Hanno Becker89dcc882019-04-26 13:56:39 +0100541 return( 0 );
542}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100543#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
546static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200547 const unsigned char *buf,
548 size_t len )
549{
550 if( len != 0 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
554 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200556 }
557
558 ((void) buf);
559
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200560 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200562
563 return( 0 );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
568static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100569 const unsigned char *buf,
570 size_t len )
571{
572 if( len != 0 )
573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200575 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
576 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100578 }
579
580 ((void) buf);
581
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200582 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100586 }
587
588 return( 0 );
589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
593static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200594 const unsigned char *buf,
595 size_t len )
596{
597 if( len != 0 )
598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200600 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
601 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200603 }
604
605 ((void) buf);
606
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200607 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200611 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200612
613 return( 0 );
614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
618static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200619 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200620 size_t len )
621{
Janos Follath865b3eb2019-12-16 11:46:15 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200623 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200624
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200625 mbedtls_ssl_session_init( &session );
626
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200627 if( ssl->conf->f_ticket_parse == NULL ||
628 ssl->conf->f_ticket_write == NULL )
629 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200630 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200631 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200632
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200633 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200634 ssl->handshake->new_session_ticket = 1;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200637
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200638 if( len == 0 )
639 return( 0 );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_SSL_RENEGOTIATION)
642 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200645 return( 0 );
646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200648
649 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200650 * Failures are ok: just ignore the ticket and proceed.
651 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200652 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
653 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200654 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200655 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200656
657 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
659 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
661 else
662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
663
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200666
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200667 /*
668 * Keep the session ID sent by the client, since we MUST send it back to
669 * inform them we're accepting the ticket (RFC 5077 section 3.4)
670 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200671 session.id_len = ssl->session_negotiate->id_len;
672 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200673
674 mbedtls_ssl_session_free( ssl->session_negotiate );
675 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
676
677 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500678 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200683
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200684 /* Don't send a new ticket after all, this one is OK */
685 ssl->handshake->new_session_ticket = 0;
686
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200687 return( 0 );
688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_ALPN)
692static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200693 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200694{
Paul Bakker14b16c62014-05-28 11:33:54 +0200695 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200696 const unsigned char *theirs, *start, *end;
697 const char **ours;
698
699 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200700 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200701 return( 0 );
702
703 /*
704 * opaque ProtocolName<1..2^8-1>;
705 *
706 * struct {
707 * ProtocolName protocol_name_list<2..2^16-1>
708 * } ProtocolNameList;
709 */
710
711 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
712 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200713 {
714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200717 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718
719 list_len = ( buf[0] << 8 ) | buf[1];
720 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200721 {
722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200725 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200726
727 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100728 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200729 */
730 start = buf + 2;
731 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100732 for( theirs = start; theirs != end; theirs += cur_len )
733 {
734 cur_len = *theirs++;
735
736 /* Current identifier must fit in list */
737 if( cur_len > (size_t)( end - theirs ) )
738 {
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
742 }
743
744 /* Empty strings MUST NOT be included */
745 if( cur_len == 0 )
746 {
747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
748 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
749 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
750 }
751 }
752
753 /*
754 * Use our order of preference
755 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200756 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200757 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200758 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200759 for( theirs = start; theirs != end; theirs += cur_len )
760 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200761 cur_len = *theirs++;
762
Paul Bakker14b16c62014-05-28 11:33:54 +0200763 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200764 memcmp( theirs, *ours, cur_len ) == 0 )
765 {
766 ssl->alpn_chosen = *ours;
767 return( 0 );
768 }
769 }
770 }
771
772 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
774 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
775 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200778
Johan Pascalb62bb512015-12-03 21:56:45 +0100779#if defined(MBEDTLS_SSL_DTLS_SRTP)
780static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300781 const unsigned char *buf,
782 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100783{
Ron Eldor3adb9922017-12-21 10:15:08 +0200784 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
Johan Pascalb62bb512015-12-03 21:56:45 +0100785 size_t i,j;
Ron Eldor591f1622018-01-22 12:30:04 +0200786 size_t profile_length;
Ron Eldoref72faf2018-07-12 11:54:20 +0300787 const mbedtls_ssl_srtp_profile_info *profile_info;
Ron Eldor313d7b52018-12-10 14:56:21 +0200788 /*! 2 bytes for profile length and 1 byte for mki len */
789 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100790
791 /* If use_srtp is not configured, just ignore the extension */
Ron Eldoref72faf2018-07-12 11:54:20 +0300792 if( ssl->conf->dtls_srtp_profile_list == NULL ||
793 ssl->conf->dtls_srtp_profile_list_len == 0 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100794 return( 0 );
795
796 /* RFC5764 section 4.1.1
797 * uint8 SRTPProtectionProfile[2];
798 *
799 * struct {
800 * SRTPProtectionProfiles SRTPProtectionProfiles;
801 * opaque srtp_mki<0..255>;
802 * } UseSRTPData;
803
804 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100805 */
806
Ron Eldoref72faf2018-07-12 11:54:20 +0300807 /*
808 * Min length is 5: at least one protection profile(2 bytes)
809 * and length(2 bytes) + srtp_mki length(1 byte)
810 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200811 if( len < size_of_lengths + 2 )
812 {
813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
814 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100815 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200816 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100817
Ron Eldor591f1622018-01-22 12:30:04 +0200818 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
819
Ron Eldoref72faf2018-07-12 11:54:20 +0300820 /* first 2 bytes are protection profile length(in bytes) */
821 profile_length = ( buf[0] << 8 ) | buf[1];
Ron Eldor591f1622018-01-22 12:30:04 +0200822
Ron Eldor313d7b52018-12-10 14:56:21 +0200823 if( profile_length > len - size_of_lengths )
824 {
825 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
826 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
827 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
828 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300829 /*
830 * parse the extension list values are defined in
831 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
832 */
833 for( j=0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100834 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300835 /* + 2 to skip the length field */
836 uint16_t protection_profile_value = buf[j + 2] << 8 | buf[j+3];
Ron Eldor089c9fe2018-12-06 17:12:49 +0200837 client_protection = mbedtls_ssl_get_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100838
Ron Eldorb4655392018-07-05 18:25:39 +0300839 profile_info = mbedtls_ssl_dtls_srtp_profile_info_from_id( client_protection );
840 if( profile_info != NULL )
841 {
842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", profile_info->name ) );
843 }
Ron Eldor591f1622018-01-22 12:30:04 +0200844 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200845 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200846 {
847 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
848 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200849 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Ron Eldorb4655392018-07-05 18:25:39 +0300850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", profile_info->name ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200851 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100852 }
853 }
Ron Eldor591f1622018-01-22 12:30:04 +0200854 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE )
855 break;
856 }
857 if( ( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) &&
Ron Eldora9788042018-12-05 11:04:31 +0200858 ( len > ( profile_length + 2 ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +0200859 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300860 ssl->dtls_srtp_info.mki_len = buf[profile_length + 2];
Ron Eldor313d7b52018-12-10 14:56:21 +0200861 if( ssl->dtls_srtp_info.mki_len > MBEDTLS_DTLS_SRTP_MAX_MKI_LENGTH ||
862 ssl->dtls_srtp_info.mki_len + profile_length + size_of_lengths != len )
Ron Eldor591f1622018-01-22 12:30:04 +0200863 {
864 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
865 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
866 ssl->dtls_srtp_info.mki_len = 0;
867 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
868 }
869
870 for( i=0; i < ssl->dtls_srtp_info.mki_len; i++ )
871 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300872 ssl->dtls_srtp_info.mki_value[i] = buf[profile_length + 2 + 1 + i];
Ron Eldor591f1622018-01-22 12:30:04 +0200873 }
Ron Eldorb4655392018-07-05 18:25:39 +0300874
Ron Eldoref72faf2018-07-12 11:54:20 +0300875 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
876 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100877 }
878
Ron Eldor591f1622018-01-22 12:30:04 +0200879 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100880}
881#endif /* MBEDTLS_SSL_DTLS_SRTP */
882
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883/*
884 * Auxiliary functions for ServerHello parsing and related actions
885 */
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100888/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100889 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#if defined(MBEDTLS_ECDSA_C)
892static int ssl_check_key_curve( mbedtls_pk_context *pk,
893 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 const mbedtls_ecp_curve_info **crv = curves;
896 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100897
898 while( *crv != NULL )
899 {
900 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100901 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100902 crv++;
903 }
904
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100905 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100908
909/*
910 * Try picking a certificate for this ciphersuite,
911 * return 0 on success and -1 on failure.
912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913static int ssl_pick_cert( mbedtls_ssl_context *ssl,
914 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100917 mbedtls_pk_type_t pk_alg =
918 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200919 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922 if( ssl->handshake->sni_key_cert != NULL )
923 list = ssl->handshake->sni_key_cert;
924 else
925#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200926 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100929 return( 0 );
930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000932
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200933 if( list == NULL )
934 {
935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
936 return( -1 );
937 }
938
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100939 for( cur = list; cur != NULL; cur = cur->next )
940 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400941 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000943 cur->cert );
944
Gilles Peskinee198df52018-01-05 21:17:45 +0100945 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100948 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000949 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100950
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200951 /*
952 * This avoids sending the client a cert it'll reject based on
953 * keyUsage or other extensions.
954 *
955 * It also allows the user to provision different certificates for
956 * different uses based on keyUsage, eg if they want to avoid signing
957 * and decrypting with the same RSA key.
958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100960 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000963 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200964 continue;
965 }
966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#if defined(MBEDTLS_ECDSA_C)
968 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200969 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100972 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000973 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100974#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100975
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100976 /*
977 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
978 * present them a SHA-higher cert rather than failing if it's the only
979 * one we got that satisfies the other conditions.
980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
982 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100983 {
984 if( fallback == NULL )
985 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000988 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100989 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000990 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100991 }
992
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100993 /* If we get there, we got a winner */
994 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100995 }
996
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000997 if( cur == NULL )
998 cur = fallback;
999
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001000 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001001 if( cur != NULL )
1002 {
1003 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001006 return( 0 );
1007 }
1008
1009 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001012
1013/*
1014 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1015 * Sets ciphersuite_info only if the suite matches.
1016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1018 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001021
Hanno Becker7e5437a2017-04-28 17:15:26 +01001022#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001023 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001024 mbedtls_pk_type_t sig_type;
1025#endif
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001028 if( suite_info == NULL )
1029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001032 }
1033
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Hanno Beckerecea07d2018-11-07 16:24:35 +00001035 suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001036
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001037 if( suite_info->min_minor_ver > ssl->minor_ver ||
1038 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001041 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001042 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001045 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001047 return( 0 );
1048#endif
1049
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001050#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001051 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001055 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001056 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001057#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001058
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001059#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1060 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001061 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001062 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1064 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001065 return( 0 );
1066 }
1067#endif
1068
1069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1071 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001072 ( ssl->handshake->curves == NULL ||
1073 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001076 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001077 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001078 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001079#endif
1080
Gilles Peskineeccd8882020-03-10 12:19:08 +01001081#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001082 /* If the ciphersuite requires a pre-shared key and we don't
1083 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001085 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001088 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001089 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001090#endif
1091
Hanno Becker7e5437a2017-04-28 17:15:26 +01001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001093 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001094 /* If the ciphersuite requires signing, check whether
1095 * a suitable hash algorithm is present. */
1096 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1097 {
1098 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1099 if( sig_type != MBEDTLS_PK_NONE &&
1100 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1101 {
1102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1103 "for signature algorithm %d", sig_type ) );
1104 return( 0 );
1105 }
1106 }
1107
1108#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001109 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001112 /*
1113 * Final check: if ciphersuite requires us to have a
1114 * certificate/key of a particular type:
1115 * - select the appropriate certificate if we have one, or
1116 * - try the next ciphersuite if we don't
1117 * This must be done last since we modify the key_cert list.
1118 */
1119 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001122 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001123 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001124 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001125#endif
1126
1127 *ciphersuite_info = suite_info;
1128 return( 0 );
1129}
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1132static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001133{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001134 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001135 unsigned int i, j;
1136 size_t n;
1137 unsigned int ciph_len, sess_len, chal_len;
1138 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001139 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_RENEGOTIATION)
1145 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001148 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1149 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001153
1154 buf = ssl->in_hdr;
1155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001159 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001161 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001163 buf[3], buf[4] ) );
1164
1165 /*
1166 * SSLv2 Client Hello
1167 *
1168 * Record layer:
1169 * 0 . 1 message length
1170 *
1171 * SSL layer:
1172 * 2 . 2 message type
1173 * 3 . 4 protocol version
1174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1176 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1179 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001180 }
1181
1182 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1183
1184 if( n < 17 || n > 512 )
1185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1187 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001188 }
1189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001191 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1192 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001193
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001194 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001197 " [%d:%d] < [%d:%d]",
1198 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001199 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1202 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1203 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001204 }
1205
Paul Bakker2fbefde2013-06-29 16:01:15 +02001206 ssl->handshake->max_major_ver = buf[3];
1207 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001212 return( ret );
1213 }
1214
1215 ssl->handshake->update_checksum( ssl, buf + 2, n );
1216
1217 buf = ssl->in_msg;
1218 n = ssl->in_left - 5;
1219
1220 /*
1221 * 0 . 1 ciphersuitelist length
1222 * 2 . 3 session id length
1223 * 4 . 5 challenge length
1224 * 6 . .. ciphersuitelist
1225 * .. . .. session id
1226 * .. . .. challenge
1227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001229
1230 ciph_len = ( buf[0] << 8 ) | buf[1];
1231 sess_len = ( buf[2] << 8 ) | buf[3];
1232 chal_len = ( buf[4] << 8 ) | buf[5];
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001235 ciph_len, sess_len, chal_len ) );
1236
1237 /*
1238 * Make sure each parameter length is valid
1239 */
1240 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1243 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001244 }
1245
1246 if( sess_len > 32 )
1247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1249 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001250 }
1251
1252 if( chal_len < 8 || chal_len > 32 )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1255 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001256 }
1257
1258 if( n != 6 + ciph_len + sess_len + chal_len )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1261 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001262 }
1263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001265 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001267 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001269 buf + 6 + ciph_len + sess_len, chal_len );
1270
1271 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001272 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001273 memset( ssl->session_negotiate->id, 0,
1274 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001275 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001276
1277 p += sess_len;
1278 memset( ssl->handshake->randbytes, 0, 64 );
1279 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1280
1281 /*
1282 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1283 */
1284 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1289#if defined(MBEDTLS_SSL_RENEGOTIATION)
1290 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001293 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001294
Gilles Peskinec94f7352017-05-10 16:37:56 +02001295 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1296 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#endif /* MBEDTLS_SSL_RENEGOTIATION */
1300 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001301 break;
1302 }
1303 }
1304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001306 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1307 {
1308 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1310 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001313
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001314 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1319 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001322 }
1323
1324 break;
1325 }
1326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001328
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001329 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001330 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001331 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001333 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001334 for( i = 0; ciphersuites[i] != 0; i++ )
1335#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001336 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001337 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001338#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001339 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001340 if( p[0] != 0 ||
1341 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1342 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1343 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001344
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001345 got_common_suite = 1;
1346
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001347 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1348 &ciphersuite_info ) ) != 0 )
1349 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001350
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001351 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001352 goto have_ciphersuite_v2;
1353 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001354
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001355 if( got_common_suite )
1356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001358 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001360 }
1361 else
1362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1364 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001365 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001366
1367have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001369
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001370 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001371 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001372
1373 /*
1374 * SSLv2 Client Hello relevant renegotiation security checks
1375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001377 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001380 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1381 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001383 }
1384
1385 ssl->in_left = 0;
1386 ssl->state++;
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001389
1390 return( 0 );
1391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001393
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001394/* This function doesn't alert on errors that happen early during
1395 ClientHello parsing because they might indicate that the client is
1396 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001398{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001399 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001400 size_t i, j;
1401 size_t ciph_offset, comp_offset, ext_offset;
1402 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001404 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001405#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001406 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001408 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001409#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001410 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001411 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001413 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Hanno Becker7e5437a2017-04-28 17:15:26 +01001415 /* If there is no signature-algorithm extension present,
1416 * we need to fall back to the default values for allowed
1417 * signature-hash pairs. */
1418#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001419 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001420 int sig_hash_alg_ext_present = 0;
1421#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001422 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001427read_record_header:
1428#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001429 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001431 * otherwise read it ourselves manually in order to support SSLv2
1432 * ClientHello, which doesn't use the same record layer format.
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_SSL_RENEGOTIATION)
1435 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001436#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001439 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001440 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001442 return( ret );
1443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 }
1445
1446 buf = ssl->in_hdr;
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001451#endif
1452 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001453 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001454#endif
1455
Hanno Becker5903de42019-05-03 14:46:38 +01001456 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001457
Paul Bakkerec636f32012-09-09 19:17:02 +00001458 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001459 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001460 *
1461 * Record layer:
1462 * 0 . 0 message type
1463 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001464 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001465 * 3 . 4 message length
1466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001468 buf[0] ) );
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1473 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001474 }
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001477 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001480 buf[1], buf[2] ) );
1481
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001482 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001483
1484 /* According to RFC 5246 Appendix E.1, the version here is typically
1485 * "{03,00}, the lowest version number supported by the client, [or] the
1486 * value of ClientHello.client_version", so the only meaningful check here
1487 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1491 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001494 /* For DTLS if this is the initial handshake, remember the client sequence
1495 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001497 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_SSL_RENEGOTIATION)
1499 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001500#endif
1501 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001502 {
1503 /* Epoch should be 0 for initial handshakes */
1504 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1507 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001508 }
1509
Hanno Becker19859472018-08-06 09:40:20 +01001510 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1513 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001516 ssl->next_record_offset = 0;
1517 ssl->in_left = 0;
1518 goto read_record_header;
1519 }
1520
1521 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001523#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001524 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001526
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001527 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_SSL_RENEGOTIATION)
1530 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001533 msg_len = ssl->in_hslen;
1534 }
1535 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001536#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001537 {
Angus Grattond8213d02016-05-25 20:56:48 +10001538 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1541 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001542 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001543
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001544 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001545 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001548 return( ret );
1549 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001550
1551 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001553 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001554 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001555 else
1556#endif
1557 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001558 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001559
1560 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001563
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001564 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001565
1566 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001567 * Handshake layer:
1568 * 0 . 0 handshake type
1569 * 1 . 3 handshake length
1570 * 4 . 5 DTLS only: message seqence number
1571 * 6 . 8 DTLS only: fragment offset
1572 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1577 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001578 }
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1585 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001586 }
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001589 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1590
1591 /* We don't support fragmentation of ClientHello (yet?) */
1592 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1596 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001597 }
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001600 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001601 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001602 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001603 * Copy the client's handshake message_seq on initial handshakes,
1604 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_SSL_RENEGOTIATION)
1607 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001608 {
1609 /* This couldn't be done in ssl_prepare_handshake_record() */
1610 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1611 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001612
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001613 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001616 "%d (expected %d)", cli_msg_seq,
1617 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001619 }
1620
1621 ssl->handshake->in_msg_seq++;
1622 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001623 else
1624#endif
1625 {
1626 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1627 ssl->in_msg[5];
1628 ssl->handshake->out_msg_seq = cli_msg_seq;
1629 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1630 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001631
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001632 /*
1633 * For now we don't support fragmentation, so make sure
1634 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001635 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001636 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1637 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1640 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001641 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 buf += mbedtls_ssl_hs_hdr_len( ssl );
1646 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001647
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001648 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001649 * ClientHello layer:
1650 * 0 . 1 protocol version
1651 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1652 * 34 . 35 session id length (1 byte)
1653 * 35 . 34+x session id
1654 * 35+x . 35+x DTLS only: cookie length (1 byte)
1655 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001656 * .. . .. ciphersuite list length (2 bytes)
1657 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001658 * .. . .. compression alg. list length (1 byte)
1659 * .. . .. compression alg. list
1660 * .. . .. extensions length (2 bytes, optional)
1661 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001662 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001663
1664 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001665 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001666 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1667 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001668 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001669 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1672 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001673 }
1674
1675 /*
1676 * Check and save the protocol version
1677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001681 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001682
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001683 ssl->handshake->max_major_ver = ssl->major_ver;
1684 ssl->handshake->max_minor_ver = ssl->minor_ver;
1685
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001686 if( ssl->major_ver < ssl->conf->min_major_ver ||
1687 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001690 " [%d:%d] < [%d:%d]",
1691 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001692 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1694 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001696 }
1697
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001698 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001699 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001700 ssl->major_ver = ssl->conf->max_major_ver;
1701 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001702 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001703 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1704 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001705
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001706 /*
1707 * Save client random (inc. Unix time)
1708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001710
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001711 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001712
1713 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001714 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001715 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001716 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001717
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001718 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001719 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001725 }
1726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001728
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001729 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001730 memset( ssl->session_negotiate->id, 0,
1731 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001732 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001733 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001734
1735 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001736 * Check the cookie length and content
1737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001740 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001741 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001742 cookie_len = buf[cookie_offset];
1743
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001744 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1748 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001750 }
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001753 buf + cookie_offset + 1, cookie_len );
1754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001756 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#if defined(MBEDTLS_SSL_RENEGOTIATION)
1758 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001759#endif
1760 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001761 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001762 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001763 buf + cookie_offset + 1, cookie_len,
1764 ssl->cli_id, ssl->cli_id_len ) != 0 )
1765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001767 ssl->handshake->verify_cookie_len = 1;
1768 }
1769 else
1770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001772 ssl->handshake->verify_cookie_len = 0;
1773 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001774 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001775 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001777 {
1778 /* We know we didn't send a cookie, so it should be empty */
1779 if( cookie_len != 0 )
1780 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001781 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1783 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001784 }
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001787 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001788
1789 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001790 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001791 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001792 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001793 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001794 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001796 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001797
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001798 ciph_len = ( buf[ciph_offset + 0] << 8 )
1799 | ( buf[ciph_offset + 1] );
1800
1801 if( ciph_len < 2 ||
1802 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1803 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1807 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001809 }
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001812 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001813
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001814 /*
1815 * Check the compression algorithms length and pick one
1816 */
1817 comp_offset = ciph_offset + 2 + ciph_len;
1818
1819 comp_len = buf[comp_offset];
1820
1821 if( comp_len < 1 ||
1822 comp_len > 16 ||
1823 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001826 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1827 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001829 }
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001832 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1835#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001836 for( i = 0; i < comp_len; ++i )
1837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001841 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 }
1843 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844#endif
1845
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001846 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001848 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001850#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001851
Janos Follathc6dab2b2016-05-23 14:27:02 +01001852 /* Do not parse the extensions if the protocol is SSLv3 */
1853#if defined(MBEDTLS_SSL_PROTO_SSL3)
1854 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1855 {
1856#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001857 /*
1858 * Check the extension length
1859 */
1860 ext_offset = comp_offset + 1 + comp_len;
1861 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001862 {
Simon Butcher584a5472016-05-23 16:24:52 +01001863 if( msg_len < ext_offset + 2 )
1864 {
1865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001866 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1867 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001868 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1869 }
1870
1871 ext_len = ( buf[ext_offset + 0] << 8 )
1872 | ( buf[ext_offset + 1] );
1873
1874 if( ( ext_len > 0 && ext_len < 4 ) ||
1875 msg_len != ext_offset + 2 + ext_len )
1876 {
1877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1879 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001880 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1881 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001882 }
Simon Butcher584a5472016-05-23 16:24:52 +01001883 else
1884 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001885
Simon Butcher584a5472016-05-23 16:24:52 +01001886 ext = buf + ext_offset + 2;
1887 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001888
Simon Butcher584a5472016-05-23 16:24:52 +01001889 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001890 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001891 unsigned int ext_id;
1892 unsigned int ext_size;
1893 if ( ext_len < 4 ) {
1894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1896 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1897 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1898 }
1899 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1900 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001901
Simon Butcher584a5472016-05-23 16:24:52 +01001902 if( ext_size + 4 > ext_len )
1903 {
1904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001905 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1906 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001907 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1908 }
1909 switch( ext_id )
1910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001912 case MBEDTLS_TLS_EXT_SERVERNAME:
1913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1914 if( ssl->conf->f_sni == NULL )
1915 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001916
Simon Butcher584a5472016-05-23 16:24:52 +01001917 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1918 if( ret != 0 )
1919 return( ret );
1920 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001922
Simon Butcher584a5472016-05-23 16:24:52 +01001923 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001926 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001927#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001928
Simon Butcher584a5472016-05-23 16:24:52 +01001929 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1930 if( ret != 0 )
1931 return( ret );
1932 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001935 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001936 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1938
Simon Butcher584a5472016-05-23 16:24:52 +01001939 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1940 if( ret != 0 )
1941 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001942
1943 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001944 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001946 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001947
Robert Cragie136884c2015-10-02 13:34:31 +01001948#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001949 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001950 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001952
Simon Butcher584a5472016-05-23 16:24:52 +01001953 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1954 if( ret != 0 )
1955 return( ret );
1956 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001957
Simon Butcher584a5472016-05-23 16:24:52 +01001958 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1960 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001961
Simon Butcher584a5472016-05-23 16:24:52 +01001962 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1963 if( ret != 0 )
1964 return( ret );
1965 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001966#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1967 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001968
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001969#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001970 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001972
Simon Butcher584a5472016-05-23 16:24:52 +01001973 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1974 if( ret != 0 )
1975 return( ret );
1976 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001977#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001980 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001982
Simon Butcher584a5472016-05-23 16:24:52 +01001983 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1984 if( ret != 0 )
1985 return( ret );
1986 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001990 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001992
Simon Butcher584a5472016-05-23 16:24:52 +01001993 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1994 if( ret != 0 )
1995 return( ret );
1996 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001998
Hanno Beckera0e20d02019-05-15 14:03:01 +01001999#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01002000 case MBEDTLS_TLS_EXT_CID:
2001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2002
2003 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2004 if( ret != 0 )
2005 return( ret );
2006 break;
2007#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002010 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002012
Simon Butcher584a5472016-05-23 16:24:52 +01002013 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2014 if( ret != 0 )
2015 return( ret );
2016 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01002020 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002022
Simon Butcher584a5472016-05-23 16:24:52 +01002023 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
2024 if( ret != 0 )
2025 return( ret );
2026 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01002030 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2031 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002032
Simon Butcher584a5472016-05-23 16:24:52 +01002033 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2034 if( ret != 0 )
2035 return( ret );
2036 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002040 case MBEDTLS_TLS_EXT_ALPN:
2041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002042
Simon Butcher584a5472016-05-23 16:24:52 +01002043 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2044 if( ret != 0 )
2045 return( ret );
2046 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002048
Johan Pascalb62bb512015-12-03 21:56:45 +01002049#if defined(MBEDTLS_SSL_DTLS_SRTP)
2050 case MBEDTLS_TLS_EXT_USE_SRTP:
2051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2052 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2053 if ( ret != 0 )
2054 return( ret );
2055 break;
2056#endif /* MBEDTLS_SSL_DTLS_SRTP */
2057
Simon Butcher584a5472016-05-23 16:24:52 +01002058 default:
2059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
2060 ext_id ) );
2061 }
2062
2063 ext_len -= 4 + ext_size;
2064 ext += 4 + ext_size;
2065
2066 if( ext_len > 0 && ext_len < 4 )
2067 {
2068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002069 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2070 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01002071 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2072 }
Paul Bakker48916f92012-09-16 19:57:18 +00002073 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002074#if defined(MBEDTLS_SSL_PROTO_SSL3)
2075 }
2076#endif
2077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002079 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
2082 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002083 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002085
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002086 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002087 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2091 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002094 }
2095
2096 break;
2097 }
2098 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002100
Hanno Becker7e5437a2017-04-28 17:15:26 +01002101#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002102 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002103
2104 /*
2105 * Try to fall back to default hash SHA1 if the client
2106 * hasn't provided any preferred signature-hash combinations.
2107 */
2108 if( sig_hash_alg_ext_present == 0 )
2109 {
2110 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2111
2112 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2113 md_default = MBEDTLS_MD_NONE;
2114
2115 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2116 }
2117
2118#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002119 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002120
Paul Bakker48916f92012-09-16 19:57:18 +00002121 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002122 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2123 */
2124 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2129#if defined(MBEDTLS_SSL_RENEGOTIATION)
2130 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002131 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2133 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002134 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2135 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002137 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002138#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002140 break;
2141 }
2142 }
2143
2144 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002145 * Renegotiation security checks
2146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002148 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002151 handshake_failure = 1;
2152 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_SSL_RENEGOTIATION)
2154 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2155 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002156 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002159 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002160 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2162 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002163 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002166 handshake_failure = 1;
2167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2169 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002170 renegotiation_info_seen == 1 )
2171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002173 handshake_failure = 1;
2174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002176
2177 if( handshake_failure == 1 )
2178 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002179 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2180 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002182 }
Paul Bakker380da532012-04-18 16:10:25 +00002183
Paul Bakker41c83d32013-03-20 14:39:14 +01002184 /*
2185 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002186 * (At the end because we need information from the EC-based extensions
2187 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002188 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002189 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002190 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002191 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002193 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002194 for( i = 0; ciphersuites[i] != 0; i++ )
2195#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002196 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002197 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002198#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002199 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002200 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2201 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2202 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002203
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002204 got_common_suite = 1;
2205
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002206 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2207 &ciphersuite_info ) ) != 0 )
2208 return( ret );
2209
2210 if( ciphersuite_info != NULL )
2211 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002212 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002213
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002214 if( got_common_suite )
2215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002217 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002218 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2219 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002221 }
2222 else
2223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002225 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2226 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002228 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002229
2230have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002232
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002233 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002234 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002235
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 ssl->state++;
2237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002239 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002241#endif
2242
Hanno Becker7e5437a2017-04-28 17:15:26 +01002243 /* Debugging-only output for testsuite */
2244#if defined(MBEDTLS_DEBUG_C) && \
2245 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002246 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002247 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2248 {
2249 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2250 if( sig_alg != MBEDTLS_PK_NONE )
2251 {
2252 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2253 sig_alg );
2254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2255 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2256 }
2257 else
2258 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2260 "%d - should not happen", sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002261 }
2262 }
2263#endif
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
2267 return( 0 );
2268}
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2271static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002272 unsigned char *buf,
2273 size_t *olen )
2274{
2275 unsigned char *p = buf;
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002278 {
2279 *olen = 0;
2280 return;
2281 }
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2286 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002287
2288 *p++ = 0x00;
2289 *p++ = 0x00;
2290
2291 *olen = 4;
2292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002294
Hanno Beckera0e20d02019-05-15 14:03:01 +01002295#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002296static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2297 unsigned char *buf,
2298 size_t *olen )
2299{
2300 unsigned char *p = buf;
2301 size_t ext_len;
2302 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2303
2304 *olen = 0;
2305
2306 /* Skip writing the extension if we don't want to use it or if
2307 * the client hasn't offered it. */
2308 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2309 return;
2310
2311 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2312 * which is at most 255, so the increment cannot overflow. */
2313 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2314 {
2315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2316 return;
2317 }
2318
2319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2320
2321 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002322 * Quoting draft-ietf-tls-dtls-connection-id-05
2323 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002324 *
2325 * struct {
2326 * opaque cid<0..2^8-1>;
2327 * } ConnectionId;
2328 */
2329
2330 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2331 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2332 ext_len = (size_t) ssl->own_cid_len + 1;
2333 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2334 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2335
2336 *p++ = (uint8_t) ssl->own_cid_len;
2337 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2338
2339 *olen = ssl->own_cid_len + 5;
2340}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002341#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2344static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002345 unsigned char *buf,
2346 size_t *olen )
2347{
2348 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2350 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002351
Hanno Becker27b34d52017-10-20 14:24:51 +01002352 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002354 {
2355 *olen = 0;
2356 return;
2357 }
2358
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002359 /*
2360 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2361 * from a client and then selects a stream or Authenticated Encryption
2362 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2363 * encrypt-then-MAC response extension back to the client."
2364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002366 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2368 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002369 {
2370 *olen = 0;
2371 return;
2372 }
2373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2377 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002378
2379 *p++ = 0x00;
2380 *p++ = 0x00;
2381
2382 *olen = 4;
2383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2387static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002388 unsigned char *buf,
2389 size_t *olen )
2390{
2391 unsigned char *p = buf;
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2394 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002395 {
2396 *olen = 0;
2397 return;
2398 }
2399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002401 "extension" ) );
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2404 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002405
2406 *p++ = 0x00;
2407 *p++ = 0x00;
2408
2409 *olen = 4;
2410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2414static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002415 unsigned char *buf,
2416 size_t *olen )
2417{
2418 unsigned char *p = buf;
2419
2420 if( ssl->handshake->new_session_ticket == 0 )
2421 {
2422 *olen = 0;
2423 return;
2424 }
2425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2429 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002430
2431 *p++ = 0x00;
2432 *p++ = 0x00;
2433
2434 *olen = 4;
2435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002439 unsigned char *buf,
2440 size_t *olen )
2441{
2442 unsigned char *p = buf;
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002445 {
2446 *olen = 0;
2447 return;
2448 }
2449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2453 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#if defined(MBEDTLS_SSL_RENEGOTIATION)
2456 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002457 {
2458 *p++ = 0x00;
2459 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2460 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002461
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002462 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2463 p += ssl->verify_data_len;
2464 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2465 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002466 }
2467 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002469 {
2470 *p++ = 0x00;
2471 *p++ = 0x01;
2472 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002473 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002474
2475 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002476}
2477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2479static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002480 unsigned char *buf,
2481 size_t *olen )
2482{
2483 unsigned char *p = buf;
2484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002486 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002487 *olen = 0;
2488 return;
2489 }
2490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2494 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002495
2496 *p++ = 0x00;
2497 *p++ = 1;
2498
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002499 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002500
2501 *olen = 5;
2502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002504
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002505#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002506 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002508 unsigned char *buf,
2509 size_t *olen )
2510{
2511 unsigned char *p = buf;
2512 ((void) ssl);
2513
Paul Bakker677377f2013-10-28 12:54:26 +01002514 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002516 {
2517 *olen = 0;
2518 return;
2519 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2524 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002525
2526 *p++ = 0x00;
2527 *p++ = 2;
2528
2529 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002531
2532 *olen = 6;
2533}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002534#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002535
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002536#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2537static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2538 unsigned char *buf,
2539 size_t *olen )
2540{
Janos Follath865b3eb2019-12-16 11:46:15 +00002541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002542 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002543 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002544 size_t kkpp_len;
2545
2546 *olen = 0;
2547
2548 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002549 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002550 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2551 return;
2552
2553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2554
2555 if( end - p < 4 )
2556 {
2557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2558 return;
2559 }
2560
2561 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2562 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2563
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002564 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2565 p + 2, end - p - 2, &kkpp_len,
2566 ssl->conf->f_rng, ssl->conf->p_rng );
2567 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002568 {
2569 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2570 return;
2571 }
2572
2573 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2574 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2575
2576 *olen = kkpp_len + 4;
2577}
2578#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#if defined(MBEDTLS_SSL_ALPN )
2581static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002582 unsigned char *buf, size_t *olen )
2583{
2584 if( ssl->alpn_chosen == NULL )
2585 {
2586 *olen = 0;
2587 return;
2588 }
2589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002591
2592 /*
2593 * 0 . 1 ext identifier
2594 * 2 . 3 ext length
2595 * 4 . 5 protocol list length
2596 * 6 . 6 protocol name length
2597 * 7 . 7+n protocol name
2598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2600 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002601
2602 *olen = 7 + strlen( ssl->alpn_chosen );
2603
2604 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2605 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2606
2607 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2608 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2609
2610 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2611
2612 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002615
Ron Eldor3adb9922017-12-21 10:15:08 +02002616#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002617static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002618 unsigned char *buf,
2619 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002620{
Ron Eldor75870ec2018-12-06 17:31:55 +02002621 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002622 uint16_t profile_value = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002623
Ron Eldor3adb9922017-12-21 10:15:08 +02002624 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
Johan Pascalb62bb512015-12-03 21:56:45 +01002625 {
2626 *olen = 0;
2627 return;
2628 }
2629
2630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2631
Ron Eldor591f1622018-01-22 12:30:04 +02002632 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
Ron Eldoref72faf2018-07-12 11:54:20 +03002633 ssl->dtls_srtp_info.mki_len != 0 )
Ron Eldor591f1622018-01-22 12:30:04 +02002634 {
2635 mki_len = ssl->dtls_srtp_info.mki_len;
2636 }
2637
Johan Pascalb62bb512015-12-03 21:56:45 +01002638 /* extension */
2639 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2640 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002641 /*
2642 * total length 5 and mki value: only one profile(2 bytes)
2643 * and length(2 bytes) and srtp_mki )
2644 */
Ron Eldor591f1622018-01-22 12:30:04 +02002645 ext_len = 5 + mki_len;
2646 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2647 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002648
2649 /* protection profile length: 2 */
2650 buf[4] = 0x00;
2651 buf[5] = 0x02;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002652 profile_value = mbedtls_ssl_get_srtp_profile_iana_value( ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
2653 if( profile_value != 0xFFFF )
2654 {
2655 buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF );
2656 buf[7] = (unsigned char)( profile_value & 0xFF );
2657 }
2658 else
2659 {
2660 *olen = 0;
2661 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002662 }
2663
Ron Eldor591f1622018-01-22 12:30:04 +02002664 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002665 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002666
Ron Eldor591f1622018-01-22 12:30:04 +02002667 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002668}
2669#endif /* MBEDTLS_SSL_DTLS_SRTP */
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2672static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002673{
Janos Follath865b3eb2019-12-16 11:46:15 +00002674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002675 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002676 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002679
2680 /*
2681 * struct {
2682 * ProtocolVersion server_version;
2683 * opaque cookie<0..2^8-1>;
2684 * } HelloVerifyRequest;
2685 */
2686
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002687 /* The RFC is not clear on this point, but sending the actual negotiated
2688 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002690 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002692 p += 2;
2693
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002694 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002695 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002699 }
2700
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002701 /* Skip length byte until we know the length */
2702 cookie_len_byte = p++;
2703
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002704 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002705 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002706 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002709 return( ret );
2710 }
2711
2712 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002715
2716 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2718 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002721
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002722 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002723 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002725 return( ret );
2726 }
2727
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002728#if defined(MBEDTLS_SSL_PROTO_DTLS)
2729 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2730 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2731 {
2732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2733 return( ret );
2734 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002735#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002738
2739 return( 0 );
2740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002746 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002747#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002749 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 unsigned char *buf, *p;
2751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002756 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002760
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002761 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002762 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002764
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002765 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2768 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002769 }
2770
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 /*
2772 * 0 . 0 handshake type
2773 * 1 . 3 handshake length
2774 * 4 . 5 protocol version
2775 * 6 . 9 UNIX time()
2776 * 10 . 37 random bytes
2777 */
2778 buf = ssl->out_msg;
2779 p = buf + 4;
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002782 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002783 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002786 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002789 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 *p++ = (unsigned char)( t >> 24 );
2791 *p++ = (unsigned char)( t >> 16 );
2792 *p++ = (unsigned char)( t >> 8 );
2793 *p++ = (unsigned char)( t );
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002796#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002797 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002798 return( ret );
2799
2800 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002803 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002804 return( ret );
2805
2806 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
Paul Bakker48916f92012-09-16 19:57:18 +00002808 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
2812 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002813 * Resume is 0 by default, see ssl_handshake_init().
2814 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2815 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002817 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_RENEGOTIATION)
2819 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002820#endif
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002821 ssl->session_negotiate->id_len != 0 &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002822 ssl->conf->f_get_cache != NULL &&
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002823 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002826 ssl->handshake->resume = 1;
2827 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002829 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002830 {
2831 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002832 * New session, create a new session id,
2833 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 ssl->state++;
2836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002838 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002839#endif
2840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002842 if( ssl->handshake->new_session_ticket != 0 )
2843 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002844 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002845 memset( ssl->session_negotiate->id, 0, 32 );
2846 }
2847 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002849 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002850 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002851 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002852 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002853 return( ret );
2854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 }
2856 else
2857 {
2858 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002859 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002861 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002867 return( ret );
2868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 }
2870
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002871 /*
2872 * 38 . 38 session id length
2873 * 39 . 38+n session id
2874 * 39+n . 40+n chosen ciphersuite
2875 * 41+n . 41+n chosen compression alg.
2876 * 42+n . 43+n extensions length
2877 * 44+n . 43+n+m extensions
2878 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002879 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2880 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2881 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2884 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002886 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker48916f92012-09-16 19:57:18 +00002888 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2889 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2890 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2893 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2894 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002895 ssl->session_negotiate->compression ) );
2896
Janos Follathc6dab2b2016-05-23 14:27:02 +01002897 /* Do not write the extensions if the protocol is SSLv3 */
2898#if defined(MBEDTLS_SSL_PROTO_SSL3)
2899 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2900 {
2901#endif
2902
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002903 /*
2904 * First write extensions, then the total length
2905 */
2906 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2907 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002910 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2911 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002912#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002915 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2916 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002917#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002918
Hanno Beckera0e20d02019-05-15 14:03:01 +01002919#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002920 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2921 ext_len += olen;
2922#endif
2923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002925 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2926 ext_len += olen;
2927#endif
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002930 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2931 ext_len += olen;
2932#endif
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002935 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2936 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002937#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002938
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002939#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002940 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002941 if ( mbedtls_ssl_ciphersuite_uses_ec(
2942 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2943 {
2944 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2945 ext_len += olen;
2946 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002947#endif
2948
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002949#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2950 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2951 ext_len += olen;
2952#endif
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002955 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2956 ext_len += olen;
2957#endif
2958
Johan Pascalb62bb512015-12-03 21:56:45 +01002959#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002960 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
Johan Pascalb62bb512015-12-03 21:56:45 +01002961 ext_len += olen;
2962#endif
2963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002965
Paul Bakkera7036632014-04-30 10:15:38 +02002966 if( ext_len > 0 )
2967 {
2968 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2969 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2970 p += ext_len;
2971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Janos Follathc6dab2b2016-05-23 14:27:02 +01002973#if defined(MBEDTLS_SSL_PROTO_SSL3)
2974 }
2975#endif
2976
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2979 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002981 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
2985 return( ret );
2986}
2987
Gilles Peskineeccd8882020-03-10 12:19:08 +01002988#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002990{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002991 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002992 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002995
Hanno Becker77adddc2019-02-07 12:32:43 +00002996 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002999 ssl->state++;
3000 return( 0 );
3001 }
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003006#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003010 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003011 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03003012 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003013 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10003015 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003017 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
3021 ssl->state++;
3022
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003023#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3024 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3025 authmode = ssl->handshake->sni_authmode;
3026 else
3027#endif
Ron Eldor57cc70e2018-04-02 18:25:16 +03003028#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03003029 /*
3030 * check if we have a chosen srtp protection profile,
3031 * force verify mode to be at least OPTIONAL
3032 */
3033 if ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE &&
Ron Eldora9788042018-12-05 11:04:31 +02003034 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_NONE )
3035 {
Ron Eldor1c399bd2018-07-04 18:45:27 +03003036 authmode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Ron Eldor57cc70e2018-04-02 18:25:16 +03003037 }
Ron Eldoref72faf2018-07-12 11:54:20 +03003038 else
Ron Eldor57cc70e2018-04-02 18:25:16 +03003039#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003040 authmode = ssl->conf->authmode;
3041
Hanno Becker77adddc2019-02-07 12:32:43 +00003042 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003043 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 {
Ron Eldor57cc70e2018-04-02 18:25:16 +03003045#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldora9788042018-12-05 11:04:31 +02003046 /* check if we have a chosen srtp protection profile */
3047 if ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE )
3048 {
Ron Eldor57cc70e2018-04-02 18:25:16 +03003049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "should not happen" ) );
Ron Eldoref72faf2018-07-12 11:54:20 +03003050 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Ron Eldor57cc70e2018-04-02 18:25:16 +03003051 }
3052 else
3053 {
3054#endif
3055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3056 return( 0 );
3057#if defined(MBEDTLS_SSL_DTLS_SRTP)
3058 }
3059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 }
3061
3062 /*
3063 * 0 . 0 handshake type
3064 * 1 . 3 handshake length
3065 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003066 * 5 .. m-1 cert types
3067 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003068 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 * n .. n+1 length of all DNs
3070 * n+2 .. n+3 length of DN 1
3071 * n+4 .. ... Distinguished Name #1
3072 * ... .. ... length of DN 2, etc.
3073 */
3074 buf = ssl->out_msg;
3075 p = buf + 4;
3076
3077 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003078 * Supported certificate types
3079 *
3080 * ClientCertificateType certificate_types<1..2^8-1>;
3081 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003083 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085#if defined(MBEDTLS_RSA_C)
3086 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#if defined(MBEDTLS_ECDSA_C)
3089 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003090#endif
3091
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003092 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003093 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003094
Paul Bakker577e0062013-08-28 11:57:20 +02003095 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003097 /*
3098 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003099 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003100 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3101 *
3102 * struct {
3103 * HashAlgorithm hash;
3104 * SignatureAlgorithm signature;
3105 * } SignatureAndHashAlgorithm;
3106 *
3107 * enum { (255) } HashAlgorithm;
3108 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003111 {
Simon Butcher99000142016-10-13 17:21:01 +01003112 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003113
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003114 /*
3115 * Supported signature algorithms
3116 */
Simon Butcher99000142016-10-13 17:21:01 +01003117 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3118 {
3119 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3120
3121 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3122 continue;
3123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003125 p[2 + sa_len++] = hash;
3126 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003129 p[2 + sa_len++] = hash;
3130 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003131#endif
Simon Butcher99000142016-10-13 17:21:01 +01003132 }
Paul Bakker926af752012-11-23 13:38:07 +01003133
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003134 p[0] = (unsigned char)( sa_len >> 8 );
3135 p[1] = (unsigned char)( sa_len );
3136 sa_len += 2;
3137 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003141 /*
3142 * DistinguishedName certificate_authorities<0..2^16-1>;
3143 * opaque DistinguishedName<1..2^16-1>;
3144 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003147 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003148
3149 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003151 /* NOTE: If trusted certificates are provisioned
3152 * via a CA callback (configured through
3153 * `mbedtls_ssl_conf_ca_cb()`, then the
3154 * CertificateRequest is currently left empty. */
3155
Janos Follath088ce432017-04-10 12:42:31 +01003156#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3157 if( ssl->handshake->sni_ca_chain != NULL )
3158 crt = ssl->handshake->sni_ca_chain;
3159 else
3160#endif
3161 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003162
Janos Follath088ce432017-04-10 12:42:31 +01003163 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003164 {
irwirc9bc3002020-04-01 13:46:36 +03003165 /* It follows from RFC 5280 A.1 that this length
3166 * can be represented in at most 11 bits. */
3167 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003168
irwirc9bc3002020-04-01 13:46:36 +03003169 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003170 {
3171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3172 break;
3173 }
3174
3175 *p++ = (unsigned char)( dn_size >> 8 );
3176 *p++ = (unsigned char)( dn_size );
3177 memcpy( p, crt->subject_raw.p, dn_size );
3178 p += dn_size;
3179
3180 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3181
3182 total_dn_size += 2 + dn_size;
3183 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 }
3186
Paul Bakker926af752012-11-23 13:38:07 +01003187 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3189 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003190 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3191 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003193 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
3197 return( ret );
3198}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003199#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3202 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3203static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003204{
Janos Follath865b3eb2019-12-16 11:46:15 +00003205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3210 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003211 }
3212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3214 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3215 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003218 return( ret );
3219 }
3220
3221 return( 0 );
3222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3224 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003225
Gilles Peskineeccd8882020-03-10 12:19:08 +01003226#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003227 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003228static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003229 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003230{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003231 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3232 * signature length which will be added in ssl_write_server_key_exchange
3233 * after the call to ssl_prepare_server_key_exchange.
3234 * ssl_write_server_key_exchange also takes care of incrementing
3235 * ssl->out_msglen. */
3236 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003237 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003238 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003239 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003240 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003241 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3242 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003243 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003244 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003245 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003246 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003247 return( ret );
3248}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003249#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003250 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003251
Gilles Peskined3eb0612018-01-08 17:07:44 +01003252/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003253 * calculating the signature if any, but excluding formatting the
3254 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003255static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3256 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003259 ssl->handshake->ciphersuite_info;
3260
Gilles Peskineeccd8882020-03-10 12:19:08 +01003261#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3262#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003263 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003264#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3265#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003266
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003267 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003268#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003269 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003270#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003271
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003272 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003274 /*
3275 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003276 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003277 *
3278 */
3279
3280 /*
3281 * - ECJPAKE key exchanges
3282 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003283#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3284 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3285 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003287 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003288
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003289 ret = mbedtls_ecjpake_write_round_two(
3290 &ssl->handshake->ecjpake_ctx,
3291 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003292 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003293 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003294 if( ret != 0 )
3295 {
3296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3297 return( ret );
3298 }
3299
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003300 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003301 }
3302#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3303
Hanno Becker1aa267c2017-04-28 17:08:27 +01003304 /*
3305 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3306 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3307 * we use empty support identity hints here.
3308 **/
3309#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3311 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3312 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003313 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003314 ssl->out_msg[ssl->out_msglen++] = 0x00;
3315 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3318 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003319
Hanno Becker7e5437a2017-04-28 17:15:26 +01003320 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003321 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003322 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003323#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003324 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003325 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003327 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003328
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003329 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3330 {
3331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3333 }
3334
Paul Bakker41c83d32013-03-20 14:39:14 +01003335 /*
3336 * Ephemeral DH parameters:
3337 *
3338 * struct {
3339 * opaque dh_p<1..2^16-1>;
3340 * opaque dh_g<1..2^16-1>;
3341 * opaque dh_Ys<1..2^16-1>;
3342 * } ServerDHParams;
3343 */
Hanno Beckerab740562017-10-04 13:15:37 +01003344 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3345 &ssl->conf->dhm_P,
3346 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003347 {
Hanno Beckerab740562017-10-04 13:15:37 +01003348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003349 return( ret );
3350 }
Paul Bakker48916f92012-09-16 19:57:18 +00003351
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003352 if( ( ret = mbedtls_dhm_make_params(
3353 &ssl->handshake->dhm_ctx,
3354 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3355 ssl->out_msg + ssl->out_msglen, &len,
3356 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003359 return( ret );
3360 }
3361
Gilles Peskineeccd8882020-03-10 12:19:08 +01003362#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003363 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003364#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003365
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003366 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3369 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3370 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3371 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003372 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003373#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003374
Hanno Becker1aa267c2017-04-28 17:08:27 +01003375 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003376 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003377 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003378#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003379 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003381 /*
3382 * Ephemeral ECDH parameters:
3383 *
3384 * struct {
3385 * ECParameters curve_params;
3386 * ECPoint public;
3387 * } ServerECDHParams;
3388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003392 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003393
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003394 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003395 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003396 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3397 if( (*curve)->grp_id == *gid )
3398 goto curve_matching_done;
3399
3400curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003401 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3404 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003405 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003408
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003409 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3410 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003411 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003413 return( ret );
3414 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003415
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003416 if( ( ret = mbedtls_ecdh_make_params(
3417 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003418 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003419 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003420 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003423 return( ret );
3424 }
3425
Gilles Peskineeccd8882020-03-10 12:19:08 +01003426#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003427 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003428#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003429
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003430 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003431
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003432 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3433 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003434 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003435#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003436
Hanno Becker1aa267c2017-04-28 17:08:27 +01003437 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003438 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003439 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003440 * exchange parameters, compute and add the signature here.
3441 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003442 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003443#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003444 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003445 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003446 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003447 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003448 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003450
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003451 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003452 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003453 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003454 * to choose appropriate hash.
3455 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3456 * (RFC 4492, Sec. 5.4)
3457 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003458 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003459
3460 mbedtls_md_type_t md_alg;
3461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003463 mbedtls_pk_type_t sig_alg =
3464 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003466 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003467 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3468 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003469 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003470 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3471 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003474 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003475 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003477 }
3478 }
Paul Bakker577e0062013-08-28 11:57:20 +02003479 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3481#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3482 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003483 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003484 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003485 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003487 }
3488 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003489#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3490 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003491 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003492 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003494 }
3495
Hanno Becker7e5437a2017-04-28 17:15:26 +01003496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3497
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003498 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003499 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3502 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3503 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003504 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003505 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003506 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3507 dig_signed,
3508 dig_signed_len );
3509 if( ret != 0 )
3510 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003511 }
3512 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3514 MBEDTLS_SSL_PROTO_TLS1_1 */
3515#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3516 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3517 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003518 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003519 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003520 dig_signed,
3521 dig_signed_len,
3522 md_alg );
3523 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003524 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003525 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3528 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003532 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003533
Gilles Peskineebd652f2018-01-05 21:18:59 +01003534 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003535
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003536 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003537 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3540 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003541 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003542 /*
3543 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003544 * explicitly through a prefix to the signature.
3545 *
3546 * struct {
3547 * HashAlgorithm hash;
3548 * SignatureAlgorithm signature;
3549 * } SignatureAndHashAlgorithm;
3550 *
3551 * struct {
3552 * SignatureAndHashAlgorithm algorithm;
3553 * opaque signature<0..2^16-1>;
3554 * } DigitallySigned;
3555 *
3556 */
3557
Gilles Peskine1004c192018-01-08 16:59:14 +01003558 ssl->out_msg[ssl->out_msglen++] =
3559 mbedtls_ssl_hash_from_md_alg( md_alg );
3560 ssl->out_msg[ssl->out_msglen++] =
3561 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003564
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003565#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003566 if( ssl->conf->f_async_sign_start != NULL )
3567 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003568 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003569 mbedtls_ssl_own_cert( ssl ),
3570 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003571 switch( ret )
3572 {
3573 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3574 /* act as if f_async_sign was null */
3575 break;
3576 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003577 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003578 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003579 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003580 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003581 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3582 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003583 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003584 return( ret );
3585 }
3586 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003587#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003588
3589 if( mbedtls_ssl_own_key( ssl ) == NULL )
3590 {
3591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3592 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3593 }
3594
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003595 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3596 * signature length which will be added in ssl_write_server_key_exchange
3597 * after the call to ssl_prepare_server_key_exchange.
3598 * ssl_write_server_key_exchange also takes care of incrementing
3599 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003600 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3601 md_alg, hash, hashlen,
3602 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003603 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003604 ssl->conf->f_rng,
3605 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003608 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003609 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003610 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003611#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003612
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003613 return( 0 );
3614}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003615
Gilles Peskined3eb0612018-01-08 17:07:44 +01003616/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003617 * that do not include a ServerKeyExchange message, do nothing. Either
3618 * way, if successful, move on to the next step in the SSL state
3619 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003620static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3621{
Janos Follath865b3eb2019-12-16 11:46:15 +00003622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003623 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003624#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003625 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003626 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003627#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003628
Gilles Peskined3eb0612018-01-08 17:07:44 +01003629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3630
Gilles Peskineeccd8882020-03-10 12:19:08 +01003631#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003632 /* Extract static ECDH parameters and abort if ServerKeyExchange
3633 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003634 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3635 {
3636 /* For suites involving ECDH, extract DH parameters
3637 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003638#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003639 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3640 {
3641 ssl_get_ecdh_params_from_cert( ssl );
3642 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003643#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003644
3645 /* Key exchanges not involving ephemeral keys don't use
3646 * ServerKeyExchange, so end here. */
3647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3648 ssl->state++;
3649 return( 0 );
3650 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003651#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003652
Gilles Peskineeccd8882020-03-10 12:19:08 +01003653#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003654 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003655 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003656 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003657 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003658 {
3659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3660 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003661 }
3662 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003663#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003664 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003665 {
3666 /* ServerKeyExchange is needed. Prepare the message. */
3667 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003668 }
3669
3670 if( ret != 0 )
3671 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003672 /* If we're starting to write a new message, set ssl->out_msglen
3673 * to 0. But if we're resuming after an asynchronous message,
3674 * out_msglen is the amount of data written so far and mst be
3675 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003676 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3678 else
3679 ssl->out_msglen = 0;
3680 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003681 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003682
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003683 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003684 * ssl_prepare_server_key_exchange already wrote the signature
3685 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003686#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003687 if( signature_len != 0 )
3688 {
3689 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3690 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3691
3692 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3693 ssl->out_msg + ssl->out_msglen,
3694 signature_len );
3695
3696 /* Skip over the already-written signature */
3697 ssl->out_msglen += signature_len;
3698 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003699#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003700
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003701 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3703 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003704
3705 ssl->state++;
3706
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003707 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 return( ret );
3711 }
3712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715}
3716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003718{
Janos Follath865b3eb2019-12-16 11:46:15 +00003719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
3723 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3725 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
3727 ssl->state++;
3728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003732#endif
3733
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003734 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737 return( ret );
3738 }
3739
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003740#if defined(MBEDTLS_SSL_PROTO_DTLS)
3741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3742 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3743 {
3744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3745 return( ret );
3746 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003747#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003750
3751 return( 0 );
3752}
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3755 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3756static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003757 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003760 size_t n;
3761
3762 /*
3763 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3764 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003765 if( *p + 2 > end )
3766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3768 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003769 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003770
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003771 n = ( (*p)[0] << 8 ) | (*p)[1];
3772 *p += 2;
3773
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003774 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3777 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003778 }
3779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3783 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003784 }
3785
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003786 *p += n;
3787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003789
Paul Bakker70df2fb2013-04-17 17:19:09 +02003790 return( ret );
3791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3793 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3796 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003797
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003798#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003799static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3800 unsigned char *peer_pms,
3801 size_t *peer_pmslen,
3802 size_t peer_pmssize )
3803{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003804 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003805 peer_pms, peer_pmslen, peer_pmssize );
3806 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3807 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003808 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003809 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003810 }
3811 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3812 return( ret );
3813}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003814#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003815
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003816static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3817 const unsigned char *p,
3818 const unsigned char *end,
3819 unsigned char *peer_pms,
3820 size_t *peer_pmslen,
3821 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003822{
Janos Follath865b3eb2019-12-16 11:46:15 +00003823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003824 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3825 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3826 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003827
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003828#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003829 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003830 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003831 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003832 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3834 return( ssl_resume_decrypt_pms( ssl,
3835 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003836 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003837#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003838
3839 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003840 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3843 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3844 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003845 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003846 if ( p + 2 > end ) {
3847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3848 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3849 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003850 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3851 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3854 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003855 }
3856 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003857#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003858
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003859 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3862 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003863 }
3864
Gilles Peskine422ccab2018-01-11 18:29:01 +01003865 /*
3866 * Decrypt the premaster secret
3867 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003868#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003869 if( ssl->conf->f_async_decrypt_start != NULL )
3870 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003871 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003872 mbedtls_ssl_own_cert( ssl ),
3873 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003874 switch( ret )
3875 {
3876 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3877 /* act as if f_async_decrypt_start was null */
3878 break;
3879 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003880 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003881 return( ssl_resume_decrypt_pms( ssl,
3882 peer_pms,
3883 peer_pmslen,
3884 peer_pmssize ) );
3885 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003886 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003887 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3888 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003889 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003890 return( ret );
3891 }
3892 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003893#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003894
Gilles Peskine422ccab2018-01-11 18:29:01 +01003895 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3896 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3898 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3899 }
3900
3901 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003902 peer_pms, peer_pmslen, peer_pmssize,
3903 ssl->conf->f_rng, ssl->conf->p_rng );
3904 return( ret );
3905}
3906
3907static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3908 const unsigned char *p,
3909 const unsigned char *end,
3910 size_t pms_offset )
3911{
Janos Follath865b3eb2019-12-16 11:46:15 +00003912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003913 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3914 unsigned char ver[2];
3915 unsigned char fake_pms[48], peer_pms[48];
3916 unsigned char mask;
3917 size_t i, peer_pmslen;
3918 unsigned int diff;
3919
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003920 /* In case of a failure in decryption, the decryption may write less than
3921 * 2 bytes of output, but we always read the first two bytes. It doesn't
3922 * matter in the end because diff will be nonzero in that case due to
3923 * peer_pmslen being less than 48, and we only care whether diff is 0.
3924 * But do initialize peer_pms for robustness anyway. This also makes
3925 * memory analyzers happy (don't access uninitialized memory, even
3926 * if it's an unsigned char). */
3927 peer_pms[0] = peer_pms[1] = ~0;
3928
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003929 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3930 peer_pms,
3931 &peer_pmslen,
3932 sizeof( peer_pms ) );
3933
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003934#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003935 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3936 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003937#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003940 ssl->handshake->max_minor_ver,
3941 ssl->conf->transport, ver );
3942
3943 /* Avoid data-dependent branches while checking for invalid
3944 * padding, to protect against timing-based Bleichenbacher-type
3945 * attacks. */
3946 diff = (unsigned int) ret;
3947 diff |= peer_pmslen ^ 48;
3948 diff |= peer_pms[0] ^ ver[0];
3949 diff |= peer_pms[1] ^ ver[1];
3950
3951 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3952 /* MSVC has a warning about unary minus on unsigned, but this is
3953 * well-defined and precisely what we want to do here */
3954#if defined(_MSC_VER)
3955#pragma warning( push )
3956#pragma warning( disable : 4146 )
3957#endif
3958 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3959#if defined(_MSC_VER)
3960#pragma warning( pop )
3961#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003962
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003963 /*
3964 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3965 * must not cause the connection to end immediately; instead, send a
3966 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003967 * To protect against timing-based variants of the attack, we must
3968 * not have any branch that depends on whether the decryption was
3969 * successful. In particular, always generate the fake premaster secret,
3970 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003971 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003972 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003973 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003974 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003975 /* It's ok to abort on an RNG failure, since this does not reveal
3976 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003977 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003978 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003979
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003980#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003981 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003983#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003984
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003985 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3986 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003990 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003991 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003992
Gilles Peskine422ccab2018-01-11 18:29:01 +01003993 /* Set pms to either the true or the fake PMS, without
3994 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003995 for( i = 0; i < ssl->handshake->pmslen; i++ )
3996 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3997
3998 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4001 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02004002
Gilles Peskineeccd8882020-03-10 12:19:08 +01004003#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004005 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004006{
Paul Bakker6db455e2013-09-18 17:29:31 +02004007 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03004008 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004009
Hanno Becker845b9462018-10-26 12:07:29 +01004010 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4013 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004014 }
4015
4016 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004017 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02004018 */
Hanno Becker83c9f492017-06-26 13:52:14 +01004019 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4022 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004023 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004024
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004025 n = ( (*p)[0] << 8 ) | (*p)[1];
4026 *p += 2;
4027
irwir6527bd62019-09-21 18:51:25 +03004028 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4031 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004032 }
4033
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004034 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004035 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004036 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004038 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004039 else
Paul Bakker6db455e2013-09-18 17:29:31 +02004040 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01004041 /* Identity is not a big secret since clients send it in the clear,
4042 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004043 if( n != ssl->conf->psk_identity_len ||
4044 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02004045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004047 }
4048 }
4049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02004053 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4054 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004056 }
4057
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004058 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004059
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004060 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004061}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004062#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004065{
Janos Follath865b3eb2019-12-16 11:46:15 +00004066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004068 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004069
Hanno Beckere694c3e2017-12-27 21:34:08 +00004070 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004073
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004074#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004075 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4076 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4077 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4078 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004079 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004080 {
4081 /* We've already read a record and there is an asynchronous
4082 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004083 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4085 }
4086 else
4087#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004088 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 return( ret );
4092 }
4093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004095 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4100 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004101 }
4102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4106 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004107 }
4108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4110 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004111 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004112 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004115 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004116 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004117
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004118 if( p != end )
4119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4121 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004122 }
4123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004125 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004126 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004127 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004128 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4131 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004132 }
4133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004135 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004136 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4138#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4139 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4140 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4141 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4142 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4143 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4144 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4145 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004148 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4151 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004152 }
4153
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004154 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4155 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004158 &ssl->handshake->pmslen,
4159 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004161 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4164 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004165 }
4166
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004167 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4168 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4172 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4173 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4174 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4175#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4176 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004177 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004178 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004181 return( ret );
4182 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004183
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004184 if( p != end )
4185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4187 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004188 }
4189
Hanno Becker845b9462018-10-26 12:07:29 +01004190#if defined(MBEDTLS_USE_PSA_CRYPTO)
4191 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4192 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004193 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4195 else
4196#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004198 ciphersuite_info->key_exchange ) ) != 0 )
4199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004201 return( ret );
4202 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4206#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4207 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004208 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004209#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004210 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004211 {
4212 /* There is an asynchronous operation in progress to
4213 * decrypt the encrypted premaster secret, so skip
4214 * directly to resuming this operation. */
4215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4216 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4217 * won't actually use it, but maintain p anyway for robustness. */
4218 p += ssl->conf->psk_identity_len + 2;
4219 }
4220 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004221#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004222 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004225 return( ret );
4226 }
4227
Hanno Becker845b9462018-10-26 12:07:29 +01004228#if defined(MBEDTLS_USE_PSA_CRYPTO)
4229 /* Opaque PSKs are currently only supported for PSK-only. */
4230 if( ssl_use_opaque_psk( ssl ) == 1 )
4231 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4232#endif
4233
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004234 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004237 return( ret );
4238 }
4239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004241 ciphersuite_info->key_exchange ) ) != 0 )
4242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004244 return( ret );
4245 }
4246 }
4247 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4249#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4250 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004251 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004252 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004255 return( ret );
4256 }
4257 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004260 return( ret );
4261 }
4262
Hanno Becker845b9462018-10-26 12:07:29 +01004263#if defined(MBEDTLS_USE_PSA_CRYPTO)
4264 /* Opaque PSKs are currently only supported for PSK-only. */
4265 if( ssl_use_opaque_psk( ssl ) == 1 )
4266 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4267#endif
4268
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004269 if( p != end )
4270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4272 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004273 }
4274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004276 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004279 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004280 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004281 }
4282 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4284#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4285 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004286 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004287 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004290 return( ret );
4291 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004294 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4297 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004298 }
4299
Hanno Becker845b9462018-10-26 12:07:29 +01004300#if defined(MBEDTLS_USE_PSA_CRYPTO)
4301 /* Opaque PSKs are currently only supported for PSK-only. */
4302 if( ssl_use_opaque_psk( ssl ) == 1 )
4303 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4304#endif
4305
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004306 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4307 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004310 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004313 return( ret );
4314 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004315 }
4316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4318#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4319 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004320 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004321 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004324 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 }
4326 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004329#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4330 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4331 {
4332 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4333 p, end - p );
4334 if( ret != 0 )
4335 {
4336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4337 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4338 }
4339
4340 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4341 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4342 ssl->conf->f_rng, ssl->conf->p_rng );
4343 if( ret != 0 )
4344 {
4345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4346 return( ret );
4347 }
4348 }
4349 else
4350#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004359 return( ret );
4360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004361
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 ssl->state++;
4363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004365
4366 return( 0 );
4367}
4368
Gilles Peskineeccd8882020-03-10 12:19:08 +01004369#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004371{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004372 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004373 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004376
Hanno Becker77adddc2019-02-07 12:32:43 +00004377 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004380 ssl->state++;
4381 return( 0 );
4382 }
4383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004386}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004387#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004391 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004392 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004393 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004394 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4396 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004399 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004400 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004401 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004404
Hanno Becker2a831a42019-02-07 13:17:25 +00004405 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004408 ssl->state++;
4409 return( 0 );
4410 }
4411
Hanno Becker2a831a42019-02-07 13:17:25 +00004412#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4413 if( ssl->session_negotiate->peer_cert == NULL )
4414 {
4415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4416 ssl->state++;
4417 return( 0 );
4418 }
4419#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4420 if( ssl->session_negotiate->peer_cert_digest == NULL )
4421 {
4422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4423 ssl->state++;
4424 return( 0 );
4425 }
4426#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4427
Simon Butcher99000142016-10-13 17:21:01 +01004428 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004429 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004430 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004432 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 return( ret );
4434 }
4435
4436 ssl->state++;
4437
Simon Butcher99000142016-10-13 17:21:01 +01004438 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4440 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4443 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 }
4445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004447
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004448#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4449 peer_pk = &ssl->handshake->peer_pubkey;
4450#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4451 if( ssl->session_negotiate->peer_cert == NULL )
4452 {
4453 /* Should never happen */
4454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4455 }
4456 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4457#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4458
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004459 /*
4460 * struct {
4461 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4462 * opaque signature<0..2^16-1>;
4463 * } DigitallySigned;
4464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4466 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4467 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004470 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004471
4472 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004473 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004474 {
4475 hash_start += 16;
4476 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004478 }
Paul Bakker926af752012-11-23 13:38:07 +01004479 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004480 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4482 MBEDTLS_SSL_PROTO_TLS1_1 */
4483#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4484 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004485 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004486 if( i + 2 > ssl->in_hslen )
4487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4489 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004490 }
4491
Paul Bakker5121ce52009-01-03 21:22:43 +00004492 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004493 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 */
Simon Butcher99000142016-10-13 17:21:01 +01004495 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4496
4497 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004500 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004502 }
4503
Simon Butcher99000142016-10-13 17:21:01 +01004504#if !defined(MBEDTLS_MD_SHA1)
4505 if( MBEDTLS_MD_SHA1 == md_alg )
4506 hash_start += 16;
4507#endif
Paul Bakker926af752012-11-23 13:38:07 +01004508
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004509 /* Info from md_alg will be used instead */
4510 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004511
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004512 i++;
4513
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004514 /*
4515 * Signature
4516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4518 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004521 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004523 }
4524
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004525 /*
4526 * Check the certificate's key type matches the signature alg
4527 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004528 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4531 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004532 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004533
4534 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004535 }
4536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004541 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004542
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004543 if( i + 2 > ssl->in_hslen )
4544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4546 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004547 }
4548
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004549 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4550 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004551
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004552 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4555 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 }
4557
Simon Butcher99000142016-10-13 17:21:01 +01004558 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004559 {
4560 size_t dummy_hlen;
4561 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4562 }
Simon Butcher99000142016-10-13 17:21:01 +01004563
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004564 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004565 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004566 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 return( ret );
4570 }
4571
Simon Butcher99000142016-10-13 17:21:01 +01004572 mbedtls_ssl_update_handshake_status( ssl );
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004575
Paul Bakkered27a042013-04-18 22:46:23 +02004576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004577}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004578#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4581static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004582{
Janos Follath865b3eb2019-12-16 11:46:15 +00004583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004584 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004585 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4590 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004591
4592 /*
4593 * struct {
4594 * uint32 ticket_lifetime_hint;
4595 * opaque ticket<0..2^16-1>;
4596 * } NewSessionTicket;
4597 *
4598 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4599 * 8 . 9 ticket_len (n)
4600 * 10 . 9+n ticket content
4601 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004602
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004603 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004604 ssl->session_negotiate,
4605 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004606 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004607 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004608 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004610 tlen = 0;
4611 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004612
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004613 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4614 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4615 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4616 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4617
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004618 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4619 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004620
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004621 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004622
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004623 /*
4624 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4625 * ChangeCipherSpec share the same state.
4626 */
4627 ssl->handshake->new_session_ticket = 0;
4628
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004629 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004630 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004632 return( ret );
4633 }
4634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004636
4637 return( 0 );
4638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004640
Paul Bakker5121ce52009-01-03 21:22:43 +00004641/*
Paul Bakker1961b702013-01-25 14:49:24 +01004642 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004645{
4646 int ret = 0;
4647
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004648 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004654 return( ret );
4655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004657 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004659 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004660 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004661 return( ret );
4662 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004663#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004664
Paul Bakker1961b702013-01-25 14:49:24 +01004665 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 case MBEDTLS_SSL_HELLO_REQUEST:
4668 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 break;
4670
Paul Bakker1961b702013-01-25 14:49:24 +01004671 /*
4672 * <== ClientHello
4673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004675 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004676 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004678#if defined(MBEDTLS_SSL_PROTO_DTLS)
4679 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4680 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004681#endif
4682
Paul Bakker1961b702013-01-25 14:49:24 +01004683 /*
4684 * ==> ServerHello
4685 * Certificate
4686 * ( ServerKeyExchange )
4687 * ( CertificateRequest )
4688 * ServerHelloDone
4689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004691 ret = ssl_write_server_hello( ssl );
4692 break;
4693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4695 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004696 break;
4697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004699 ret = ssl_write_server_key_exchange( ssl );
4700 break;
4701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004703 ret = ssl_write_certificate_request( ssl );
4704 break;
4705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004707 ret = ssl_write_server_hello_done( ssl );
4708 break;
4709
4710 /*
4711 * <== ( Certificate/Alert )
4712 * ClientKeyExchange
4713 * ( CertificateVerify )
4714 * ChangeCipherSpec
4715 * Finished
4716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4718 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004719 break;
4720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004722 ret = ssl_parse_client_key_exchange( ssl );
4723 break;
4724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004726 ret = ssl_parse_certificate_verify( ssl );
4727 break;
4728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4730 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004731 break;
4732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 case MBEDTLS_SSL_CLIENT_FINISHED:
4734 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004735 break;
4736
4737 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004738 * ==> ( NewSessionTicket )
4739 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004740 * Finished
4741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4743#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004744 if( ssl->handshake->new_session_ticket != 0 )
4745 ret = ssl_write_new_session_ticket( ssl );
4746 else
Paul Bakkera503a632013-08-14 13:48:06 +02004747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004749 break;
4750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 case MBEDTLS_SSL_SERVER_FINISHED:
4752 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004753 break;
4754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 case MBEDTLS_SSL_FLUSH_BUFFERS:
4756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4757 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004758 break;
4759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4761 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004762 break;
4763
4764 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 }
4768
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 return( ret );
4770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771#endif /* MBEDTLS_SSL_SRV_C */