blob: 5c56a70d914ba35c28009de6866592fb7c327452 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
SimonBd5800b72016-04-26 07:43:27 +010024#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
27#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010030#endif
31
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020033#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050036#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000037
38#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010045#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
49int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020050 const unsigned char *info,
51 size_t ilen )
52{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020053 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020057
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020060
61 memcpy( ssl->cli_id, info, ilen );
62 ssl->cli_id_len = ilen;
63
64 return( 0 );
65}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020066
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020067void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_ssl_cookie_write_t *f_cookie_write,
69 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020070 void *p_cookie )
71{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020072 conf->f_cookie_write = f_cookie_write;
73 conf->f_cookie_check = f_cookie_check;
74 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000080 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000081 size_t len )
82{
Janos Follath865b3eb2019-12-16 11:46:15 +000083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000084 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000085 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010088
Philippe Antoine747fd532018-05-30 09:13:21 +020089 if( len < 2 )
90 {
91 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
92 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
93 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
94 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
95 }
Paul Bakker5701cdc2012-09-27 21:49:42 +000096 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
97 if( servername_list_size + 2 != len )
98 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000103 }
104
105 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200106 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000107 {
108 hostname_len = ( ( p[1] << 8 ) | p[2] );
109 if( hostname_len + 3 > servername_list_size )
110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000115 }
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000118 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200119 ret = ssl->conf->f_sni( ssl->conf->p_sni,
120 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000121 if( ret != 0 )
122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
125 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
126 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000127 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000128 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000129 }
130
131 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000132 p += hostname_len + 3;
133 }
134
135 if( servername_list_size != 0 )
136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200138 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
139 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000141 }
142
143 return( 0 );
144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000146
Gilles Peskineeccd8882020-03-10 12:19:08 +0100147#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100148static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
149{
150 if( conf->f_psk != NULL )
151 return( 1 );
152
153 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
154 return( 0 );
155
156 if( conf->psk != NULL && conf->psk_len != 0 )
157 return( 1 );
158
159#if defined(MBEDTLS_USE_PSA_CRYPTO)
160 if( conf->psk_opaque != 0 )
161 return( 1 );
162#endif /* MBEDTLS_USE_PSA_CRYPTO */
163
164 return( 0 );
165}
166
167#if defined(MBEDTLS_USE_PSA_CRYPTO)
168static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
169{
170 if( ssl->conf->f_psk != NULL )
171 {
172 /* If we've used a callback to select the PSK,
173 * the static configuration is irrelevant. */
174
175 if( ssl->handshake->psk_opaque != 0 )
176 return( 1 );
177
178 return( 0 );
179 }
180
181 if( ssl->conf->psk_opaque != 0 )
182 return( 1 );
183
184 return( 0 );
185}
186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100187#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000190 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000191 size_t len )
192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_RENEGOTIATION)
194 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100195 {
196 /* Check verify-data in constant-time. The length OTOH is no secret */
197 if( len != 1 + ssl->verify_data_len ||
198 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100200 ssl->verify_data_len ) != 0 )
201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200203 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
204 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100206 }
207 }
208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000210 {
211 if( len != 1 || buf[0] != 0x0 )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200214 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
215 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +0000217 }
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000220 }
Paul Bakker48916f92012-09-16 19:57:18 +0000221
222 return( 0 );
223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100226 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100227
228/*
229 * Status of the implementation of signature-algorithms extension:
230 *
231 * Currently, we are only considering the signature-algorithm extension
232 * to pick a ciphersuite which allows us to send the ServerKeyExchange
233 * message with a signature-hash combination that the user allows.
234 *
235 * We do *not* check whether all certificates in our certificate
236 * chain are signed with an allowed signature-hash pair.
237 * This needs to be done at a later stage.
238 *
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000241 const unsigned char *buf,
242 size_t len )
243{
244 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100245
Paul Bakker23f36802012-09-28 14:15:14 +0000246 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200247 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200248
Hanno Becker7e5437a2017-04-28 17:15:26 +0100249 mbedtls_md_type_t md_cur;
250 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000251
Philippe Antoine747fd532018-05-30 09:13:21 +0200252 if ( len < 2 ) {
253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
255 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
256 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
257 }
Paul Bakker23f36802012-09-28 14:15:14 +0000258 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
259 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200260 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker23f36802012-09-28 14:15:14 +0000266 }
267
Hanno Becker7e5437a2017-04-28 17:15:26 +0100268 /* Currently we only guarantee signing the ServerKeyExchange message according
269 * to the constraints specified in this extension (see above), so it suffices
270 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200271 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100272 * This will change when we also consider certificate signatures,
273 * in which case we will need to remember the whole signature-hash
274 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200275 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100276
277 for( p = buf + 2; p < end; p += 2 )
278 {
279 /* Silently ignore unknown signature or hash algorithms. */
280
281 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
282 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
284 " unknown sig alg encoding %d", p[1] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100285 continue;
286 }
287
288 /* Check if we support the hash the user proposes */
289 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
290 if( md_cur == MBEDTLS_MD_NONE )
291 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
293 " unknown hash alg encoding %d", p[0] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100294 continue;
295 }
296
297 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
298 {
299 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
301 " match sig %d and hash %d",
Hanno Becker7e5437a2017-04-28 17:15:26 +0100302 sig_cur, md_cur ) );
303 }
304 else
305 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
307 "hash alg %d not supported", md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000308 }
Paul Bakker23f36802012-09-28 14:15:14 +0000309 }
310
Paul Bakker23f36802012-09-28 14:15:14 +0000311 return( 0 );
312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100314 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000315
Robert Cragie136884c2015-10-02 13:34:31 +0100316#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100317 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200319 const unsigned char *buf,
320 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100321{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200322 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100323 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100325
Philippe Antoine747fd532018-05-30 09:13:21 +0200326 if ( len < 2 ) {
327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
330 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
331 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100332 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
333 if( list_size + 2 != len ||
334 list_size % 2 != 0 )
335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100340 }
341
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200342 /* Should never happen unless client duplicates the extension */
343 if( ssl->handshake->curves != NULL )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
347 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200349 }
350
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100351 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200352 * and leave room for a final 0 */
353 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( our_size > MBEDTLS_ECP_DP_MAX )
355 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200356
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200357 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200358 {
359 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
360 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200361 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200362 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200363
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200364 ssl->handshake->curves = curves;
365
Paul Bakker41c83d32013-03-20 14:39:14 +0100366 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200367 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200370
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200371 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100372 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200373 *curves++ = curve_info;
374 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100375 }
376
377 list_size -= 2;
378 p += 2;
379 }
380
381 return( 0 );
382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200385 const unsigned char *buf,
386 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100387{
388 size_t list_size;
389 const unsigned char *p;
390
Philippe Antoine747fd532018-05-30 09:13:21 +0200391 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100397 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200398 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100399
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200400 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100401 while( list_size > 0 )
402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
404 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100405 {
Robert Cragie136884c2015-10-02 13:34:31 +0100406#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200407 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100408#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100409#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Robert Cragie136884c2015-10-02 13:34:31 +0100410 ssl->handshake->ecjpake_ctx.point_format = p[0];
411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100413 return( 0 );
414 }
415
416 list_size--;
417 p++;
418 }
419
420 return( 0 );
421}
Robert Cragieae8535d2015-10-06 17:11:18 +0100422#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
423 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100424
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200425#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
426static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
427 const unsigned char *buf,
428 size_t len )
429{
Janos Follath865b3eb2019-12-16 11:46:15 +0000430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200431
432 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
433 {
434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
435 return( 0 );
436 }
437
438 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
439 buf, len ) ) != 0 )
440 {
441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
443 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200444 return( ret );
445 }
446
447 /* Only mark the extension as OK when we're sure it is */
448 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
449
450 return( 0 );
451}
452#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
455static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200456 const unsigned char *buf,
457 size_t len )
458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200462 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
463 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200465 }
466
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200467 ssl->session_negotiate->mfl_code = buf[0];
468
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200469 return( 0 );
470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200472
Hanno Beckera0e20d02019-05-15 14:03:01 +0100473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100474static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
475 const unsigned char *buf,
476 size_t len )
477{
478 size_t peer_cid_len;
479
480 /* CID extension only makes sense in DTLS */
481 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
482 {
483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
485 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
486 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
487 }
488
489 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100490 * Quoting draft-ietf-tls-dtls-connection-id-05
491 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100492 *
493 * struct {
494 * opaque cid<0..2^8-1>;
495 * } ConnectionId;
496 */
497
498 if( len < 1 )
499 {
500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
501 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
502 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
503 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
504 }
505
506 peer_cid_len = *buf++;
507 len--;
508
509 if( len != peer_cid_len )
510 {
511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
512 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
513 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
514 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
515 }
516
517 /* Ignore CID if the user has disabled its use. */
518 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
519 {
520 /* Leave ssl->handshake->cid_in_use in its default
521 * value of MBEDTLS_SSL_CID_DISABLED. */
522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
523 return( 0 );
524 }
525
526 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
527 {
528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
530 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
531 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
532 }
533
Hanno Becker08556bf2019-05-03 12:43:44 +0100534 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100535 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
536 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
537
538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
539 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
540
Hanno Becker89dcc882019-04-26 13:56:39 +0100541 return( 0 );
542}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100543#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
546static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200547 const unsigned char *buf,
548 size_t len )
549{
550 if( len != 0 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
554 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200556 }
557
558 ((void) buf);
559
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200560 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200562
563 return( 0 );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
568static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100569 const unsigned char *buf,
570 size_t len )
571{
572 if( len != 0 )
573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200575 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
576 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100578 }
579
580 ((void) buf);
581
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200582 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100586 }
587
588 return( 0 );
589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
593static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200594 const unsigned char *buf,
595 size_t len )
596{
597 if( len != 0 )
598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200600 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
601 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200603 }
604
605 ((void) buf);
606
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200607 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200611 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200612
613 return( 0 );
614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
618static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200619 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200620 size_t len )
621{
Janos Follath865b3eb2019-12-16 11:46:15 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200623 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200624
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200625 mbedtls_ssl_session_init( &session );
626
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200627 if( ssl->conf->f_ticket_parse == NULL ||
628 ssl->conf->f_ticket_write == NULL )
629 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200630 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200631 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200632
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200633 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200634 ssl->handshake->new_session_ticket = 1;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200637
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200638 if( len == 0 )
639 return( 0 );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_SSL_RENEGOTIATION)
642 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200645 return( 0 );
646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200648
649 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200650 * Failures are ok: just ignore the ticket and proceed.
651 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200652 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
653 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200654 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200655 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200656
657 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
659 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
661 else
662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
663
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200666
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200667 /*
668 * Keep the session ID sent by the client, since we MUST send it back to
669 * inform them we're accepting the ticket (RFC 5077 section 3.4)
670 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200671 session.id_len = ssl->session_negotiate->id_len;
672 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200673
674 mbedtls_ssl_session_free( ssl->session_negotiate );
675 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
676
677 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500678 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200683
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200684 /* Don't send a new ticket after all, this one is OK */
685 ssl->handshake->new_session_ticket = 0;
686
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200687 return( 0 );
688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_ALPN)
692static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200693 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200694{
Paul Bakker14b16c62014-05-28 11:33:54 +0200695 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200696 const unsigned char *theirs, *start, *end;
697 const char **ours;
698
699 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200700 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200701 return( 0 );
702
703 /*
704 * opaque ProtocolName<1..2^8-1>;
705 *
706 * struct {
707 * ProtocolName protocol_name_list<2..2^16-1>
708 * } ProtocolNameList;
709 */
710
711 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
712 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200713 {
714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200717 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718
719 list_len = ( buf[0] << 8 ) | buf[1];
720 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200721 {
722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200725 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200726
727 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100728 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200729 */
730 start = buf + 2;
731 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100732 for( theirs = start; theirs != end; theirs += cur_len )
733 {
734 cur_len = *theirs++;
735
736 /* Current identifier must fit in list */
737 if( cur_len > (size_t)( end - theirs ) )
738 {
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
742 }
743
744 /* Empty strings MUST NOT be included */
745 if( cur_len == 0 )
746 {
747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
748 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
749 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
750 }
751 }
752
753 /*
754 * Use our order of preference
755 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200756 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200757 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200758 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200759 for( theirs = start; theirs != end; theirs += cur_len )
760 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200761 cur_len = *theirs++;
762
Paul Bakker14b16c62014-05-28 11:33:54 +0200763 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200764 memcmp( theirs, *ours, cur_len ) == 0 )
765 {
766 ssl->alpn_chosen = *ours;
767 return( 0 );
768 }
769 }
770 }
771
772 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
774 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
775 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200778
Johan Pascalb62bb512015-12-03 21:56:45 +0100779#if defined(MBEDTLS_SSL_DTLS_SRTP)
780static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300781 const unsigned char *buf,
782 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100783{
Johan Pascal43f94902020-09-22 12:25:52 +0200784 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100785 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200786 size_t profile_length;
787 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200788 /*! 2 bytes for profile length and 1 byte for mki len */
789 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100790
791 /* If use_srtp is not configured, just ignore the extension */
Ron Eldoref72faf2018-07-12 11:54:20 +0300792 if( ssl->conf->dtls_srtp_profile_list == NULL ||
793 ssl->conf->dtls_srtp_profile_list_len == 0 )
Johan Pascal85269572020-08-25 10:01:54 +0200794 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100795 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200796 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100797
798 /* RFC5764 section 4.1.1
799 * uint8 SRTPProtectionProfile[2];
800 *
801 * struct {
802 * SRTPProtectionProfiles SRTPProtectionProfiles;
803 * opaque srtp_mki<0..255>;
804 * } UseSRTPData;
805
806 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100807 */
808
Ron Eldoref72faf2018-07-12 11:54:20 +0300809 /*
810 * Min length is 5: at least one protection profile(2 bytes)
811 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200812 * Check here that we have at least 2 bytes of protection profiles length
Ron Eldoref72faf2018-07-12 11:54:20 +0300813 */
Johan Pascal042d4562020-08-25 12:14:02 +0200814 if( len < 2 )
Ron Eldor313d7b52018-12-10 14:56:21 +0200815 {
816 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
817 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100818 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200819 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100820
Johan Pascal43f94902020-09-22 12:25:52 +0200821 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200822
Ron Eldoref72faf2018-07-12 11:54:20 +0300823 /* first 2 bytes are protection profile length(in bytes) */
824 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200825 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200826
Johan Pascal042d4562020-08-25 12:14:02 +0200827 /* check the buffer size: at least profiles + profile and mki length */
828 if( profile_length + size_of_lengths > len ||
829 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200830 {
831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
832 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
833 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
834 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300835 /*
836 * parse the extension list values are defined in
837 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
838 */
839 for( j=0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100840 {
Johan Pascal042d4562020-08-25 12:14:02 +0200841 uint16_t protection_profile_value = buf[j] << 8 | buf[j+1];
Johan Pascal43f94902020-09-22 12:25:52 +0200842 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100843
Johan Pascal43f94902020-09-22 12:25:52 +0200844 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300845 {
Johan Pascal43f94902020-09-22 12:25:52 +0200846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
847 mbedtls_ssl_get_srtp_profile_as_string(
848 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300849 }
Johan Pascal85269572020-08-25 10:01:54 +0200850 else
851 {
852 continue;
853 }
Ron Eldor591f1622018-01-22 12:30:04 +0200854 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200855 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200856 {
857 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
858 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200859 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
861 mbedtls_ssl_get_srtp_profile_as_string(
862 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200863 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100864 }
865 }
Johan Pascal43f94902020-09-22 12:25:52 +0200866 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200867 break;
868 }
Johan Pascal042d4562020-08-25 12:14:02 +0200869 buf += profile_length; /* buf points to the mki length */
870 mki_length = *buf;
871 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200872
Johan Pascal042d4562020-08-25 12:14:02 +0200873 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
874 mki_length + profile_length + size_of_lengths != len )
875 {
876 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
877 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
878 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
879 }
880
881 /* Parse the mki only if present and mki is supported locally */
882 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
883 mki_length > 0 )
884 {
885 ssl->dtls_srtp_info.mki_len = mki_length;
886
887 for( i=0; i < mki_length; i++ )
Ron Eldor591f1622018-01-22 12:30:04 +0200888 {
Johan Pascal042d4562020-08-25 12:14:02 +0200889 ssl->dtls_srtp_info.mki_value[i] = buf[i];
Ron Eldor591f1622018-01-22 12:30:04 +0200890 }
Ron Eldorb4655392018-07-05 18:25:39 +0300891
Ron Eldoref72faf2018-07-12 11:54:20 +0300892 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
893 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100894 }
895
Johan Pascal042d4562020-08-25 12:14:02 +0200896 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100897}
898#endif /* MBEDTLS_SSL_DTLS_SRTP */
899
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100900/*
901 * Auxiliary functions for ServerHello parsing and related actions
902 */
903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100905/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100906 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_ECDSA_C)
909static int ssl_check_key_curve( mbedtls_pk_context *pk,
910 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 const mbedtls_ecp_curve_info **crv = curves;
913 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100914
915 while( *crv != NULL )
916 {
917 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100918 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100919 crv++;
920 }
921
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100922 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100923}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100925
926/*
927 * Try picking a certificate for this ciphersuite,
928 * return 0 on success and -1 on failure.
929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930static int ssl_pick_cert( mbedtls_ssl_context *ssl,
931 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100934 mbedtls_pk_type_t pk_alg =
935 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200936 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100939 if( ssl->handshake->sni_key_cert != NULL )
940 list = ssl->handshake->sni_key_cert;
941 else
942#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200943 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100946 return( 0 );
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000949
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200950 if( list == NULL )
951 {
952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
953 return( -1 );
954 }
955
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100956 for( cur = list; cur != NULL; cur = cur->next )
957 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400958 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000960 cur->cert );
961
Gilles Peskinee198df52018-01-05 21:17:45 +0100962 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100965 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000966 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100967
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200968 /*
969 * This avoids sending the client a cert it'll reject based on
970 * keyUsage or other extensions.
971 *
972 * It also allows the user to provision different certificates for
973 * different uses based on keyUsage, eg if they want to avoid signing
974 * and decrypting with the same RSA key.
975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100977 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000980 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200981 continue;
982 }
983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_ECDSA_C)
985 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200986 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100989 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000990 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100991#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100992
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100993 /*
994 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
995 * present them a SHA-higher cert rather than failing if it's the only
996 * one we got that satisfies the other conditions.
997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
999 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001000 {
1001 if( fallback == NULL )
1002 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001006 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001007 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001008 }
1009
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001010 /* If we get there, we got a winner */
1011 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001012 }
1013
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001014 if( cur == NULL )
1015 cur = fallback;
1016
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001017 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001018 if( cur != NULL )
1019 {
1020 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001022 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001023 return( 0 );
1024 }
1025
1026 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001027}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001029
1030/*
1031 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1032 * Sets ciphersuite_info only if the suite matches.
1033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1035 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001038
Hanno Becker7e5437a2017-04-28 17:15:26 +01001039#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001040 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001041 mbedtls_pk_type_t sig_type;
1042#endif
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001045 if( suite_info == NULL )
1046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001049 }
1050
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Hanno Beckerecea07d2018-11-07 16:24:35 +00001052 suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001053
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001054 if( suite_info->min_minor_ver > ssl->minor_ver ||
1055 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001058 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001059 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001062 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001064 return( 0 );
1065#endif
1066
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001067#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001068 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001072 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001073 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001074#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001075
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001076#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1077 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001078 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001079 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1081 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001082 return( 0 );
1083 }
1084#endif
1085
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1088 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001089 ( ssl->handshake->curves == NULL ||
1090 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001093 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001094 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001095 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001096#endif
1097
Gilles Peskineeccd8882020-03-10 12:19:08 +01001098#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001099 /* If the ciphersuite requires a pre-shared key and we don't
1100 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001102 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001105 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001106 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001107#endif
1108
Hanno Becker7e5437a2017-04-28 17:15:26 +01001109#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001110 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001111 /* If the ciphersuite requires signing, check whether
1112 * a suitable hash algorithm is present. */
1113 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1114 {
1115 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1116 if( sig_type != MBEDTLS_PK_NONE &&
1117 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1118 {
1119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1120 "for signature algorithm %d", sig_type ) );
1121 return( 0 );
1122 }
1123 }
1124
1125#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001126 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001129 /*
1130 * Final check: if ciphersuite requires us to have a
1131 * certificate/key of a particular type:
1132 * - select the appropriate certificate if we have one, or
1133 * - try the next ciphersuite if we don't
1134 * This must be done last since we modify the key_cert list.
1135 */
1136 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001139 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001140 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001141 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001142#endif
1143
1144 *ciphersuite_info = suite_info;
1145 return( 0 );
1146}
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1149static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001150{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001151 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001152 unsigned int i, j;
1153 size_t n;
1154 unsigned int ciph_len, sess_len, chal_len;
1155 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001156 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_RENEGOTIATION)
1162 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001165 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1166 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001168 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001170
1171 buf = ssl->in_hdr;
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001176 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001178 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001180 buf[3], buf[4] ) );
1181
1182 /*
1183 * SSLv2 Client Hello
1184 *
1185 * Record layer:
1186 * 0 . 1 message length
1187 *
1188 * SSL layer:
1189 * 2 . 2 message type
1190 * 3 . 4 protocol version
1191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1193 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1196 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001197 }
1198
1199 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1200
1201 if( n < 17 || n > 512 )
1202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1204 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001205 }
1206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001208 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1209 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001210
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001211 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001214 " [%d:%d] < [%d:%d]",
1215 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001216 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1219 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1220 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001221 }
1222
Paul Bakker2fbefde2013-06-29 16:01:15 +02001223 ssl->handshake->max_major_ver = buf[3];
1224 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001229 return( ret );
1230 }
1231
1232 ssl->handshake->update_checksum( ssl, buf + 2, n );
1233
1234 buf = ssl->in_msg;
1235 n = ssl->in_left - 5;
1236
1237 /*
1238 * 0 . 1 ciphersuitelist length
1239 * 2 . 3 session id length
1240 * 4 . 5 challenge length
1241 * 6 . .. ciphersuitelist
1242 * .. . .. session id
1243 * .. . .. challenge
1244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001246
1247 ciph_len = ( buf[0] << 8 ) | buf[1];
1248 sess_len = ( buf[2] << 8 ) | buf[3];
1249 chal_len = ( buf[4] << 8 ) | buf[5];
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001252 ciph_len, sess_len, chal_len ) );
1253
1254 /*
1255 * Make sure each parameter length is valid
1256 */
1257 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1260 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001261 }
1262
1263 if( sess_len > 32 )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001267 }
1268
1269 if( chal_len < 8 || chal_len > 32 )
1270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1272 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001273 }
1274
1275 if( n != 6 + ciph_len + sess_len + chal_len )
1276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1278 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001279 }
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001282 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001284 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001286 buf + 6 + ciph_len + sess_len, chal_len );
1287
1288 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001289 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001290 memset( ssl->session_negotiate->id, 0,
1291 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001292 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001293
1294 p += sess_len;
1295 memset( ssl->handshake->randbytes, 0, 64 );
1296 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1297
1298 /*
1299 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1300 */
1301 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1306#if defined(MBEDTLS_SSL_RENEGOTIATION)
1307 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001310 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001311
Gilles Peskinec94f7352017-05-10 16:37:56 +02001312 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1313 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001315 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#endif /* MBEDTLS_SSL_RENEGOTIATION */
1317 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001318 break;
1319 }
1320 }
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001323 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1324 {
1325 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1327 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001330
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001331 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1336 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001339 }
1340
1341 break;
1342 }
1343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001345
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001346 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001347 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001348 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001350 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001351 for( i = 0; ciphersuites[i] != 0; i++ )
1352#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001353 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001354 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001355#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001356 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001357 if( p[0] != 0 ||
1358 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1359 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1360 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001361
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001362 got_common_suite = 1;
1363
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001364 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1365 &ciphersuite_info ) ) != 0 )
1366 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001367
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001368 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001369 goto have_ciphersuite_v2;
1370 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001371
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001372 if( got_common_suite )
1373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001375 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001377 }
1378 else
1379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1381 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001382 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001383
1384have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001386
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001387 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001388 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001389
1390 /*
1391 * SSLv2 Client Hello relevant renegotiation security checks
1392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001394 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1398 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001400 }
1401
1402 ssl->in_left = 0;
1403 ssl->state++;
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001406
1407 return( 0 );
1408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001410
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001411/* This function doesn't alert on errors that happen early during
1412 ClientHello parsing because they might indicate that the client is
1413 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001415{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001416 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001417 size_t i, j;
1418 size_t ciph_offset, comp_offset, ext_offset;
1419 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001421 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001422#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001423 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001425 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001426#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001427 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001428 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001430 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Hanno Becker7e5437a2017-04-28 17:15:26 +01001432 /* If there is no signature-algorithm extension present,
1433 * we need to fall back to the default values for allowed
1434 * signature-hash pairs. */
1435#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001436 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001437 int sig_hash_alg_ext_present = 0;
1438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001439 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001444read_record_header:
1445#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001446 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001448 * otherwise read it ourselves manually in order to support SSLv2
1449 * ClientHello, which doesn't use the same record layer format.
1450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_SSL_RENEGOTIATION)
1452 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001453#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001456 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001457 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001459 return( ret );
1460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 }
1462
1463 buf = ssl->in_hdr;
1464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001468#endif
1469 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001470 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001471#endif
1472
Hanno Becker5903de42019-05-03 14:46:38 +01001473 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001474
Paul Bakkerec636f32012-09-09 19:17:02 +00001475 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001476 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001477 *
1478 * Record layer:
1479 * 0 . 0 message type
1480 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001481 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001482 * 3 . 4 message length
1483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001485 buf[0] ) );
1486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1490 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001491 }
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001494 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001497 buf[1], buf[2] ) );
1498
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001499 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001500
1501 /* According to RFC 5246 Appendix E.1, the version here is typically
1502 * "{03,00}, the lowest version number supported by the client, [or] the
1503 * value of ClientHello.client_version", so the only meaningful check here
1504 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1508 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001511 /* For DTLS if this is the initial handshake, remember the client sequence
1512 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_SSL_RENEGOTIATION)
1516 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001517#endif
1518 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001519 {
1520 /* Epoch should be 0 for initial handshakes */
1521 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1524 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001525 }
1526
Hanno Becker19859472018-08-06 09:40:20 +01001527 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1530 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001533 ssl->next_record_offset = 0;
1534 ssl->in_left = 0;
1535 goto read_record_header;
1536 }
1537
1538 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001540#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001543
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001544 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_SSL_RENEGOTIATION)
1547 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001550 msg_len = ssl->in_hslen;
1551 }
1552 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001553#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001554 {
Angus Grattond8213d02016-05-25 20:56:48 +10001555 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1558 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001559 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001560
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001561 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001562 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001565 return( ret );
1566 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001567
1568 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001570 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001571 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001572 else
1573#endif
1574 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001575 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001576
1577 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001580
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001581 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001582
1583 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001584 * Handshake layer:
1585 * 0 . 0 handshake type
1586 * 1 . 3 handshake length
1587 * 4 . 5 DTLS only: message seqence number
1588 * 6 . 8 DTLS only: fragment offset
1589 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1594 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001595 }
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1602 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001603 }
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001606 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1607
1608 /* We don't support fragmentation of ClientHello (yet?) */
1609 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1613 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001614 }
1615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001618 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001619 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001620 * Copy the client's handshake message_seq on initial handshakes,
1621 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_SSL_RENEGOTIATION)
1624 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001625 {
1626 /* This couldn't be done in ssl_prepare_handshake_record() */
1627 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1628 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001629
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001630 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001633 "%d (expected %d)", cli_msg_seq,
1634 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001636 }
1637
1638 ssl->handshake->in_msg_seq++;
1639 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001640 else
1641#endif
1642 {
1643 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1644 ssl->in_msg[5];
1645 ssl->handshake->out_msg_seq = cli_msg_seq;
1646 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1647 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001648
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001649 /*
1650 * For now we don't support fragmentation, so make sure
1651 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001652 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001653 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1654 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1657 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001658 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001659 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 buf += mbedtls_ssl_hs_hdr_len( ssl );
1663 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001664
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001665 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001666 * ClientHello layer:
1667 * 0 . 1 protocol version
1668 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1669 * 34 . 35 session id length (1 byte)
1670 * 35 . 34+x session id
1671 * 35+x . 35+x DTLS only: cookie length (1 byte)
1672 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001673 * .. . .. ciphersuite list length (2 bytes)
1674 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001675 * .. . .. compression alg. list length (1 byte)
1676 * .. . .. compression alg. list
1677 * .. . .. extensions length (2 bytes, optional)
1678 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001679 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001680
1681 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001682 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001683 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1684 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001685 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001686 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1689 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001690 }
1691
1692 /*
1693 * Check and save the protocol version
1694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001698 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001699
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001700 ssl->handshake->max_major_ver = ssl->major_ver;
1701 ssl->handshake->max_minor_ver = ssl->minor_ver;
1702
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001703 if( ssl->major_ver < ssl->conf->min_major_ver ||
1704 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001707 " [%d:%d] < [%d:%d]",
1708 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001709 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1711 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001713 }
1714
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001715 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001716 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001717 ssl->major_ver = ssl->conf->max_major_ver;
1718 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001719 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001720 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1721 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001722
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001723 /*
1724 * Save client random (inc. Unix time)
1725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001727
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001728 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001729
1730 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001731 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001732 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001733 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001734
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001735 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001736 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001742 }
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001745
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001746 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001747 memset( ssl->session_negotiate->id, 0,
1748 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001749 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001750 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001751
1752 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001753 * Check the cookie length and content
1754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001757 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001758 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001759 cookie_len = buf[cookie_offset];
1760
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001761 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001764 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1765 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001767 }
1768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001770 buf + cookie_offset + 1, cookie_len );
1771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001773 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#if defined(MBEDTLS_SSL_RENEGOTIATION)
1775 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001776#endif
1777 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001778 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001779 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001780 buf + cookie_offset + 1, cookie_len,
1781 ssl->cli_id, ssl->cli_id_len ) != 0 )
1782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001784 ssl->handshake->verify_cookie_len = 1;
1785 }
1786 else
1787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001789 ssl->handshake->verify_cookie_len = 0;
1790 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001791 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001792 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001794 {
1795 /* We know we didn't send a cookie, so it should be empty */
1796 if( cookie_len != 0 )
1797 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001798 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1800 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001801 }
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001804 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001805
1806 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001807 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001808 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001809 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001810 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001813 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001814
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001815 ciph_len = ( buf[ciph_offset + 0] << 8 )
1816 | ( buf[ciph_offset + 1] );
1817
1818 if( ciph_len < 2 ||
1819 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1820 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1824 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001826 }
1827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001829 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001830
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001831 /*
1832 * Check the compression algorithms length and pick one
1833 */
1834 comp_offset = ciph_offset + 2 + ciph_len;
1835
1836 comp_len = buf[comp_offset];
1837
1838 if( comp_len < 1 ||
1839 comp_len > 16 ||
1840 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001843 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1844 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001846 }
1847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001849 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1852#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001853 for( i = 0; i < comp_len; ++i )
1854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001858 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 }
1860 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001861#endif
1862
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001863 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001865 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001867#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001868
Janos Follathc6dab2b2016-05-23 14:27:02 +01001869 /* Do not parse the extensions if the protocol is SSLv3 */
1870#if defined(MBEDTLS_SSL_PROTO_SSL3)
1871 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1872 {
1873#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001874 /*
1875 * Check the extension length
1876 */
1877 ext_offset = comp_offset + 1 + comp_len;
1878 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001879 {
Simon Butcher584a5472016-05-23 16:24:52 +01001880 if( msg_len < ext_offset + 2 )
1881 {
1882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001883 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1884 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001885 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1886 }
1887
1888 ext_len = ( buf[ext_offset + 0] << 8 )
1889 | ( buf[ext_offset + 1] );
1890
1891 if( ( ext_len > 0 && ext_len < 4 ) ||
1892 msg_len != ext_offset + 2 + ext_len )
1893 {
1894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1896 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001897 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1898 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001899 }
Simon Butcher584a5472016-05-23 16:24:52 +01001900 else
1901 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001902
Simon Butcher584a5472016-05-23 16:24:52 +01001903 ext = buf + ext_offset + 2;
1904 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001905
Simon Butcher584a5472016-05-23 16:24:52 +01001906 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001907 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001908 unsigned int ext_id;
1909 unsigned int ext_size;
1910 if ( ext_len < 4 ) {
1911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1912 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1913 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1914 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1915 }
1916 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1917 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001918
Simon Butcher584a5472016-05-23 16:24:52 +01001919 if( ext_size + 4 > ext_len )
1920 {
1921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001922 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1923 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001924 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1925 }
1926 switch( ext_id )
1927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001929 case MBEDTLS_TLS_EXT_SERVERNAME:
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1931 if( ssl->conf->f_sni == NULL )
1932 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001933
Simon Butcher584a5472016-05-23 16:24:52 +01001934 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1935 if( ret != 0 )
1936 return( ret );
1937 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001939
Simon Butcher584a5472016-05-23 16:24:52 +01001940 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001943 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001944#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001945
Simon Butcher584a5472016-05-23 16:24:52 +01001946 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1947 if( ret != 0 )
1948 return( ret );
1949 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001952 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001953 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1955
Simon Butcher584a5472016-05-23 16:24:52 +01001956 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1957 if( ret != 0 )
1958 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001959
1960 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001961 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001963 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001964
Robert Cragie136884c2015-10-02 13:34:31 +01001965#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001966 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001967 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001969
Simon Butcher584a5472016-05-23 16:24:52 +01001970 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1971 if( ret != 0 )
1972 return( ret );
1973 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001974
Simon Butcher584a5472016-05-23 16:24:52 +01001975 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1977 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001978
Simon Butcher584a5472016-05-23 16:24:52 +01001979 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1980 if( ret != 0 )
1981 return( ret );
1982 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001983#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1984 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001985
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001986#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001987 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001989
Simon Butcher584a5472016-05-23 16:24:52 +01001990 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1991 if( ret != 0 )
1992 return( ret );
1993 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001994#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001997 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001999
Simon Butcher584a5472016-05-23 16:24:52 +01002000 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
2001 if( ret != 0 )
2002 return( ret );
2003 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002007 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
2008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002009
Simon Butcher584a5472016-05-23 16:24:52 +01002010 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
2011 if( ret != 0 )
2012 return( ret );
2013 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002015
Hanno Beckera0e20d02019-05-15 14:03:01 +01002016#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01002017 case MBEDTLS_TLS_EXT_CID:
2018 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2019
2020 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2021 if( ret != 0 )
2022 return( ret );
2023 break;
2024#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002027 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2028 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002029
Simon Butcher584a5472016-05-23 16:24:52 +01002030 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2031 if( ret != 0 )
2032 return( ret );
2033 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01002037 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002039
Simon Butcher584a5472016-05-23 16:24:52 +01002040 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
2041 if( ret != 0 )
2042 return( ret );
2043 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01002047 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002049
Simon Butcher584a5472016-05-23 16:24:52 +01002050 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2051 if( ret != 0 )
2052 return( ret );
2053 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002057 case MBEDTLS_TLS_EXT_ALPN:
2058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002059
Simon Butcher584a5472016-05-23 16:24:52 +01002060 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2061 if( ret != 0 )
2062 return( ret );
2063 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002065
Johan Pascalb62bb512015-12-03 21:56:45 +01002066#if defined(MBEDTLS_SSL_DTLS_SRTP)
2067 case MBEDTLS_TLS_EXT_USE_SRTP:
2068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02002069
Johan Pascalb62bb512015-12-03 21:56:45 +01002070 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2071 if ( ret != 0 )
2072 return( ret );
2073 break;
2074#endif /* MBEDTLS_SSL_DTLS_SRTP */
2075
Simon Butcher584a5472016-05-23 16:24:52 +01002076 default:
2077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
2078 ext_id ) );
2079 }
2080
2081 ext_len -= 4 + ext_size;
2082 ext += 4 + ext_size;
2083
2084 if( ext_len > 0 && ext_len < 4 )
2085 {
2086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2088 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01002089 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2090 }
Paul Bakker48916f92012-09-16 19:57:18 +00002091 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002092#if defined(MBEDTLS_SSL_PROTO_SSL3)
2093 }
2094#endif
2095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002097 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
2100 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002101 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002103
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002104 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002105 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2109 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002112 }
2113
2114 break;
2115 }
2116 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002118
Hanno Becker7e5437a2017-04-28 17:15:26 +01002119#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002120 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002121
2122 /*
2123 * Try to fall back to default hash SHA1 if the client
2124 * hasn't provided any preferred signature-hash combinations.
2125 */
2126 if( sig_hash_alg_ext_present == 0 )
2127 {
2128 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2129
2130 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2131 md_default = MBEDTLS_MD_NONE;
2132
2133 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2134 }
2135
2136#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002137 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002138
Paul Bakker48916f92012-09-16 19:57:18 +00002139 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002140 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2141 */
2142 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2147#if defined(MBEDTLS_SSL_RENEGOTIATION)
2148 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002149 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2151 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002152 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2153 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002155 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002158 break;
2159 }
2160 }
2161
2162 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002163 * Renegotiation security checks
2164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002166 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002169 handshake_failure = 1;
2170 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171#if defined(MBEDTLS_SSL_RENEGOTIATION)
2172 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2173 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002174 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002177 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2180 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002181 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002184 handshake_failure = 1;
2185 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2187 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002188 renegotiation_info_seen == 1 )
2189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002191 handshake_failure = 1;
2192 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002194
2195 if( handshake_failure == 1 )
2196 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002197 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2198 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002200 }
Paul Bakker380da532012-04-18 16:10:25 +00002201
Paul Bakker41c83d32013-03-20 14:39:14 +01002202 /*
2203 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002204 * (At the end because we need information from the EC-based extensions
2205 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002206 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002207 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002208 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002209 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002211 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002212 for( i = 0; ciphersuites[i] != 0; i++ )
2213#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002214 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002215 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002216#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002217 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002218 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2219 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2220 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002221
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002222 got_common_suite = 1;
2223
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002224 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2225 &ciphersuite_info ) ) != 0 )
2226 return( ret );
2227
2228 if( ciphersuite_info != NULL )
2229 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002230 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002231
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002232 if( got_common_suite )
2233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002235 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002236 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2237 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002239 }
2240 else
2241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002243 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2244 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002246 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002247
2248have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002250
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002251 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002252 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002253
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 ssl->state++;
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002257 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002259#endif
2260
Hanno Becker7e5437a2017-04-28 17:15:26 +01002261 /* Debugging-only output for testsuite */
2262#if defined(MBEDTLS_DEBUG_C) && \
2263 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002264 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002265 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2266 {
2267 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2268 if( sig_alg != MBEDTLS_PK_NONE )
2269 {
2270 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2271 sig_alg );
2272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2273 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2274 }
2275 else
2276 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2278 "%d - should not happen", sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002279 }
2280 }
2281#endif
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
2285 return( 0 );
2286}
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2289static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002290 unsigned char *buf,
2291 size_t *olen )
2292{
2293 unsigned char *p = buf;
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002296 {
2297 *olen = 0;
2298 return;
2299 }
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2304 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002305
2306 *p++ = 0x00;
2307 *p++ = 0x00;
2308
2309 *olen = 4;
2310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002312
Hanno Beckera0e20d02019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002314static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2315 unsigned char *buf,
2316 size_t *olen )
2317{
2318 unsigned char *p = buf;
2319 size_t ext_len;
2320 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2321
2322 *olen = 0;
2323
2324 /* Skip writing the extension if we don't want to use it or if
2325 * the client hasn't offered it. */
2326 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2327 return;
2328
2329 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2330 * which is at most 255, so the increment cannot overflow. */
2331 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2332 {
2333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2334 return;
2335 }
2336
2337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2338
2339 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002340 * Quoting draft-ietf-tls-dtls-connection-id-05
2341 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002342 *
2343 * struct {
2344 * opaque cid<0..2^8-1>;
2345 * } ConnectionId;
2346 */
2347
2348 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2349 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2350 ext_len = (size_t) ssl->own_cid_len + 1;
2351 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2352 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2353
2354 *p++ = (uint8_t) ssl->own_cid_len;
2355 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2356
2357 *olen = ssl->own_cid_len + 5;
2358}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002359#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2362static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002363 unsigned char *buf,
2364 size_t *olen )
2365{
2366 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2368 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002369
Hanno Becker27b34d52017-10-20 14:24:51 +01002370 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002372 {
2373 *olen = 0;
2374 return;
2375 }
2376
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002377 /*
2378 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2379 * from a client and then selects a stream or Authenticated Encryption
2380 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2381 * encrypt-then-MAC response extension back to the client."
2382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002384 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2386 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002387 {
2388 *olen = 0;
2389 return;
2390 }
2391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2395 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002396
2397 *p++ = 0x00;
2398 *p++ = 0x00;
2399
2400 *olen = 4;
2401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2405static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002406 unsigned char *buf,
2407 size_t *olen )
2408{
2409 unsigned char *p = buf;
2410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2412 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002413 {
2414 *olen = 0;
2415 return;
2416 }
2417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002419 "extension" ) );
2420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2422 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002423
2424 *p++ = 0x00;
2425 *p++ = 0x00;
2426
2427 *olen = 4;
2428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2432static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002433 unsigned char *buf,
2434 size_t *olen )
2435{
2436 unsigned char *p = buf;
2437
2438 if( ssl->handshake->new_session_ticket == 0 )
2439 {
2440 *olen = 0;
2441 return;
2442 }
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2447 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002448
2449 *p++ = 0x00;
2450 *p++ = 0x00;
2451
2452 *olen = 4;
2453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002457 unsigned char *buf,
2458 size_t *olen )
2459{
2460 unsigned char *p = buf;
2461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002463 {
2464 *olen = 0;
2465 return;
2466 }
2467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2471 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#if defined(MBEDTLS_SSL_RENEGOTIATION)
2474 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002475 {
2476 *p++ = 0x00;
2477 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2478 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002479
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002480 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2481 p += ssl->verify_data_len;
2482 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2483 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002484 }
2485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002487 {
2488 *p++ = 0x00;
2489 *p++ = 0x01;
2490 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002491 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002492
2493 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002494}
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2497static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002498 unsigned char *buf,
2499 size_t *olen )
2500{
2501 unsigned char *p = buf;
2502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002504 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002505 *olen = 0;
2506 return;
2507 }
2508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2512 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002513
2514 *p++ = 0x00;
2515 *p++ = 1;
2516
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002517 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002518
2519 *olen = 5;
2520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002522
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002523#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002524 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002526 unsigned char *buf,
2527 size_t *olen )
2528{
2529 unsigned char *p = buf;
2530 ((void) ssl);
2531
Paul Bakker677377f2013-10-28 12:54:26 +01002532 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002534 {
2535 *olen = 0;
2536 return;
2537 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2542 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002543
2544 *p++ = 0x00;
2545 *p++ = 2;
2546
2547 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002549
2550 *olen = 6;
2551}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002552#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002553
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002554#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2555static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2556 unsigned char *buf,
2557 size_t *olen )
2558{
Janos Follath865b3eb2019-12-16 11:46:15 +00002559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002560 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002561 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002562 size_t kkpp_len;
2563
2564 *olen = 0;
2565
2566 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002567 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002568 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2569 return;
2570
2571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2572
2573 if( end - p < 4 )
2574 {
2575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2576 return;
2577 }
2578
2579 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2580 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2581
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002582 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2583 p + 2, end - p - 2, &kkpp_len,
2584 ssl->conf->f_rng, ssl->conf->p_rng );
2585 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002586 {
2587 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2588 return;
2589 }
2590
2591 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2592 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2593
2594 *olen = kkpp_len + 4;
2595}
2596#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_ALPN )
2599static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002600 unsigned char *buf, size_t *olen )
2601{
2602 if( ssl->alpn_chosen == NULL )
2603 {
2604 *olen = 0;
2605 return;
2606 }
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002609
2610 /*
2611 * 0 . 1 ext identifier
2612 * 2 . 3 ext length
2613 * 4 . 5 protocol list length
2614 * 6 . 6 protocol name length
2615 * 7 . 7+n protocol name
2616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2618 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002619
2620 *olen = 7 + strlen( ssl->alpn_chosen );
2621
2622 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2623 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2624
2625 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2626 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2627
2628 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2629
2630 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002633
Ron Eldor3adb9922017-12-21 10:15:08 +02002634#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002635static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002636 unsigned char *buf,
2637 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002638{
Ron Eldor75870ec2018-12-06 17:31:55 +02002639 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002640 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002641 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2642
2643 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002644
Johan Pascal43f94902020-09-22 12:25:52 +02002645 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascalb62bb512015-12-03 21:56:45 +01002646 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002647 return;
2648 }
2649
2650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2651
Johan Pascald576fdb2020-09-22 10:39:53 +02002652 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002653 {
2654 mki_len = ssl->dtls_srtp_info.mki_len;
2655 }
2656
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002657 /* The extension total size is 9 bytes :
2658 * - 2 bytes for the extension tag
2659 * - 2 bytes for the total size
2660 * - 2 bytes for the protection profile length
2661 * - 2 bytes for the protection profile
2662 * - 1 byte for the mki length
2663 * + the actual mki length
2664 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002665 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002666 {
2667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2668 return;
2669 }
2670
Johan Pascalb62bb512015-12-03 21:56:45 +01002671 /* extension */
2672 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2673 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002674 /*
2675 * total length 5 and mki value: only one profile(2 bytes)
2676 * and length(2 bytes) and srtp_mki )
2677 */
Ron Eldor591f1622018-01-22 12:30:04 +02002678 ext_len = 5 + mki_len;
2679 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2680 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002681
2682 /* protection profile length: 2 */
2683 buf[4] = 0x00;
2684 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002685 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002686 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002687 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002688 {
2689 buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF );
2690 buf[7] = (unsigned char)( profile_value & 0xFF );
2691 }
2692 else
2693 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002695 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002696 }
2697
Ron Eldor591f1622018-01-22 12:30:04 +02002698 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002699 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002700
Ron Eldor591f1622018-01-22 12:30:04 +02002701 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002702}
2703#endif /* MBEDTLS_SSL_DTLS_SRTP */
2704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2706static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002707{
Janos Follath865b3eb2019-12-16 11:46:15 +00002708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002709 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002710 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002713
2714 /*
2715 * struct {
2716 * ProtocolVersion server_version;
2717 * opaque cookie<0..2^8-1>;
2718 * } HelloVerifyRequest;
2719 */
2720
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002721 /* The RFC is not clear on this point, but sending the actual negotiated
2722 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002724 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002726 p += 2;
2727
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002728 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002729 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002733 }
2734
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002735 /* Skip length byte until we know the length */
2736 cookie_len_byte = p++;
2737
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002738 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002739 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002740 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002743 return( ret );
2744 }
2745
2746 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002749
2750 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2752 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002755
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002756 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002757 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002759 return( ret );
2760 }
2761
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002762#if defined(MBEDTLS_SSL_PROTO_DTLS)
2763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2764 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2765 {
2766 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2767 return( ret );
2768 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002769#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002772
2773 return( 0 );
2774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002780 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002781#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002783 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 unsigned char *buf, *p;
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002789 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002790 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002794
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002795 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002798
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002799 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2802 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002803 }
2804
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 /*
2806 * 0 . 0 handshake type
2807 * 1 . 3 handshake length
2808 * 4 . 5 protocol version
2809 * 6 . 9 UNIX time()
2810 * 10 . 37 random bytes
2811 */
2812 buf = ssl->out_msg;
2813 p = buf + 4;
2814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002816 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002817 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002820 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002823 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 *p++ = (unsigned char)( t >> 24 );
2825 *p++ = (unsigned char)( t >> 16 );
2826 *p++ = (unsigned char)( t >> 8 );
2827 *p++ = (unsigned char)( t );
2828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002830#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002831 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002832 return( ret );
2833
2834 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002837 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002838 return( ret );
2839
2840 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Paul Bakker48916f92012-09-16 19:57:18 +00002842 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
2846 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002847 * Resume is 0 by default, see ssl_handshake_init().
2848 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2849 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002851 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#if defined(MBEDTLS_SSL_RENEGOTIATION)
2853 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002854#endif
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002855 ssl->session_negotiate->id_len != 0 &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002856 ssl->conf->f_get_cache != NULL &&
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002857 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002860 ssl->handshake->resume = 1;
2861 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002863 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 {
2865 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002866 * New session, create a new session id,
2867 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 ssl->state++;
2870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002872 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002873#endif
2874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002876 if( ssl->handshake->new_session_ticket != 0 )
2877 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002878 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002879 memset( ssl->session_negotiate->id, 0, 32 );
2880 }
2881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002883 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002884 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002885 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002886 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002887 return( ret );
2888 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 }
2890 else
2891 {
2892 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002893 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002895 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002901 return( ret );
2902 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 }
2904
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002905 /*
2906 * 38 . 38 session id length
2907 * 39 . 38+n session id
2908 * 39+n . 40+n chosen ciphersuite
2909 * 41+n . 41+n chosen compression alg.
2910 * 42+n . 43+n extensions length
2911 * 44+n . 43+n+m extensions
2912 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002913 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2914 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2915 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2918 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002920 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakker48916f92012-09-16 19:57:18 +00002922 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2923 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2924 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2927 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002929 ssl->session_negotiate->compression ) );
2930
Janos Follathc6dab2b2016-05-23 14:27:02 +01002931 /* Do not write the extensions if the protocol is SSLv3 */
2932#if defined(MBEDTLS_SSL_PROTO_SSL3)
2933 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2934 {
2935#endif
2936
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002937 /*
2938 * First write extensions, then the total length
2939 */
2940 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2941 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002944 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2945 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002946#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002949 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2950 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002951#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002952
Hanno Beckera0e20d02019-05-15 14:03:01 +01002953#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002954 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2955 ext_len += olen;
2956#endif
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002959 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2960 ext_len += olen;
2961#endif
2962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002964 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2965 ext_len += olen;
2966#endif
2967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002969 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2970 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002971#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002972
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002973#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002974 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002975 if ( mbedtls_ssl_ciphersuite_uses_ec(
2976 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2977 {
2978 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2979 ext_len += olen;
2980 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002981#endif
2982
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002983#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2984 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2985 ext_len += olen;
2986#endif
2987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002989 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2990 ext_len += olen;
2991#endif
2992
Johan Pascalb62bb512015-12-03 21:56:45 +01002993#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002994 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
Johan Pascalb62bb512015-12-03 21:56:45 +01002995 ext_len += olen;
2996#endif
2997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002999
Paul Bakkera7036632014-04-30 10:15:38 +02003000 if( ext_len > 0 )
3001 {
3002 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
3003 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
3004 p += ext_len;
3005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Janos Follathc6dab2b2016-05-23 14:27:02 +01003007#if defined(MBEDTLS_SSL_PROTO_SSL3)
3008 }
3009#endif
3010
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3013 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003015 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
3019 return( ret );
3020}
3021
Gilles Peskineeccd8882020-03-10 12:19:08 +01003022#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003024{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003025 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003026 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003029
Hanno Becker77adddc2019-02-07 12:32:43 +00003030 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033 ssl->state++;
3034 return( 0 );
3035 }
3036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003039}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003040#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003044 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003045 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03003046 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003047 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10003049 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003051 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00003052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003054
3055 ssl->state++;
3056
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003057#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3058 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3059 authmode = ssl->handshake->sni_authmode;
3060 else
3061#endif
3062 authmode = ssl->conf->authmode;
3063
Hanno Becker77adddc2019-02-07 12:32:43 +00003064 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003065 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 {
Johan Pascal4f099262020-09-22 10:59:26 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3068 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 }
3070
3071 /*
3072 * 0 . 0 handshake type
3073 * 1 . 3 handshake length
3074 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003075 * 5 .. m-1 cert types
3076 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003077 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 * n .. n+1 length of all DNs
3079 * n+2 .. n+3 length of DN 1
3080 * n+4 .. ... Distinguished Name #1
3081 * ... .. ... length of DN 2, etc.
3082 */
3083 buf = ssl->out_msg;
3084 p = buf + 4;
3085
3086 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003087 * Supported certificate types
3088 *
3089 * ClientCertificateType certificate_types<1..2^8-1>;
3090 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003092 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_RSA_C)
3095 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#if defined(MBEDTLS_ECDSA_C)
3098 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003099#endif
3100
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003101 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003102 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003103
Paul Bakker577e0062013-08-28 11:57:20 +02003104 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003106 /*
3107 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003108 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003109 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3110 *
3111 * struct {
3112 * HashAlgorithm hash;
3113 * SignatureAlgorithm signature;
3114 * } SignatureAndHashAlgorithm;
3115 *
3116 * enum { (255) } HashAlgorithm;
3117 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003120 {
Simon Butcher99000142016-10-13 17:21:01 +01003121 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003122
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003123 /*
3124 * Supported signature algorithms
3125 */
Simon Butcher99000142016-10-13 17:21:01 +01003126 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3127 {
3128 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3129
3130 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3131 continue;
3132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003134 p[2 + sa_len++] = hash;
3135 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003138 p[2 + sa_len++] = hash;
3139 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003140#endif
Simon Butcher99000142016-10-13 17:21:01 +01003141 }
Paul Bakker926af752012-11-23 13:38:07 +01003142
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003143 p[0] = (unsigned char)( sa_len >> 8 );
3144 p[1] = (unsigned char)( sa_len );
3145 sa_len += 2;
3146 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003147 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003150 /*
3151 * DistinguishedName certificate_authorities<0..2^16-1>;
3152 * opaque DistinguishedName<1..2^16-1>;
3153 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003154 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003156 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003157
3158 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003160 /* NOTE: If trusted certificates are provisioned
3161 * via a CA callback (configured through
3162 * `mbedtls_ssl_conf_ca_cb()`, then the
3163 * CertificateRequest is currently left empty. */
3164
Janos Follath088ce432017-04-10 12:42:31 +01003165#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3166 if( ssl->handshake->sni_ca_chain != NULL )
3167 crt = ssl->handshake->sni_ca_chain;
3168 else
3169#endif
3170 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003171
Janos Follath088ce432017-04-10 12:42:31 +01003172 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003173 {
irwirc9bc3002020-04-01 13:46:36 +03003174 /* It follows from RFC 5280 A.1 that this length
3175 * can be represented in at most 11 bits. */
3176 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003177
irwirc9bc3002020-04-01 13:46:36 +03003178 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003179 {
3180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3181 break;
3182 }
3183
3184 *p++ = (unsigned char)( dn_size >> 8 );
3185 *p++ = (unsigned char)( dn_size );
3186 memcpy( p, crt->subject_raw.p, dn_size );
3187 p += dn_size;
3188
3189 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3190
3191 total_dn_size += 2 + dn_size;
3192 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 }
3195
Paul Bakker926af752012-11-23 13:38:07 +01003196 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3198 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003199 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3200 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003202 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
3206 return( ret );
3207}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003208#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3211 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3212static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003213{
Janos Follath865b3eb2019-12-16 11:46:15 +00003214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3219 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003220 }
3221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3223 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3224 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003227 return( ret );
3228 }
3229
3230 return( 0 );
3231}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3233 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003234
Gilles Peskineeccd8882020-03-10 12:19:08 +01003235#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003236 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003237static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003238 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003239{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003240 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3241 * signature length which will be added in ssl_write_server_key_exchange
3242 * after the call to ssl_prepare_server_key_exchange.
3243 * ssl_write_server_key_exchange also takes care of incrementing
3244 * ssl->out_msglen. */
3245 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003246 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003247 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003248 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003249 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003250 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3251 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003252 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003253 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003254 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003255 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003256 return( ret );
3257}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003258#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003259 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003260
Gilles Peskined3eb0612018-01-08 17:07:44 +01003261/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003262 * calculating the signature if any, but excluding formatting the
3263 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003264static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3265 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003266{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003268 ssl->handshake->ciphersuite_info;
3269
Gilles Peskineeccd8882020-03-10 12:19:08 +01003270#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3271#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003272 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003273#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3274#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003275
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003276 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003277#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003278 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003279#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003280
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003281 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003283 /*
3284 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003285 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003286 *
3287 */
3288
3289 /*
3290 * - ECJPAKE key exchanges
3291 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003292#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3293 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3294 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003296 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003297
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003298 ret = mbedtls_ecjpake_write_round_two(
3299 &ssl->handshake->ecjpake_ctx,
3300 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003301 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003302 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003303 if( ret != 0 )
3304 {
3305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3306 return( ret );
3307 }
3308
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003309 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003310 }
3311#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3312
Hanno Becker1aa267c2017-04-28 17:08:27 +01003313 /*
3314 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3315 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3316 * we use empty support identity hints here.
3317 **/
3318#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3320 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3321 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003322 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003323 ssl->out_msg[ssl->out_msglen++] = 0x00;
3324 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3327 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003328
Hanno Becker7e5437a2017-04-28 17:15:26 +01003329 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003330 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003331 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003332#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003333 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003334 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003336 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003337
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003338 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3339 {
3340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3342 }
3343
Paul Bakker41c83d32013-03-20 14:39:14 +01003344 /*
3345 * Ephemeral DH parameters:
3346 *
3347 * struct {
3348 * opaque dh_p<1..2^16-1>;
3349 * opaque dh_g<1..2^16-1>;
3350 * opaque dh_Ys<1..2^16-1>;
3351 * } ServerDHParams;
3352 */
Hanno Beckerab740562017-10-04 13:15:37 +01003353 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3354 &ssl->conf->dhm_P,
3355 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003356 {
Hanno Beckerab740562017-10-04 13:15:37 +01003357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003358 return( ret );
3359 }
Paul Bakker48916f92012-09-16 19:57:18 +00003360
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003361 if( ( ret = mbedtls_dhm_make_params(
3362 &ssl->handshake->dhm_ctx,
3363 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3364 ssl->out_msg + ssl->out_msglen, &len,
3365 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003368 return( ret );
3369 }
3370
Gilles Peskineeccd8882020-03-10 12:19:08 +01003371#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003372 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003373#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003374
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003375 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3378 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3379 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3380 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003381 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003382#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003383
Hanno Becker1aa267c2017-04-28 17:08:27 +01003384 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003385 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003386 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003387#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003388 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003390 /*
3391 * Ephemeral ECDH parameters:
3392 *
3393 * struct {
3394 * ECParameters curve_params;
3395 * ECPoint public;
3396 * } ServerECDHParams;
3397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003401 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003402
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003403 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003404 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003405 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3406 if( (*curve)->grp_id == *gid )
3407 goto curve_matching_done;
3408
3409curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003410 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3413 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003414 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003417
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003418 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3419 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003420 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003422 return( ret );
3423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003424
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003425 if( ( ret = mbedtls_ecdh_make_params(
3426 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003427 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003428 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003429 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003432 return( ret );
3433 }
3434
Gilles Peskineeccd8882020-03-10 12:19:08 +01003435#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003436 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003437#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003438
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003439 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003440
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003441 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3442 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003443 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003444#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003445
Hanno Becker1aa267c2017-04-28 17:08:27 +01003446 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003447 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003448 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003449 * exchange parameters, compute and add the signature here.
3450 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003451 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003452#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003453 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003454 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003455 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003456 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003457 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003459
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003460 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003461 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003462 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003463 * to choose appropriate hash.
3464 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3465 * (RFC 4492, Sec. 5.4)
3466 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003467 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003468
3469 mbedtls_md_type_t md_alg;
3470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003472 mbedtls_pk_type_t sig_alg =
3473 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003475 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003476 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3477 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003478 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003479 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3480 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003483 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003484 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003486 }
3487 }
Paul Bakker577e0062013-08-28 11:57:20 +02003488 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3490#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3491 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003492 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003493 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003494 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003496 }
3497 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003498#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3499 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003500 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003501 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003503 }
3504
Hanno Becker7e5437a2017-04-28 17:15:26 +01003505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3506
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003507 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003508 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3511 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3512 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003513 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003514 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003515 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3516 dig_signed,
3517 dig_signed_len );
3518 if( ret != 0 )
3519 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003520 }
3521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3523 MBEDTLS_SSL_PROTO_TLS1_1 */
3524#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3525 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3526 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003527 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003528 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003529 dig_signed,
3530 dig_signed_len,
3531 md_alg );
3532 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003533 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003534 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003535 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3537 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003541 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003542
Gilles Peskineebd652f2018-01-05 21:18:59 +01003543 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003544
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003545 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003546 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3549 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003550 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003551 /*
3552 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003553 * explicitly through a prefix to the signature.
3554 *
3555 * struct {
3556 * HashAlgorithm hash;
3557 * SignatureAlgorithm signature;
3558 * } SignatureAndHashAlgorithm;
3559 *
3560 * struct {
3561 * SignatureAndHashAlgorithm algorithm;
3562 * opaque signature<0..2^16-1>;
3563 * } DigitallySigned;
3564 *
3565 */
3566
Gilles Peskine1004c192018-01-08 16:59:14 +01003567 ssl->out_msg[ssl->out_msglen++] =
3568 mbedtls_ssl_hash_from_md_alg( md_alg );
3569 ssl->out_msg[ssl->out_msglen++] =
3570 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003571 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003573
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003574#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003575 if( ssl->conf->f_async_sign_start != NULL )
3576 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003577 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003578 mbedtls_ssl_own_cert( ssl ),
3579 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003580 switch( ret )
3581 {
3582 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3583 /* act as if f_async_sign was null */
3584 break;
3585 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003586 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003587 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003588 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003589 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003590 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3591 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003592 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003593 return( ret );
3594 }
3595 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003596#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003597
3598 if( mbedtls_ssl_own_key( ssl ) == NULL )
3599 {
3600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3601 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3602 }
3603
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003604 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3605 * signature length which will be added in ssl_write_server_key_exchange
3606 * after the call to ssl_prepare_server_key_exchange.
3607 * ssl_write_server_key_exchange also takes care of incrementing
3608 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003609 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3610 md_alg, hash, hashlen,
3611 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003612 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003613 ssl->conf->f_rng,
3614 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003617 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003618 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003619 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003620#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003621
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003622 return( 0 );
3623}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003624
Gilles Peskined3eb0612018-01-08 17:07:44 +01003625/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003626 * that do not include a ServerKeyExchange message, do nothing. Either
3627 * way, if successful, move on to the next step in the SSL state
3628 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003629static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3630{
Janos Follath865b3eb2019-12-16 11:46:15 +00003631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003632 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003633#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003634 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003635 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003636#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003637
Gilles Peskined3eb0612018-01-08 17:07:44 +01003638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3639
Gilles Peskineeccd8882020-03-10 12:19:08 +01003640#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003641 /* Extract static ECDH parameters and abort if ServerKeyExchange
3642 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003643 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3644 {
3645 /* For suites involving ECDH, extract DH parameters
3646 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003647#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003648 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3649 {
3650 ssl_get_ecdh_params_from_cert( ssl );
3651 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003652#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003653
3654 /* Key exchanges not involving ephemeral keys don't use
3655 * ServerKeyExchange, so end here. */
3656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3657 ssl->state++;
3658 return( 0 );
3659 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003660#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003661
Gilles Peskineeccd8882020-03-10 12:19:08 +01003662#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003663 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003664 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003665 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003666 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003667 {
3668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3669 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003670 }
3671 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003672#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003673 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003674 {
3675 /* ServerKeyExchange is needed. Prepare the message. */
3676 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003677 }
3678
3679 if( ret != 0 )
3680 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003681 /* If we're starting to write a new message, set ssl->out_msglen
3682 * to 0. But if we're resuming after an asynchronous message,
3683 * out_msglen is the amount of data written so far and mst be
3684 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003685 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3687 else
3688 ssl->out_msglen = 0;
3689 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003690 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003691
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003692 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003693 * ssl_prepare_server_key_exchange already wrote the signature
3694 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003695#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003696 if( signature_len != 0 )
3697 {
3698 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3699 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3700
3701 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3702 ssl->out_msg + ssl->out_msglen,
3703 signature_len );
3704
3705 /* Skip over the already-written signature */
3706 ssl->out_msglen += signature_len;
3707 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003708#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003709
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003710 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3712 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
3714 ssl->state++;
3715
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003716 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003719 return( ret );
3720 }
3721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003723 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724}
3725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003727{
Janos Follath865b3eb2019-12-16 11:46:15 +00003728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
3732 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3734 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003735
3736 ssl->state++;
3737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003741#endif
3742
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003743 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 return( ret );
3747 }
3748
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003749#if defined(MBEDTLS_SSL_PROTO_DTLS)
3750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3751 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3752 {
3753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3754 return( ret );
3755 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003756#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
3760 return( 0 );
3761}
3762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3764 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3765static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003766 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003769 size_t n;
3770
3771 /*
3772 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3773 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003774 if( *p + 2 > end )
3775 {
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 Bakker48f7a5d2013-04-19 14:30:58 +02003778 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003779
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003780 n = ( (*p)[0] << 8 ) | (*p)[1];
3781 *p += 2;
3782
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003783 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3786 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003787 }
3788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3792 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003793 }
3794
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003795 *p += n;
3796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003798
Paul Bakker70df2fb2013-04-17 17:19:09 +02003799 return( ret );
3800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3802 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3805 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003806
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003807#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003808static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3809 unsigned char *peer_pms,
3810 size_t *peer_pmslen,
3811 size_t peer_pmssize )
3812{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003813 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003814 peer_pms, peer_pmslen, peer_pmssize );
3815 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3816 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003817 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003818 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003819 }
3820 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3821 return( ret );
3822}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003823#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003824
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003825static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3826 const unsigned char *p,
3827 const unsigned char *end,
3828 unsigned char *peer_pms,
3829 size_t *peer_pmslen,
3830 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003831{
Janos Follath865b3eb2019-12-16 11:46:15 +00003832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003833 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3834 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3835 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003836
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003837#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003838 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003839 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003840 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003841 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3843 return( ssl_resume_decrypt_pms( ssl,
3844 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003845 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003846#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003847
3848 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003849 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3852 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3853 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003854 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003855 if ( p + 2 > end ) {
3856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3857 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3858 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003859 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3860 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3863 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003864 }
3865 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003866#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003867
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003868 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3871 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003872 }
3873
Gilles Peskine422ccab2018-01-11 18:29:01 +01003874 /*
3875 * Decrypt the premaster secret
3876 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003877#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003878 if( ssl->conf->f_async_decrypt_start != NULL )
3879 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003880 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003881 mbedtls_ssl_own_cert( ssl ),
3882 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003883 switch( ret )
3884 {
3885 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3886 /* act as if f_async_decrypt_start was null */
3887 break;
3888 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003889 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003890 return( ssl_resume_decrypt_pms( ssl,
3891 peer_pms,
3892 peer_pmslen,
3893 peer_pmssize ) );
3894 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003895 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003896 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3897 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003898 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003899 return( ret );
3900 }
3901 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003902#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003903
Gilles Peskine422ccab2018-01-11 18:29:01 +01003904 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3905 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3907 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3908 }
3909
3910 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003911 peer_pms, peer_pmslen, peer_pmssize,
3912 ssl->conf->f_rng, ssl->conf->p_rng );
3913 return( ret );
3914}
3915
3916static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3917 const unsigned char *p,
3918 const unsigned char *end,
3919 size_t pms_offset )
3920{
Janos Follath865b3eb2019-12-16 11:46:15 +00003921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003922 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3923 unsigned char ver[2];
3924 unsigned char fake_pms[48], peer_pms[48];
3925 unsigned char mask;
3926 size_t i, peer_pmslen;
3927 unsigned int diff;
3928
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003929 /* In case of a failure in decryption, the decryption may write less than
3930 * 2 bytes of output, but we always read the first two bytes. It doesn't
3931 * matter in the end because diff will be nonzero in that case due to
3932 * peer_pmslen being less than 48, and we only care whether diff is 0.
3933 * But do initialize peer_pms for robustness anyway. This also makes
3934 * memory analyzers happy (don't access uninitialized memory, even
3935 * if it's an unsigned char). */
3936 peer_pms[0] = peer_pms[1] = ~0;
3937
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003938 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3939 peer_pms,
3940 &peer_pmslen,
3941 sizeof( peer_pms ) );
3942
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003943#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003944 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3945 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003946#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003949 ssl->handshake->max_minor_ver,
3950 ssl->conf->transport, ver );
3951
3952 /* Avoid data-dependent branches while checking for invalid
3953 * padding, to protect against timing-based Bleichenbacher-type
3954 * attacks. */
3955 diff = (unsigned int) ret;
3956 diff |= peer_pmslen ^ 48;
3957 diff |= peer_pms[0] ^ ver[0];
3958 diff |= peer_pms[1] ^ ver[1];
3959
3960 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3961 /* MSVC has a warning about unary minus on unsigned, but this is
3962 * well-defined and precisely what we want to do here */
3963#if defined(_MSC_VER)
3964#pragma warning( push )
3965#pragma warning( disable : 4146 )
3966#endif
3967 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3968#if defined(_MSC_VER)
3969#pragma warning( pop )
3970#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003971
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003972 /*
3973 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3974 * must not cause the connection to end immediately; instead, send a
3975 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003976 * To protect against timing-based variants of the attack, we must
3977 * not have any branch that depends on whether the decryption was
3978 * successful. In particular, always generate the fake premaster secret,
3979 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003980 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003981 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003982 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003983 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003984 /* It's ok to abort on an RNG failure, since this does not reveal
3985 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003986 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003987 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003988
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003989#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003990 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003992#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003993
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003994 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3995 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003999 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004000 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004001
Gilles Peskine422ccab2018-01-11 18:29:01 +01004002 /* Set pms to either the true or the fake PMS, without
4003 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004004 for( i = 0; i < ssl->handshake->pmslen; i++ )
4005 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
4006
4007 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004008}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4010 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02004011
Gilles Peskineeccd8882020-03-10 12:19:08 +01004012#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004014 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004015{
Paul Bakker6db455e2013-09-18 17:29:31 +02004016 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03004017 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004018
Hanno Becker845b9462018-10-26 12:07:29 +01004019 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4022 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004023 }
4024
4025 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004026 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02004027 */
Hanno Becker83c9f492017-06-26 13:52:14 +01004028 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +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 Bakker48f7a5d2013-04-19 14:30:58 +02004032 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004033
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004034 n = ( (*p)[0] << 8 ) | (*p)[1];
4035 *p += 2;
4036
irwir6527bd62019-09-21 18:51:25 +03004037 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4040 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004041 }
4042
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004043 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004044 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004045 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004047 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004048 else
Paul Bakker6db455e2013-09-18 17:29:31 +02004049 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01004050 /* Identity is not a big secret since clients send it in the clear,
4051 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004052 if( n != ssl->conf->psk_identity_len ||
4053 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02004054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004056 }
4057 }
4058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02004062 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4063 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004065 }
4066
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004067 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004068
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004069 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004070}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004071#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004074{
Janos Follath865b3eb2019-12-16 11:46:15 +00004075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004077 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004078
Hanno Beckere694c3e2017-12-27 21:34:08 +00004079 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004082
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004083#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004084 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4085 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4086 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4087 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004088 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004089 {
4090 /* We've already read a record and there is an asynchronous
4091 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004092 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4094 }
4095 else
4096#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004097 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 return( ret );
4101 }
4102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004104 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4109 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004110 }
4111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4115 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004116 }
4117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4119 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004120 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004121 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004124 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004125 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004126
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004127 if( p != end )
4128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4130 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004131 }
4132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004134 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004135 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004136 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004137 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4140 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004141 }
4142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004144 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4147#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4148 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4149 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4150 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4151 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4152 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4153 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4154 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004157 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4160 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004161 }
4162
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004163 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4164 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004167 &ssl->handshake->pmslen,
4168 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004170 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4173 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004174 }
4175
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004176 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4177 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004178 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004179 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4181 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4182 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4183 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4184#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4185 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004186 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004187 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004190 return( ret );
4191 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004192
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004193 if( p != end )
4194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4196 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004197 }
4198
Hanno Becker845b9462018-10-26 12:07:29 +01004199#if defined(MBEDTLS_USE_PSA_CRYPTO)
4200 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4201 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004202 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4204 else
4205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004207 ciphersuite_info->key_exchange ) ) != 0 )
4208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004210 return( ret );
4211 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004213 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4215#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4216 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004217 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004218#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004219 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004220 {
4221 /* There is an asynchronous operation in progress to
4222 * decrypt the encrypted premaster secret, so skip
4223 * directly to resuming this operation. */
4224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4225 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4226 * won't actually use it, but maintain p anyway for robustness. */
4227 p += ssl->conf->psk_identity_len + 2;
4228 }
4229 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004230#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004231 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004234 return( ret );
4235 }
4236
Hanno Becker845b9462018-10-26 12:07:29 +01004237#if defined(MBEDTLS_USE_PSA_CRYPTO)
4238 /* Opaque PSKs are currently only supported for PSK-only. */
4239 if( ssl_use_opaque_psk( ssl ) == 1 )
4240 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4241#endif
4242
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004243 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004246 return( ret );
4247 }
4248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004250 ciphersuite_info->key_exchange ) ) != 0 )
4251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004253 return( ret );
4254 }
4255 }
4256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4258#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4259 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004260 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004261 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004264 return( ret );
4265 }
4266 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004269 return( ret );
4270 }
4271
Hanno Becker845b9462018-10-26 12:07:29 +01004272#if defined(MBEDTLS_USE_PSA_CRYPTO)
4273 /* Opaque PSKs are currently only supported for PSK-only. */
4274 if( ssl_use_opaque_psk( ssl ) == 1 )
4275 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4276#endif
4277
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004278 if( p != end )
4279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4281 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004282 }
4283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004285 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004288 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004289 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004290 }
4291 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4293#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4294 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004295 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004296 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004299 return( ret );
4300 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004303 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4306 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004307 }
4308
Hanno Becker845b9462018-10-26 12:07:29 +01004309#if defined(MBEDTLS_USE_PSA_CRYPTO)
4310 /* Opaque PSKs are currently only supported for PSK-only. */
4311 if( ssl_use_opaque_psk( ssl ) == 1 )
4312 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4313#endif
4314
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004315 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4316 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004319 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004322 return( ret );
4323 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004324 }
4325 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4327#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4328 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004329 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004330 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004333 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 }
4335 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004338#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4339 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4340 {
4341 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4342 p, end - p );
4343 if( ret != 0 )
4344 {
4345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4346 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4347 }
4348
4349 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4350 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4351 ssl->conf->f_rng, ssl->conf->p_rng );
4352 if( ret != 0 )
4353 {
4354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4355 return( ret );
4356 }
4357 }
4358 else
4359#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004368 return( ret );
4369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004370
Paul Bakker5121ce52009-01-03 21:22:43 +00004371 ssl->state++;
4372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004374
4375 return( 0 );
4376}
4377
Gilles Peskineeccd8882020-03-10 12:19:08 +01004378#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004380{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004381 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004382 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004385
Hanno Becker77adddc2019-02-07 12:32:43 +00004386 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004389 ssl->state++;
4390 return( 0 );
4391 }
4392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004395}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004396#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004400 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004401 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004402 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004403 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4405 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004408 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004409 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004410 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004413
Hanno Becker2a831a42019-02-07 13:17:25 +00004414 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004417 ssl->state++;
4418 return( 0 );
4419 }
4420
Hanno Becker2a831a42019-02-07 13:17:25 +00004421#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4422 if( ssl->session_negotiate->peer_cert == NULL )
4423 {
4424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4425 ssl->state++;
4426 return( 0 );
4427 }
4428#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4429 if( ssl->session_negotiate->peer_cert_digest == NULL )
4430 {
4431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4432 ssl->state++;
4433 return( 0 );
4434 }
4435#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4436
Simon Butcher99000142016-10-13 17:21:01 +01004437 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004438 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004439 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004441 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004442 return( ret );
4443 }
4444
4445 ssl->state++;
4446
Simon Butcher99000142016-10-13 17:21:01 +01004447 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4449 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4452 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 }
4454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004456
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004457#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4458 peer_pk = &ssl->handshake->peer_pubkey;
4459#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4460 if( ssl->session_negotiate->peer_cert == NULL )
4461 {
4462 /* Should never happen */
4463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4464 }
4465 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4466#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4467
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004468 /*
4469 * struct {
4470 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4471 * opaque signature<0..2^16-1>;
4472 * } DigitallySigned;
4473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4475 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4476 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004479 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004480
4481 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004482 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004483 {
4484 hash_start += 16;
4485 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004487 }
Paul Bakker926af752012-11-23 13:38:07 +01004488 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004489 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4491 MBEDTLS_SSL_PROTO_TLS1_1 */
4492#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4493 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004494 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004495 if( i + 2 > ssl->in_hslen )
4496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4498 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004499 }
4500
Paul Bakker5121ce52009-01-03 21:22:43 +00004501 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004502 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 */
Simon Butcher99000142016-10-13 17:21:01 +01004504 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4505
4506 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004509 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004511 }
4512
Simon Butcher99000142016-10-13 17:21:01 +01004513#if !defined(MBEDTLS_MD_SHA1)
4514 if( MBEDTLS_MD_SHA1 == md_alg )
4515 hash_start += 16;
4516#endif
Paul Bakker926af752012-11-23 13:38:07 +01004517
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004518 /* Info from md_alg will be used instead */
4519 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004520
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004521 i++;
4522
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004523 /*
4524 * Signature
4525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4527 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004530 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004532 }
4533
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004534 /*
4535 * Check the certificate's key type matches the signature alg
4536 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004537 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4540 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004541 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004542
4543 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004544 }
4545 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004550 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004551
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004552 if( i + 2 > ssl->in_hslen )
4553 {
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 );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004556 }
4557
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004558 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4559 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004560
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004561 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4564 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004565 }
4566
Simon Butcher99000142016-10-13 17:21:01 +01004567 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004568 {
4569 size_t dummy_hlen;
4570 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4571 }
Simon Butcher99000142016-10-13 17:21:01 +01004572
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004573 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004574 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004575 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004578 return( ret );
4579 }
4580
Simon Butcher99000142016-10-13 17:21:01 +01004581 mbedtls_ssl_update_handshake_status( ssl );
4582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004584
Paul Bakkered27a042013-04-18 22:46:23 +02004585 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004586}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004587#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4590static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004591{
Janos Follath865b3eb2019-12-16 11:46:15 +00004592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004593 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004594 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4599 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004600
4601 /*
4602 * struct {
4603 * uint32 ticket_lifetime_hint;
4604 * opaque ticket<0..2^16-1>;
4605 * } NewSessionTicket;
4606 *
4607 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4608 * 8 . 9 ticket_len (n)
4609 * 10 . 9+n ticket content
4610 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004611
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004612 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004613 ssl->session_negotiate,
4614 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004615 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004616 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004617 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004619 tlen = 0;
4620 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004621
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004622 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4623 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4624 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4625 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4626
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004627 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4628 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004629
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004630 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004631
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004632 /*
4633 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4634 * ChangeCipherSpec share the same state.
4635 */
4636 ssl->handshake->new_session_ticket = 0;
4637
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004638 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004639 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004641 return( ret );
4642 }
4643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004645
4646 return( 0 );
4647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004649
Paul Bakker5121ce52009-01-03 21:22:43 +00004650/*
Paul Bakker1961b702013-01-25 14:49:24 +01004651 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654{
4655 int ret = 0;
4656
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004657 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004663 return( ret );
4664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004666 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004668 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004669 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004670 return( ret );
4671 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004672#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004673
Paul Bakker1961b702013-01-25 14:49:24 +01004674 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 case MBEDTLS_SSL_HELLO_REQUEST:
4677 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 break;
4679
Paul Bakker1961b702013-01-25 14:49:24 +01004680 /*
4681 * <== ClientHello
4682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004684 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687#if defined(MBEDTLS_SSL_PROTO_DTLS)
4688 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4689 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004690#endif
4691
Paul Bakker1961b702013-01-25 14:49:24 +01004692 /*
4693 * ==> ServerHello
4694 * Certificate
4695 * ( ServerKeyExchange )
4696 * ( CertificateRequest )
4697 * ServerHelloDone
4698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004700 ret = ssl_write_server_hello( ssl );
4701 break;
4702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4704 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004705 break;
4706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004708 ret = ssl_write_server_key_exchange( ssl );
4709 break;
4710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004712 ret = ssl_write_certificate_request( ssl );
4713 break;
4714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004716 ret = ssl_write_server_hello_done( ssl );
4717 break;
4718
4719 /*
4720 * <== ( Certificate/Alert )
4721 * ClientKeyExchange
4722 * ( CertificateVerify )
4723 * ChangeCipherSpec
4724 * Finished
4725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4727 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004728 break;
4729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004731 ret = ssl_parse_client_key_exchange( ssl );
4732 break;
4733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004735 ret = ssl_parse_certificate_verify( ssl );
4736 break;
4737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4739 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004740 break;
4741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 case MBEDTLS_SSL_CLIENT_FINISHED:
4743 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004744 break;
4745
4746 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004747 * ==> ( NewSessionTicket )
4748 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004749 * Finished
4750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4752#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004753 if( ssl->handshake->new_session_ticket != 0 )
4754 ret = ssl_write_new_session_ticket( ssl );
4755 else
Paul Bakkera503a632013-08-14 13:48:06 +02004756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004758 break;
4759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 case MBEDTLS_SSL_SERVER_FINISHED:
4761 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004762 break;
4763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 case MBEDTLS_SSL_FLUSH_BUFFERS:
4765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4766 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004767 break;
4768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4770 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004771 break;
4772
4773 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004776 }
4777
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 return( ret );
4779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780#endif /* MBEDTLS_SSL_SRV_C */