blob: 8f13a2cec026b20d6579a2dcda2411956996f731 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS server-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
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"
Chris Jones84a773f2021-03-05 18:38:47 +000033#include "ssl_misc.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)
Ronald Croncf56a0a2020-08-04 09:51:30 +0200160 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100161 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
Ronald Croncf56a0a2020-08-04 09:51:30 +0200175 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100176 return( 1 );
177
178 return( 0 );
179 }
180
Ronald Croncf56a0a2020-08-04 09:51:30 +0200181 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100182 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:"
Paul Elliott9f352112020-12-09 14:55:45 +0000301 " match sig %u and hash %u",
Paul Elliott3891caf2020-12-17 18:42:40 +0000302 (unsigned) sig_cur, (unsigned) md_cur ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100303 }
304 else
305 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
Paul Elliott3891caf2020-12-17 18:42:40 +0000307 "hash alg %u not supported", (unsigned) 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
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100582 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100585 }
586
587 return( 0 );
588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
592static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200593 const unsigned char *buf,
594 size_t len )
595{
596 if( len != 0 )
597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200599 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
600 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200602 }
603
604 ((void) buf);
605
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100606 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200609 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200610
611 return( 0 );
612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#if defined(MBEDTLS_SSL_SESSION_TICKETS)
616static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200617 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200618 size_t len )
619{
Janos Follath865b3eb2019-12-16 11:46:15 +0000620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200621 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200622
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200623 mbedtls_ssl_session_init( &session );
624
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200625 if( ssl->conf->f_ticket_parse == NULL ||
626 ssl->conf->f_ticket_write == NULL )
627 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200628 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200629 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200630
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200631 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200632 ssl->handshake->new_session_ticket = 1;
633
Paul Elliottd48d5c62021-01-07 14:47:05 +0000634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200635
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200636 if( len == 0 )
637 return( 0 );
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_SSL_RENEGOTIATION)
640 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200643 return( 0 );
644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200646
647 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200648 * Failures are ok: just ignore the ticket and proceed.
649 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200650 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
651 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200652 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200653 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200654
655 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
657 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
659 else
660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
661
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200662 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200663 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200665 /*
666 * Keep the session ID sent by the client, since we MUST send it back to
667 * inform them we're accepting the ticket (RFC 5077 section 3.4)
668 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200669 session.id_len = ssl->session_negotiate->id_len;
670 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200671
672 mbedtls_ssl_session_free( ssl->session_negotiate );
673 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
674
675 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500676 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200679
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200680 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200681
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200682 /* Don't send a new ticket after all, this one is OK */
683 ssl->handshake->new_session_ticket = 0;
684
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200685 return( 0 );
686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_ALPN)
690static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200691 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200692{
Paul Bakker14b16c62014-05-28 11:33:54 +0200693 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200694 const unsigned char *theirs, *start, *end;
695 const char **ours;
696
697 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200698 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200699 return( 0 );
700
701 /*
702 * opaque ProtocolName<1..2^8-1>;
703 *
704 * struct {
705 * ProtocolName protocol_name_list<2..2^16-1>
706 * } ProtocolNameList;
707 */
708
709 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
710 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200711 {
712 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
713 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200715 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200716
717 list_len = ( buf[0] << 8 ) | buf[1];
718 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200719 {
720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
721 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200723 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200724
725 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100726 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200727 */
728 start = buf + 2;
729 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100730 for( theirs = start; theirs != end; theirs += cur_len )
731 {
732 cur_len = *theirs++;
733
734 /* Current identifier must fit in list */
735 if( cur_len > (size_t)( end - theirs ) )
736 {
737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
738 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
739 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
740 }
741
742 /* Empty strings MUST NOT be included */
743 if( cur_len == 0 )
744 {
745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
746 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
747 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
748 }
749 }
750
751 /*
752 * Use our order of preference
753 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200754 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200755 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200756 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200757 for( theirs = start; theirs != end; theirs += cur_len )
758 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200759 cur_len = *theirs++;
760
Paul Bakker14b16c62014-05-28 11:33:54 +0200761 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200762 memcmp( theirs, *ours, cur_len ) == 0 )
763 {
764 ssl->alpn_chosen = *ours;
765 return( 0 );
766 }
767 }
768 }
769
770 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
772 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
773 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200776
Johan Pascalb62bb512015-12-03 21:56:45 +0100777#if defined(MBEDTLS_SSL_DTLS_SRTP)
778static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300779 const unsigned char *buf,
780 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100781{
Johan Pascal43f94902020-09-22 12:25:52 +0200782 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100783 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200784 size_t profile_length;
785 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200786 /*! 2 bytes for profile length and 1 byte for mki len */
787 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100788
789 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100790 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
791 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
792 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200793 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100794 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200795 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100796
797 /* RFC5764 section 4.1.1
798 * uint8 SRTPProtectionProfile[2];
799 *
800 * struct {
801 * SRTPProtectionProfiles SRTPProtectionProfiles;
802 * opaque srtp_mki<0..255>;
803 * } UseSRTPData;
804
805 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100806 */
807
Ron Eldoref72faf2018-07-12 11:54:20 +0300808 /*
809 * Min length is 5: at least one protection profile(2 bytes)
810 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200811 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200812 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300813 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200814 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200815 {
816 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
817 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100818 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200819 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100820
Johan Pascal43f94902020-09-22 12:25:52 +0200821 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200822
Ron Eldoref72faf2018-07-12 11:54:20 +0300823 /* first 2 bytes are protection profile length(in bytes) */
824 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200825 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200826
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200827 /* The profile length cannot be bigger than input buffer size - lengths fields */
828 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200829 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200830 {
831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
832 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
833 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
834 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300835 /*
836 * parse the extension list values are defined in
837 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
838 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200839 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100840 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200841 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200842 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100843
Johan Pascal43f94902020-09-22 12:25:52 +0200844 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300845 {
Johan Pascal43f94902020-09-22 12:25:52 +0200846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
847 mbedtls_ssl_get_srtp_profile_as_string(
848 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300849 }
Johan Pascal85269572020-08-25 10:01:54 +0200850 else
851 {
852 continue;
853 }
Ron Eldor591f1622018-01-22 12:30:04 +0200854 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200855 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200856 {
857 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
858 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200859 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
861 mbedtls_ssl_get_srtp_profile_as_string(
862 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200863 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100864 }
865 }
Johan Pascal43f94902020-09-22 12:25:52 +0200866 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200867 break;
868 }
Johan Pascal042d4562020-08-25 12:14:02 +0200869 buf += profile_length; /* buf points to the mki length */
870 mki_length = *buf;
871 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200872
Johan Pascal042d4562020-08-25 12:14:02 +0200873 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
874 mki_length + profile_length + size_of_lengths != len )
875 {
876 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
877 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
878 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
879 }
880
881 /* Parse the mki only if present and mki is supported locally */
882 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
883 mki_length > 0 )
884 {
885 ssl->dtls_srtp_info.mki_len = mki_length;
886
Johan Pascal5ef72d22020-10-28 17:05:47 +0100887 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300888
Ron Eldoref72faf2018-07-12 11:54:20 +0300889 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
890 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100891 }
892
Johan Pascal042d4562020-08-25 12:14:02 +0200893 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100894}
895#endif /* MBEDTLS_SSL_DTLS_SRTP */
896
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100897/*
898 * Auxiliary functions for ServerHello parsing and related actions
899 */
900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100902/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100903 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#if defined(MBEDTLS_ECDSA_C)
906static int ssl_check_key_curve( mbedtls_pk_context *pk,
907 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 const mbedtls_ecp_curve_info **crv = curves;
910 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100911
912 while( *crv != NULL )
913 {
914 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100915 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100916 crv++;
917 }
918
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100919 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922
923/*
924 * Try picking a certificate for this ciphersuite,
925 * return 0 on success and -1 on failure.
926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927static int ssl_pick_cert( mbedtls_ssl_context *ssl,
928 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100931 mbedtls_pk_type_t pk_alg =
932 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200933 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100936 if( ssl->handshake->sni_key_cert != NULL )
937 list = ssl->handshake->sni_key_cert;
938 else
939#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200940 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100943 return( 0 );
944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000946
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200947 if( list == NULL )
948 {
949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
950 return( -1 );
951 }
952
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100953 for( cur = list; cur != NULL; cur = cur->next )
954 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400955 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000957 cur->cert );
958
Gilles Peskinee198df52018-01-05 21:17:45 +0100959 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100962 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000963 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100964
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200965 /*
966 * This avoids sending the client a cert it'll reject based on
967 * keyUsage or other extensions.
968 *
969 * It also allows the user to provision different certificates for
970 * different uses based on keyUsage, eg if they want to avoid signing
971 * and decrypting with the same RSA key.
972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100974 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000977 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200978 continue;
979 }
980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_ECDSA_C)
982 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200983 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100986 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000987 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100988#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100989
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100990 /*
991 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
992 * present them a SHA-higher cert rather than failing if it's the only
993 * one we got that satisfies the other conditions.
994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
996 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100997 {
998 if( fallback == NULL )
999 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001002 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001003 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001004 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001005 }
1006
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001007 /* If we get there, we got a winner */
1008 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001009 }
1010
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001011 if( cur == NULL )
1012 cur = fallback;
1013
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001014 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001015 if( cur != NULL )
1016 {
1017 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001019 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001020 return( 0 );
1021 }
1022
1023 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001024}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001026
1027/*
1028 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1029 * Sets ciphersuite_info only if the suite matches.
1030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1032 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001035
Hanno Becker7e5437a2017-04-28 17:15:26 +01001036#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001037 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001038 mbedtls_pk_type_t sig_type;
1039#endif
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001042 if( suite_info == NULL )
1043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001046 }
1047
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +00001049 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001050
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001051 if( suite_info->min_minor_ver > ssl->minor_ver ||
1052 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001055 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001056 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001061 return( 0 );
1062#endif
1063
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001064#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1065 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001066 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001067 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1069 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001070 return( 0 );
1071 }
1072#endif
1073
1074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1076 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001077 ( ssl->handshake->curves == NULL ||
1078 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001081 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001082 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001083 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001084#endif
1085
Gilles Peskineeccd8882020-03-10 12:19:08 +01001086#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001087 /* If the ciphersuite requires a pre-shared key and we don't
1088 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001090 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001093 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001094 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001095#endif
1096
Hanno Becker7e5437a2017-04-28 17:15:26 +01001097#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001098 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001099 /* If the ciphersuite requires signing, check whether
1100 * a suitable hash algorithm is present. */
1101 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1102 {
1103 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1104 if( sig_type != MBEDTLS_PK_NONE &&
1105 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1106 {
1107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001108 "for signature algorithm %u", (unsigned) sig_type ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001109 return( 0 );
1110 }
1111 }
1112
1113#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001114 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001117 /*
1118 * Final check: if ciphersuite requires us to have a
1119 * certificate/key of a particular type:
1120 * - select the appropriate certificate if we have one, or
1121 * - try the next ciphersuite if we don't
1122 * This must be done last since we modify the key_cert list.
1123 */
1124 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001127 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001128 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001129 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001130#endif
1131
1132 *ciphersuite_info = suite_info;
1133 return( 0 );
1134}
1135
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001136/* This function doesn't alert on errors that happen early during
1137 ClientHello parsing because they might indicate that the client is
1138 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001140{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001141 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001142 size_t i, j;
1143 size_t ciph_offset, comp_offset, ext_offset;
1144 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001146 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001147#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001148 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001150 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001151#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001152 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001153 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001155 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Hanno Becker7e5437a2017-04-28 17:15:26 +01001157 /* If there is no signature-algorithm extension present,
1158 * we need to fall back to the default values for allowed
1159 * signature-hash pairs. */
1160#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001161 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001162 int sig_hash_alg_ext_present = 0;
1163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001164 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001169read_record_header:
1170#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001171 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001173 * otherwise read it ourselves manually in order to support SSLv2
1174 * ClientHello, which doesn't use the same record layer format.
1175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_RENEGOTIATION)
1177 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001178#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001181 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001182 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001184 return( ret );
1185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
1187
1188 buf = ssl->in_hdr;
1189
Hanno Becker5903de42019-05-03 14:46:38 +01001190 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001191
Paul Bakkerec636f32012-09-09 19:17:02 +00001192 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001193 * TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001194 *
1195 * Record layer:
1196 * 0 . 0 message type
1197 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001198 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001199 * 3 . 4 message length
1200 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001202 buf[0] ) );
1203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1207 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001208 }
1209
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001211 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1212
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001214 buf[1], buf[2] ) );
1215
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001216 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001217
1218 /* According to RFC 5246 Appendix E.1, the version here is typically
1219 * "{03,00}, the lowest version number supported by the client, [or] the
1220 * value of ClientHello.client_version", so the only meaningful check here
1221 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1225 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001228 /* For DTLS if this is the initial handshake, remember the client sequence
1229 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_RENEGOTIATION)
1233 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001234#endif
1235 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001236 {
1237 /* Epoch should be 0 for initial handshakes */
1238 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1241 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001242 }
1243
Hanno Becker19859472018-08-06 09:40:20 +01001244 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1247 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001250 ssl->next_record_offset = 0;
1251 ssl->in_left = 0;
1252 goto read_record_header;
1253 }
1254
1255 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001257#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001260
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001261 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_SSL_RENEGOTIATION)
1264 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001267 msg_len = ssl->in_hslen;
1268 }
1269 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001270#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001271 {
Angus Grattond8213d02016-05-25 20:56:48 +10001272 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1275 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001276 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001277
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001278 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001279 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001282 return( ret );
1283 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001284
1285 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001287 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001288 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001289 else
1290#endif
1291 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001292 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001293
1294 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001297
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001298 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001299
1300 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001301 * Handshake layer:
1302 * 0 . 0 handshake type
1303 * 1 . 3 handshake length
1304 * 4 . 5 DTLS only: message seqence number
1305 * 6 . 8 DTLS only: fragment offset
1306 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1311 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001312 }
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1319 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001320 }
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001323 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1324
1325 /* We don't support fragmentation of ClientHello (yet?) */
1326 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1330 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001331 }
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001335 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001336 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001337 * Copy the client's handshake message_seq on initial handshakes,
1338 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_SSL_RENEGOTIATION)
1341 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001342 {
1343 /* This couldn't be done in ssl_prepare_handshake_record() */
1344 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1345 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001346
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001347 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001350 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001351 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001353 }
1354
1355 ssl->handshake->in_msg_seq++;
1356 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001357 else
1358#endif
1359 {
1360 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1361 ssl->in_msg[5];
1362 ssl->handshake->out_msg_seq = cli_msg_seq;
1363 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1364 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001365
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001366 /*
1367 * For now we don't support fragmentation, so make sure
1368 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001369 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001370 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1371 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1374 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001375 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 buf += mbedtls_ssl_hs_hdr_len( ssl );
1380 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001381
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001382 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001383 * ClientHello layer:
1384 * 0 . 1 protocol version
1385 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1386 * 34 . 35 session id length (1 byte)
1387 * 35 . 34+x session id
1388 * 35+x . 35+x DTLS only: cookie length (1 byte)
1389 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001390 * .. . .. ciphersuite list length (2 bytes)
1391 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001392 * .. . .. compression alg. list length (1 byte)
1393 * .. . .. compression alg. list
1394 * .. . .. extensions length (2 bytes, optional)
1395 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001396 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001397
1398 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001399 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001400 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1401 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001402 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001403 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1406 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001407 }
1408
1409 /*
1410 * Check and save the protocol version
1411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001415 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001416
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001417 ssl->handshake->max_major_ver = ssl->major_ver;
1418 ssl->handshake->max_minor_ver = ssl->minor_ver;
1419
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001420 if( ssl->major_ver < ssl->conf->min_major_ver ||
1421 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001424 " [%d:%d] < [%d:%d]",
1425 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001426 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1428 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001430 }
1431
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001432 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001433 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001434 ssl->major_ver = ssl->conf->max_major_ver;
1435 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001436 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001437 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1438 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001439
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001440 /*
1441 * Save client random (inc. Unix time)
1442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001444
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001445 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001446
1447 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001448 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001449 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001450 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001451
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001452 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001453 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001456 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1457 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001459 }
1460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001462
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001463 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001464 memset( ssl->session_negotiate->id, 0,
1465 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001466 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001467 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001468
1469 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001470 * Check the cookie length and content
1471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001473 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001474 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001475 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001476 cookie_len = buf[cookie_offset];
1477
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001478 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001481 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1482 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001484 }
1485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001487 buf + cookie_offset + 1, cookie_len );
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001490 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_SSL_RENEGOTIATION)
1492 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001493#endif
1494 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001495 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001496 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001497 buf + cookie_offset + 1, cookie_len,
1498 ssl->cli_id, ssl->cli_id_len ) != 0 )
1499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001501 ssl->handshake->verify_cookie_len = 1;
1502 }
1503 else
1504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001506 ssl->handshake->verify_cookie_len = 0;
1507 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001508 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001511 {
1512 /* We know we didn't send a cookie, so it should be empty */
1513 if( cookie_len != 0 )
1514 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001515 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1517 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001518 }
1519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001521 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001522
1523 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001524 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001525 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001526 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001527 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001528 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001530 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001531
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001532 ciph_len = ( buf[ciph_offset + 0] << 8 )
1533 | ( buf[ciph_offset + 1] );
1534
1535 if( ciph_len < 2 ||
1536 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1537 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001540 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1541 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001543 }
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001546 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001547
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001548 /*
1549 * Check the compression algorithms length and pick one
1550 */
1551 comp_offset = ciph_offset + 2 + ciph_len;
1552
1553 comp_len = buf[comp_offset];
1554
1555 if( comp_len < 1 ||
1556 comp_len > 16 ||
1557 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001560 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1561 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001563 }
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001566 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001569 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001571 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001573#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001574 /*
1575 * Check the extension length
1576 */
1577 ext_offset = comp_offset + 1 + comp_len;
1578 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001579 {
Simon Butcher584a5472016-05-23 16:24:52 +01001580 if( msg_len < ext_offset + 2 )
1581 {
1582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001583 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1584 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001585 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1586 }
1587
1588 ext_len = ( buf[ext_offset + 0] << 8 )
1589 | ( buf[ext_offset + 1] );
1590
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001591 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001592 {
1593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001594 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1595 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001596 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1597 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001598 }
Simon Butcher584a5472016-05-23 16:24:52 +01001599 else
1600 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001601
Simon Butcher584a5472016-05-23 16:24:52 +01001602 ext = buf + ext_offset + 2;
1603 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001604
Simon Butcher584a5472016-05-23 16:24:52 +01001605 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001606 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001607 unsigned int ext_id;
1608 unsigned int ext_size;
1609 if ( ext_len < 4 ) {
1610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1611 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1612 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1613 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1614 }
1615 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1616 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001617
Simon Butcher584a5472016-05-23 16:24:52 +01001618 if( ext_size + 4 > ext_len )
1619 {
1620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001621 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1622 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001623 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1624 }
1625 switch( ext_id )
1626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001628 case MBEDTLS_TLS_EXT_SERVERNAME:
1629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1630 if( ssl->conf->f_sni == NULL )
1631 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001632
Simon Butcher584a5472016-05-23 16:24:52 +01001633 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1634 if( ret != 0 )
1635 return( ret );
1636 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001638
Simon Butcher584a5472016-05-23 16:24:52 +01001639 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001642 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001643#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001644
Simon Butcher584a5472016-05-23 16:24:52 +01001645 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1646 if( ret != 0 )
1647 return( ret );
1648 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001651 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001652 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1654
Simon Butcher584a5472016-05-23 16:24:52 +01001655 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1656 if( ret != 0 )
1657 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001658
1659 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001660 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001662 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001663
Robert Cragie136884c2015-10-02 13:34:31 +01001664#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001665 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001666 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001668
Simon Butcher584a5472016-05-23 16:24:52 +01001669 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1670 if( ret != 0 )
1671 return( ret );
1672 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001673
Simon Butcher584a5472016-05-23 16:24:52 +01001674 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1676 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001677
Simon Butcher584a5472016-05-23 16:24:52 +01001678 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1679 if( ret != 0 )
1680 return( ret );
1681 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001682#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1683 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001684
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001685#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001686 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001688
Simon Butcher584a5472016-05-23 16:24:52 +01001689 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1690 if( ret != 0 )
1691 return( ret );
1692 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001693#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001696 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001698
Simon Butcher584a5472016-05-23 16:24:52 +01001699 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1700 if( ret != 0 )
1701 return( ret );
1702 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001706 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001708
Simon Butcher584a5472016-05-23 16:24:52 +01001709 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1710 if( ret != 0 )
1711 return( ret );
1712 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001714
Hanno Beckera0e20d02019-05-15 14:03:01 +01001715#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001716 case MBEDTLS_TLS_EXT_CID:
1717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1718
1719 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1720 if( ret != 0 )
1721 return( ret );
1722 break;
1723#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001726 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001728
Simon Butcher584a5472016-05-23 16:24:52 +01001729 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1730 if( ret != 0 )
1731 return( ret );
1732 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001736 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001738
Simon Butcher584a5472016-05-23 16:24:52 +01001739 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1740 if( ret != 0 )
1741 return( ret );
1742 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001746 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001748
Simon Butcher584a5472016-05-23 16:24:52 +01001749 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1750 if( ret != 0 )
1751 return( ret );
1752 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001756 case MBEDTLS_TLS_EXT_ALPN:
1757 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001758
Simon Butcher584a5472016-05-23 16:24:52 +01001759 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1760 if( ret != 0 )
1761 return( ret );
1762 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001764
Johan Pascalb62bb512015-12-03 21:56:45 +01001765#if defined(MBEDTLS_SSL_DTLS_SRTP)
1766 case MBEDTLS_TLS_EXT_USE_SRTP:
1767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02001768
Johan Pascalc3ccd982020-10-28 17:18:18 +01001769 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
1770 if( ret != 0 )
1771 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001772 break;
1773#endif /* MBEDTLS_SSL_DTLS_SRTP */
1774
Simon Butcher584a5472016-05-23 16:24:52 +01001775 default:
Paul Elliott9f352112020-12-09 14:55:45 +00001776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01001777 ext_id ) );
1778 }
1779
1780 ext_len -= 4 + ext_size;
1781 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00001782 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01001783
Hanno Becker7e5437a2017-04-28 17:15:26 +01001784#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001785 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001786
1787 /*
1788 * Try to fall back to default hash SHA1 if the client
1789 * hasn't provided any preferred signature-hash combinations.
1790 */
1791 if( sig_hash_alg_ext_present == 0 )
1792 {
1793 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
1794
1795 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
1796 md_default = MBEDTLS_MD_NONE;
1797
1798 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1799 }
1800
1801#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001802 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001803
Paul Bakker48916f92012-09-16 19:57:18 +00001804 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001805 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1806 */
1807 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1812#if defined(MBEDTLS_SSL_RENEGOTIATION)
1813 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001814 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1816 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001817 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1818 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001820 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001821#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001823 break;
1824 }
1825 }
1826
1827 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001828 * Renegotiation security checks
1829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001831 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001834 handshake_failure = 1;
1835 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#if defined(MBEDTLS_SSL_RENEGOTIATION)
1837 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1838 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001839 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001842 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001843 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1845 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001846 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001849 handshake_failure = 1;
1850 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1852 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001853 renegotiation_info_seen == 1 )
1854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001856 handshake_failure = 1;
1857 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001859
1860 if( handshake_failure == 1 )
1861 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02001862 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1863 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001865 }
Paul Bakker380da532012-04-18 16:10:25 +00001866
Paul Bakker41c83d32013-03-20 14:39:14 +01001867 /*
1868 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001869 * (At the end because we need information from the EC-based extensions
1870 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001871 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001872 got_common_suite = 0;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001873 ciphersuites = mbedtls_ssl_get_protocol_version_ciphersuites( ssl->conf, ssl->minor_ver );
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001874 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001876 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001877 for( i = 0; ciphersuites[i] != 0; i++ )
1878#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001879 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001880 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001881#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001882 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001883 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1884 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1885 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001886
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001887 got_common_suite = 1;
1888
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001889 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1890 &ciphersuite_info ) ) != 0 )
1891 return( ret );
1892
1893 if( ciphersuite_info != NULL )
1894 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001895 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001896
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001897 if( got_common_suite )
1898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001900 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001901 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1902 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001904 }
1905 else
1906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001908 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1909 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001911 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001912
1913have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001915
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001916 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001917 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001918
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 ssl->state++;
1920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001922 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001924#endif
1925
Hanno Becker7e5437a2017-04-28 17:15:26 +01001926 /* Debugging-only output for testsuite */
1927#if defined(MBEDTLS_DEBUG_C) && \
1928 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001929 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001930 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1931 {
1932 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
1933 if( sig_alg != MBEDTLS_PK_NONE )
1934 {
1935 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1936 sig_alg );
1937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1938 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
1939 }
1940 else
1941 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001943 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001944 }
1945 }
1946#endif
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
1950 return( 0 );
1951}
1952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1954static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001955 unsigned char *buf,
1956 size_t *olen )
1957{
1958 unsigned char *p = buf;
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001961 {
1962 *olen = 0;
1963 return;
1964 }
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1969 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001970
1971 *p++ = 0x00;
1972 *p++ = 0x00;
1973
1974 *olen = 4;
1975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001977
Hanno Beckera0e20d02019-05-15 14:03:01 +01001978#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01001979static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
1980 unsigned char *buf,
1981 size_t *olen )
1982{
1983 unsigned char *p = buf;
1984 size_t ext_len;
1985 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1986
1987 *olen = 0;
1988
1989 /* Skip writing the extension if we don't want to use it or if
1990 * the client hasn't offered it. */
1991 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
1992 return;
1993
1994 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
1995 * which is at most 255, so the increment cannot overflow. */
1996 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
1997 {
1998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1999 return;
2000 }
2001
2002 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2003
2004 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002005 * Quoting draft-ietf-tls-dtls-connection-id-05
2006 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002007 *
2008 * struct {
2009 * opaque cid<0..2^8-1>;
2010 * } ConnectionId;
2011 */
2012
2013 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2014 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2015 ext_len = (size_t) ssl->own_cid_len + 1;
2016 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2017 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2018
2019 *p++ = (uint8_t) ssl->own_cid_len;
2020 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2021
2022 *olen = ssl->own_cid_len + 5;
2023}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002024#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2027static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002028 unsigned char *buf,
2029 size_t *olen )
2030{
2031 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2033 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002034
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002035 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002036 {
2037 *olen = 0;
2038 return;
2039 }
2040
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002041 /*
2042 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2043 * from a client and then selects a stream or Authenticated Encryption
2044 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2045 * encrypt-then-MAC response extension back to the client."
2046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002048 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2050 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002051 {
2052 *olen = 0;
2053 return;
2054 }
2055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2059 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002060
2061 *p++ = 0x00;
2062 *p++ = 0x00;
2063
2064 *olen = 4;
2065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2069static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002070 unsigned char *buf,
2071 size_t *olen )
2072{
2073 unsigned char *p = buf;
2074
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002075 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002076 {
2077 *olen = 0;
2078 return;
2079 }
2080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002082 "extension" ) );
2083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2085 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002086
2087 *p++ = 0x00;
2088 *p++ = 0x00;
2089
2090 *olen = 4;
2091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2095static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002096 unsigned char *buf,
2097 size_t *olen )
2098{
2099 unsigned char *p = buf;
2100
2101 if( ssl->handshake->new_session_ticket == 0 )
2102 {
2103 *olen = 0;
2104 return;
2105 }
2106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2110 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002111
2112 *p++ = 0x00;
2113 *p++ = 0x00;
2114
2115 *olen = 4;
2116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002120 unsigned char *buf,
2121 size_t *olen )
2122{
2123 unsigned char *p = buf;
2124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002126 {
2127 *olen = 0;
2128 return;
2129 }
2130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2134 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136#if defined(MBEDTLS_SSL_RENEGOTIATION)
2137 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002138 {
2139 *p++ = 0x00;
2140 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2141 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002142
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002143 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2144 p += ssl->verify_data_len;
2145 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2146 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002147 }
2148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002150 {
2151 *p++ = 0x00;
2152 *p++ = 0x01;
2153 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002154 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002155
2156 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002157}
2158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2160static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002161 unsigned char *buf,
2162 size_t *olen )
2163{
2164 unsigned char *p = buf;
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002167 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002168 *olen = 0;
2169 return;
2170 }
2171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2175 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002176
2177 *p++ = 0x00;
2178 *p++ = 1;
2179
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002180 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002181
2182 *olen = 5;
2183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002185
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002186#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002187 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002189 unsigned char *buf,
2190 size_t *olen )
2191{
2192 unsigned char *p = buf;
2193 ((void) ssl);
2194
Paul Bakker677377f2013-10-28 12:54:26 +01002195 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002197 {
2198 *olen = 0;
2199 return;
2200 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2205 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002206
2207 *p++ = 0x00;
2208 *p++ = 2;
2209
2210 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002212
2213 *olen = 6;
2214}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002215#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002216
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002217#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2218static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2219 unsigned char *buf,
2220 size_t *olen )
2221{
Janos Follath865b3eb2019-12-16 11:46:15 +00002222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002223 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002224 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002225 size_t kkpp_len;
2226
2227 *olen = 0;
2228
2229 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002230 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002231 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2232 return;
2233
2234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2235
2236 if( end - p < 4 )
2237 {
2238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2239 return;
2240 }
2241
2242 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2243 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2244
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002245 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2246 p + 2, end - p - 2, &kkpp_len,
2247 ssl->conf->f_rng, ssl->conf->p_rng );
2248 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002249 {
2250 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2251 return;
2252 }
2253
2254 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2255 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2256
2257 *olen = kkpp_len + 4;
2258}
2259#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_SSL_ALPN )
2262static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002263 unsigned char *buf, size_t *olen )
2264{
2265 if( ssl->alpn_chosen == NULL )
2266 {
2267 *olen = 0;
2268 return;
2269 }
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002272
2273 /*
2274 * 0 . 1 ext identifier
2275 * 2 . 3 ext length
2276 * 4 . 5 protocol list length
2277 * 6 . 6 protocol name length
2278 * 7 . 7+n protocol name
2279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2281 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002282
2283 *olen = 7 + strlen( ssl->alpn_chosen );
2284
2285 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2286 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2287
2288 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2289 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2290
2291 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2292
2293 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002296
Ron Eldor3adb9922017-12-21 10:15:08 +02002297#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002298static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002299 unsigned char *buf,
2300 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002301{
Ron Eldor75870ec2018-12-06 17:31:55 +02002302 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002303 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002304 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2305
2306 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002307
Johan Pascalc3ccd982020-10-28 17:18:18 +01002308 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2309 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002310 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002311 return;
2312 }
2313
2314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2315
Johan Pascald576fdb2020-09-22 10:39:53 +02002316 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002317 {
2318 mki_len = ssl->dtls_srtp_info.mki_len;
2319 }
2320
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002321 /* The extension total size is 9 bytes :
2322 * - 2 bytes for the extension tag
2323 * - 2 bytes for the total size
2324 * - 2 bytes for the protection profile length
2325 * - 2 bytes for the protection profile
2326 * - 1 byte for the mki length
2327 * + the actual mki length
2328 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002329 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002330 {
2331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2332 return;
2333 }
2334
Johan Pascalb62bb512015-12-03 21:56:45 +01002335 /* extension */
2336 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
2337 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
Ron Eldoref72faf2018-07-12 11:54:20 +03002338 /*
2339 * total length 5 and mki value: only one profile(2 bytes)
2340 * and length(2 bytes) and srtp_mki )
2341 */
Ron Eldor591f1622018-01-22 12:30:04 +02002342 ext_len = 5 + mki_len;
2343 buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2344 buf[3] = (unsigned char)( ext_len & 0xFF );
Johan Pascalb62bb512015-12-03 21:56:45 +01002345
2346 /* protection profile length: 2 */
2347 buf[4] = 0x00;
2348 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002349 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002350 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002351 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002352 {
2353 buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF );
2354 buf[7] = (unsigned char)( profile_value & 0xFF );
2355 }
2356 else
2357 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002359 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002360 }
2361
Ron Eldor591f1622018-01-22 12:30:04 +02002362 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002363 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002364
Ron Eldor591f1622018-01-22 12:30:04 +02002365 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002366}
2367#endif /* MBEDTLS_SSL_DTLS_SRTP */
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2370static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002371{
Janos Follath865b3eb2019-12-16 11:46:15 +00002372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002373 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002374 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002377
2378 /*
2379 * struct {
2380 * ProtocolVersion server_version;
2381 * opaque cookie<0..2^8-1>;
2382 * } HelloVerifyRequest;
2383 */
2384
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002385 /* The RFC is not clear on this point, but sending the actual negotiated
2386 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002388 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002390 p += 2;
2391
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002392 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002393 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002397 }
2398
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002399 /* Skip length byte until we know the length */
2400 cookie_len_byte = p++;
2401
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002402 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002403 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002404 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002407 return( ret );
2408 }
2409
2410 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002413
2414 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2416 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002419
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002420 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002421 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002423 return( ret );
2424 }
2425
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002426#if defined(MBEDTLS_SSL_PROTO_DTLS)
2427 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2428 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2429 {
2430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2431 return( ret );
2432 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002433#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002436
2437 return( 0 );
2438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002440
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002441static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
Hanno Becker64ce9742021-04-15 08:19:40 +01002442{
2443 int ret;
Hanno Beckera5b1a392021-04-15 16:48:01 +01002444 mbedtls_ssl_session session_tmp;
Hanno Becker64ce9742021-04-15 08:19:40 +01002445 mbedtls_ssl_session * const session = ssl->session_negotiate;
2446
2447 /* Resume is 0 by default, see ssl_handshake_init().
2448 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2449 if( ssl->handshake->resume == 1 )
2450 return;
Hanno Becker7ad77962021-05-13 06:13:34 +01002451 if( session->id_len == 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +01002452 return;
2453 if( ssl->conf->f_get_cache == NULL )
2454 return;
2455#if defined(MBEDTLS_SSL_RENEGOTIATION)
2456 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2457 return;
2458#endif
2459
Hanno Beckera5b1a392021-04-15 16:48:01 +01002460 mbedtls_ssl_session_init( &session_tmp );
2461
Hanno Becker64ce9742021-04-15 08:19:40 +01002462 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
Hanno Beckerccdaf6e2021-04-15 09:26:17 +01002463 session->id,
2464 session->id_len,
Hanno Becker64ce9742021-04-15 08:19:40 +01002465 &session_tmp );
2466 if( ret != 0 )
2467 goto exit;
2468
2469 if( session->ciphersuite != session_tmp.ciphersuite ||
2470 session->compression != session_tmp.compression )
2471 {
2472 /* Mismatch between cached and negotiated session */
2473 goto exit;
2474 }
2475
2476 /* Move semantics */
Hanno Becker64ce9742021-04-15 08:19:40 +01002477 mbedtls_ssl_session_free( session );
2478 *session = session_tmp;
Hanno Beckerfc1f4132021-05-14 14:57:54 +01002479 memset( &session_tmp, 0, sizeof( session_tmp ) );
Hanno Becker64ce9742021-04-15 08:19:40 +01002480
2481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2482 ssl->handshake->resume = 1;
2483
2484exit:
2485
Hanno Beckerdf564022021-05-13 06:26:37 +01002486 mbedtls_ssl_session_free( &session_tmp );
Hanno Becker64ce9742021-04-15 08:19:40 +01002487}
2488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002492 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002493#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002494 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002495 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 unsigned char *buf, *p;
2497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002502 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002506
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002507 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002510
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002511 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2514 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002515 }
2516
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 /*
2518 * 0 . 0 handshake type
2519 * 1 . 3 handshake length
2520 * 4 . 5 protocol version
2521 * 6 . 9 UNIX time()
2522 * 10 . 37 random bytes
2523 */
2524 buf = ssl->out_msg;
2525 p = buf + 4;
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002528 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002529 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002532 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002535 t = mbedtls_time( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 *p++ = (unsigned char)( t >> 24 );
2537 *p++ = (unsigned char)( t >> 16 );
2538 *p++ = (unsigned char)( t >> 8 );
2539 *p++ = (unsigned char)( t );
2540
Paul Elliottd48d5c62021-01-07 14:47:05 +00002541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2542 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002543#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002544 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002545 return( ret );
2546
2547 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002550 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002551 return( ret );
2552
2553 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Paul Bakker48916f92012-09-16 19:57:18 +00002555 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002559 ssl_handle_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002561 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 {
2563 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002564 * New session, create a new session id,
2565 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 ssl->state++;
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002570 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002571#endif
2572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002574 if( ssl->handshake->new_session_ticket != 0 )
2575 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002576 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002577 memset( ssl->session_negotiate->id, 0, 32 );
2578 }
2579 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002581 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002582 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002583 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002584 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002585 return( ret );
2586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
2588 else
2589 {
2590 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002591 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002593 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002599 return( ret );
2600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002603 /*
2604 * 38 . 38 session id length
2605 * 39 . 38+n session id
2606 * 39+n . 40+n chosen ciphersuite
2607 * 41+n . 41+n chosen compression alg.
2608 * 42+n . 43+n extensions length
2609 * 44+n . 43+n+m extensions
2610 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002611 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2612 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2613 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Paul Elliottd48d5c62021-01-07 14:47:05 +00002615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002618 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Paul Bakker48916f92012-09-16 19:57:18 +00002620 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2621 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2622 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2625 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002627 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002628
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002629 /*
2630 * First write extensions, then the total length
2631 */
2632 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2633 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002636 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2637 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002638#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002641 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2642 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002643#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002644
Hanno Beckera0e20d02019-05-15 14:03:01 +01002645#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002646 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2647 ext_len += olen;
2648#endif
2649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002651 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2652 ext_len += olen;
2653#endif
2654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002656 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2657 ext_len += olen;
2658#endif
2659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002661 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2662 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002663#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002664
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002665#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002666 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002667 if ( mbedtls_ssl_ciphersuite_uses_ec(
2668 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2669 {
2670 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2671 ext_len += olen;
2672 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002673#endif
2674
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002675#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2676 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2677 ext_len += olen;
2678#endif
2679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002681 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2682 ext_len += olen;
2683#endif
2684
Johan Pascalb62bb512015-12-03 21:56:45 +01002685#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01002686 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
2687 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002688#endif
2689
Paul Elliottd48d5c62021-01-07 14:47:05 +00002690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2691 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002692
Paul Bakkera7036632014-04-30 10:15:38 +02002693 if( ext_len > 0 )
2694 {
2695 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2696 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2697 p += ext_len;
2698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
2700 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2702 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002704 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 return( ret );
2709}
2710
Gilles Peskineeccd8882020-03-10 12:19:08 +01002711#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002714 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002715 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002718
Hanno Becker77adddc2019-02-07 12:32:43 +00002719 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002722 ssl->state++;
2723 return( 0 );
2724 }
2725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002728}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002729#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002733 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002734 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002735 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002736 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002738 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002740 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
2744 ssl->state++;
2745
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002746#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2747 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2748 authmode = ssl->handshake->sni_authmode;
2749 else
2750#endif
2751 authmode = ssl->conf->authmode;
2752
Hanno Becker77adddc2019-02-07 12:32:43 +00002753 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002754 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 {
Johan Pascal4f099262020-09-22 10:59:26 +02002756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2757 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 }
2759
2760 /*
2761 * 0 . 0 handshake type
2762 * 1 . 3 handshake length
2763 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002764 * 5 .. m-1 cert types
2765 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002766 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 * n .. n+1 length of all DNs
2768 * n+2 .. n+3 length of DN 1
2769 * n+4 .. ... Distinguished Name #1
2770 * ... .. ... length of DN 2, etc.
2771 */
2772 buf = ssl->out_msg;
2773 p = buf + 4;
2774
2775 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002776 * Supported certificate types
2777 *
2778 * ClientCertificateType certificate_types<1..2^8-1>;
2779 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002781 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783#if defined(MBEDTLS_RSA_C)
2784 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786#if defined(MBEDTLS_ECDSA_C)
2787 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002788#endif
2789
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002790 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002791 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002792
Paul Bakker577e0062013-08-28 11:57:20 +02002793 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002795 /*
2796 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002797 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002798 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2799 *
2800 * struct {
2801 * HashAlgorithm hash;
2802 * SignatureAlgorithm signature;
2803 * } SignatureAndHashAlgorithm;
2804 *
2805 * enum { (255) } HashAlgorithm;
2806 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002809 {
Simon Butcher99000142016-10-13 17:21:01 +01002810 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002811
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002812 /*
2813 * Supported signature algorithms
2814 */
Simon Butcher99000142016-10-13 17:21:01 +01002815 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
2816 {
2817 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
2818
2819 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
2820 continue;
2821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002823 p[2 + sa_len++] = hash;
2824 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002825#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002827 p[2 + sa_len++] = hash;
2828 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002829#endif
Simon Butcher99000142016-10-13 17:21:01 +01002830 }
Paul Bakker926af752012-11-23 13:38:07 +01002831
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002832 p[0] = (unsigned char)( sa_len >> 8 );
2833 p[1] = (unsigned char)( sa_len );
2834 sa_len += 2;
2835 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002836 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002839 /*
2840 * DistinguishedName certificate_authorities<0..2^16-1>;
2841 * opaque DistinguishedName<1..2^16-1>;
2842 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002845 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01002846
2847 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00002849 /* NOTE: If trusted certificates are provisioned
2850 * via a CA callback (configured through
2851 * `mbedtls_ssl_conf_ca_cb()`, then the
2852 * CertificateRequest is currently left empty. */
2853
Janos Follath088ce432017-04-10 12:42:31 +01002854#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2855 if( ssl->handshake->sni_ca_chain != NULL )
2856 crt = ssl->handshake->sni_ca_chain;
2857 else
2858#endif
2859 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002860
Janos Follath088ce432017-04-10 12:42:31 +01002861 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002862 {
irwirc9bc3002020-04-01 13:46:36 +03002863 /* It follows from RFC 5280 A.1 that this length
2864 * can be represented in at most 11 bits. */
2865 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01002866
irwirc9bc3002020-04-01 13:46:36 +03002867 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01002868 {
2869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2870 break;
2871 }
2872
2873 *p++ = (unsigned char)( dn_size >> 8 );
2874 *p++ = (unsigned char)( dn_size );
2875 memcpy( p, crt->subject_raw.p, dn_size );
2876 p += dn_size;
2877
2878 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2879
2880 total_dn_size += 2 + dn_size;
2881 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 }
2884
Paul Bakker926af752012-11-23 13:38:07 +01002885 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2887 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002888 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2889 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002891 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
2895 return( ret );
2896}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002897#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2900 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2901static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002902{
Janos Follath865b3eb2019-12-16 11:46:15 +00002903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2908 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002909 }
2910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2912 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2913 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002916 return( ret );
2917 }
2918
2919 return( 0 );
2920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2922 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002923
Gilles Peskineeccd8882020-03-10 12:19:08 +01002924#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002925 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002926static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002927 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002928{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002929 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2930 * signature length which will be added in ssl_write_server_key_exchange
2931 * after the call to ssl_prepare_server_key_exchange.
2932 * ssl_write_server_key_exchange also takes care of incrementing
2933 * ssl->out_msglen. */
2934 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10002935 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002936 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02002937 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002938 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002939 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
2940 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002941 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02002942 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002943 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01002944 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002945 return( ret );
2946}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002947#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002948 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002949
Gilles Peskined3eb0612018-01-08 17:07:44 +01002950/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02002951 * calculating the signature if any, but excluding formatting the
2952 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01002953static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
2954 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00002955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002957 ssl->handshake->ciphersuite_info;
2958
Gilles Peskineeccd8882020-03-10 12:19:08 +01002959#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
2960#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01002961 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002962#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
2963#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002964
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002965 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002966#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02002967 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002968#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002969
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002970 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002972 /*
2973 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002974 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002975 *
2976 */
2977
2978 /*
2979 * - ECJPAKE key exchanges
2980 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002981#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2982 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2983 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002985 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002986
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002987 ret = mbedtls_ecjpake_write_round_two(
2988 &ssl->handshake->ecjpake_ctx,
2989 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10002990 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002991 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002992 if( ret != 0 )
2993 {
2994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
2995 return( ret );
2996 }
2997
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002998 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002999 }
3000#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3001
Hanno Becker1aa267c2017-04-28 17:08:27 +01003002 /*
3003 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3004 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3005 * we use empty support identity hints here.
3006 **/
3007#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3009 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3010 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003011 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003012 ssl->out_msg[ssl->out_msglen++] = 0x00;
3013 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003014 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3016 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003017
Hanno Becker7e5437a2017-04-28 17:15:26 +01003018 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003019 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003020 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003021#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003022 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003023 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003025 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003026
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003027 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3028 {
3029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3031 }
3032
Paul Bakker41c83d32013-03-20 14:39:14 +01003033 /*
3034 * Ephemeral DH parameters:
3035 *
3036 * struct {
3037 * opaque dh_p<1..2^16-1>;
3038 * opaque dh_g<1..2^16-1>;
3039 * opaque dh_Ys<1..2^16-1>;
3040 * } ServerDHParams;
3041 */
Hanno Beckerab740562017-10-04 13:15:37 +01003042 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3043 &ssl->conf->dhm_P,
3044 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003045 {
Hanno Beckerab740562017-10-04 13:15:37 +01003046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003047 return( ret );
3048 }
Paul Bakker48916f92012-09-16 19:57:18 +00003049
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003050 if( ( ret = mbedtls_dhm_make_params(
3051 &ssl->handshake->dhm_ctx,
3052 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3053 ssl->out_msg + ssl->out_msglen, &len,
3054 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003057 return( ret );
3058 }
3059
Gilles Peskineeccd8882020-03-10 12:19:08 +01003060#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003061 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003062#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003063
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003064 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3067 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3068 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3069 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003070 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003071#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003072
Hanno Becker1aa267c2017-04-28 17:08:27 +01003073 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003074 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003075 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003076#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003077 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003079 /*
3080 * Ephemeral ECDH parameters:
3081 *
3082 * struct {
3083 * ECParameters curve_params;
3084 * ECPoint public;
3085 * } ServerECDHParams;
3086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003090 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003091
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003092 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003093 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003094 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3095 if( (*curve)->grp_id == *gid )
3096 goto curve_matching_done;
3097
3098curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003099 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3102 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003103 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003106
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003107 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3108 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003109 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003111 return( ret );
3112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003114 if( ( ret = mbedtls_ecdh_make_params(
3115 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003116 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003117 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003118 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003121 return( ret );
3122 }
3123
Gilles Peskineeccd8882020-03-10 12:19:08 +01003124#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003125 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003126#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003127
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003128 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003129
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003130 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3131 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003132 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003133#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
Hanno Becker1aa267c2017-04-28 17:08:27 +01003135 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003136 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003137 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003138 * exchange parameters, compute and add the signature here.
3139 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003140 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003141#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003142 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003143 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003144 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003145 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003146 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003148
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003149 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003150 * 2.1: Choose hash algorithm:
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003151 * For TLS 1.2, obey signature-hash-algorithm extension
3152 * to choose appropriate hash.
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003153 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003154
3155 mbedtls_md_type_t md_alg;
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003158 mbedtls_pk_type_t sig_alg =
3159 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003161 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003162 /* For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003163 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003164 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003165 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3166 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003169 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003170 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003172 }
3173 }
Paul Bakker577e0062013-08-28 11:57:20 +02003174 else
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003175 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003178 }
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003179#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003180
Paul Elliott3891caf2020-12-17 18:42:40 +00003181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003182
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003183 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003184 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003185 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003186#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003188 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003189 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003190 dig_signed,
3191 dig_signed_len,
3192 md_alg );
3193 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003194 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003195 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003196 else
TRodziewicz0f82ec62021-05-12 17:49:18 +02003197#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003201 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003202
Gilles Peskineebd652f2018-01-05 21:18:59 +01003203 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003204
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003205 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003206 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3209 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003210 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003211 /*
3212 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003213 * explicitly through a prefix to the signature.
3214 *
3215 * struct {
3216 * HashAlgorithm hash;
3217 * SignatureAlgorithm signature;
3218 * } SignatureAndHashAlgorithm;
3219 *
3220 * struct {
3221 * SignatureAndHashAlgorithm algorithm;
3222 * opaque signature<0..2^16-1>;
3223 * } DigitallySigned;
3224 *
3225 */
3226
Gilles Peskine1004c192018-01-08 16:59:14 +01003227 ssl->out_msg[ssl->out_msglen++] =
3228 mbedtls_ssl_hash_from_md_alg( md_alg );
3229 ssl->out_msg[ssl->out_msglen++] =
3230 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003233
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003234#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003235 if( ssl->conf->f_async_sign_start != NULL )
3236 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003237 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003238 mbedtls_ssl_own_cert( ssl ),
3239 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003240 switch( ret )
3241 {
3242 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3243 /* act as if f_async_sign was null */
3244 break;
3245 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003246 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003247 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003248 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003249 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003250 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3251 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003252 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003253 return( ret );
3254 }
3255 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003256#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003257
3258 if( mbedtls_ssl_own_key( ssl ) == NULL )
3259 {
3260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3261 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3262 }
3263
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003264 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3265 * signature length which will be added in ssl_write_server_key_exchange
3266 * after the call to ssl_prepare_server_key_exchange.
3267 * ssl_write_server_key_exchange also takes care of incrementing
3268 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003269 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3270 md_alg, hash, hashlen,
3271 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003272 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003273 ssl->conf->f_rng,
3274 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003277 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003278 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003280#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003281
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003282 return( 0 );
3283}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003284
Gilles Peskined3eb0612018-01-08 17:07:44 +01003285/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003286 * that do not include a ServerKeyExchange message, do nothing. Either
3287 * way, if successful, move on to the next step in the SSL state
3288 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003289static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3290{
Janos Follath865b3eb2019-12-16 11:46:15 +00003291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003292 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003293#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003294 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003295 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003296#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003297
Gilles Peskined3eb0612018-01-08 17:07:44 +01003298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3299
Gilles Peskineeccd8882020-03-10 12:19:08 +01003300#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003301 /* Extract static ECDH parameters and abort if ServerKeyExchange
3302 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003303 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3304 {
3305 /* For suites involving ECDH, extract DH parameters
3306 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003307#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003308 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3309 {
3310 ssl_get_ecdh_params_from_cert( ssl );
3311 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003312#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003313
3314 /* Key exchanges not involving ephemeral keys don't use
3315 * ServerKeyExchange, so end here. */
3316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3317 ssl->state++;
3318 return( 0 );
3319 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003320#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003321
Gilles Peskineeccd8882020-03-10 12:19:08 +01003322#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003323 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003324 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003325 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003326 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003327 {
3328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3329 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003330 }
3331 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003332#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003333 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003334 {
3335 /* ServerKeyExchange is needed. Prepare the message. */
3336 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003337 }
3338
3339 if( ret != 0 )
3340 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003341 /* If we're starting to write a new message, set ssl->out_msglen
3342 * to 0. But if we're resuming after an asynchronous message,
3343 * out_msglen is the amount of data written so far and mst be
3344 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003345 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3347 else
3348 ssl->out_msglen = 0;
3349 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003350 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003351
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003352 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003353 * ssl_prepare_server_key_exchange already wrote the signature
3354 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003355#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003356 if( signature_len != 0 )
3357 {
3358 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3359 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3360
3361 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3362 ssl->out_msg + ssl->out_msglen,
3363 signature_len );
3364
3365 /* Skip over the already-written signature */
3366 ssl->out_msglen += signature_len;
3367 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003368#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003369
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003370 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3372 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
3374 ssl->state++;
3375
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003376 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 return( ret );
3380 }
3381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003384}
3385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003387{
Janos Follath865b3eb2019-12-16 11:46:15 +00003388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
3392 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3394 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
3396 ssl->state++;
3397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003401#endif
3402
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003403 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 return( ret );
3407 }
3408
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003409#if defined(MBEDTLS_SSL_PROTO_DTLS)
3410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3411 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3412 {
3413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3414 return( ret );
3415 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003416#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419
3420 return( 0 );
3421}
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3424 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3425static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003426 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003429 size_t n;
3430
3431 /*
3432 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3433 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003434 if( *p + 2 > end )
3435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3437 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003438 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003439
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003440 n = ( (*p)[0] << 8 ) | (*p)[1];
3441 *p += 2;
3442
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003443 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3446 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003447 }
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3452 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003453 }
3454
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003455 *p += n;
3456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003458
Paul Bakker70df2fb2013-04-17 17:19:09 +02003459 return( ret );
3460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3462 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3465 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003466
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003467#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003468static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3469 unsigned char *peer_pms,
3470 size_t *peer_pmslen,
3471 size_t peer_pmssize )
3472{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003473 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003474 peer_pms, peer_pmslen, peer_pmssize );
3475 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3476 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003477 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003478 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003479 }
3480 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3481 return( ret );
3482}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003483#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003484
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003485static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3486 const unsigned char *p,
3487 const unsigned char *end,
3488 unsigned char *peer_pms,
3489 size_t *peer_pmslen,
3490 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003491{
Janos Follath865b3eb2019-12-16 11:46:15 +00003492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003493 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3494 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3495 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003496
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003497#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003498 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003499 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003500 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003501 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3503 return( ssl_resume_decrypt_pms( ssl,
3504 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003505 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003506#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003507
3508 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003509 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003510 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003511#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003512 if ( p + 2 > end ) {
3513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3514 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3515 }
3516 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3517 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003518 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3520 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003521 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003522#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003523
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003524 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3527 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003528 }
3529
Gilles Peskine422ccab2018-01-11 18:29:01 +01003530 /*
3531 * Decrypt the premaster secret
3532 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003533#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003534 if( ssl->conf->f_async_decrypt_start != NULL )
3535 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003536 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003537 mbedtls_ssl_own_cert( ssl ),
3538 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003539 switch( ret )
3540 {
3541 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3542 /* act as if f_async_decrypt_start was null */
3543 break;
3544 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003545 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003546 return( ssl_resume_decrypt_pms( ssl,
3547 peer_pms,
3548 peer_pmslen,
3549 peer_pmssize ) );
3550 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003551 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003552 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3553 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003554 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003555 return( ret );
3556 }
3557 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003558#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003559
Gilles Peskine422ccab2018-01-11 18:29:01 +01003560 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3561 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3563 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3564 }
3565
3566 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003567 peer_pms, peer_pmslen, peer_pmssize,
3568 ssl->conf->f_rng, ssl->conf->p_rng );
3569 return( ret );
3570}
3571
3572static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3573 const unsigned char *p,
3574 const unsigned char *end,
3575 size_t pms_offset )
3576{
Janos Follath865b3eb2019-12-16 11:46:15 +00003577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003578 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3579 unsigned char ver[2];
3580 unsigned char fake_pms[48], peer_pms[48];
3581 unsigned char mask;
3582 size_t i, peer_pmslen;
3583 unsigned int diff;
3584
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003585 /* In case of a failure in decryption, the decryption may write less than
3586 * 2 bytes of output, but we always read the first two bytes. It doesn't
3587 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003588 * ret being nonzero, and we only care whether diff is 0.
3589 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3590 * also makes memory analyzers happy (don't access uninitialized memory,
3591 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003592 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003593 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003594
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003595 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3596 peer_pms,
3597 &peer_pmslen,
3598 sizeof( peer_pms ) );
3599
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003600#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003601 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3602 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003603#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003606 ssl->handshake->max_minor_ver,
3607 ssl->conf->transport, ver );
3608
3609 /* Avoid data-dependent branches while checking for invalid
3610 * padding, to protect against timing-based Bleichenbacher-type
3611 * attacks. */
3612 diff = (unsigned int) ret;
3613 diff |= peer_pmslen ^ 48;
3614 diff |= peer_pms[0] ^ ver[0];
3615 diff |= peer_pms[1] ^ ver[1];
3616
3617 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3618 /* MSVC has a warning about unary minus on unsigned, but this is
3619 * well-defined and precisely what we want to do here */
3620#if defined(_MSC_VER)
3621#pragma warning( push )
3622#pragma warning( disable : 4146 )
3623#endif
3624 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3625#if defined(_MSC_VER)
3626#pragma warning( pop )
3627#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003628
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003629 /*
3630 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3631 * must not cause the connection to end immediately; instead, send a
3632 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003633 * To protect against timing-based variants of the attack, we must
3634 * not have any branch that depends on whether the decryption was
3635 * successful. In particular, always generate the fake premaster secret,
3636 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003637 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003638 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003639 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003640 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003641 /* It's ok to abort on an RNG failure, since this does not reveal
3642 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003643 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003644 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003645
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003646#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003647 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003649#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003650
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003651 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3652 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3655 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003656 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003657 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003658
Gilles Peskine422ccab2018-01-11 18:29:01 +01003659 /* Set pms to either the true or the fake PMS, without
3660 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003661 for( i = 0; i < ssl->handshake->pmslen; i++ )
3662 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3663
3664 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3667 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003668
Gilles Peskineeccd8882020-03-10 12:19:08 +01003669#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003671 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003672{
Paul Bakker6db455e2013-09-18 17:29:31 +02003673 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003674 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003675
Hanno Becker845b9462018-10-26 12:07:29 +01003676 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3679 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003680 }
3681
3682 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003683 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003684 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003685 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3688 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003689 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003690
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003691 n = ( (*p)[0] << 8 ) | (*p)[1];
3692 *p += 2;
3693
irwir6527bd62019-09-21 18:51:25 +03003694 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3697 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003698 }
3699
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003700 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003701 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003702 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003704 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003705 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003706 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003707 /* Identity is not a big secret since clients send it in the clear,
3708 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003709 if( n != ssl->conf->psk_identity_len ||
3710 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003713 }
3714 }
3715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003719 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3720 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003722 }
3723
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003724 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003725
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003726 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003727}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003728#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731{
Janos Follath865b3eb2019-12-16 11:46:15 +00003732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003734 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003735
Hanno Beckere694c3e2017-12-27 21:34:08 +00003736 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003739
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003740#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003741 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3742 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3743 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3744 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003745 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003746 {
3747 /* We've already read a record and there is an asynchronous
3748 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003749 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3751 }
3752 else
3753#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01003754 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 return( ret );
3758 }
3759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003761 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3766 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 }
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3772 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 }
3774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3776 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003778 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003781 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003783
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003784 if( p != end )
3785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3787 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003788 }
3789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003791 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01003792 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003793 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003794 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3797 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003798 }
3799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003801 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003802 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3804#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3805 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3806 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3807 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3808 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3809 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3810 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3811 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003814 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
3817 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003818 }
3819
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003820 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3821 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003824 &ssl->handshake->pmslen,
3825 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003827 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
3830 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003831 }
3832
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003833 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3834 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003836 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3838 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3839 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3840 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3841#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3842 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003843 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003844 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003847 return( ret );
3848 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003849
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003850 if( p != end )
3851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3853 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003854 }
3855
Hanno Becker845b9462018-10-26 12:07:29 +01003856#if defined(MBEDTLS_USE_PSA_CRYPTO)
3857 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
3858 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00003859 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01003860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
3861 else
3862#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003864 ciphersuite_info->key_exchange ) ) != 0 )
3865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003867 return( ret );
3868 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3872#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3873 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003874 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003875#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003876 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003877 {
3878 /* There is an asynchronous operation in progress to
3879 * decrypt the encrypted premaster secret, so skip
3880 * directly to resuming this operation. */
3881 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
3882 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3883 * won't actually use it, but maintain p anyway for robustness. */
3884 p += ssl->conf->psk_identity_len + 2;
3885 }
3886 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003887#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003888 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003891 return( ret );
3892 }
3893
Hanno Becker845b9462018-10-26 12:07:29 +01003894#if defined(MBEDTLS_USE_PSA_CRYPTO)
3895 /* Opaque PSKs are currently only supported for PSK-only. */
3896 if( ssl_use_opaque_psk( ssl ) == 1 )
3897 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3898#endif
3899
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003900 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003903 return( ret );
3904 }
3905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003907 ciphersuite_info->key_exchange ) ) != 0 )
3908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003910 return( ret );
3911 }
3912 }
3913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3915#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3916 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003917 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003918 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003921 return( ret );
3922 }
3923 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003926 return( ret );
3927 }
3928
Hanno Becker845b9462018-10-26 12:07:29 +01003929#if defined(MBEDTLS_USE_PSA_CRYPTO)
3930 /* Opaque PSKs are currently only supported for PSK-only. */
3931 if( ssl_use_opaque_psk( ssl ) == 1 )
3932 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3933#endif
3934
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003935 if( p != end )
3936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3938 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003939 }
3940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003942 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003945 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003946 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003947 }
3948 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3950#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3951 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003952 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003953 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003956 return( ret );
3957 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003960 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
3963 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003964 }
3965
Hanno Becker845b9462018-10-26 12:07:29 +01003966#if defined(MBEDTLS_USE_PSA_CRYPTO)
3967 /* Opaque PSKs are currently only supported for PSK-only. */
3968 if( ssl_use_opaque_psk( ssl ) == 1 )
3969 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3970#endif
3971
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003972 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3973 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003976 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003979 return( ret );
3980 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003981 }
3982 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3984#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3985 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003986 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003987 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003990 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 }
3992 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003993 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003995#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3996 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3997 {
3998 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3999 p, end - p );
4000 if( ret != 0 )
4001 {
4002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4003 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4004 }
4005
4006 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4007 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4008 ssl->conf->f_rng, ssl->conf->p_rng );
4009 if( ret != 0 )
4010 {
4011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4012 return( ret );
4013 }
4014 }
4015 else
4016#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004025 return( ret );
4026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004027
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 ssl->state++;
4029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031
4032 return( 0 );
4033}
4034
Gilles Peskineeccd8882020-03-10 12:19:08 +01004035#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004037{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004038 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004039 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Hanno Becker77adddc2019-02-07 12:32:43 +00004043 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004046 ssl->state++;
4047 return( 0 );
4048 }
4049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004052}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004053#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004057 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004058 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004059 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004060 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4062 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004065 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004066 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004067 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004070
Hanno Becker2a831a42019-02-07 13:17:25 +00004071 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004074 ssl->state++;
4075 return( 0 );
4076 }
4077
Hanno Becker2a831a42019-02-07 13:17:25 +00004078#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4079 if( ssl->session_negotiate->peer_cert == NULL )
4080 {
4081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4082 ssl->state++;
4083 return( 0 );
4084 }
4085#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4086 if( ssl->session_negotiate->peer_cert_digest == NULL )
4087 {
4088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4089 ssl->state++;
4090 return( 0 );
4091 }
4092#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4093
Simon Butcher99000142016-10-13 17:21:01 +01004094 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004095 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004096 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004098 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 return( ret );
4100 }
4101
4102 ssl->state++;
4103
Simon Butcher99000142016-10-13 17:21:01 +01004104 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4106 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4109 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004110 }
4111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004113
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004114#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4115 peer_pk = &ssl->handshake->peer_pubkey;
4116#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4117 if( ssl->session_negotiate->peer_cert == NULL )
4118 {
4119 /* Should never happen */
4120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4121 }
4122 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4123#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4124
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004125 /*
4126 * struct {
4127 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4128 * opaque signature<0..2^16-1>;
4129 * } DigitallySigned;
4130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4132 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004133 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004134 if( i + 2 > ssl->in_hslen )
4135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4137 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004138 }
4139
Paul Bakker5121ce52009-01-03 21:22:43 +00004140 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004141 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004142 */
Simon Butcher99000142016-10-13 17:21:01 +01004143 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4144
4145 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004148 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004150 }
4151
Simon Butcher99000142016-10-13 17:21:01 +01004152#if !defined(MBEDTLS_MD_SHA1)
4153 if( MBEDTLS_MD_SHA1 == md_alg )
4154 hash_start += 16;
4155#endif
Paul Bakker926af752012-11-23 13:38:07 +01004156
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004157 /* Info from md_alg will be used instead */
4158 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004159
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004160 i++;
4161
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004162 /*
4163 * Signature
4164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4166 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004169 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004171 }
4172
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004173 /*
4174 * Check the certificate's key type matches the signature alg
4175 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004176 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4179 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004180 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004181
4182 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004183 }
4184 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004189 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004190
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004191 if( i + 2 > ssl->in_hslen )
4192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4194 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004195 }
4196
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004197 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4198 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004199
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004200 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4203 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004204 }
4205
Simon Butcher99000142016-10-13 17:21:01 +01004206 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004207 {
4208 size_t dummy_hlen;
4209 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4210 }
Simon Butcher99000142016-10-13 17:21:01 +01004211
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004212 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004213 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004214 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004217 return( ret );
4218 }
4219
Simon Butcher99000142016-10-13 17:21:01 +01004220 mbedtls_ssl_update_handshake_status( ssl );
4221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004223
Paul Bakkered27a042013-04-18 22:46:23 +02004224 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004225}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004226#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4229static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004230{
Janos Follath865b3eb2019-12-16 11:46:15 +00004231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004232 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004233 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4238 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004239
4240 /*
4241 * struct {
4242 * uint32 ticket_lifetime_hint;
4243 * opaque ticket<0..2^16-1>;
4244 * } NewSessionTicket;
4245 *
4246 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4247 * 8 . 9 ticket_len (n)
4248 * 10 . 9+n ticket content
4249 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004250
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004251 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004252 ssl->session_negotiate,
4253 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004254 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004255 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004256 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004258 tlen = 0;
4259 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004260
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004261 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4262 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4263 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4264 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4265
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004266 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4267 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004268
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004269 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004270
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004271 /*
4272 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4273 * ChangeCipherSpec share the same state.
4274 */
4275 ssl->handshake->new_session_ticket = 0;
4276
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004277 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004278 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004280 return( ret );
4281 }
4282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004284
4285 return( 0 );
4286}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004288
Paul Bakker5121ce52009-01-03 21:22:43 +00004289/*
Paul Bakker1961b702013-01-25 14:49:24 +01004290 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004293{
4294 int ret = 0;
4295
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004296 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004302 return( ret );
4303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004307 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004308 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004309 return( ret );
4310 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004311#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004312
Paul Bakker1961b702013-01-25 14:49:24 +01004313 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 case MBEDTLS_SSL_HELLO_REQUEST:
4316 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 break;
4318
Paul Bakker1961b702013-01-25 14:49:24 +01004319 /*
4320 * <== ClientHello
4321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004323 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326#if defined(MBEDTLS_SSL_PROTO_DTLS)
4327 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4328 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004329#endif
4330
Paul Bakker1961b702013-01-25 14:49:24 +01004331 /*
4332 * ==> ServerHello
4333 * Certificate
4334 * ( ServerKeyExchange )
4335 * ( CertificateRequest )
4336 * ServerHelloDone
4337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004339 ret = ssl_write_server_hello( ssl );
4340 break;
4341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4343 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004344 break;
4345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004347 ret = ssl_write_server_key_exchange( ssl );
4348 break;
4349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004351 ret = ssl_write_certificate_request( ssl );
4352 break;
4353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004355 ret = ssl_write_server_hello_done( ssl );
4356 break;
4357
4358 /*
4359 * <== ( Certificate/Alert )
4360 * ClientKeyExchange
4361 * ( CertificateVerify )
4362 * ChangeCipherSpec
4363 * Finished
4364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4366 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004367 break;
4368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004370 ret = ssl_parse_client_key_exchange( ssl );
4371 break;
4372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004374 ret = ssl_parse_certificate_verify( ssl );
4375 break;
4376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4378 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004379 break;
4380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 case MBEDTLS_SSL_CLIENT_FINISHED:
4382 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004383 break;
4384
4385 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004386 * ==> ( NewSessionTicket )
4387 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004388 * Finished
4389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4391#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004392 if( ssl->handshake->new_session_ticket != 0 )
4393 ret = ssl_write_new_session_ticket( ssl );
4394 else
Paul Bakkera503a632013-08-14 13:48:06 +02004395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004397 break;
4398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 case MBEDTLS_SSL_SERVER_FINISHED:
4400 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004401 break;
4402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 case MBEDTLS_SSL_FLUSH_BUFFERS:
4404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4405 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004406 break;
4407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4409 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004410 break;
4411
4412 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 }
4416
Paul Bakker5121ce52009-01-03 21:22:43 +00004417 return( ret );
4418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419#endif /* MBEDTLS_SSL_SRV_C */