blob: 0c7e6fdee390c0906450e34279a7b365c913fc2f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
SimonBd5800b72016-04-26 07:43:27 +010024#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
27#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010030#endif
31
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020033#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050036#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000037
38#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010045#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
49int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020050 const unsigned char *info,
51 size_t ilen )
52{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020053 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020057
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020060
61 memcpy( ssl->cli_id, info, ilen );
62 ssl->cli_id_len = ilen;
63
64 return( 0 );
65}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020066
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020067void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_ssl_cookie_write_t *f_cookie_write,
69 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020070 void *p_cookie )
71{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020072 conf->f_cookie_write = f_cookie_write;
73 conf->f_cookie_check = f_cookie_check;
74 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000080 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000081 size_t len )
82{
Janos Follath865b3eb2019-12-16 11:46:15 +000083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000084 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000085 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010088
Philippe Antoine747fd532018-05-30 09:13:21 +020089 if( len < 2 )
90 {
91 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
92 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
93 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
94 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
95 }
Paul Bakker5701cdc2012-09-27 21:49:42 +000096 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
97 if( servername_list_size + 2 != len )
98 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000103 }
104
105 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200106 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000107 {
108 hostname_len = ( ( p[1] << 8 ) | p[2] );
109 if( hostname_len + 3 > servername_list_size )
110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000115 }
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000118 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200119 ret = ssl->conf->f_sni( ssl->conf->p_sni,
120 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000121 if( ret != 0 )
122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
125 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
126 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000127 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000128 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000129 }
130
131 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000132 p += hostname_len + 3;
133 }
134
135 if( servername_list_size != 0 )
136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200138 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
139 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000141 }
142
143 return( 0 );
144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000146
Gilles Peskineeccd8882020-03-10 12:19:08 +0100147#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100148static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
149{
150 if( conf->f_psk != NULL )
151 return( 1 );
152
153 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
154 return( 0 );
155
156 if( conf->psk != NULL && conf->psk_len != 0 )
157 return( 1 );
158
159#if defined(MBEDTLS_USE_PSA_CRYPTO)
160 if( conf->psk_opaque != 0 )
161 return( 1 );
162#endif /* MBEDTLS_USE_PSA_CRYPTO */
163
164 return( 0 );
165}
166
167#if defined(MBEDTLS_USE_PSA_CRYPTO)
168static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
169{
170 if( ssl->conf->f_psk != NULL )
171 {
172 /* If we've used a callback to select the PSK,
173 * the static configuration is irrelevant. */
174
175 if( ssl->handshake->psk_opaque != 0 )
176 return( 1 );
177
178 return( 0 );
179 }
180
181 if( ssl->conf->psk_opaque != 0 )
182 return( 1 );
183
184 return( 0 );
185}
186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100187#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000190 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000191 size_t len )
192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_SSL_RENEGOTIATION)
194 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100195 {
196 /* Check verify-data in constant-time. The length OTOH is no secret */
197 if( len != 1 + ssl->verify_data_len ||
198 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100200 ssl->verify_data_len ) != 0 )
201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200203 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
204 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100206 }
207 }
208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000210 {
211 if( len != 1 || buf[0] != 0x0 )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200214 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
215 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +0000217 }
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000220 }
Paul Bakker48916f92012-09-16 19:57:18 +0000221
222 return( 0 );
223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100226 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100227
228/*
229 * Status of the implementation of signature-algorithms extension:
230 *
231 * Currently, we are only considering the signature-algorithm extension
232 * to pick a ciphersuite which allows us to send the ServerKeyExchange
233 * message with a signature-hash combination that the user allows.
234 *
235 * We do *not* check whether all certificates in our certificate
236 * chain are signed with an allowed signature-hash pair.
237 * This needs to be done at a later stage.
238 *
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000241 const unsigned char *buf,
242 size_t len )
243{
244 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100245
Paul Bakker23f36802012-09-28 14:15:14 +0000246 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200247 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200248
Hanno Becker7e5437a2017-04-28 17:15:26 +0100249 mbedtls_md_type_t md_cur;
250 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000251
Philippe Antoine747fd532018-05-30 09:13:21 +0200252 if ( len < 2 ) {
253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
255 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
256 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
257 }
Paul Bakker23f36802012-09-28 14:15:14 +0000258 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
259 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200260 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker23f36802012-09-28 14:15:14 +0000266 }
267
Hanno Becker7e5437a2017-04-28 17:15:26 +0100268 /* Currently we only guarantee signing the ServerKeyExchange message according
269 * to the constraints specified in this extension (see above), so it suffices
270 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200271 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100272 * This will change when we also consider certificate signatures,
273 * in which case we will need to remember the whole signature-hash
274 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200275 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100276
277 for( p = buf + 2; p < end; p += 2 )
278 {
279 /* Silently ignore unknown signature or hash algorithms. */
280
281 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
282 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
284 " unknown sig alg encoding %d", p[1] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100285 continue;
286 }
287
288 /* Check if we support the hash the user proposes */
289 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
290 if( md_cur == MBEDTLS_MD_NONE )
291 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
293 " unknown hash alg encoding %d", p[0] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100294 continue;
295 }
296
297 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
298 {
299 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
301 " match sig %d and hash %d",
Hanno Becker7e5437a2017-04-28 17:15:26 +0100302 sig_cur, md_cur ) );
303 }
304 else
305 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
307 "hash alg %d not supported", md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000308 }
Paul Bakker23f36802012-09-28 14:15:14 +0000309 }
310
Paul Bakker23f36802012-09-28 14:15:14 +0000311 return( 0 );
312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100314 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000315
Robert Cragie136884c2015-10-02 13:34:31 +0100316#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100317 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200319 const unsigned char *buf,
320 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100321{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200322 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100323 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100325
Philippe Antoine747fd532018-05-30 09:13:21 +0200326 if ( len < 2 ) {
327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
330 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
331 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100332 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
333 if( list_size + 2 != len ||
334 list_size % 2 != 0 )
335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100340 }
341
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200342 /* Should never happen unless client duplicates the extension */
343 if( ssl->handshake->curves != NULL )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
347 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200349 }
350
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100351 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200352 * and leave room for a final 0 */
353 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( our_size > MBEDTLS_ECP_DP_MAX )
355 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200356
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200357 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200358 {
359 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
360 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200361 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200362 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200363
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200364 ssl->handshake->curves = curves;
365
Paul Bakker41c83d32013-03-20 14:39:14 +0100366 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200367 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200370
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200371 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100372 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200373 *curves++ = curve_info;
374 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100375 }
376
377 list_size -= 2;
378 p += 2;
379 }
380
381 return( 0 );
382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200385 const unsigned char *buf,
386 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100387{
388 size_t list_size;
389 const unsigned char *p;
390
Philippe Antoine747fd532018-05-30 09:13:21 +0200391 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100397 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200398 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100399
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200400 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100401 while( list_size > 0 )
402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
404 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100405 {
Robert Cragie136884c2015-10-02 13:34:31 +0100406#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200407 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100408#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100409#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Robert Cragie136884c2015-10-02 13:34:31 +0100410 ssl->handshake->ecjpake_ctx.point_format = p[0];
411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100413 return( 0 );
414 }
415
416 list_size--;
417 p++;
418 }
419
420 return( 0 );
421}
Robert Cragieae8535d2015-10-06 17:11:18 +0100422#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
423 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100424
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200425#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
426static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
427 const unsigned char *buf,
428 size_t len )
429{
Janos Follath865b3eb2019-12-16 11:46:15 +0000430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200431
432 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
433 {
434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
435 return( 0 );
436 }
437
438 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
439 buf, len ) ) != 0 )
440 {
441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
443 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200444 return( ret );
445 }
446
447 /* Only mark the extension as OK when we're sure it is */
448 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
449
450 return( 0 );
451}
452#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
455static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200456 const unsigned char *buf,
457 size_t len )
458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200462 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
463 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200465 }
466
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200467 ssl->session_negotiate->mfl_code = buf[0];
468
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200469 return( 0 );
470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200472
Hanno Beckera0e20d02019-05-15 14:03:01 +0100473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100474static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
475 const unsigned char *buf,
476 size_t len )
477{
478 size_t peer_cid_len;
479
480 /* CID extension only makes sense in DTLS */
481 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
482 {
483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
485 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
486 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
487 }
488
489 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100490 * Quoting draft-ietf-tls-dtls-connection-id-05
491 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100492 *
493 * struct {
494 * opaque cid<0..2^8-1>;
495 * } ConnectionId;
496 */
497
498 if( len < 1 )
499 {
500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
501 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
502 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
503 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
504 }
505
506 peer_cid_len = *buf++;
507 len--;
508
509 if( len != peer_cid_len )
510 {
511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
512 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
513 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
514 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
515 }
516
517 /* Ignore CID if the user has disabled its use. */
518 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
519 {
520 /* Leave ssl->handshake->cid_in_use in its default
521 * value of MBEDTLS_SSL_CID_DISABLED. */
522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
523 return( 0 );
524 }
525
526 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
527 {
528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
530 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
531 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
532 }
533
Hanno Becker08556bf2019-05-03 12:43:44 +0100534 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100535 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
536 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
537
538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
539 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
540
Hanno Becker89dcc882019-04-26 13:56:39 +0100541 return( 0 );
542}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100543#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
546static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200547 const unsigned char *buf,
548 size_t len )
549{
550 if( len != 0 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
554 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200556 }
557
558 ((void) buf);
559
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200560 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200562
563 return( 0 );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
568static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100569 const unsigned char *buf,
570 size_t len )
571{
572 if( len != 0 )
573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200575 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
576 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100578 }
579
580 ((void) buf);
581
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200582 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100586 }
587
588 return( 0 );
589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
593static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200594 const unsigned char *buf,
595 size_t len )
596{
597 if( len != 0 )
598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200600 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
601 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200603 }
604
605 ((void) buf);
606
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200607 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200611 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200612
613 return( 0 );
614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
618static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200619 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200620 size_t len )
621{
Janos Follath865b3eb2019-12-16 11:46:15 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200623 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200624
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200625 mbedtls_ssl_session_init( &session );
626
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200627 if( ssl->conf->f_ticket_parse == NULL ||
628 ssl->conf->f_ticket_write == NULL )
629 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200630 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200631 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200632
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200633 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200634 ssl->handshake->new_session_ticket = 1;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200637
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200638 if( len == 0 )
639 return( 0 );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_SSL_RENEGOTIATION)
642 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200645 return( 0 );
646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200648
649 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200650 * Failures are ok: just ignore the ticket and proceed.
651 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200652 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
653 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200654 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200655 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200656
657 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
659 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
661 else
662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
663
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200666
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200667 /*
668 * Keep the session ID sent by the client, since we MUST send it back to
669 * inform them we're accepting the ticket (RFC 5077 section 3.4)
670 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200671 session.id_len = ssl->session_negotiate->id_len;
672 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200673
674 mbedtls_ssl_session_free( ssl->session_negotiate );
675 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
676
677 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500678 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200683
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200684 /* Don't send a new ticket after all, this one is OK */
685 ssl->handshake->new_session_ticket = 0;
686
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200687 return( 0 );
688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_ALPN)
692static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200693 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200694{
Paul Bakker14b16c62014-05-28 11:33:54 +0200695 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200696 const unsigned char *theirs, *start, *end;
697 const char **ours;
698
699 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200700 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200701 return( 0 );
702
703 /*
704 * opaque ProtocolName<1..2^8-1>;
705 *
706 * struct {
707 * ProtocolName protocol_name_list<2..2^16-1>
708 * } ProtocolNameList;
709 */
710
711 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
712 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200713 {
714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200717 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718
719 list_len = ( buf[0] << 8 ) | buf[1];
720 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200721 {
722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200725 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200726
727 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100728 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200729 */
730 start = buf + 2;
731 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100732 for( theirs = start; theirs != end; theirs += cur_len )
733 {
734 cur_len = *theirs++;
735
736 /* Current identifier must fit in list */
737 if( cur_len > (size_t)( end - theirs ) )
738 {
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
742 }
743
744 /* Empty strings MUST NOT be included */
745 if( cur_len == 0 )
746 {
747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
748 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
749 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
750 }
751 }
752
753 /*
754 * Use our order of preference
755 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200756 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200757 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200758 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200759 for( theirs = start; theirs != end; theirs += cur_len )
760 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200761 cur_len = *theirs++;
762
Paul Bakker14b16c62014-05-28 11:33:54 +0200763 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200764 memcmp( theirs, *ours, cur_len ) == 0 )
765 {
766 ssl->alpn_chosen = *ours;
767 return( 0 );
768 }
769 }
770 }
771
772 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
774 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
775 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200778
Johan Pascalb62bb512015-12-03 21:56:45 +0100779#if defined(MBEDTLS_SSL_DTLS_SRTP)
780static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300781 const unsigned char *buf,
782 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100783{
Ron Eldor3adb9922017-12-21 10:15:08 +0200784 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_SRTP_UNSET_PROFILE;
Johan Pascalb62bb512015-12-03 21:56:45 +0100785 size_t i,j;
Ron Eldor591f1622018-01-22 12:30:04 +0200786 size_t profile_length;
Ron Eldoref72faf2018-07-12 11:54:20 +0300787 const mbedtls_ssl_srtp_profile_info *profile_info;
Ron Eldor313d7b52018-12-10 14:56:21 +0200788 /*! 2 bytes for profile length and 1 byte for mki len */
789 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100790
791 /* If use_srtp is not configured, just ignore the extension */
Ron Eldoref72faf2018-07-12 11:54:20 +0300792 if( ssl->conf->dtls_srtp_profile_list == NULL ||
793 ssl->conf->dtls_srtp_profile_list_len == 0 )
Johan 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)
812 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200813 if( len < size_of_lengths + 2 )
814 {
815 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
816 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100817 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200818 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100819
Ron Eldor591f1622018-01-22 12:30:04 +0200820 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE;
821
Ron Eldoref72faf2018-07-12 11:54:20 +0300822 /* first 2 bytes are protection profile length(in bytes) */
823 profile_length = ( buf[0] << 8 ) | buf[1];
Ron Eldor591f1622018-01-22 12:30:04 +0200824
Ron Eldor313d7b52018-12-10 14:56:21 +0200825 if( profile_length > len - size_of_lengths )
826 {
827 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
828 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
829 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
830 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300831 /*
832 * parse the extension list values are defined in
833 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
834 */
835 for( j=0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100836 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300837 /* + 2 to skip the length field */
838 uint16_t protection_profile_value = buf[j + 2] << 8 | buf[j+3];
Ron Eldor089c9fe2018-12-06 17:12:49 +0200839 client_protection = mbedtls_ssl_get_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100840
Ron Eldorb4655392018-07-05 18:25:39 +0300841 profile_info = mbedtls_ssl_dtls_srtp_profile_info_from_id( client_protection );
842 if( profile_info != NULL )
843 {
844 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", profile_info->name ) );
845 }
Johan Pascal85269572020-08-25 10:01:54 +0200846 else
847 {
848 continue;
849 }
Ron Eldor591f1622018-01-22 12:30:04 +0200850 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200851 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200852 {
853 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
854 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200855 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Ron Eldorb4655392018-07-05 18:25:39 +0300856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", profile_info->name ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200857 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100858 }
859 }
Ron Eldor591f1622018-01-22 12:30:04 +0200860 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE )
861 break;
862 }
863 if( ( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) &&
Ron Eldora9788042018-12-05 11:04:31 +0200864 ( len > ( profile_length + 2 ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +0200865 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300866 ssl->dtls_srtp_info.mki_len = buf[profile_length + 2];
Johan Pascal85269572020-08-25 10:01:54 +0200867 if( ssl->dtls_srtp_info.mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
Ron Eldor313d7b52018-12-10 14:56:21 +0200868 ssl->dtls_srtp_info.mki_len + profile_length + size_of_lengths != len )
Ron Eldor591f1622018-01-22 12:30:04 +0200869 {
870 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
871 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
872 ssl->dtls_srtp_info.mki_len = 0;
873 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
874 }
875
876 for( i=0; i < ssl->dtls_srtp_info.mki_len; i++ )
877 {
Ron Eldoref72faf2018-07-12 11:54:20 +0300878 ssl->dtls_srtp_info.mki_value[i] = buf[profile_length + 2 + 1 + i];
Ron Eldor591f1622018-01-22 12:30:04 +0200879 }
Ron Eldorb4655392018-07-05 18:25:39 +0300880
Ron Eldoref72faf2018-07-12 11:54:20 +0300881 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
882 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100883 }
884
Ron Eldor591f1622018-01-22 12:30:04 +0200885 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100886}
887#endif /* MBEDTLS_SSL_DTLS_SRTP */
888
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100889/*
890 * Auxiliary functions for ServerHello parsing and related actions
891 */
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100894/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100895 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_ECDSA_C)
898static int ssl_check_key_curve( mbedtls_pk_context *pk,
899 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 const mbedtls_ecp_curve_info **crv = curves;
902 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100903
904 while( *crv != NULL )
905 {
906 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100907 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100908 crv++;
909 }
910
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100911 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100914
915/*
916 * Try picking a certificate for this ciphersuite,
917 * return 0 on success and -1 on failure.
918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919static int ssl_pick_cert( mbedtls_ssl_context *ssl,
920 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100923 mbedtls_pk_type_t pk_alg =
924 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200925 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100928 if( ssl->handshake->sni_key_cert != NULL )
929 list = ssl->handshake->sni_key_cert;
930 else
931#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200932 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100935 return( 0 );
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000938
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200939 if( list == NULL )
940 {
941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
942 return( -1 );
943 }
944
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100945 for( cur = list; cur != NULL; cur = cur->next )
946 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400947 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000949 cur->cert );
950
Gilles Peskinee198df52018-01-05 21:17:45 +0100951 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100954 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000955 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100956
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200957 /*
958 * This avoids sending the client a cert it'll reject based on
959 * keyUsage or other extensions.
960 *
961 * It also allows the user to provision different certificates for
962 * different uses based on keyUsage, eg if they want to avoid signing
963 * and decrypting with the same RSA key.
964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100966 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000969 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200970 continue;
971 }
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_ECDSA_C)
974 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200975 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100978 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000979 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100980#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100981
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100982 /*
983 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
984 * present them a SHA-higher cert rather than failing if it's the only
985 * one we got that satisfies the other conditions.
986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
988 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100989 {
990 if( fallback == NULL )
991 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000994 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100995 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000996 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100997 }
998
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100999 /* If we get there, we got a winner */
1000 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001001 }
1002
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001003 if( cur == NULL )
1004 cur = fallback;
1005
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001006 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001007 if( cur != NULL )
1008 {
1009 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001011 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001012 return( 0 );
1013 }
1014
1015 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001018
1019/*
1020 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1021 * Sets ciphersuite_info only if the suite matches.
1022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1024 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001027
Hanno Becker7e5437a2017-04-28 17:15:26 +01001028#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001029 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001030 mbedtls_pk_type_t sig_type;
1031#endif
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001034 if( suite_info == NULL )
1035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001038 }
1039
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Hanno Beckerecea07d2018-11-07 16:24:35 +00001041 suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001042
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001043 if( suite_info->min_minor_ver > ssl->minor_ver ||
1044 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001047 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001048 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001051 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001053 return( 0 );
1054#endif
1055
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001056#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001057 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001061 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001062 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001063#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001064
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001065#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1066 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001067 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001068 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1070 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001071 return( 0 );
1072 }
1073#endif
1074
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1077 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001078 ( ssl->handshake->curves == NULL ||
1079 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001082 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001083 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001084 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001085#endif
1086
Gilles Peskineeccd8882020-03-10 12:19:08 +01001087#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001088 /* If the ciphersuite requires a pre-shared key and we don't
1089 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001091 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
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
Hanno Becker7e5437a2017-04-28 17:15:26 +01001098#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001099 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001100 /* If the ciphersuite requires signing, check whether
1101 * a suitable hash algorithm is present. */
1102 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1103 {
1104 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1105 if( sig_type != MBEDTLS_PK_NONE &&
1106 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1107 {
1108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1109 "for signature algorithm %d", sig_type ) );
1110 return( 0 );
1111 }
1112 }
1113
1114#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001115 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001118 /*
1119 * Final check: if ciphersuite requires us to have a
1120 * certificate/key of a particular type:
1121 * - select the appropriate certificate if we have one, or
1122 * - try the next ciphersuite if we don't
1123 * This must be done last since we modify the key_cert list.
1124 */
1125 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001128 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001129 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001130 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001131#endif
1132
1133 *ciphersuite_info = suite_info;
1134 return( 0 );
1135}
1136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1138static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001139{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001140 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001141 unsigned int i, j;
1142 size_t n;
1143 unsigned int ciph_len, sess_len, chal_len;
1144 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001145 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_SSL_RENEGOTIATION)
1151 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001154 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1155 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001157 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001159
1160 buf = ssl->in_hdr;
1161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001165 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001167 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001169 buf[3], buf[4] ) );
1170
1171 /*
1172 * SSLv2 Client Hello
1173 *
1174 * Record layer:
1175 * 0 . 1 message length
1176 *
1177 * SSL layer:
1178 * 2 . 2 message type
1179 * 3 . 4 protocol version
1180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1182 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1185 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001186 }
1187
1188 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1189
1190 if( n < 17 || n > 512 )
1191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1193 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001194 }
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001197 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1198 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001199
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001200 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001203 " [%d:%d] < [%d:%d]",
1204 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001205 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1208 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1209 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001210 }
1211
Paul Bakker2fbefde2013-06-29 16:01:15 +02001212 ssl->handshake->max_major_ver = buf[3];
1213 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001218 return( ret );
1219 }
1220
1221 ssl->handshake->update_checksum( ssl, buf + 2, n );
1222
1223 buf = ssl->in_msg;
1224 n = ssl->in_left - 5;
1225
1226 /*
1227 * 0 . 1 ciphersuitelist length
1228 * 2 . 3 session id length
1229 * 4 . 5 challenge length
1230 * 6 . .. ciphersuitelist
1231 * .. . .. session id
1232 * .. . .. challenge
1233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001235
1236 ciph_len = ( buf[0] << 8 ) | buf[1];
1237 sess_len = ( buf[2] << 8 ) | buf[3];
1238 chal_len = ( buf[4] << 8 ) | buf[5];
1239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001241 ciph_len, sess_len, chal_len ) );
1242
1243 /*
1244 * Make sure each parameter length is valid
1245 */
1246 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1249 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001250 }
1251
1252 if( sess_len > 32 )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1255 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001256 }
1257
1258 if( chal_len < 8 || chal_len > 32 )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1261 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001262 }
1263
1264 if( n != 6 + ciph_len + sess_len + chal_len )
1265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1267 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001268 }
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001271 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001273 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001275 buf + 6 + ciph_len + sess_len, chal_len );
1276
1277 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001278 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001279 memset( ssl->session_negotiate->id, 0,
1280 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001281 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001282
1283 p += sess_len;
1284 memset( ssl->handshake->randbytes, 0, 64 );
1285 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1286
1287 /*
1288 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1289 */
1290 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1295#if defined(MBEDTLS_SSL_RENEGOTIATION)
1296 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001299 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001300
Gilles Peskinec94f7352017-05-10 16:37:56 +02001301 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1302 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#endif /* MBEDTLS_SSL_RENEGOTIATION */
1306 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001307 break;
1308 }
1309 }
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001312 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1313 {
1314 if( p[0] == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1316 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001319
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001320 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1325 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001328 }
1329
1330 break;
1331 }
1332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001334
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001335 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001336 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001337 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001339 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001340 for( i = 0; ciphersuites[i] != 0; i++ )
1341#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001342 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001343 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001344#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001345 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001346 if( p[0] != 0 ||
1347 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1348 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1349 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001350
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001351 got_common_suite = 1;
1352
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001353 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1354 &ciphersuite_info ) ) != 0 )
1355 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001356
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001357 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001358 goto have_ciphersuite_v2;
1359 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001360
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001361 if( got_common_suite )
1362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001364 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001366 }
1367 else
1368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1370 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001371 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001372
1373have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001375
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001376 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001377 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001378
1379 /*
1380 * SSLv2 Client Hello relevant renegotiation security checks
1381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001383 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001386 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1387 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001389 }
1390
1391 ssl->in_left = 0;
1392 ssl->state++;
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001395
1396 return( 0 );
1397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001399
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001400/* This function doesn't alert on errors that happen early during
1401 ClientHello parsing because they might indicate that the client is
1402 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001404{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001405 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001406 size_t i, j;
1407 size_t ciph_offset, comp_offset, ext_offset;
1408 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001410 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001411#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001412 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001414 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001415#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001416 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001417 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001419 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Hanno Becker7e5437a2017-04-28 17:15:26 +01001421 /* If there is no signature-algorithm extension present,
1422 * we need to fall back to the default values for allowed
1423 * signature-hash pairs. */
1424#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001425 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001426 int sig_hash_alg_ext_present = 0;
1427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001428 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001433read_record_header:
1434#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001435 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001437 * otherwise read it ourselves manually in order to support SSLv2
1438 * ClientHello, which doesn't use the same record layer format.
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_SSL_RENEGOTIATION)
1441 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001442#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001445 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001446 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001448 return( ret );
1449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 }
1451
1452 buf = ssl->in_hdr;
1453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001456 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001457#endif
1458 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001459 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001460#endif
1461
Hanno Becker5903de42019-05-03 14:46:38 +01001462 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001463
Paul Bakkerec636f32012-09-09 19:17:02 +00001464 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001465 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001466 *
1467 * Record layer:
1468 * 0 . 0 message type
1469 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001470 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001471 * 3 . 4 message length
1472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001474 buf[0] ) );
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1479 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001480 }
1481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001483 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001486 buf[1], buf[2] ) );
1487
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001488 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001489
1490 /* According to RFC 5246 Appendix E.1, the version here is typically
1491 * "{03,00}, the lowest version number supported by the client, [or] the
1492 * value of ClientHello.client_version", so the only meaningful check here
1493 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1497 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001500 /* For DTLS if this is the initial handshake, remember the client sequence
1501 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001503 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_SSL_RENEGOTIATION)
1505 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001506#endif
1507 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001508 {
1509 /* Epoch should be 0 for initial handshakes */
1510 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1513 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001514 }
1515
Hanno Becker19859472018-08-06 09:40:20 +01001516 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1519 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001522 ssl->next_record_offset = 0;
1523 ssl->in_left = 0;
1524 goto read_record_header;
1525 }
1526
1527 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001529#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001532
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001533 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_SSL_RENEGOTIATION)
1536 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001539 msg_len = ssl->in_hslen;
1540 }
1541 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001542#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001543 {
Angus Grattond8213d02016-05-25 20:56:48 +10001544 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1547 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001548 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001549
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001550 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001551 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001554 return( ret );
1555 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001556
1557 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001559 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001560 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001561 else
1562#endif
1563 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001564 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001565
1566 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001569
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001570 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001571
1572 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 * Handshake layer:
1574 * 0 . 0 handshake type
1575 * 1 . 3 handshake length
1576 * 4 . 5 DTLS only: message seqence number
1577 * 6 . 8 DTLS only: fragment offset
1578 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1583 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001584 }
1585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1591 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001592 }
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001595 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1596
1597 /* We don't support fragmentation of ClientHello (yet?) */
1598 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
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#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001606 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001607 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001608 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001609 * Copy the client's handshake message_seq on initial handshakes,
1610 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_SSL_RENEGOTIATION)
1613 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001614 {
1615 /* This couldn't be done in ssl_prepare_handshake_record() */
1616 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1617 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001618
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001619 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001622 "%d (expected %d)", cli_msg_seq,
1623 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001625 }
1626
1627 ssl->handshake->in_msg_seq++;
1628 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001629 else
1630#endif
1631 {
1632 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1633 ssl->in_msg[5];
1634 ssl->handshake->out_msg_seq = cli_msg_seq;
1635 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1636 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001637
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001638 /*
1639 * For now we don't support fragmentation, so make sure
1640 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001641 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001642 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1643 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1646 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001647 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 buf += mbedtls_ssl_hs_hdr_len( ssl );
1652 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001653
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001654 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001655 * ClientHello layer:
1656 * 0 . 1 protocol version
1657 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1658 * 34 . 35 session id length (1 byte)
1659 * 35 . 34+x session id
1660 * 35+x . 35+x DTLS only: cookie length (1 byte)
1661 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001662 * .. . .. ciphersuite list length (2 bytes)
1663 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001664 * .. . .. compression alg. list length (1 byte)
1665 * .. . .. compression alg. list
1666 * .. . .. extensions length (2 bytes, optional)
1667 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001668 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001669
1670 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001671 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001672 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1673 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001674 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001675 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1678 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001679 }
1680
1681 /*
1682 * Check and save the protocol version
1683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001687 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001688
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001689 ssl->handshake->max_major_ver = ssl->major_ver;
1690 ssl->handshake->max_minor_ver = ssl->minor_ver;
1691
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001692 if( ssl->major_ver < ssl->conf->min_major_ver ||
1693 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001696 " [%d:%d] < [%d:%d]",
1697 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001698 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1700 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001702 }
1703
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001704 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001705 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001706 ssl->major_ver = ssl->conf->max_major_ver;
1707 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001708 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001709 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1710 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001711
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001712 /*
1713 * Save client random (inc. Unix time)
1714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001716
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001717 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001718
1719 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001720 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001721 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001722 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001723
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001724 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001725 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001731 }
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001734
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001735 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001736 memset( ssl->session_negotiate->id, 0,
1737 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001738 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001739 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001740
1741 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001742 * Check the cookie length and content
1743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001745 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001746 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001747 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001748 cookie_len = buf[cookie_offset];
1749
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001750 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001753 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1754 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001756 }
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001759 buf + cookie_offset + 1, cookie_len );
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001762 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763#if defined(MBEDTLS_SSL_RENEGOTIATION)
1764 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001765#endif
1766 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001767 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001768 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001769 buf + cookie_offset + 1, cookie_len,
1770 ssl->cli_id, ssl->cli_id_len ) != 0 )
1771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001773 ssl->handshake->verify_cookie_len = 1;
1774 }
1775 else
1776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001778 ssl->handshake->verify_cookie_len = 0;
1779 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001780 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001783 {
1784 /* We know we didn't send a cookie, so it should be empty */
1785 if( cookie_len != 0 )
1786 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001787 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1789 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001790 }
1791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001793 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001794
1795 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001796 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001797 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001798 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001799 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001800 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001802 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001803
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001804 ciph_len = ( buf[ciph_offset + 0] << 8 )
1805 | ( buf[ciph_offset + 1] );
1806
1807 if( ciph_len < 2 ||
1808 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1809 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001812 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1813 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001815 }
1816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001818 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001819
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001820 /*
1821 * Check the compression algorithms length and pick one
1822 */
1823 comp_offset = ciph_offset + 2 + ciph_len;
1824
1825 comp_len = buf[comp_offset];
1826
1827 if( comp_len < 1 ||
1828 comp_len > 16 ||
1829 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1833 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001835 }
1836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001838 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1841#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001842 for( i = 0; i < comp_len; ++i )
1843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001847 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 }
1849 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001850#endif
1851
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001852 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001854 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001856#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001857
Janos Follathc6dab2b2016-05-23 14:27:02 +01001858 /* Do not parse the extensions if the protocol is SSLv3 */
1859#if defined(MBEDTLS_SSL_PROTO_SSL3)
1860 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1861 {
1862#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001863 /*
1864 * Check the extension length
1865 */
1866 ext_offset = comp_offset + 1 + comp_len;
1867 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001868 {
Simon Butcher584a5472016-05-23 16:24:52 +01001869 if( msg_len < ext_offset + 2 )
1870 {
1871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001872 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1873 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001874 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1875 }
1876
1877 ext_len = ( buf[ext_offset + 0] << 8 )
1878 | ( buf[ext_offset + 1] );
1879
1880 if( ( ext_len > 0 && ext_len < 4 ) ||
1881 msg_len != ext_offset + 2 + ext_len )
1882 {
1883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001884 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1885 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001886 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1887 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001888 }
Simon Butcher584a5472016-05-23 16:24:52 +01001889 else
1890 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001891
Simon Butcher584a5472016-05-23 16:24:52 +01001892 ext = buf + ext_offset + 2;
1893 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001894
Simon Butcher584a5472016-05-23 16:24:52 +01001895 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001896 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001897 unsigned int ext_id;
1898 unsigned int ext_size;
1899 if ( ext_len < 4 ) {
1900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1901 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1902 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1903 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1904 }
1905 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1906 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001907
Simon Butcher584a5472016-05-23 16:24:52 +01001908 if( ext_size + 4 > ext_len )
1909 {
1910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001911 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1912 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001913 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1914 }
1915 switch( ext_id )
1916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001918 case MBEDTLS_TLS_EXT_SERVERNAME:
1919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1920 if( ssl->conf->f_sni == NULL )
1921 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001922
Simon Butcher584a5472016-05-23 16:24:52 +01001923 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1924 if( ret != 0 )
1925 return( ret );
1926 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001928
Simon Butcher584a5472016-05-23 16:24:52 +01001929 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001932 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001933#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001934
Simon Butcher584a5472016-05-23 16:24:52 +01001935 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1936 if( ret != 0 )
1937 return( ret );
1938 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001941 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001942 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1944
Simon Butcher584a5472016-05-23 16:24:52 +01001945 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1946 if( ret != 0 )
1947 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001948
1949 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001950 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001952 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001953
Robert Cragie136884c2015-10-02 13:34:31 +01001954#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001955 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001956 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001958
Simon Butcher584a5472016-05-23 16:24:52 +01001959 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1960 if( ret != 0 )
1961 return( ret );
1962 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001963
Simon Butcher584a5472016-05-23 16:24:52 +01001964 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1966 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001967
Simon Butcher584a5472016-05-23 16:24:52 +01001968 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1969 if( ret != 0 )
1970 return( ret );
1971 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001972#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1973 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001974
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001975#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001976 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001978
Simon Butcher584a5472016-05-23 16:24:52 +01001979 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1980 if( ret != 0 )
1981 return( ret );
1982 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001983#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001986 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001988
Simon Butcher584a5472016-05-23 16:24:52 +01001989 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1990 if( ret != 0 )
1991 return( ret );
1992 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001996 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001998
Simon Butcher584a5472016-05-23 16:24:52 +01001999 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
2000 if( ret != 0 )
2001 return( ret );
2002 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002004
Hanno Beckera0e20d02019-05-15 14:03:01 +01002005#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01002006 case MBEDTLS_TLS_EXT_CID:
2007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2008
2009 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2010 if( ret != 0 )
2011 return( ret );
2012 break;
2013#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002016 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002018
Simon Butcher584a5472016-05-23 16:24:52 +01002019 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2020 if( ret != 0 )
2021 return( ret );
2022 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01002026 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2027 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002028
Simon Butcher584a5472016-05-23 16:24:52 +01002029 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
2030 if( ret != 0 )
2031 return( ret );
2032 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01002036 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002038
Simon Butcher584a5472016-05-23 16:24:52 +01002039 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2040 if( ret != 0 )
2041 return( ret );
2042 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002046 case MBEDTLS_TLS_EXT_ALPN:
2047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002048
Simon Butcher584a5472016-05-23 16:24:52 +01002049 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2050 if( ret != 0 )
2051 return( ret );
2052 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002054
Johan Pascalb62bb512015-12-03 21:56:45 +01002055#if defined(MBEDTLS_SSL_DTLS_SRTP)
2056 case MBEDTLS_TLS_EXT_USE_SRTP:
2057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2058 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2059 if ( ret != 0 )
2060 return( ret );
2061 break;
2062#endif /* MBEDTLS_SSL_DTLS_SRTP */
2063
Simon Butcher584a5472016-05-23 16:24:52 +01002064 default:
2065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
2066 ext_id ) );
2067 }
2068
2069 ext_len -= 4 + ext_size;
2070 ext += 4 + ext_size;
2071
2072 if( ext_len > 0 && ext_len < 4 )
2073 {
2074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002075 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2076 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01002077 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2078 }
Paul Bakker48916f92012-09-16 19:57:18 +00002079 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002080#if defined(MBEDTLS_SSL_PROTO_SSL3)
2081 }
2082#endif
2083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002085 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
2088 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002089 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002091
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002092 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002093 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2097 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002100 }
2101
2102 break;
2103 }
2104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002106
Hanno Becker7e5437a2017-04-28 17:15:26 +01002107#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002108 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002109
2110 /*
2111 * Try to fall back to default hash SHA1 if the client
2112 * hasn't provided any preferred signature-hash combinations.
2113 */
2114 if( sig_hash_alg_ext_present == 0 )
2115 {
2116 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2117
2118 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2119 md_default = MBEDTLS_MD_NONE;
2120
2121 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2122 }
2123
2124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002125 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002126
Paul Bakker48916f92012-09-16 19:57:18 +00002127 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002128 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2129 */
2130 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2135#if defined(MBEDTLS_SSL_RENEGOTIATION)
2136 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002137 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2139 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2141 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002143 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002146 break;
2147 }
2148 }
2149
2150 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002151 * Renegotiation security checks
2152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002154 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002157 handshake_failure = 1;
2158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#if defined(MBEDTLS_SSL_RENEGOTIATION)
2160 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2161 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002162 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002165 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2168 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002169 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002172 handshake_failure = 1;
2173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2175 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002176 renegotiation_info_seen == 1 )
2177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002179 handshake_failure = 1;
2180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002182
2183 if( handshake_failure == 1 )
2184 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2186 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002188 }
Paul Bakker380da532012-04-18 16:10:25 +00002189
Paul Bakker41c83d32013-03-20 14:39:14 +01002190 /*
2191 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002192 * (At the end because we need information from the EC-based extensions
2193 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002194 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002195 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002196 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002197 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002199 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002200 for( i = 0; ciphersuites[i] != 0; i++ )
2201#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002202 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002203 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002204#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002205 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002206 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2207 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2208 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002209
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002210 got_common_suite = 1;
2211
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002212 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2213 &ciphersuite_info ) ) != 0 )
2214 return( ret );
2215
2216 if( ciphersuite_info != NULL )
2217 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002218 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002219
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002220 if( got_common_suite )
2221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002223 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002224 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2225 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002227 }
2228 else
2229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002231 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2232 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002234 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002235
2236have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002238
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002239 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002240 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002241
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 ssl->state++;
2243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002247#endif
2248
Hanno Becker7e5437a2017-04-28 17:15:26 +01002249 /* Debugging-only output for testsuite */
2250#if defined(MBEDTLS_DEBUG_C) && \
2251 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002252 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002253 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2254 {
2255 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2256 if( sig_alg != MBEDTLS_PK_NONE )
2257 {
2258 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2259 sig_alg );
2260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2261 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2262 }
2263 else
2264 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2266 "%d - should not happen", sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002267 }
2268 }
2269#endif
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 return( 0 );
2274}
2275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2277static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002278 unsigned char *buf,
2279 size_t *olen )
2280{
2281 unsigned char *p = buf;
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002284 {
2285 *olen = 0;
2286 return;
2287 }
2288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2292 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002293
2294 *p++ = 0x00;
2295 *p++ = 0x00;
2296
2297 *olen = 4;
2298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002300
Hanno Beckera0e20d02019-05-15 14:03:01 +01002301#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002302static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2303 unsigned char *buf,
2304 size_t *olen )
2305{
2306 unsigned char *p = buf;
2307 size_t ext_len;
2308 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2309
2310 *olen = 0;
2311
2312 /* Skip writing the extension if we don't want to use it or if
2313 * the client hasn't offered it. */
2314 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2315 return;
2316
2317 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2318 * which is at most 255, so the increment cannot overflow. */
2319 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2320 {
2321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2322 return;
2323 }
2324
2325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2326
2327 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002328 * Quoting draft-ietf-tls-dtls-connection-id-05
2329 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002330 *
2331 * struct {
2332 * opaque cid<0..2^8-1>;
2333 * } ConnectionId;
2334 */
2335
2336 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2337 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2338 ext_len = (size_t) ssl->own_cid_len + 1;
2339 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2340 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2341
2342 *p++ = (uint8_t) ssl->own_cid_len;
2343 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2344
2345 *olen = ssl->own_cid_len + 5;
2346}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2350static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002351 unsigned char *buf,
2352 size_t *olen )
2353{
2354 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2356 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002357
Hanno Becker27b34d52017-10-20 14:24:51 +01002358 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002360 {
2361 *olen = 0;
2362 return;
2363 }
2364
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002365 /*
2366 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2367 * from a client and then selects a stream or Authenticated Encryption
2368 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2369 * encrypt-then-MAC response extension back to the client."
2370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002372 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2374 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002375 {
2376 *olen = 0;
2377 return;
2378 }
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2383 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002384
2385 *p++ = 0x00;
2386 *p++ = 0x00;
2387
2388 *olen = 4;
2389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2393static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002394 unsigned char *buf,
2395 size_t *olen )
2396{
2397 unsigned char *p = buf;
2398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2400 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002401 {
2402 *olen = 0;
2403 return;
2404 }
2405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002407 "extension" ) );
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2410 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002411
2412 *p++ = 0x00;
2413 *p++ = 0x00;
2414
2415 *olen = 4;
2416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2420static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002421 unsigned char *buf,
2422 size_t *olen )
2423{
2424 unsigned char *p = buf;
2425
2426 if( ssl->handshake->new_session_ticket == 0 )
2427 {
2428 *olen = 0;
2429 return;
2430 }
2431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2435 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002436
2437 *p++ = 0x00;
2438 *p++ = 0x00;
2439
2440 *olen = 4;
2441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002445 unsigned char *buf,
2446 size_t *olen )
2447{
2448 unsigned char *p = buf;
2449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002451 {
2452 *olen = 0;
2453 return;
2454 }
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2459 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461#if defined(MBEDTLS_SSL_RENEGOTIATION)
2462 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002463 {
2464 *p++ = 0x00;
2465 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2466 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002467
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002468 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2469 p += ssl->verify_data_len;
2470 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2471 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002472 }
2473 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002475 {
2476 *p++ = 0x00;
2477 *p++ = 0x01;
2478 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002479 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002480
2481 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002482}
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2485static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002486 unsigned char *buf,
2487 size_t *olen )
2488{
2489 unsigned char *p = buf;
2490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002492 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002493 *olen = 0;
2494 return;
2495 }
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2500 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002501
2502 *p++ = 0x00;
2503 *p++ = 1;
2504
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002505 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002506
2507 *olen = 5;
2508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002510
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002511#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002512 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002514 unsigned char *buf,
2515 size_t *olen )
2516{
2517 unsigned char *p = buf;
2518 ((void) ssl);
2519
Paul Bakker677377f2013-10-28 12:54:26 +01002520 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002522 {
2523 *olen = 0;
2524 return;
2525 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2530 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002531
2532 *p++ = 0x00;
2533 *p++ = 2;
2534
2535 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002537
2538 *olen = 6;
2539}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002540#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002541
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002542#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2543static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2544 unsigned char *buf,
2545 size_t *olen )
2546{
Janos Follath865b3eb2019-12-16 11:46:15 +00002547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002548 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002549 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002550 size_t kkpp_len;
2551
2552 *olen = 0;
2553
2554 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002555 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002556 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2557 return;
2558
2559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2560
2561 if( end - p < 4 )
2562 {
2563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2564 return;
2565 }
2566
2567 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2568 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2569
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002570 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2571 p + 2, end - p - 2, &kkpp_len,
2572 ssl->conf->f_rng, ssl->conf->p_rng );
2573 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002574 {
2575 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2576 return;
2577 }
2578
2579 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2580 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2581
2582 *olen = kkpp_len + 4;
2583}
2584#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_SSL_ALPN )
2587static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002588 unsigned char *buf, size_t *olen )
2589{
2590 if( ssl->alpn_chosen == NULL )
2591 {
2592 *olen = 0;
2593 return;
2594 }
2595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002597
2598 /*
2599 * 0 . 1 ext identifier
2600 * 2 . 3 ext length
2601 * 4 . 5 protocol list length
2602 * 6 . 6 protocol name length
2603 * 7 . 7+n protocol name
2604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2606 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002607
2608 *olen = 7 + strlen( ssl->alpn_chosen );
2609
2610 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2611 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2612
2613 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2614 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2615
2616 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2617
2618 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2619}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002621
Ron Eldor3adb9922017-12-21 10:15:08 +02002622#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002623static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002624 unsigned char *buf,
2625 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002626{
Ron Eldor75870ec2018-12-06 17:31:55 +02002627 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002628 uint16_t profile_value = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002629
Ron Eldor3adb9922017-12-21 10:15:08 +02002630 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE )
Johan Pascalb62bb512015-12-03 21:56:45 +01002631 {
2632 *olen = 0;
2633 return;
2634 }
2635
2636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2637
Ron Eldor591f1622018-01-22 12:30:04 +02002638 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
Ron Eldoref72faf2018-07-12 11:54:20 +03002639 ssl->dtls_srtp_info.mki_len != 0 )
Ron Eldor591f1622018-01-22 12:30:04 +02002640 {
2641 mki_len = ssl->dtls_srtp_info.mki_len;
2642 }
2643
Johan Pascalb62bb512015-12-03 21:56:45 +01002644 /* extension */
2645 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2646 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002647 /*
2648 * total length 5 and mki value: only one profile(2 bytes)
2649 * and length(2 bytes) and srtp_mki )
2650 */
Ron Eldor591f1622018-01-22 12:30:04 +02002651 ext_len = 5 + mki_len;
2652 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2653 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002654
2655 /* protection profile length: 2 */
2656 buf[4] = 0x00;
2657 buf[5] = 0x02;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002658 profile_value = mbedtls_ssl_get_srtp_profile_iana_value( ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
2659 if( profile_value != 0xFFFF )
2660 {
2661 buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF );
2662 buf[7] = (unsigned char)( profile_value & 0xFF );
2663 }
2664 else
2665 {
2666 *olen = 0;
2667 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002668 }
2669
Ron Eldor591f1622018-01-22 12:30:04 +02002670 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002671 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002672
Ron Eldor591f1622018-01-22 12:30:04 +02002673 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002674}
2675#endif /* MBEDTLS_SSL_DTLS_SRTP */
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2678static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002679{
Janos Follath865b3eb2019-12-16 11:46:15 +00002680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002681 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002682 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002685
2686 /*
2687 * struct {
2688 * ProtocolVersion server_version;
2689 * opaque cookie<0..2^8-1>;
2690 * } HelloVerifyRequest;
2691 */
2692
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002693 /* The RFC is not clear on this point, but sending the actual negotiated
2694 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002696 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002698 p += 2;
2699
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002700 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002701 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002705 }
2706
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002707 /* Skip length byte until we know the length */
2708 cookie_len_byte = p++;
2709
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002710 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002711 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002712 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002715 return( ret );
2716 }
2717
2718 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002721
2722 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2724 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002727
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002728 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002729 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002731 return( ret );
2732 }
2733
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002734#if defined(MBEDTLS_SSL_PROTO_DTLS)
2735 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2736 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2737 {
2738 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2739 return( ret );
2740 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002741#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002744
2745 return( 0 );
2746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002752 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002753#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002755 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 unsigned char *buf, *p;
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002762 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002766
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002767 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002768 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002770
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002771 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2774 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002775 }
2776
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 /*
2778 * 0 . 0 handshake type
2779 * 1 . 3 handshake length
2780 * 4 . 5 protocol version
2781 * 6 . 9 UNIX time()
2782 * 10 . 37 random bytes
2783 */
2784 buf = ssl->out_msg;
2785 p = buf + 4;
2786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002788 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002789 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002792 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002795 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 *p++ = (unsigned char)( t >> 24 );
2797 *p++ = (unsigned char)( t >> 16 );
2798 *p++ = (unsigned char)( t >> 8 );
2799 *p++ = (unsigned char)( t );
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002802#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002803 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002804 return( ret );
2805
2806 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002809 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002810 return( ret );
2811
2812 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Paul Bakker48916f92012-09-16 19:57:18 +00002814 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
2818 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002819 * Resume is 0 by default, see ssl_handshake_init().
2820 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2821 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002823 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#if defined(MBEDTLS_SSL_RENEGOTIATION)
2825 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002826#endif
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002827 ssl->session_negotiate->id_len != 0 &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002828 ssl->conf->f_get_cache != NULL &&
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01002829 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002832 ssl->handshake->resume = 1;
2833 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002835 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 {
2837 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002838 * New session, create a new session id,
2839 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 ssl->state++;
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002844 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002845#endif
2846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002848 if( ssl->handshake->new_session_ticket != 0 )
2849 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002850 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002851 memset( ssl->session_negotiate->id, 0, 32 );
2852 }
2853 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002855 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002856 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002857 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002858 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002859 return( ret );
2860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 }
2862 else
2863 {
2864 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002865 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002867 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002873 return( ret );
2874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 }
2876
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002877 /*
2878 * 38 . 38 session id length
2879 * 39 . 38+n session id
2880 * 39+n . 40+n chosen ciphersuite
2881 * 41+n . 41+n chosen compression alg.
2882 * 42+n . 43+n extensions length
2883 * 44+n . 43+n+m extensions
2884 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002885 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2886 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2887 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2890 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002892 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakker48916f92012-09-16 19:57:18 +00002894 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2895 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2896 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2899 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002901 ssl->session_negotiate->compression ) );
2902
Janos Follathc6dab2b2016-05-23 14:27:02 +01002903 /* Do not write the extensions if the protocol is SSLv3 */
2904#if defined(MBEDTLS_SSL_PROTO_SSL3)
2905 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2906 {
2907#endif
2908
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002909 /*
2910 * First write extensions, then the total length
2911 */
2912 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2913 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002916 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2917 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002918#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002921 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2922 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002923#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002924
Hanno Beckera0e20d02019-05-15 14:03:01 +01002925#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002926 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2927 ext_len += olen;
2928#endif
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002931 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2932 ext_len += olen;
2933#endif
2934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002936 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2937 ext_len += olen;
2938#endif
2939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002941 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2942 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002943#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002944
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002945#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002946 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002947 if ( mbedtls_ssl_ciphersuite_uses_ec(
2948 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2949 {
2950 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2951 ext_len += olen;
2952 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002953#endif
2954
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002955#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2956 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2957 ext_len += olen;
2958#endif
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002961 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2962 ext_len += olen;
2963#endif
2964
Johan Pascalb62bb512015-12-03 21:56:45 +01002965#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002966 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
Johan Pascalb62bb512015-12-03 21:56:45 +01002967 ext_len += olen;
2968#endif
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002971
Paul Bakkera7036632014-04-30 10:15:38 +02002972 if( ext_len > 0 )
2973 {
2974 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2975 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2976 p += ext_len;
2977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Janos Follathc6dab2b2016-05-23 14:27:02 +01002979#if defined(MBEDTLS_SSL_PROTO_SSL3)
2980 }
2981#endif
2982
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2985 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002987 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
2991 return( ret );
2992}
2993
Gilles Peskineeccd8882020-03-10 12:19:08 +01002994#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002996{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002997 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002998 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003001
Hanno Becker77adddc2019-02-07 12:32:43 +00003002 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005 ssl->state++;
3006 return( 0 );
3007 }
3008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003011}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003012#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003016 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003017 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03003018 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003019 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10003021 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003023 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
3027 ssl->state++;
3028
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003029#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3030 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3031 authmode = ssl->handshake->sni_authmode;
3032 else
3033#endif
Ron Eldor57cc70e2018-04-02 18:25:16 +03003034#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03003035 /*
3036 * check if we have a chosen srtp protection profile,
3037 * force verify mode to be at least OPTIONAL
3038 */
3039 if ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE &&
Ron Eldora9788042018-12-05 11:04:31 +02003040 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_NONE )
3041 {
Ron Eldor1c399bd2018-07-04 18:45:27 +03003042 authmode = MBEDTLS_SSL_VERIFY_OPTIONAL;
Ron Eldor57cc70e2018-04-02 18:25:16 +03003043 }
Ron Eldoref72faf2018-07-12 11:54:20 +03003044 else
Ron Eldor57cc70e2018-04-02 18:25:16 +03003045#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003046 authmode = ssl->conf->authmode;
3047
Hanno Becker77adddc2019-02-07 12:32:43 +00003048 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003049 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 {
Ron Eldor57cc70e2018-04-02 18:25:16 +03003051#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldora9788042018-12-05 11:04:31 +02003052 /* check if we have a chosen srtp protection profile */
3053 if ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE )
3054 {
Ron Eldor57cc70e2018-04-02 18:25:16 +03003055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "should not happen" ) );
Ron Eldoref72faf2018-07-12 11:54:20 +03003056 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Ron Eldor57cc70e2018-04-02 18:25:16 +03003057 }
3058 else
3059 {
3060#endif
3061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3062 return( 0 );
3063#if defined(MBEDTLS_SSL_DTLS_SRTP)
3064 }
3065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 }
3067
3068 /*
3069 * 0 . 0 handshake type
3070 * 1 . 3 handshake length
3071 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003072 * 5 .. m-1 cert types
3073 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003074 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 * n .. n+1 length of all DNs
3076 * n+2 .. n+3 length of DN 1
3077 * n+4 .. ... Distinguished Name #1
3078 * ... .. ... length of DN 2, etc.
3079 */
3080 buf = ssl->out_msg;
3081 p = buf + 4;
3082
3083 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003084 * Supported certificate types
3085 *
3086 * ClientCertificateType certificate_types<1..2^8-1>;
3087 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003089 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#if defined(MBEDTLS_RSA_C)
3092 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_ECDSA_C)
3095 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003096#endif
3097
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003098 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003099 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003100
Paul Bakker577e0062013-08-28 11:57:20 +02003101 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003103 /*
3104 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003105 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003106 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3107 *
3108 * struct {
3109 * HashAlgorithm hash;
3110 * SignatureAlgorithm signature;
3111 * } SignatureAndHashAlgorithm;
3112 *
3113 * enum { (255) } HashAlgorithm;
3114 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003117 {
Simon Butcher99000142016-10-13 17:21:01 +01003118 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003119
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003120 /*
3121 * Supported signature algorithms
3122 */
Simon Butcher99000142016-10-13 17:21:01 +01003123 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3124 {
3125 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3126
3127 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3128 continue;
3129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003131 p[2 + sa_len++] = hash;
3132 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003135 p[2 + sa_len++] = hash;
3136 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003137#endif
Simon Butcher99000142016-10-13 17:21:01 +01003138 }
Paul Bakker926af752012-11-23 13:38:07 +01003139
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003140 p[0] = (unsigned char)( sa_len >> 8 );
3141 p[1] = (unsigned char)( sa_len );
3142 sa_len += 2;
3143 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003144 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003147 /*
3148 * DistinguishedName certificate_authorities<0..2^16-1>;
3149 * opaque DistinguishedName<1..2^16-1>;
3150 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003153 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003154
3155 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003156 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003157 /* NOTE: If trusted certificates are provisioned
3158 * via a CA callback (configured through
3159 * `mbedtls_ssl_conf_ca_cb()`, then the
3160 * CertificateRequest is currently left empty. */
3161
Janos Follath088ce432017-04-10 12:42:31 +01003162#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3163 if( ssl->handshake->sni_ca_chain != NULL )
3164 crt = ssl->handshake->sni_ca_chain;
3165 else
3166#endif
3167 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003168
Janos Follath088ce432017-04-10 12:42:31 +01003169 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003170 {
irwirc9bc3002020-04-01 13:46:36 +03003171 /* It follows from RFC 5280 A.1 that this length
3172 * can be represented in at most 11 bits. */
3173 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003174
irwirc9bc3002020-04-01 13:46:36 +03003175 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003176 {
3177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3178 break;
3179 }
3180
3181 *p++ = (unsigned char)( dn_size >> 8 );
3182 *p++ = (unsigned char)( dn_size );
3183 memcpy( p, crt->subject_raw.p, dn_size );
3184 p += dn_size;
3185
3186 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3187
3188 total_dn_size += 2 + dn_size;
3189 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 }
3192
Paul Bakker926af752012-11-23 13:38:07 +01003193 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3195 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003196 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
3197 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003199 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
3203 return( ret );
3204}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003205#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3208 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3209static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003210{
Janos Follath865b3eb2019-12-16 11:46:15 +00003211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3216 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003217 }
3218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3220 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3221 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003224 return( ret );
3225 }
3226
3227 return( 0 );
3228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3230 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003231
Gilles Peskineeccd8882020-03-10 12:19:08 +01003232#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003233 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003234static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003235 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003236{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003237 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3238 * signature length which will be added in ssl_write_server_key_exchange
3239 * after the call to ssl_prepare_server_key_exchange.
3240 * ssl_write_server_key_exchange also takes care of incrementing
3241 * ssl->out_msglen. */
3242 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003243 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003244 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003245 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003246 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003247 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3248 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003249 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003250 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003251 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003252 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003253 return( ret );
3254}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003255#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003256 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003257
Gilles Peskined3eb0612018-01-08 17:07:44 +01003258/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003259 * calculating the signature if any, but excluding formatting the
3260 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003261static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3262 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003265 ssl->handshake->ciphersuite_info;
3266
Gilles Peskineeccd8882020-03-10 12:19:08 +01003267#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3268#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003269 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003270#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3271#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003272
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003273 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003274#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003275 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003276#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003277
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003278 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003279
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003280 /*
3281 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003282 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003283 *
3284 */
3285
3286 /*
3287 * - ECJPAKE key exchanges
3288 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003289#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3290 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3291 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003293 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003294
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003295 ret = mbedtls_ecjpake_write_round_two(
3296 &ssl->handshake->ecjpake_ctx,
3297 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003298 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003299 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003300 if( ret != 0 )
3301 {
3302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3303 return( ret );
3304 }
3305
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003306 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003307 }
3308#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3309
Hanno Becker1aa267c2017-04-28 17:08:27 +01003310 /*
3311 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3312 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3313 * we use empty support identity hints here.
3314 **/
3315#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3317 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3318 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003319 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003320 ssl->out_msg[ssl->out_msglen++] = 0x00;
3321 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3324 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003325
Hanno Becker7e5437a2017-04-28 17:15:26 +01003326 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003327 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003328 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003329#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003330 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003331 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003333 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003334
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003335 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3336 {
3337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3339 }
3340
Paul Bakker41c83d32013-03-20 14:39:14 +01003341 /*
3342 * Ephemeral DH parameters:
3343 *
3344 * struct {
3345 * opaque dh_p<1..2^16-1>;
3346 * opaque dh_g<1..2^16-1>;
3347 * opaque dh_Ys<1..2^16-1>;
3348 * } ServerDHParams;
3349 */
Hanno Beckerab740562017-10-04 13:15:37 +01003350 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3351 &ssl->conf->dhm_P,
3352 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003353 {
Hanno Beckerab740562017-10-04 13:15:37 +01003354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003355 return( ret );
3356 }
Paul Bakker48916f92012-09-16 19:57:18 +00003357
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003358 if( ( ret = mbedtls_dhm_make_params(
3359 &ssl->handshake->dhm_ctx,
3360 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3361 ssl->out_msg + ssl->out_msglen, &len,
3362 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003365 return( ret );
3366 }
3367
Gilles Peskineeccd8882020-03-10 12:19:08 +01003368#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003369 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003370#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003371
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003372 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3375 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3376 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3377 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003378 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003379#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003380
Hanno Becker1aa267c2017-04-28 17:08:27 +01003381 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003382 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003383 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003384#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003385 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003387 /*
3388 * Ephemeral ECDH parameters:
3389 *
3390 * struct {
3391 * ECParameters curve_params;
3392 * ECPoint public;
3393 * } ServerECDHParams;
3394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003398 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003399
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003400 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003401 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003402 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3403 if( (*curve)->grp_id == *gid )
3404 goto curve_matching_done;
3405
3406curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003407 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3410 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003411 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003414
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003415 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3416 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003417 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003419 return( ret );
3420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003421
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003422 if( ( ret = mbedtls_ecdh_make_params(
3423 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003424 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003425 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003426 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003429 return( ret );
3430 }
3431
Gilles Peskineeccd8882020-03-10 12:19:08 +01003432#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003433 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003434#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003435
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003436 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003437
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003438 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3439 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003440 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003441#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003442
Hanno Becker1aa267c2017-04-28 17:08:27 +01003443 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003444 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003445 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003446 * exchange parameters, compute and add the signature here.
3447 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003448 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003449#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003450 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003451 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003452 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003453 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003454 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003456
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003457 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003458 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003459 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003460 * to choose appropriate hash.
3461 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3462 * (RFC 4492, Sec. 5.4)
3463 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003464 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003465
3466 mbedtls_md_type_t md_alg;
3467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003469 mbedtls_pk_type_t sig_alg =
3470 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003472 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003473 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3474 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003475 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003476 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3477 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003480 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003481 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003483 }
3484 }
Paul Bakker577e0062013-08-28 11:57:20 +02003485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3487#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3488 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003489 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003490 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003491 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003493 }
3494 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003495#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3496 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003497 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003498 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003500 }
3501
Hanno Becker7e5437a2017-04-28 17:15:26 +01003502 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3503
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003504 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003505 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3508 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3509 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003510 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003511 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003512 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3513 dig_signed,
3514 dig_signed_len );
3515 if( ret != 0 )
3516 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003517 }
3518 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3520 MBEDTLS_SSL_PROTO_TLS1_1 */
3521#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3522 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3523 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003524 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003525 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003526 dig_signed,
3527 dig_signed_len,
3528 md_alg );
3529 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003530 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003531 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003532 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3534 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003538 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003539
Gilles Peskineebd652f2018-01-05 21:18:59 +01003540 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003541
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003542 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003543 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3546 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003547 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003548 /*
3549 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003550 * explicitly through a prefix to the signature.
3551 *
3552 * struct {
3553 * HashAlgorithm hash;
3554 * SignatureAlgorithm signature;
3555 * } SignatureAndHashAlgorithm;
3556 *
3557 * struct {
3558 * SignatureAndHashAlgorithm algorithm;
3559 * opaque signature<0..2^16-1>;
3560 * } DigitallySigned;
3561 *
3562 */
3563
Gilles Peskine1004c192018-01-08 16:59:14 +01003564 ssl->out_msg[ssl->out_msglen++] =
3565 mbedtls_ssl_hash_from_md_alg( md_alg );
3566 ssl->out_msg[ssl->out_msglen++] =
3567 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003568 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003570
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003571#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003572 if( ssl->conf->f_async_sign_start != NULL )
3573 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003574 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003575 mbedtls_ssl_own_cert( ssl ),
3576 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003577 switch( ret )
3578 {
3579 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3580 /* act as if f_async_sign was null */
3581 break;
3582 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003583 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003584 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003585 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003586 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003587 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3588 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003589 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003590 return( ret );
3591 }
3592 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003593#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003594
3595 if( mbedtls_ssl_own_key( ssl ) == NULL )
3596 {
3597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3598 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3599 }
3600
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003601 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3602 * signature length which will be added in ssl_write_server_key_exchange
3603 * after the call to ssl_prepare_server_key_exchange.
3604 * ssl_write_server_key_exchange also takes care of incrementing
3605 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003606 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3607 md_alg, hash, hashlen,
3608 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003609 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003610 ssl->conf->f_rng,
3611 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003614 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003615 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003616 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003617#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003618
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003619 return( 0 );
3620}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003621
Gilles Peskined3eb0612018-01-08 17:07:44 +01003622/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003623 * that do not include a ServerKeyExchange message, do nothing. Either
3624 * way, if successful, move on to the next step in the SSL state
3625 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003626static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3627{
Janos Follath865b3eb2019-12-16 11:46:15 +00003628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003629 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003630#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003631 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003632 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003633#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003634
Gilles Peskined3eb0612018-01-08 17:07:44 +01003635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3636
Gilles Peskineeccd8882020-03-10 12:19:08 +01003637#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003638 /* Extract static ECDH parameters and abort if ServerKeyExchange
3639 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003640 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3641 {
3642 /* For suites involving ECDH, extract DH parameters
3643 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003644#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003645 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3646 {
3647 ssl_get_ecdh_params_from_cert( ssl );
3648 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003649#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003650
3651 /* Key exchanges not involving ephemeral keys don't use
3652 * ServerKeyExchange, so end here. */
3653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3654 ssl->state++;
3655 return( 0 );
3656 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003657#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003658
Gilles Peskineeccd8882020-03-10 12:19:08 +01003659#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003660 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003661 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003662 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003663 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003664 {
3665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3666 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003667 }
3668 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003669#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003670 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003671 {
3672 /* ServerKeyExchange is needed. Prepare the message. */
3673 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003674 }
3675
3676 if( ret != 0 )
3677 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003678 /* If we're starting to write a new message, set ssl->out_msglen
3679 * to 0. But if we're resuming after an asynchronous message,
3680 * out_msglen is the amount of data written so far and mst be
3681 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003682 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3684 else
3685 ssl->out_msglen = 0;
3686 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003687 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003688
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003689 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003690 * ssl_prepare_server_key_exchange already wrote the signature
3691 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003692#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003693 if( signature_len != 0 )
3694 {
3695 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3696 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3697
3698 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3699 ssl->out_msg + ssl->out_msglen,
3700 signature_len );
3701
3702 /* Skip over the already-written signature */
3703 ssl->out_msglen += signature_len;
3704 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003705#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003706
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003707 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3709 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
3711 ssl->state++;
3712
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003713 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003715 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 return( ret );
3717 }
3718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721}
3722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003724{
Janos Follath865b3eb2019-12-16 11:46:15 +00003725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003728
3729 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3731 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
3733 ssl->state++;
3734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003736 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003738#endif
3739
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003740 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 return( ret );
3744 }
3745
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003746#if defined(MBEDTLS_SSL_PROTO_DTLS)
3747 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3748 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3749 {
3750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3751 return( ret );
3752 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003753#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003756
3757 return( 0 );
3758}
3759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3761 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3762static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003763 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003766 size_t n;
3767
3768 /*
3769 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3770 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003771 if( *p + 2 > end )
3772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3774 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003775 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003776
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003777 n = ( (*p)[0] << 8 ) | (*p)[1];
3778 *p += 2;
3779
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003780 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3783 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003784 }
3785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3789 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003790 }
3791
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003792 *p += n;
3793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003795
Paul Bakker70df2fb2013-04-17 17:19:09 +02003796 return( ret );
3797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3799 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3802 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003803
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003804#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003805static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3806 unsigned char *peer_pms,
3807 size_t *peer_pmslen,
3808 size_t peer_pmssize )
3809{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003810 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003811 peer_pms, peer_pmslen, peer_pmssize );
3812 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3813 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003814 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003815 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003816 }
3817 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3818 return( ret );
3819}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003820#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003821
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003822static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3823 const unsigned char *p,
3824 const unsigned char *end,
3825 unsigned char *peer_pms,
3826 size_t *peer_pmslen,
3827 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003828{
Janos Follath865b3eb2019-12-16 11:46:15 +00003829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003830 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3831 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3832 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003833
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003834#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003835 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003836 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003837 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003838 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3840 return( ssl_resume_decrypt_pms( ssl,
3841 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003842 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003843#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003844
3845 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003846 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3849 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3850 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003851 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003852 if ( p + 2 > end ) {
3853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3854 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3855 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003856 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3857 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3860 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003861 }
3862 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003863#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003864
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003865 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3868 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003869 }
3870
Gilles Peskine422ccab2018-01-11 18:29:01 +01003871 /*
3872 * Decrypt the premaster secret
3873 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003874#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003875 if( ssl->conf->f_async_decrypt_start != NULL )
3876 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003877 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003878 mbedtls_ssl_own_cert( ssl ),
3879 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003880 switch( ret )
3881 {
3882 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3883 /* act as if f_async_decrypt_start was null */
3884 break;
3885 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003886 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003887 return( ssl_resume_decrypt_pms( ssl,
3888 peer_pms,
3889 peer_pmslen,
3890 peer_pmssize ) );
3891 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003892 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003893 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3894 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003895 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003896 return( ret );
3897 }
3898 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003899#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003900
Gilles Peskine422ccab2018-01-11 18:29:01 +01003901 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3902 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3904 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3905 }
3906
3907 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003908 peer_pms, peer_pmslen, peer_pmssize,
3909 ssl->conf->f_rng, ssl->conf->p_rng );
3910 return( ret );
3911}
3912
3913static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3914 const unsigned char *p,
3915 const unsigned char *end,
3916 size_t pms_offset )
3917{
Janos Follath865b3eb2019-12-16 11:46:15 +00003918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003919 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3920 unsigned char ver[2];
3921 unsigned char fake_pms[48], peer_pms[48];
3922 unsigned char mask;
3923 size_t i, peer_pmslen;
3924 unsigned int diff;
3925
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003926 /* In case of a failure in decryption, the decryption may write less than
3927 * 2 bytes of output, but we always read the first two bytes. It doesn't
3928 * matter in the end because diff will be nonzero in that case due to
3929 * peer_pmslen being less than 48, and we only care whether diff is 0.
3930 * But do initialize peer_pms for robustness anyway. This also makes
3931 * memory analyzers happy (don't access uninitialized memory, even
3932 * if it's an unsigned char). */
3933 peer_pms[0] = peer_pms[1] = ~0;
3934
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003935 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3936 peer_pms,
3937 &peer_pmslen,
3938 sizeof( peer_pms ) );
3939
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003940#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003941 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3942 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003943#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003946 ssl->handshake->max_minor_ver,
3947 ssl->conf->transport, ver );
3948
3949 /* Avoid data-dependent branches while checking for invalid
3950 * padding, to protect against timing-based Bleichenbacher-type
3951 * attacks. */
3952 diff = (unsigned int) ret;
3953 diff |= peer_pmslen ^ 48;
3954 diff |= peer_pms[0] ^ ver[0];
3955 diff |= peer_pms[1] ^ ver[1];
3956
3957 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3958 /* MSVC has a warning about unary minus on unsigned, but this is
3959 * well-defined and precisely what we want to do here */
3960#if defined(_MSC_VER)
3961#pragma warning( push )
3962#pragma warning( disable : 4146 )
3963#endif
3964 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3965#if defined(_MSC_VER)
3966#pragma warning( pop )
3967#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003968
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003969 /*
3970 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3971 * must not cause the connection to end immediately; instead, send a
3972 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003973 * To protect against timing-based variants of the attack, we must
3974 * not have any branch that depends on whether the decryption was
3975 * successful. In particular, always generate the fake premaster secret,
3976 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003977 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003978 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003979 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003980 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003981 /* It's ok to abort on an RNG failure, since this does not reveal
3982 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003983 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003984 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003985
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003986#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003987 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003989#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003990
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003991 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3992 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003996 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003997 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003998
Gilles Peskine422ccab2018-01-11 18:29:01 +01003999 /* Set pms to either the true or the fake PMS, without
4000 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004001 for( i = 0; i < ssl->handshake->pmslen; i++ )
4002 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
4003
4004 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4007 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02004008
Gilles Peskineeccd8882020-03-10 12:19:08 +01004009#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004011 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004012{
Paul Bakker6db455e2013-09-18 17:29:31 +02004013 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03004014 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004015
Hanno Becker845b9462018-10-26 12:07:29 +01004016 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4019 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004020 }
4021
4022 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004023 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02004024 */
Hanno Becker83c9f492017-06-26 13:52:14 +01004025 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4028 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004029 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004030
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004031 n = ( (*p)[0] << 8 ) | (*p)[1];
4032 *p += 2;
4033
irwir6527bd62019-09-21 18:51:25 +03004034 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4037 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004038 }
4039
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004040 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004041 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004042 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004044 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004045 else
Paul Bakker6db455e2013-09-18 17:29:31 +02004046 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01004047 /* Identity is not a big secret since clients send it in the clear,
4048 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004049 if( n != ssl->conf->psk_identity_len ||
4050 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02004051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004053 }
4054 }
4055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02004059 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4060 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004062 }
4063
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004064 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004065
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004066 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004067}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004068#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004071{
Janos Follath865b3eb2019-12-16 11:46:15 +00004072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004074 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004075
Hanno Beckere694c3e2017-12-27 21:34:08 +00004076 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004079
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004080#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004081 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4082 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4083 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4084 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004085 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004086 {
4087 /* We've already read a record and there is an asynchronous
4088 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004089 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4091 }
4092 else
4093#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004094 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 return( ret );
4098 }
4099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004101 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4106 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004107 }
4108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4112 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004113 }
4114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004115#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4116 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004117 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004118 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004121 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004123
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004124 if( p != end )
4125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4127 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004128 }
4129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004131 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004132 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004133 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004134 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4137 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004138 }
4139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004141 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004142 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4144#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4145 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4146 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4147 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4148 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4149 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4150 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4151 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004154 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4157 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004158 }
4159
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004160 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4161 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004164 &ssl->handshake->pmslen,
4165 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004167 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4170 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004171 }
4172
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004173 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4174 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004175 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4178 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4179 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4180 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4181#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4182 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004183 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004184 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004187 return( ret );
4188 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004189
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004190 if( p != end )
4191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4193 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004194 }
4195
Hanno Becker845b9462018-10-26 12:07:29 +01004196#if defined(MBEDTLS_USE_PSA_CRYPTO)
4197 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4198 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004199 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4201 else
4202#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004204 ciphersuite_info->key_exchange ) ) != 0 )
4205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004207 return( ret );
4208 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004210 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4212#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4213 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004214 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004215#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004216 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004217 {
4218 /* There is an asynchronous operation in progress to
4219 * decrypt the encrypted premaster secret, so skip
4220 * directly to resuming this operation. */
4221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4222 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4223 * won't actually use it, but maintain p anyway for robustness. */
4224 p += ssl->conf->psk_identity_len + 2;
4225 }
4226 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004227#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004228 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004231 return( ret );
4232 }
4233
Hanno Becker845b9462018-10-26 12:07:29 +01004234#if defined(MBEDTLS_USE_PSA_CRYPTO)
4235 /* Opaque PSKs are currently only supported for PSK-only. */
4236 if( ssl_use_opaque_psk( ssl ) == 1 )
4237 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4238#endif
4239
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004240 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004243 return( ret );
4244 }
4245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004247 ciphersuite_info->key_exchange ) ) != 0 )
4248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004250 return( ret );
4251 }
4252 }
4253 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4255#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4256 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004257 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004258 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004261 return( ret );
4262 }
4263 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004266 return( ret );
4267 }
4268
Hanno Becker845b9462018-10-26 12:07:29 +01004269#if defined(MBEDTLS_USE_PSA_CRYPTO)
4270 /* Opaque PSKs are currently only supported for PSK-only. */
4271 if( ssl_use_opaque_psk( ssl ) == 1 )
4272 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4273#endif
4274
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004275 if( p != end )
4276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4278 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004279 }
4280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004282 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004285 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004286 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004287 }
4288 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4290#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4291 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004292 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004293 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004296 return( ret );
4297 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004300 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4303 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004304 }
4305
Hanno Becker845b9462018-10-26 12:07:29 +01004306#if defined(MBEDTLS_USE_PSA_CRYPTO)
4307 /* Opaque PSKs are currently only supported for PSK-only. */
4308 if( ssl_use_opaque_psk( ssl ) == 1 )
4309 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4310#endif
4311
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004312 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4313 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004316 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004319 return( ret );
4320 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004321 }
4322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4324#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4325 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004326 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004327 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004330 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 }
4332 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004335#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4336 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4337 {
4338 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4339 p, end - p );
4340 if( ret != 0 )
4341 {
4342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4343 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4344 }
4345
4346 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4347 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4348 ssl->conf->f_rng, ssl->conf->p_rng );
4349 if( ret != 0 )
4350 {
4351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4352 return( ret );
4353 }
4354 }
4355 else
4356#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004365 return( ret );
4366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004367
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 ssl->state++;
4369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004371
4372 return( 0 );
4373}
4374
Gilles Peskineeccd8882020-03-10 12:19:08 +01004375#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004377{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004379 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004382
Hanno Becker77adddc2019-02-07 12:32:43 +00004383 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004386 ssl->state++;
4387 return( 0 );
4388 }
4389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004392}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004393#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004397 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004398 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004399 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004400 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4402 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004405 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004406 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004407 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004410
Hanno Becker2a831a42019-02-07 13:17:25 +00004411 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004414 ssl->state++;
4415 return( 0 );
4416 }
4417
Hanno Becker2a831a42019-02-07 13:17:25 +00004418#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4419 if( ssl->session_negotiate->peer_cert == NULL )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4422 ssl->state++;
4423 return( 0 );
4424 }
4425#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4426 if( ssl->session_negotiate->peer_cert_digest == NULL )
4427 {
4428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4429 ssl->state++;
4430 return( 0 );
4431 }
4432#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4433
Simon Butcher99000142016-10-13 17:21:01 +01004434 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004435 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004436 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004437 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004438 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004439 return( ret );
4440 }
4441
4442 ssl->state++;
4443
Simon Butcher99000142016-10-13 17:21:01 +01004444 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4446 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4449 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004450 }
4451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004453
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004454#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4455 peer_pk = &ssl->handshake->peer_pubkey;
4456#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4457 if( ssl->session_negotiate->peer_cert == NULL )
4458 {
4459 /* Should never happen */
4460 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4461 }
4462 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4463#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4464
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004465 /*
4466 * struct {
4467 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4468 * opaque signature<0..2^16-1>;
4469 * } DigitallySigned;
4470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4472 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4473 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004476 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004477
4478 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004479 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004480 {
4481 hash_start += 16;
4482 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004484 }
Paul Bakker926af752012-11-23 13:38:07 +01004485 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004486 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4488 MBEDTLS_SSL_PROTO_TLS1_1 */
4489#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4490 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004491 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004492 if( i + 2 > ssl->in_hslen )
4493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4495 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004496 }
4497
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004499 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 */
Simon Butcher99000142016-10-13 17:21:01 +01004501 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4502
4503 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004506 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004508 }
4509
Simon Butcher99000142016-10-13 17:21:01 +01004510#if !defined(MBEDTLS_MD_SHA1)
4511 if( MBEDTLS_MD_SHA1 == md_alg )
4512 hash_start += 16;
4513#endif
Paul Bakker926af752012-11-23 13:38:07 +01004514
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004515 /* Info from md_alg will be used instead */
4516 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004517
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004518 i++;
4519
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004520 /*
4521 * Signature
4522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4524 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004527 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004529 }
4530
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004531 /*
4532 * Check the certificate's key type matches the signature alg
4533 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004534 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4537 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004538 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004539
4540 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004541 }
4542 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004547 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004548
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004549 if( i + 2 > ssl->in_hslen )
4550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4552 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004553 }
4554
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004555 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4556 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004557
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004558 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4561 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004562 }
4563
Simon Butcher99000142016-10-13 17:21:01 +01004564 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004565 {
4566 size_t dummy_hlen;
4567 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4568 }
Simon Butcher99000142016-10-13 17:21:01 +01004569
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004570 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004571 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004572 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 return( ret );
4576 }
4577
Simon Butcher99000142016-10-13 17:21:01 +01004578 mbedtls_ssl_update_handshake_status( ssl );
4579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Paul Bakkered27a042013-04-18 22:46:23 +02004582 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004583}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004584#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4587static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004588{
Janos Follath865b3eb2019-12-16 11:46:15 +00004589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004590 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004591 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4596 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004597
4598 /*
4599 * struct {
4600 * uint32 ticket_lifetime_hint;
4601 * opaque ticket<0..2^16-1>;
4602 * } NewSessionTicket;
4603 *
4604 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4605 * 8 . 9 ticket_len (n)
4606 * 10 . 9+n ticket content
4607 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004608
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004609 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004610 ssl->session_negotiate,
4611 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004612 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004613 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004614 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004616 tlen = 0;
4617 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004618
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004619 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4620 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4621 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4622 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4623
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004624 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4625 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004626
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004627 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004628
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004629 /*
4630 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4631 * ChangeCipherSpec share the same state.
4632 */
4633 ssl->handshake->new_session_ticket = 0;
4634
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004635 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004636 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004638 return( ret );
4639 }
4640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004642
4643 return( 0 );
4644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004646
Paul Bakker5121ce52009-01-03 21:22:43 +00004647/*
Paul Bakker1961b702013-01-25 14:49:24 +01004648 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004651{
4652 int ret = 0;
4653
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004654 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004660 return( ret );
4661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004665 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004666 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004667 return( ret );
4668 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004669#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004670
Paul Bakker1961b702013-01-25 14:49:24 +01004671 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004673 case MBEDTLS_SSL_HELLO_REQUEST:
4674 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 break;
4676
Paul Bakker1961b702013-01-25 14:49:24 +01004677 /*
4678 * <== ClientHello
4679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004681 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684#if defined(MBEDTLS_SSL_PROTO_DTLS)
4685 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4686 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004687#endif
4688
Paul Bakker1961b702013-01-25 14:49:24 +01004689 /*
4690 * ==> ServerHello
4691 * Certificate
4692 * ( ServerKeyExchange )
4693 * ( CertificateRequest )
4694 * ServerHelloDone
4695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004697 ret = ssl_write_server_hello( ssl );
4698 break;
4699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4701 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004702 break;
4703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004705 ret = ssl_write_server_key_exchange( ssl );
4706 break;
4707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004709 ret = ssl_write_certificate_request( ssl );
4710 break;
4711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004713 ret = ssl_write_server_hello_done( ssl );
4714 break;
4715
4716 /*
4717 * <== ( Certificate/Alert )
4718 * ClientKeyExchange
4719 * ( CertificateVerify )
4720 * ChangeCipherSpec
4721 * Finished
4722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4724 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004725 break;
4726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004728 ret = ssl_parse_client_key_exchange( ssl );
4729 break;
4730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004732 ret = ssl_parse_certificate_verify( ssl );
4733 break;
4734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4736 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004737 break;
4738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 case MBEDTLS_SSL_CLIENT_FINISHED:
4740 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004741 break;
4742
4743 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004744 * ==> ( NewSessionTicket )
4745 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004746 * Finished
4747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4749#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004750 if( ssl->handshake->new_session_ticket != 0 )
4751 ret = ssl_write_new_session_ticket( ssl );
4752 else
Paul Bakkera503a632013-08-14 13:48:06 +02004753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004755 break;
4756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 case MBEDTLS_SSL_SERVER_FINISHED:
4758 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004759 break;
4760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 case MBEDTLS_SSL_FLUSH_BUFFERS:
4762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4763 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004764 break;
4765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004766 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4767 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004768 break;
4769
4770 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004773 }
4774
Paul Bakker5121ce52009-01-03 21:22:43 +00004775 return( ret );
4776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777#endif /* MBEDTLS_SSL_SRV_C */