blob: 881b1fd695420e323d025bc89324fa6f3a102732 [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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +010094 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +020095 }
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100102 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100114 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100126 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
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,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100139 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
140 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
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 );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100205 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
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 );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100216 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100256 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +0200257 }
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100265 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100330 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +0200331 }
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100339 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
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,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100347 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
348 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100396 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
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)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200410 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
411 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100414 return( 0 );
415 }
416
417 list_size--;
418 p++;
419 }
420
421 return( 0 );
422}
Robert Cragieae8535d2015-10-06 17:11:18 +0100423#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
424 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100425
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200426#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
427static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
428 const unsigned char *buf,
429 size_t len )
430{
Janos Follath865b3eb2019-12-16 11:46:15 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200432
433 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
434 {
435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
436 return( 0 );
437 }
438
439 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
440 buf, len ) ) != 0 )
441 {
442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
444 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200445 return( ret );
446 }
447
448 /* Only mark the extension as OK when we're sure it is */
449 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
450
451 return( 0 );
452}
453#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
456static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200457 const unsigned char *buf,
458 size_t len )
459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200463 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
464 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100465 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200466 }
467
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200468 ssl->session_negotiate->mfl_code = buf[0];
469
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200470 return( 0 );
471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200473
Hanno Beckera0e20d02019-05-15 14:03:01 +0100474#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100475static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
476 const unsigned char *buf,
477 size_t len )
478{
479 size_t peer_cid_len;
480
481 /* CID extension only makes sense in DTLS */
482 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
483 {
484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
486 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100487 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100488 }
489
490 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100491 * Quoting draft-ietf-tls-dtls-connection-id-05
492 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100493 *
494 * struct {
495 * opaque cid<0..2^8-1>;
496 * } ConnectionId;
497 */
498
499 if( len < 1 )
500 {
501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
502 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100503 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
504 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100505 }
506
507 peer_cid_len = *buf++;
508 len--;
509
510 if( len != peer_cid_len )
511 {
512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100514 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
515 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100516 }
517
518 /* Ignore CID if the user has disabled its use. */
519 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
520 {
521 /* Leave ssl->handshake->cid_in_use in its default
522 * value of MBEDTLS_SSL_CID_DISABLED. */
523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
524 return( 0 );
525 }
526
527 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
528 {
529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
530 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
531 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100532 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100533 }
534
Hanno Becker08556bf2019-05-03 12:43:44 +0100535 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100536 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
537 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
538
539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
540 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
541
Hanno Becker89dcc882019-04-26 13:56:39 +0100542 return( 0 );
543}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
547static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100548 const unsigned char *buf,
549 size_t len )
550{
551 if( len != 0 )
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200554 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
555 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100556 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100557 }
558
559 ((void) buf);
560
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100561 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100564 }
565
566 return( 0 );
567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
571static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200572 const unsigned char *buf,
573 size_t len )
574{
575 if( len != 0 )
576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200578 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
579 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100580 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200581 }
582
583 ((void) buf);
584
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100585 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200588 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200589
590 return( 0 );
591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_SSL_SESSION_TICKETS)
595static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200596 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200597 size_t len )
598{
Janos Follath865b3eb2019-12-16 11:46:15 +0000599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200600 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200601
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200602 mbedtls_ssl_session_init( &session );
603
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200604 if( ssl->conf->f_ticket_parse == NULL ||
605 ssl->conf->f_ticket_write == NULL )
606 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200607 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200608 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200609
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200610 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200611 ssl->handshake->new_session_ticket = 1;
612
Paul Elliottd48d5c62021-01-07 14:47:05 +0000613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200614
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200615 if( len == 0 )
616 return( 0 );
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SSL_RENEGOTIATION)
619 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200622 return( 0 );
623 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200625
626 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200627 * Failures are ok: just ignore the ticket and proceed.
628 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200629 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
630 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200631 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200632 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200633
634 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
636 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
638 else
639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
640
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200641 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200642 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200643
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200644 /*
645 * Keep the session ID sent by the client, since we MUST send it back to
646 * inform them we're accepting the ticket (RFC 5077 section 3.4)
647 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200648 session.id_len = ssl->session_negotiate->id_len;
649 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200650
651 mbedtls_ssl_session_free( ssl->session_negotiate );
652 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
653
654 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500655 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200658
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200659 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200660
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200661 /* Don't send a new ticket after all, this one is OK */
662 ssl->handshake->new_session_ticket = 0;
663
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200664 return( 0 );
665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#if defined(MBEDTLS_SSL_ALPN)
669static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200670 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200671{
Paul Bakker14b16c62014-05-28 11:33:54 +0200672 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200673 const unsigned char *theirs, *start, *end;
674 const char **ours;
675
676 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200677 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200678 return( 0 );
679
680 /*
681 * opaque ProtocolName<1..2^8-1>;
682 *
683 * struct {
684 * ProtocolName protocol_name_list<2..2^16-1>
685 * } ProtocolNameList;
686 */
687
688 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
689 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200690 {
691 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
692 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100693 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200694 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200695
696 list_len = ( buf[0] << 8 ) | buf[1];
697 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200698 {
699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
700 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100701 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200702 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200703
704 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100705 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200706 */
707 start = buf + 2;
708 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100709 for( theirs = start; theirs != end; theirs += cur_len )
710 {
711 cur_len = *theirs++;
712
713 /* Current identifier must fit in list */
714 if( cur_len > (size_t)( end - theirs ) )
715 {
716 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
717 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100718 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100719 }
720
721 /* Empty strings MUST NOT be included */
722 if( cur_len == 0 )
723 {
724 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
725 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100726 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100727 }
728 }
729
730 /*
731 * Use our order of preference
732 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200733 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200734 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200735 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200736 for( theirs = start; theirs != end; theirs += cur_len )
737 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200738 cur_len = *theirs++;
739
Paul Bakker14b16c62014-05-28 11:33:54 +0200740 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200741 memcmp( theirs, *ours, cur_len ) == 0 )
742 {
743 ssl->alpn_chosen = *ours;
744 return( 0 );
745 }
746 }
747 }
748
749 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
751 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
Dave Rodgman53c86892021-06-29 10:02:06 +0100752 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200755
Johan Pascalb62bb512015-12-03 21:56:45 +0100756#if defined(MBEDTLS_SSL_DTLS_SRTP)
757static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300758 const unsigned char *buf,
759 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100760{
Johan Pascal43f94902020-09-22 12:25:52 +0200761 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100762 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200763 size_t profile_length;
764 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200765 /*! 2 bytes for profile length and 1 byte for mki len */
766 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100767
768 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100769 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
770 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
771 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200772 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100773 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200774 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100775
776 /* RFC5764 section 4.1.1
777 * uint8 SRTPProtectionProfile[2];
778 *
779 * struct {
780 * SRTPProtectionProfiles SRTPProtectionProfiles;
781 * opaque srtp_mki<0..255>;
782 * } UseSRTPData;
783
784 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100785 */
786
Ron Eldoref72faf2018-07-12 11:54:20 +0300787 /*
788 * Min length is 5: at least one protection profile(2 bytes)
789 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200790 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200791 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300792 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200793 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200794 {
795 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100796 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
797 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200798 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100799
Johan Pascal43f94902020-09-22 12:25:52 +0200800 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200801
Ron Eldoref72faf2018-07-12 11:54:20 +0300802 /* first 2 bytes are protection profile length(in bytes) */
803 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200804 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200805
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200806 /* The profile length cannot be bigger than input buffer size - lengths fields */
807 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200808 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200809 {
810 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100811 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
812 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200813 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300814 /*
815 * parse the extension list values are defined in
816 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
817 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200818 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100819 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200820 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200821 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100822
Johan Pascal43f94902020-09-22 12:25:52 +0200823 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300824 {
Johan Pascal43f94902020-09-22 12:25:52 +0200825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
826 mbedtls_ssl_get_srtp_profile_as_string(
827 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300828 }
Johan Pascal85269572020-08-25 10:01:54 +0200829 else
830 {
831 continue;
832 }
Ron Eldor591f1622018-01-22 12:30:04 +0200833 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200834 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200835 {
836 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
837 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200838 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200839 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
840 mbedtls_ssl_get_srtp_profile_as_string(
841 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200842 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100843 }
844 }
Johan Pascal43f94902020-09-22 12:25:52 +0200845 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200846 break;
847 }
Johan Pascal042d4562020-08-25 12:14:02 +0200848 buf += profile_length; /* buf points to the mki length */
849 mki_length = *buf;
850 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200851
Johan Pascal042d4562020-08-25 12:14:02 +0200852 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
853 mki_length + profile_length + size_of_lengths != len )
854 {
855 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100856 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
857 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascal042d4562020-08-25 12:14:02 +0200858 }
859
860 /* Parse the mki only if present and mki is supported locally */
861 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
862 mki_length > 0 )
863 {
864 ssl->dtls_srtp_info.mki_len = mki_length;
865
Johan Pascal5ef72d22020-10-28 17:05:47 +0100866 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300867
Ron Eldoref72faf2018-07-12 11:54:20 +0300868 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
869 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100870 }
871
Johan Pascal042d4562020-08-25 12:14:02 +0200872 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100873}
874#endif /* MBEDTLS_SSL_DTLS_SRTP */
875
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100876/*
877 * Auxiliary functions for ServerHello parsing and related actions
878 */
879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100881/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100882 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#if defined(MBEDTLS_ECDSA_C)
885static int ssl_check_key_curve( mbedtls_pk_context *pk,
886 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 const mbedtls_ecp_curve_info **crv = curves;
889 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100890
891 while( *crv != NULL )
892 {
893 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100894 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100895 crv++;
896 }
897
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100898 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100901
902/*
903 * Try picking a certificate for this ciphersuite,
904 * return 0 on success and -1 on failure.
905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906static int ssl_pick_cert( mbedtls_ssl_context *ssl,
907 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100910 mbedtls_pk_type_t pk_alg =
911 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200912 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100915 if( ssl->handshake->sni_key_cert != NULL )
916 list = ssl->handshake->sni_key_cert;
917 else
918#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200919 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922 return( 0 );
923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000925
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200926 if( list == NULL )
927 {
928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
929 return( -1 );
930 }
931
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100932 for( cur = list; cur != NULL; cur = cur->next )
933 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400934 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000936 cur->cert );
937
Gilles Peskinee198df52018-01-05 21:17:45 +0100938 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100941 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000942 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100943
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200944 /*
945 * This avoids sending the client a cert it'll reject based on
946 * keyUsage or other extensions.
947 *
948 * It also allows the user to provision different certificates for
949 * different uses based on keyUsage, eg if they want to avoid signing
950 * and decrypting with the same RSA key.
951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100953 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000956 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200957 continue;
958 }
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_ECDSA_C)
961 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200962 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100965 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000966 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100967#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100968
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100969 /*
970 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
971 * present them a SHA-higher cert rather than failing if it's the only
972 * one we got that satisfies the other conditions.
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
975 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100976 {
977 if( fallback == NULL )
978 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000981 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100982 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000983 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100984 }
985
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100986 /* If we get there, we got a winner */
987 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100988 }
989
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000990 if( cur == NULL )
991 cur = fallback;
992
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200993 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100994 if( cur != NULL )
995 {
996 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000998 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100999 return( 0 );
1000 }
1001
1002 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001005
1006/*
1007 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1008 * Sets ciphersuite_info only if the suite matches.
1009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1011 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001014
Hanno Becker7e5437a2017-04-28 17:15:26 +01001015#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001016 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001017 mbedtls_pk_type_t sig_type;
1018#endif
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001021 if( suite_info == NULL )
1022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001025 }
1026
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001027 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +00001028 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001029
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001030 if( suite_info->min_minor_ver > ssl->minor_ver ||
1031 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001034 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001035 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001038 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001040 return( 0 );
1041#endif
1042
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001043#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1044 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001045 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001046 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1048 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001049 return( 0 );
1050 }
1051#endif
1052
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1055 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001056 ( ssl->handshake->curves == NULL ||
1057 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001060 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001061 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001062 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001063#endif
1064
Gilles Peskineeccd8882020-03-10 12:19:08 +01001065#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001066 /* If the ciphersuite requires a pre-shared key and we don't
1067 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001069 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001072 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001073 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001074#endif
1075
Hanno Becker7e5437a2017-04-28 17:15:26 +01001076#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001077 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001078 /* If the ciphersuite requires signing, check whether
1079 * a suitable hash algorithm is present. */
1080 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1081 {
1082 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1083 if( sig_type != MBEDTLS_PK_NONE &&
1084 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1085 {
1086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001087 "for signature algorithm %u", (unsigned) sig_type ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001088 return( 0 );
1089 }
1090 }
1091
1092#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001093 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001096 /*
1097 * Final check: if ciphersuite requires us to have a
1098 * certificate/key of a particular type:
1099 * - select the appropriate certificate if we have one, or
1100 * - try the next ciphersuite if we don't
1101 * This must be done last since we modify the key_cert list.
1102 */
1103 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001106 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001107 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001108 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001109#endif
1110
1111 *ciphersuite_info = suite_info;
1112 return( 0 );
1113}
1114
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001115/* This function doesn't alert on errors that happen early during
1116 ClientHello parsing because they might indicate that the client is
1117 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001119{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001120 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001121 size_t i, j;
1122 size_t ciph_offset, comp_offset, ext_offset;
1123 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001125 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001126#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001127 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001129 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001130#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001131 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001132 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001134 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Hanno Becker7e5437a2017-04-28 17:15:26 +01001136 /* If there is no signature-algorithm extension present,
1137 * we need to fall back to the default values for allowed
1138 * signature-hash pairs. */
1139#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001140 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001141 int sig_hash_alg_ext_present = 0;
1142#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001143 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001148read_record_header:
1149#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001150 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001152 * otherwise read it ourselves manually in order to support SSLv2
1153 * ClientHello, which doesn't use the same record layer format.
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_SSL_RENEGOTIATION)
1156 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001160 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001161 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001163 return( ret );
1164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
1166
1167 buf = ssl->in_hdr;
1168
Hanno Becker5903de42019-05-03 14:46:38 +01001169 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001170
Paul Bakkerec636f32012-09-09 19:17:02 +00001171 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001172 * TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001173 *
1174 * Record layer:
1175 * 0 . 0 message type
1176 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001177 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001178 * 3 . 4 message length
1179 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001181 buf[0] ) );
1182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001186 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001187 }
1188
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001190 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1191
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001193 buf[1], buf[2] ) );
1194
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001195 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001196
1197 /* According to RFC 5246 Appendix E.1, the version here is typically
1198 * "{03,00}, the lowest version number supported by the client, [or] the
1199 * value of ClientHello.client_version", so the only meaningful check here
1200 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001204 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakkerec636f32012-09-09 19:17:02 +00001205 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001207 /* For DTLS if this is the initial handshake, remember the client sequence
1208 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_SSL_RENEGOTIATION)
1212 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001213#endif
1214 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001215 {
1216 /* Epoch should be 0 for initial handshakes */
1217 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001220 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001221 }
1222
Jerry Yud9a94fe2021-09-28 18:58:59 +08001223 memcpy( &ssl->cur_out_ctr[2], ssl->in_ctr + 2,
Jerry Yud96a5c22021-09-29 17:46:51 +08001224 sizeof( ssl->cur_out_ctr ) - 2 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1227 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001230 ssl->next_record_offset = 0;
1231 ssl->in_left = 0;
1232 goto read_record_header;
1233 }
1234
1235 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001237#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001240
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001241 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#if defined(MBEDTLS_SSL_RENEGOTIATION)
1244 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001247 msg_len = ssl->in_hslen;
1248 }
1249 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001250#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001251 {
Angus Grattond8213d02016-05-25 20:56:48 +10001252 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001255 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001256 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001257
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001258 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001259 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001262 return( ret );
1263 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001264
1265 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001268 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001269 else
1270#endif
1271 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001272 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001273
1274 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001277
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001278 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001279
1280 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001281 * Handshake layer:
1282 * 0 . 0 handshake type
1283 * 1 . 3 handshake length
1284 * 4 . 5 DTLS only: message seqence number
1285 * 6 . 8 DTLS only: fragment offset
1286 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001291 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001292 }
1293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001299 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001300 }
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001303 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1304
1305 /* We don't support fragmentation of ClientHello (yet?) */
1306 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001310 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001311 }
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001315 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001316 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001317 * Copy the client's handshake message_seq on initial handshakes,
1318 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_SSL_RENEGOTIATION)
1321 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001322 {
1323 /* This couldn't be done in ssl_prepare_handshake_record() */
1324 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1325 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001326
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001327 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001330 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001331 ssl->handshake->in_msg_seq ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001332 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001333 }
1334
1335 ssl->handshake->in_msg_seq++;
1336 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001337 else
1338#endif
1339 {
1340 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1341 ssl->in_msg[5];
1342 ssl->handshake->out_msg_seq = cli_msg_seq;
1343 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1344 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001345
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001346 /*
1347 * For now we don't support fragmentation, so make sure
1348 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001349 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001350 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1351 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1354 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001355 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001356 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 buf += mbedtls_ssl_hs_hdr_len( ssl );
1360 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001361
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001362 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001363 * ClientHello layer:
1364 * 0 . 1 protocol version
1365 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1366 * 34 . 35 session id length (1 byte)
1367 * 35 . 34+x session id
1368 * 35+x . 35+x DTLS only: cookie length (1 byte)
1369 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001370 * .. . .. ciphersuite list length (2 bytes)
1371 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001372 * .. . .. compression alg. list length (1 byte)
1373 * .. . .. compression alg. list
1374 * .. . .. extensions length (2 bytes, optional)
1375 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001376 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001377
1378 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001379 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001380 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1381 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001382 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001383 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001386 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001387 }
1388
1389 /*
1390 * Check and save the protocol version
1391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001395 ssl->conf->transport, buf );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001396 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001397
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001398 ssl->handshake->max_major_ver = ssl->major_ver;
1399 ssl->handshake->max_minor_ver = ssl->minor_ver;
1400
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001401 if( ssl->major_ver < ssl->conf->min_major_ver ||
1402 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001405 " [%d:%d] < [%d:%d]",
1406 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001407 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1409 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Hanno Beckerbc000442021-06-24 09:18:19 +01001410 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001411 }
1412
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001413 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001414 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001415 ssl->major_ver = ssl->conf->max_major_ver;
1416 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001417 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001418 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1419 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001420
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001421 /*
1422 * Save client random (inc. Unix time)
1423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001425
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001426 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001427
1428 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001429 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001430 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001431 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001432
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001433 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001434 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001437 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1438 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001439 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001440 }
1441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001443
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001444 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001445 memset( ssl->session_negotiate->id, 0,
1446 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001447 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001448 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001449
1450 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001451 * Check the cookie length and content
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001455 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001456 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001457 cookie_len = buf[cookie_offset];
1458
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001459 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001462 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +01001463 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1464 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001465 }
1466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001468 buf + cookie_offset + 1, cookie_len );
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001471 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#if defined(MBEDTLS_SSL_RENEGOTIATION)
1473 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001474#endif
1475 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001476 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001477 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001478 buf + cookie_offset + 1, cookie_len,
1479 ssl->cli_id, ssl->cli_id_len ) != 0 )
1480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001482 ssl->handshake->verify_cookie_len = 1;
1483 }
1484 else
1485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001487 ssl->handshake->verify_cookie_len = 0;
1488 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001489 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001490 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001492 {
1493 /* We know we didn't send a cookie, so it should be empty */
1494 if( cookie_len != 0 )
1495 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001496 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001498 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001499 }
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001502 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001503
1504 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001505 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001506 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001507 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001508 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001511 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001512
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001513 ciph_len = ( buf[ciph_offset + 0] << 8 )
1514 | ( buf[ciph_offset + 1] );
1515
1516 if( ciph_len < 2 ||
1517 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1518 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001521 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1522 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001523 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001524 }
1525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001527 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001528
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001529 /*
1530 * Check the compression algorithms length and pick one
1531 */
1532 comp_offset = ciph_offset + 2 + ciph_len;
1533
1534 comp_len = buf[comp_offset];
1535
1536 if( comp_len < 1 ||
1537 comp_len > 16 ||
1538 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1542 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001543 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001544 }
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001547 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001550 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001552 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001554#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001555 /*
1556 * Check the extension length
1557 */
1558 ext_offset = comp_offset + 1 + comp_len;
1559 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001560 {
Simon Butcher584a5472016-05-23 16:24:52 +01001561 if( msg_len < ext_offset + 2 )
1562 {
1563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001564 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1565 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001566 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001567 }
1568
1569 ext_len = ( buf[ext_offset + 0] << 8 )
1570 | ( buf[ext_offset + 1] );
1571
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001572 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001573 {
1574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001575 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1576 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001577 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001578 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001579 }
Simon Butcher584a5472016-05-23 16:24:52 +01001580 else
1581 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001582
Simon Butcher584a5472016-05-23 16:24:52 +01001583 ext = buf + ext_offset + 2;
1584 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001585
Simon Butcher584a5472016-05-23 16:24:52 +01001586 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001587 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001588 unsigned int ext_id;
1589 unsigned int ext_size;
1590 if ( ext_len < 4 ) {
1591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1592 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1593 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001594 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02001595 }
1596 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1597 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001598
Simon Butcher584a5472016-05-23 16:24:52 +01001599 if( ext_size + 4 > ext_len )
1600 {
1601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001604 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001605 }
1606 switch( ext_id )
1607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001609 case MBEDTLS_TLS_EXT_SERVERNAME:
1610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1611 if( ssl->conf->f_sni == NULL )
1612 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001613
Simon Butcher584a5472016-05-23 16:24:52 +01001614 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1615 if( ret != 0 )
1616 return( ret );
1617 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001619
Simon Butcher584a5472016-05-23 16:24:52 +01001620 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001623 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001624#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001625
Simon Butcher584a5472016-05-23 16:24:52 +01001626 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1627 if( ret != 0 )
1628 return( ret );
1629 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001632 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001633 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1635
Simon Butcher584a5472016-05-23 16:24:52 +01001636 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1637 if( ret != 0 )
1638 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001639
1640 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001641 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001643 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001644
Robert Cragie136884c2015-10-02 13:34:31 +01001645#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001646 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001647 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001649
Simon Butcher584a5472016-05-23 16:24:52 +01001650 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1651 if( ret != 0 )
1652 return( ret );
1653 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001654
Simon Butcher584a5472016-05-23 16:24:52 +01001655 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1657 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001658
Simon Butcher584a5472016-05-23 16:24:52 +01001659 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1660 if( ret != 0 )
1661 return( ret );
1662 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001663#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1664 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001665
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001666#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001667 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001669
Simon Butcher584a5472016-05-23 16:24:52 +01001670 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1671 if( ret != 0 )
1672 return( ret );
1673 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001674#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001677 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001679
Simon Butcher584a5472016-05-23 16:24:52 +01001680 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1681 if( ret != 0 )
1682 return( ret );
1683 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001685
Hanno Beckera0e20d02019-05-15 14:03:01 +01001686#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001687 case MBEDTLS_TLS_EXT_CID:
1688 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1689
1690 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1691 if( ret != 0 )
1692 return( ret );
1693 break;
Thomas Daubneye1c9a402021-06-15 11:26:43 +01001694#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +01001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001697 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001699
Simon Butcher584a5472016-05-23 16:24:52 +01001700 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1701 if( ret != 0 )
1702 return( ret );
1703 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001707 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001709
Simon Butcher584a5472016-05-23 16:24:52 +01001710 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1711 if( ret != 0 )
1712 return( ret );
1713 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001717 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001719
Simon Butcher584a5472016-05-23 16:24:52 +01001720 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1721 if( ret != 0 )
1722 return( ret );
1723 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001727 case MBEDTLS_TLS_EXT_ALPN:
1728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001729
Simon Butcher584a5472016-05-23 16:24:52 +01001730 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1731 if( ret != 0 )
1732 return( ret );
1733 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001735
Johan Pascalb62bb512015-12-03 21:56:45 +01001736#if defined(MBEDTLS_SSL_DTLS_SRTP)
1737 case MBEDTLS_TLS_EXT_USE_SRTP:
1738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02001739
Johan Pascalc3ccd982020-10-28 17:18:18 +01001740 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
1741 if( ret != 0 )
1742 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001743 break;
1744#endif /* MBEDTLS_SSL_DTLS_SRTP */
1745
Simon Butcher584a5472016-05-23 16:24:52 +01001746 default:
Paul Elliott9f352112020-12-09 14:55:45 +00001747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01001748 ext_id ) );
1749 }
1750
1751 ext_len -= 4 + ext_size;
1752 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00001753 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01001754
Hanno Becker7e5437a2017-04-28 17:15:26 +01001755#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001756 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001757
1758 /*
1759 * Try to fall back to default hash SHA1 if the client
1760 * hasn't provided any preferred signature-hash combinations.
1761 */
1762 if( sig_hash_alg_ext_present == 0 )
1763 {
1764 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
1765
1766 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
1767 md_default = MBEDTLS_MD_NONE;
1768
1769 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1770 }
1771
1772#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001773 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001774
Paul Bakker48916f92012-09-16 19:57:18 +00001775 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001776 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1777 */
1778 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1783#if defined(MBEDTLS_SSL_RENEGOTIATION)
1784 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001785 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1787 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001788 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1789 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01001790 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001791 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001792#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001794 break;
1795 }
1796 }
1797
1798 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001799 * Renegotiation security checks
1800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001802 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001805 handshake_failure = 1;
1806 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#if defined(MBEDTLS_SSL_RENEGOTIATION)
1808 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1809 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001810 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001813 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1816 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001817 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001820 handshake_failure = 1;
1821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1823 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001824 renegotiation_info_seen == 1 )
1825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001827 handshake_failure = 1;
1828 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001830
1831 if( handshake_failure == 1 )
1832 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02001833 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1834 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001835 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001836 }
Paul Bakker380da532012-04-18 16:10:25 +00001837
Paul Bakker41c83d32013-03-20 14:39:14 +01001838 /*
1839 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001840 * (At the end because we need information from the EC-based extensions
1841 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001842 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001843 got_common_suite = 0;
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001844 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001845 ciphersuite_info = NULL;
TRodziewicz8476f2f2021-06-02 14:34:47 +02001846
TRodziewicz3946f792021-06-14 12:11:18 +02001847 if (ssl->conf->respect_cli_pref == MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT)
TRodziewicz8476f2f2021-06-02 14:34:47 +02001848 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001849 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001850 for( i = 0; ciphersuites[i] != 0; i++ )
1851 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001852 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001853 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001854
TRodziewicz8476f2f2021-06-02 14:34:47 +02001855 got_common_suite = 1;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001856
TRodziewicz8476f2f2021-06-02 14:34:47 +02001857 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1858 &ciphersuite_info ) ) != 0 )
1859 return( ret );
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001860
TRodziewicz8476f2f2021-06-02 14:34:47 +02001861 if( ciphersuite_info != NULL )
1862 goto have_ciphersuite;
1863 }
1864 } else {
1865 for( i = 0; ciphersuites[i] != 0; i++ )
1866 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
1867 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001868 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001869 continue;
1870
1871 got_common_suite = 1;
1872
1873 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1874 &ciphersuite_info ) ) != 0 )
1875 return( ret );
1876
1877 if( ciphersuite_info != NULL )
1878 goto have_ciphersuite;
1879 }
1880 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001881
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001882 if( got_common_suite )
1883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001885 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001886 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1887 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman096c4112021-06-29 09:52:06 +01001888 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001889 }
1890 else
1891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1894 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01001895 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001896 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001897
1898have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001900
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001901 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001902 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001903
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 ssl->state++;
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001909#endif
1910
Hanno Becker7e5437a2017-04-28 17:15:26 +01001911 /* Debugging-only output for testsuite */
1912#if defined(MBEDTLS_DEBUG_C) && \
1913 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001914 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001915 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1916 {
1917 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
1918 if( sig_alg != MBEDTLS_PK_NONE )
1919 {
1920 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1921 sig_alg );
1922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1923 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
1924 }
1925 else
1926 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001928 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001929 }
1930 }
1931#endif
1932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
1935 return( 0 );
1936}
1937
Hanno Beckera0e20d02019-05-15 14:03:01 +01001938#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01001939static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
1940 unsigned char *buf,
1941 size_t *olen )
1942{
1943 unsigned char *p = buf;
1944 size_t ext_len;
1945 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1946
1947 *olen = 0;
1948
1949 /* Skip writing the extension if we don't want to use it or if
1950 * the client hasn't offered it. */
1951 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
1952 return;
1953
1954 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
1955 * which is at most 255, so the increment cannot overflow. */
1956 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
1957 {
1958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1959 return;
1960 }
1961
1962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
1963
1964 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01001965 * Quoting draft-ietf-tls-dtls-connection-id-05
1966 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01001967 *
1968 * struct {
1969 * opaque cid<0..2^8-1>;
1970 * } ConnectionId;
1971 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001972 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
1973 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01001974 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001975 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
1976 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01001977
1978 *p++ = (uint8_t) ssl->own_cid_len;
1979 memcpy( p, ssl->own_cid, ssl->own_cid_len );
1980
1981 *olen = ssl->own_cid_len + 5;
1982}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001983#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1986static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001987 unsigned char *buf,
1988 size_t *olen )
1989{
1990 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 const mbedtls_ssl_ciphersuite_t *suite = NULL;
1992 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001993
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001994 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001995 {
1996 *olen = 0;
1997 return;
1998 }
1999
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002000 /*
2001 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2002 * from a client and then selects a stream or Authenticated Encryption
2003 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2004 * encrypt-then-MAC response extension back to the client."
2005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002007 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2009 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002010 {
2011 *olen = 0;
2012 return;
2013 }
2014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002016
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002017 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
2018 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002019
2020 *p++ = 0x00;
2021 *p++ = 0x00;
2022
2023 *olen = 4;
2024}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2028static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002029 unsigned char *buf,
2030 size_t *olen )
2031{
2032 unsigned char *p = buf;
2033
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002034 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002035 {
2036 *olen = 0;
2037 return;
2038 }
2039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002041 "extension" ) );
2042
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002043 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
2044 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002045
2046 *p++ = 0x00;
2047 *p++ = 0x00;
2048
2049 *olen = 4;
2050}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2054static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002055 unsigned char *buf,
2056 size_t *olen )
2057{
2058 unsigned char *p = buf;
2059
2060 if( ssl->handshake->new_session_ticket == 0 )
2061 {
2062 *olen = 0;
2063 return;
2064 }
2065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002067
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002068 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
2069 p += 2;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002070
2071 *p++ = 0x00;
2072 *p++ = 0x00;
2073
2074 *olen = 4;
2075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002079 unsigned char *buf,
2080 size_t *olen )
2081{
2082 unsigned char *p = buf;
2083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002085 {
2086 *olen = 0;
2087 return;
2088 }
2089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002091
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002092 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
2093 p += 2;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_SSL_RENEGOTIATION)
2096 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002097 {
2098 *p++ = 0x00;
2099 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2100 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002101
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002102 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2103 p += ssl->verify_data_len;
2104 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2105 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002106 }
2107 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002109 {
2110 *p++ = 0x00;
2111 *p++ = 0x01;
2112 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002113 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002114
2115 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002116}
2117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2119static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +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->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002126 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002127 *olen = 0;
2128 return;
2129 }
2130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002132
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002133 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
2134 p += 2;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002135
2136 *p++ = 0x00;
2137 *p++ = 1;
2138
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002139 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002140
2141 *olen = 5;
2142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002144
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002145#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002146 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002148 unsigned char *buf,
2149 size_t *olen )
2150{
2151 unsigned char *p = buf;
2152 ((void) ssl);
2153
Paul Bakker677377f2013-10-28 12:54:26 +01002154 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002156 {
2157 *olen = 0;
2158 return;
2159 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002162
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002163 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
2164 p += 2;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002165
2166 *p++ = 0x00;
2167 *p++ = 2;
2168
2169 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002171
2172 *olen = 6;
2173}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002174#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002175
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002176#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2177static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2178 unsigned char *buf,
2179 size_t *olen )
2180{
Janos Follath865b3eb2019-12-16 11:46:15 +00002181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002182 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002183 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002184 size_t kkpp_len;
2185
2186 *olen = 0;
2187
2188 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002189 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002190 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2191 return;
2192
2193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2194
2195 if( end - p < 4 )
2196 {
2197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2198 return;
2199 }
2200
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002201 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
2202 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002203
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002204 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2205 p + 2, end - p - 2, &kkpp_len,
2206 ssl->conf->f_rng, ssl->conf->p_rng );
2207 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002208 {
2209 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2210 return;
2211 }
2212
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002213 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
2214 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002215
2216 *olen = kkpp_len + 4;
2217}
2218#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_ALPN )
2221static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002222 unsigned char *buf, size_t *olen )
2223{
2224 if( ssl->alpn_chosen == NULL )
2225 {
2226 *olen = 0;
2227 return;
2228 }
2229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002231
2232 /*
2233 * 0 . 1 ext identifier
2234 * 2 . 3 ext length
2235 * 4 . 5 protocol list length
2236 * 6 . 6 protocol name length
2237 * 7 . 7+n protocol name
2238 */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002239 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002240
2241 *olen = 7 + strlen( ssl->alpn_chosen );
2242
Joe Subbiani6dd73642021-07-19 11:56:54 +01002243 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002244
Joe Subbiani6dd73642021-07-19 11:56:54 +01002245 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002246
Joe Subbiani2194dc42021-07-14 12:31:31 +01002247 buf[6] = MBEDTLS_BYTE_0( *olen - 7 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002248
2249 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002252
Ron Eldor3adb9922017-12-21 10:15:08 +02002253#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002254static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002255 unsigned char *buf,
2256 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002257{
Ron Eldor75870ec2018-12-06 17:31:55 +02002258 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002259 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002260 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2261
2262 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002263
Johan Pascalc3ccd982020-10-28 17:18:18 +01002264 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2265 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002266 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002267 return;
2268 }
2269
2270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2271
Johan Pascald576fdb2020-09-22 10:39:53 +02002272 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002273 {
2274 mki_len = ssl->dtls_srtp_info.mki_len;
2275 }
2276
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002277 /* The extension total size is 9 bytes :
2278 * - 2 bytes for the extension tag
2279 * - 2 bytes for the total size
2280 * - 2 bytes for the protection profile length
2281 * - 2 bytes for the protection profile
2282 * - 1 byte for the mki length
2283 * + the actual mki length
2284 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002285 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002286 {
2287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2288 return;
2289 }
2290
Johan Pascalb62bb512015-12-03 21:56:45 +01002291 /* extension */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002292 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 );
Ron Eldoref72faf2018-07-12 11:54:20 +03002293 /*
2294 * total length 5 and mki value: only one profile(2 bytes)
2295 * and length(2 bytes) and srtp_mki )
2296 */
Ron Eldor591f1622018-01-22 12:30:04 +02002297 ext_len = 5 + mki_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002298 MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 );
Johan Pascalb62bb512015-12-03 21:56:45 +01002299
2300 /* protection profile length: 2 */
2301 buf[4] = 0x00;
2302 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002303 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002304 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002305 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002306 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002307 MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002308 }
2309 else
2310 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002312 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002313 }
2314
Ron Eldor591f1622018-01-22 12:30:04 +02002315 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002316 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002317
Ron Eldor591f1622018-01-22 12:30:04 +02002318 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002319}
2320#endif /* MBEDTLS_SSL_DTLS_SRTP */
2321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2323static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002324{
Janos Follath865b3eb2019-12-16 11:46:15 +00002325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002326 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002327 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002330
2331 /*
2332 * struct {
2333 * ProtocolVersion server_version;
2334 * opaque cookie<0..2^8-1>;
2335 * } HelloVerifyRequest;
2336 */
2337
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002338 /* The RFC is not clear on this point, but sending the actual negotiated
2339 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002341 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002343 p += 2;
2344
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002345 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002346 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002350 }
2351
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002352 /* Skip length byte until we know the length */
2353 cookie_len_byte = p++;
2354
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002355 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002356 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002357 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002360 return( ret );
2361 }
2362
2363 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002366
2367 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2369 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002372
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002373 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002374 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002376 return( ret );
2377 }
2378
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002379#if defined(MBEDTLS_SSL_PROTO_DTLS)
2380 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2381 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2382 {
2383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2384 return( ret );
2385 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002386#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002389
2390 return( 0 );
2391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002393
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002394static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
Hanno Becker64ce9742021-04-15 08:19:40 +01002395{
2396 int ret;
Hanno Beckera5b1a392021-04-15 16:48:01 +01002397 mbedtls_ssl_session session_tmp;
Hanno Becker64ce9742021-04-15 08:19:40 +01002398 mbedtls_ssl_session * const session = ssl->session_negotiate;
2399
2400 /* Resume is 0 by default, see ssl_handshake_init().
2401 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2402 if( ssl->handshake->resume == 1 )
2403 return;
Hanno Becker7ad77962021-05-13 06:13:34 +01002404 if( session->id_len == 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +01002405 return;
2406 if( ssl->conf->f_get_cache == NULL )
2407 return;
2408#if defined(MBEDTLS_SSL_RENEGOTIATION)
2409 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2410 return;
2411#endif
2412
Hanno Beckera5b1a392021-04-15 16:48:01 +01002413 mbedtls_ssl_session_init( &session_tmp );
2414
Hanno Becker64ce9742021-04-15 08:19:40 +01002415 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
Hanno Beckerccdaf6e2021-04-15 09:26:17 +01002416 session->id,
2417 session->id_len,
Hanno Becker64ce9742021-04-15 08:19:40 +01002418 &session_tmp );
2419 if( ret != 0 )
2420 goto exit;
2421
2422 if( session->ciphersuite != session_tmp.ciphersuite ||
2423 session->compression != session_tmp.compression )
2424 {
2425 /* Mismatch between cached and negotiated session */
2426 goto exit;
2427 }
2428
2429 /* Move semantics */
Hanno Becker64ce9742021-04-15 08:19:40 +01002430 mbedtls_ssl_session_free( session );
2431 *session = session_tmp;
Hanno Beckerfc1f4132021-05-14 14:57:54 +01002432 memset( &session_tmp, 0, sizeof( session_tmp ) );
Hanno Becker64ce9742021-04-15 08:19:40 +01002433
2434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2435 ssl->handshake->resume = 1;
2436
2437exit:
2438
Hanno Beckerdf564022021-05-13 06:26:37 +01002439 mbedtls_ssl_session_free( &session_tmp );
Hanno Becker64ce9742021-04-15 08:19:40 +01002440}
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002445 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002446#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002448 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 unsigned char *buf, *p;
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002455 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002459
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002460 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002461 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002463
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002464 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2467 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002468 }
2469
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 /*
2471 * 0 . 0 handshake type
2472 * 1 . 3 handshake length
2473 * 4 . 5 protocol version
2474 * 6 . 9 UNIX time()
2475 * 10 . 37 random bytes
2476 */
2477 buf = ssl->out_msg;
2478 p = buf + 4;
2479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002481 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002482 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002485 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002488 t = mbedtls_time( NULL );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002489 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
2490 p += 4;
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Paul Elliottd48d5c62021-01-07 14:47:05 +00002492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2493 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002494#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002495 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002496 return( ret );
2497
2498 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002501 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002502 return( ret );
2503
2504 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Paul Bakker48916f92012-09-16 19:57:18 +00002506 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002510 ssl_handle_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002512 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 {
2514 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002515 * New session, create a new session id,
2516 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 ssl->state++;
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002521 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002522#endif
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002525 if( ssl->handshake->new_session_ticket != 0 )
2526 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002527 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002528 memset( ssl->session_negotiate->id, 0, 32 );
2529 }
2530 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002532 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002533 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002534 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002535 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002536 return( ret );
2537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 }
2539 else
2540 {
2541 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002542 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002544 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002550 return( ret );
2551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 }
2553
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002554 /*
2555 * 38 . 38 session id length
2556 * 39 . 38+n session id
2557 * 39+n . 40+n chosen ciphersuite
2558 * 41+n . 41+n chosen compression alg.
2559 * 42+n . 43+n extensions length
2560 * 44+n . 43+n+m extensions
2561 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002562 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2563 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2564 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Paul Elliottd48d5c62021-01-07 14:47:05 +00002566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002569 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002571 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
2572 p += 2;
Joe Subbianifbeb6922021-07-16 14:27:50 +01002573 *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2576 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002578 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002579
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002580 /*
2581 * First write extensions, then the total length
2582 */
2583 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2584 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002587 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2588 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002589#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002590
Hanno Beckera0e20d02019-05-15 14:03:01 +01002591#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002592 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2593 ext_len += olen;
2594#endif
2595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002597 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2598 ext_len += olen;
2599#endif
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002602 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2603 ext_len += olen;
2604#endif
2605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002607 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2608 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002609#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002610
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002611#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002612 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002613 if ( mbedtls_ssl_ciphersuite_uses_ec(
2614 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2615 {
2616 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2617 ext_len += olen;
2618 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002619#endif
2620
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002621#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2622 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2623 ext_len += olen;
2624#endif
2625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002627 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2628 ext_len += olen;
2629#endif
2630
Johan Pascalb62bb512015-12-03 21:56:45 +01002631#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01002632 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
2633 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002634#endif
2635
Paul Elliottd48d5c62021-01-07 14:47:05 +00002636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2637 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002638
Paul Bakkera7036632014-04-30 10:15:38 +02002639 if( ext_len > 0 )
2640 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002641 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani94180e72021-08-20 16:20:44 +01002642 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02002643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2647 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002649 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
2653 return( ret );
2654}
2655
Gilles Peskineeccd8882020-03-10 12:19:08 +01002656#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002659 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002660 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002663
Hanno Becker77adddc2019-02-07 12:32:43 +00002664 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002667 ssl->state++;
2668 return( 0 );
2669 }
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002673}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002674#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002676{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002678 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002679 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002680 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002681 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002683 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002685 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
2689 ssl->state++;
2690
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002691#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2692 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2693 authmode = ssl->handshake->sni_authmode;
2694 else
2695#endif
2696 authmode = ssl->conf->authmode;
2697
Hanno Becker77adddc2019-02-07 12:32:43 +00002698 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002699 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 {
Johan Pascal4f099262020-09-22 10:59:26 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2702 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
2705 /*
2706 * 0 . 0 handshake type
2707 * 1 . 3 handshake length
2708 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002709 * 5 .. m-1 cert types
2710 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002711 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 * n .. n+1 length of all DNs
2713 * n+2 .. n+3 length of DN 1
2714 * n+4 .. ... Distinguished Name #1
2715 * ... .. ... length of DN 2, etc.
2716 */
2717 buf = ssl->out_msg;
2718 p = buf + 4;
2719
2720 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002721 * Supported certificate types
2722 *
2723 * ClientCertificateType certificate_types<1..2^8-1>;
2724 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002726 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728#if defined(MBEDTLS_RSA_C)
2729 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_ECDSA_C)
2732 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002733#endif
2734
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002735 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002736 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002737
Paul Bakker577e0062013-08-28 11:57:20 +02002738 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002740 /*
2741 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002742 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002743 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2744 *
2745 * struct {
2746 * HashAlgorithm hash;
2747 * SignatureAlgorithm signature;
2748 * } SignatureAndHashAlgorithm;
2749 *
2750 * enum { (255) } HashAlgorithm;
2751 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002754 {
Simon Butcher99000142016-10-13 17:21:01 +01002755 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002756
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002757 /*
2758 * Supported signature algorithms
2759 */
Simon Butcher99000142016-10-13 17:21:01 +01002760 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
2761 {
2762 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
2763
2764 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
2765 continue;
2766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002768 p[2 + sa_len++] = hash;
2769 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002772 p[2 + sa_len++] = hash;
2773 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002774#endif
Simon Butcher99000142016-10-13 17:21:01 +01002775 }
Paul Bakker926af752012-11-23 13:38:07 +01002776
Joe Subbiani6dd73642021-07-19 11:56:54 +01002777 MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002778 sa_len += 2;
2779 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002780 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002783 /*
2784 * DistinguishedName certificate_authorities<0..2^16-1>;
2785 * opaque DistinguishedName<1..2^16-1>;
2786 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002789 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01002790
2791 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00002793 /* NOTE: If trusted certificates are provisioned
2794 * via a CA callback (configured through
2795 * `mbedtls_ssl_conf_ca_cb()`, then the
2796 * CertificateRequest is currently left empty. */
2797
Janos Follath088ce432017-04-10 12:42:31 +01002798#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2799 if( ssl->handshake->sni_ca_chain != NULL )
2800 crt = ssl->handshake->sni_ca_chain;
2801 else
2802#endif
2803 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002804
Janos Follath088ce432017-04-10 12:42:31 +01002805 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002806 {
irwirc9bc3002020-04-01 13:46:36 +03002807 /* It follows from RFC 5280 A.1 that this length
2808 * can be represented in at most 11 bits. */
2809 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01002810
irwirc9bc3002020-04-01 13:46:36 +03002811 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01002812 {
2813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2814 break;
2815 }
2816
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002817 MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 );
2818 p += 2;
Janos Follath088ce432017-04-10 12:42:31 +01002819 memcpy( p, crt->subject_raw.p, dn_size );
2820 p += dn_size;
2821
2822 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2823
2824 total_dn_size += 2 + dn_size;
2825 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002826 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 }
2828
Paul Bakker926af752012-11-23 13:38:07 +01002829 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2831 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002832 MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002834 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
2838 return( ret );
2839}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002840#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2843 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2844static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002845{
Janos Follath865b3eb2019-12-16 11:46:15 +00002846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2851 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002852 }
2853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2855 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2856 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002859 return( ret );
2860 }
2861
2862 return( 0 );
2863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2865 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002866
Gilles Peskineeccd8882020-03-10 12:19:08 +01002867#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002868 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002869static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002870 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002871{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002872 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2873 * signature length which will be added in ssl_write_server_key_exchange
2874 * after the call to ssl_prepare_server_key_exchange.
2875 * ssl_write_server_key_exchange also takes care of incrementing
2876 * ssl->out_msglen. */
2877 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10002878 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002879 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02002880 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002881 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002882 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
2883 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002884 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02002885 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002886 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01002887 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002888 return( ret );
2889}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002890#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002891 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002892
Gilles Peskined3eb0612018-01-08 17:07:44 +01002893/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02002894 * calculating the signature if any, but excluding formatting the
2895 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01002896static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
2897 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00002898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002900 ssl->handshake->ciphersuite_info;
2901
Gilles Peskineeccd8882020-03-10 12:19:08 +01002902#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
2903#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01002904 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002905#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
2906#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002907
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002908 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002909#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02002910 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002911#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002912
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002913#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef00f1522021-06-22 00:09:00 +02002914#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2915 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
2916#else
2917 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
2918#endif
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002919#endif
Gilles Peskinef00f1522021-06-22 00:09:00 +02002920
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002921 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002923 /*
2924 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002925 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002926 *
2927 */
2928
2929 /*
2930 * - ECJPAKE key exchanges
2931 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002932#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2933 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2934 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002936 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002937
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002938 ret = mbedtls_ecjpake_write_round_two(
2939 &ssl->handshake->ecjpake_ctx,
2940 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10002941 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002942 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002943 if( ret != 0 )
2944 {
2945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
2946 return( ret );
2947 }
2948
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002949 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002950 }
2951#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2952
Hanno Becker1aa267c2017-04-28 17:08:27 +01002953 /*
2954 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
2955 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
2956 * we use empty support identity hints here.
2957 **/
2958#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2960 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2961 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002962 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002963 ssl->out_msg[ssl->out_msglen++] = 0x00;
2964 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2967 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002968
Hanno Becker7e5437a2017-04-28 17:15:26 +01002969 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002970 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01002971 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002972#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002973 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00002974 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002976 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002977
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002978 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
2979 {
2980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
2981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2982 }
2983
Paul Bakker41c83d32013-03-20 14:39:14 +01002984 /*
2985 * Ephemeral DH parameters:
2986 *
2987 * struct {
2988 * opaque dh_p<1..2^16-1>;
2989 * opaque dh_g<1..2^16-1>;
2990 * opaque dh_Ys<1..2^16-1>;
2991 * } ServerDHParams;
2992 */
Hanno Beckerab740562017-10-04 13:15:37 +01002993 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
2994 &ssl->conf->dhm_P,
2995 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002996 {
Hanno Beckerab740562017-10-04 13:15:37 +01002997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01002998 return( ret );
2999 }
Paul Bakker48916f92012-09-16 19:57:18 +00003000
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003001 if( ( ret = mbedtls_dhm_make_params(
3002 &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003003 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003004 ssl->out_msg + ssl->out_msglen, &len,
3005 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003008 return( ret );
3009 }
3010
Gilles Peskineeccd8882020-03-10 12:19:08 +01003011#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003012 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003013#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003014
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003015 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3018 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3019 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3020 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003021 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003022#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003023
Hanno Becker1aa267c2017-04-28 17:08:27 +01003024 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003025 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003026 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003027#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003028 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003030 /*
3031 * Ephemeral ECDH parameters:
3032 *
3033 * struct {
3034 * ECParameters curve_params;
3035 * ECPoint public;
3036 * } ServerECDHParams;
3037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 const mbedtls_ecp_curve_info **curve = NULL;
Brett Warren01f3dae2021-08-17 13:50:51 +01003039 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Janos Follath865b3eb2019-12-16 11:46:15 +00003040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003041 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003042
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003043 /* Match our preference list against the offered curves */
Brett Warren01f3dae2021-08-17 13:50:51 +01003044 if( group_list == NULL )
3045 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
3046 for( ; *group_list != 0; group_list++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003047 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
Brett Warren01f3dae2021-08-17 13:50:51 +01003048 if( (*curve)->tls_id == *group_list )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003049 goto curve_matching_done;
3050
3051curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003052 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01003055 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Gergely Budai987bfb52014-01-19 21:48:42 +01003056 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003059
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003060 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3061 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003062 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003064 return( ret );
3065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003067 if( ( ret = mbedtls_ecdh_make_params(
3068 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003069 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003070 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003071 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003074 return( ret );
3075 }
3076
Gilles Peskineeccd8882020-03-10 12:19:08 +01003077#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003078 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003079#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003080
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003081 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003082
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003083 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3084 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003085 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003086#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Hanno Becker1aa267c2017-04-28 17:08:27 +01003088 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003089 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003090 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003091 * exchange parameters, compute and add the signature here.
3092 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003093 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003094#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003095 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003097 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003098 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003099 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003101
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003102 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003103 * 2.1: Choose hash algorithm:
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003104 * For TLS 1.2, obey signature-hash-algorithm extension
3105 * to choose appropriate hash.
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003106 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003107
3108 mbedtls_md_type_t md_alg;
3109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003111 mbedtls_pk_type_t sig_alg =
3112 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003114 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003115 /* For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003116 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003117 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003118 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3119 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003122 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003123 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003125 }
3126 }
Paul Bakker577e0062013-08-28 11:57:20 +02003127 else
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003128 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003131 }
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003132#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003133
Paul Elliott3891caf2020-12-17 18:42:40 +00003134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003135
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003136 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003137 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003138 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003139#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003141 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003142 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003143 dig_signed,
3144 dig_signed_len,
3145 md_alg );
3146 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003147 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003148 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003149 else
TRodziewicz0f82ec62021-05-12 17:49:18 +02003150#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003154 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003155
Gilles Peskineebd652f2018-01-05 21:18:59 +01003156 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003157
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003158 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003159 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3162 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003163 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003164 /*
3165 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003166 * explicitly through a prefix to the signature.
3167 *
3168 * struct {
3169 * HashAlgorithm hash;
3170 * SignatureAlgorithm signature;
3171 * } SignatureAndHashAlgorithm;
3172 *
3173 * struct {
3174 * SignatureAndHashAlgorithm algorithm;
3175 * opaque signature<0..2^16-1>;
3176 * } DigitallySigned;
3177 *
3178 */
3179
Gilles Peskine1004c192018-01-08 16:59:14 +01003180 ssl->out_msg[ssl->out_msglen++] =
3181 mbedtls_ssl_hash_from_md_alg( md_alg );
3182 ssl->out_msg[ssl->out_msglen++] =
3183 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003184 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003186
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003187#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003188 if( ssl->conf->f_async_sign_start != NULL )
3189 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003190 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003191 mbedtls_ssl_own_cert( ssl ),
3192 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003193 switch( ret )
3194 {
3195 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3196 /* act as if f_async_sign was null */
3197 break;
3198 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003199 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003200 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003201 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003202 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003203 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3204 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003205 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003206 return( ret );
3207 }
3208 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003209#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003210
3211 if( mbedtls_ssl_own_key( ssl ) == NULL )
3212 {
3213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3214 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3215 }
3216
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003217 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3218 * signature length which will be added in ssl_write_server_key_exchange
3219 * after the call to ssl_prepare_server_key_exchange.
3220 * ssl_write_server_key_exchange also takes care of incrementing
3221 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003222 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3223 md_alg, hash, hashlen,
3224 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003225 out_buf_len - ssl->out_msglen - 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003226 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003227 ssl->conf->f_rng,
3228 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003231 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003232 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003233 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003234#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003235
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003236 return( 0 );
3237}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003238
Gilles Peskined3eb0612018-01-08 17:07:44 +01003239/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003240 * that do not include a ServerKeyExchange message, do nothing. Either
3241 * way, if successful, move on to the next step in the SSL state
3242 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003243static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3244{
Janos Follath865b3eb2019-12-16 11:46:15 +00003245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003246 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003247#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003248 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003249 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003250#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003251
Gilles Peskined3eb0612018-01-08 17:07:44 +01003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3253
Gilles Peskineeccd8882020-03-10 12:19:08 +01003254#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003255 /* Extract static ECDH parameters and abort if ServerKeyExchange
3256 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003257 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3258 {
3259 /* For suites involving ECDH, extract DH parameters
3260 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003261#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003262 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3263 {
3264 ssl_get_ecdh_params_from_cert( ssl );
3265 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003266#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003267
3268 /* Key exchanges not involving ephemeral keys don't use
3269 * ServerKeyExchange, so end here. */
3270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3271 ssl->state++;
3272 return( 0 );
3273 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003274#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003275
Gilles Peskineeccd8882020-03-10 12:19:08 +01003276#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003277 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003278 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003279 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003280 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003281 {
3282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3283 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003284 }
3285 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003286#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003287 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003288 {
3289 /* ServerKeyExchange is needed. Prepare the message. */
3290 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003291 }
3292
3293 if( ret != 0 )
3294 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003295 /* If we're starting to write a new message, set ssl->out_msglen
3296 * to 0. But if we're resuming after an asynchronous message,
3297 * out_msglen is the amount of data written so far and mst be
3298 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003299 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3301 else
3302 ssl->out_msglen = 0;
3303 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003304 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003305
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003306 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003307 * ssl_prepare_server_key_exchange already wrote the signature
3308 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003309#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003310 if( signature_len != 0 )
3311 {
Joe Subbianifbeb6922021-07-16 14:27:50 +01003312 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len );
3313 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len );
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003314
3315 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3316 ssl->out_msg + ssl->out_msglen,
3317 signature_len );
3318
3319 /* Skip over the already-written signature */
3320 ssl->out_msglen += signature_len;
3321 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003322#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003323
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003324 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3326 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003327
3328 ssl->state++;
3329
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003330 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 return( ret );
3334 }
3335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003338}
3339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003341{
Janos Follath865b3eb2019-12-16 11:46:15 +00003342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003345
3346 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3348 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349
3350 ssl->state++;
3351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003355#endif
3356
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003357 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 return( ret );
3361 }
3362
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003363#if defined(MBEDTLS_SSL_PROTO_DTLS)
3364 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3365 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3366 {
3367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3368 return( ret );
3369 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003370#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
3374 return( 0 );
3375}
3376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3378 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3379static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003380 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003383 size_t n;
3384
3385 /*
3386 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3387 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003388 if( *p + 2 > end )
3389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003391 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003392 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003393
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003394 n = ( (*p)[0] << 8 ) | (*p)[1];
3395 *p += 2;
3396
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003397 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003400 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003401 }
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003406 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003407 }
3408
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003409 *p += n;
3410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003412
Paul Bakker70df2fb2013-04-17 17:19:09 +02003413 return( ret );
3414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3416 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3419 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003420
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003421#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003422static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3423 unsigned char *peer_pms,
3424 size_t *peer_pmslen,
3425 size_t peer_pmssize )
3426{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003427 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003428 peer_pms, peer_pmslen, peer_pmssize );
3429 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3430 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003431 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003432 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003433 }
3434 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3435 return( ret );
3436}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003437#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003438
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003439static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3440 const unsigned char *p,
3441 const unsigned char *end,
3442 unsigned char *peer_pms,
3443 size_t *peer_pmslen,
3444 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003445{
Janos Follath865b3eb2019-12-16 11:46:15 +00003446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003447 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3448 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3449 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003450
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003451#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003452 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003453 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003454 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003455 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3457 return( ssl_resume_decrypt_pms( ssl,
3458 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003459 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003460#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003461
3462 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003463 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003464 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003465#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003466 if ( p + 2 > end ) {
3467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003468 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003469 }
Joe Subbiani2194dc42021-07-14 12:31:31 +01003470 if( *p++ != MBEDTLS_BYTE_1( len ) ||
3471 *p++ != MBEDTLS_BYTE_0( len ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003472 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003474 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003475 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003476#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003477
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003478 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003481 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003482 }
3483
Gilles Peskine422ccab2018-01-11 18:29:01 +01003484 /*
3485 * Decrypt the premaster secret
3486 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003487#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003488 if( ssl->conf->f_async_decrypt_start != NULL )
3489 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003490 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003491 mbedtls_ssl_own_cert( ssl ),
3492 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003493 switch( ret )
3494 {
3495 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3496 /* act as if f_async_decrypt_start was null */
3497 break;
3498 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003499 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003500 return( ssl_resume_decrypt_pms( ssl,
3501 peer_pms,
3502 peer_pmslen,
3503 peer_pmssize ) );
3504 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003505 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003506 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3507 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003508 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003509 return( ret );
3510 }
3511 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003512#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003513
Gilles Peskine422ccab2018-01-11 18:29:01 +01003514 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3515 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3517 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3518 }
3519
3520 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003521 peer_pms, peer_pmslen, peer_pmssize,
3522 ssl->conf->f_rng, ssl->conf->p_rng );
3523 return( ret );
3524}
3525
3526static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3527 const unsigned char *p,
3528 const unsigned char *end,
3529 size_t pms_offset )
3530{
Janos Follath865b3eb2019-12-16 11:46:15 +00003531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003532 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3533 unsigned char ver[2];
3534 unsigned char fake_pms[48], peer_pms[48];
3535 unsigned char mask;
3536 size_t i, peer_pmslen;
3537 unsigned int diff;
3538
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003539 /* In case of a failure in decryption, the decryption may write less than
3540 * 2 bytes of output, but we always read the first two bytes. It doesn't
3541 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003542 * ret being nonzero, and we only care whether diff is 0.
3543 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3544 * also makes memory analyzers happy (don't access uninitialized memory,
3545 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003546 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003547 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003548
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003549 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3550 peer_pms,
3551 &peer_pmslen,
3552 sizeof( peer_pms ) );
3553
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003554#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003555 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3556 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003557#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003560 ssl->handshake->max_minor_ver,
3561 ssl->conf->transport, ver );
3562
3563 /* Avoid data-dependent branches while checking for invalid
3564 * padding, to protect against timing-based Bleichenbacher-type
3565 * attacks. */
3566 diff = (unsigned int) ret;
3567 diff |= peer_pmslen ^ 48;
3568 diff |= peer_pms[0] ^ ver[0];
3569 diff |= peer_pms[1] ^ ver[1];
3570
3571 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3572 /* MSVC has a warning about unary minus on unsigned, but this is
3573 * well-defined and precisely what we want to do here */
3574#if defined(_MSC_VER)
3575#pragma warning( push )
3576#pragma warning( disable : 4146 )
3577#endif
3578 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3579#if defined(_MSC_VER)
3580#pragma warning( pop )
3581#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003582
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003583 /*
3584 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3585 * must not cause the connection to end immediately; instead, send a
3586 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003587 * To protect against timing-based variants of the attack, we must
3588 * not have any branch that depends on whether the decryption was
3589 * successful. In particular, always generate the fake premaster secret,
3590 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003591 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003592 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003593 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003594 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003595 /* It's ok to abort on an RNG failure, since this does not reveal
3596 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003597 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003598 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003599
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003600#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003601 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003603#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003604
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003605 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3606 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3609 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003610 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003611 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003612
Gilles Peskine422ccab2018-01-11 18:29:01 +01003613 /* Set pms to either the true or the fake PMS, without
3614 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003615 for( i = 0; i < ssl->handshake->pmslen; i++ )
3616 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3617
3618 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003619}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3621 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003622
Gilles Peskineeccd8882020-03-10 12:19:08 +01003623#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003625 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003626{
Paul Bakker6db455e2013-09-18 17:29:31 +02003627 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003628 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003629
Hanno Becker845b9462018-10-26 12:07:29 +01003630 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3633 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003634 }
3635
3636 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003637 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003638 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003639 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003642 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003643 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003644
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003645 n = ( (*p)[0] << 8 ) | (*p)[1];
3646 *p += 2;
3647
irwir6527bd62019-09-21 18:51:25 +03003648 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003651 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003652 }
3653
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003654 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003655 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003656 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003658 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003659 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003660 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003661 /* Identity is not a big secret since clients send it in the clear,
3662 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003663 if( n != ssl->conf->psk_identity_len ||
3664 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003667 }
3668 }
3669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003673 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3674 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003676 }
3677
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003678 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003679
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003680 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003681}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003682#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003685{
Janos Follath865b3eb2019-12-16 11:46:15 +00003686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003688 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003689
Hanno Beckere694c3e2017-12-27 21:34:08 +00003690 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003694#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003695 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3696 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3697 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3698 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003699 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003700 {
3701 /* We've already read a record and there is an asynchronous
3702 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003703 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3705 }
3706 else
3707#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01003708 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 return( ret );
3712 }
3713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003715 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003720 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 }
3722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003726 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 }
3728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3730 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003732 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003735 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003737
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003738 if( p != end )
3739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003741 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003742 }
3743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003745 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01003746 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003747 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003748 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003751 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003752 }
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003755 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003756 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3758#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3759 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3760 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3761 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3762 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3763 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3764 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3765 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003768 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003771 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003772 }
3773
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003774 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3775 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003778 &ssl->handshake->pmslen,
3779 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003781 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003784 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003785 }
3786
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003787 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3788 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003789 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3792 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3793 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3794 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3795#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3796 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003797 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003798 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003801 return( ret );
3802 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003803
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003804 if( p != end )
3805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003807 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003808 }
3809
Hanno Becker845b9462018-10-26 12:07:29 +01003810#if defined(MBEDTLS_USE_PSA_CRYPTO)
3811 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
3812 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00003813 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01003814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
3815 else
3816#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003818 ciphersuite_info->key_exchange ) ) != 0 )
3819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003821 return( ret );
3822 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003824 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3826#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3827 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003828 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003829#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003830 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003831 {
3832 /* There is an asynchronous operation in progress to
3833 * decrypt the encrypted premaster secret, so skip
3834 * directly to resuming this operation. */
3835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
3836 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3837 * won't actually use it, but maintain p anyway for robustness. */
3838 p += ssl->conf->psk_identity_len + 2;
3839 }
3840 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003841#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003842 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003845 return( ret );
3846 }
3847
Hanno Becker845b9462018-10-26 12:07:29 +01003848#if defined(MBEDTLS_USE_PSA_CRYPTO)
3849 /* Opaque PSKs are currently only supported for PSK-only. */
3850 if( ssl_use_opaque_psk( ssl ) == 1 )
3851 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3852#endif
3853
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003854 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003857 return( ret );
3858 }
3859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003861 ciphersuite_info->key_exchange ) ) != 0 )
3862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003864 return( ret );
3865 }
3866 }
3867 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3869#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3870 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003871 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003872 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003875 return( ret );
3876 }
3877 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003880 return( ret );
3881 }
3882
Hanno Becker845b9462018-10-26 12:07:29 +01003883#if defined(MBEDTLS_USE_PSA_CRYPTO)
3884 /* Opaque PSKs are currently only supported for PSK-only. */
3885 if( ssl_use_opaque_psk( ssl ) == 1 )
3886 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3887#endif
3888
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003889 if( p != end )
3890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003892 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003893 }
3894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003896 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003899 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003900 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003901 }
3902 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3904#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3905 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003906 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003907 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003910 return( ret );
3911 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003914 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003917 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003918 }
3919
Hanno Becker845b9462018-10-26 12:07:29 +01003920#if defined(MBEDTLS_USE_PSA_CRYPTO)
3921 /* Opaque PSKs are currently only supported for PSK-only. */
3922 if( ssl_use_opaque_psk( ssl ) == 1 )
3923 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3924#endif
3925
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003926 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3927 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003930 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003933 return( ret );
3934 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003935 }
3936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3938#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3939 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003940 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003941 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003944 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003945 }
3946 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003947 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003949#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3950 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3951 {
3952 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3953 p, end - p );
3954 if( ret != 0 )
3955 {
3956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003958 }
3959
3960 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3961 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3962 ssl->conf->f_rng, ssl->conf->p_rng );
3963 if( ret != 0 )
3964 {
3965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3966 return( ret );
3967 }
3968 }
3969 else
3970#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00003977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00003979 return( ret );
3980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003981
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 ssl->state++;
3983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003985
3986 return( 0 );
3987}
3988
Gilles Peskineeccd8882020-03-10 12:19:08 +01003989#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003991{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003992 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003993 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
Hanno Becker77adddc2019-02-07 12:32:43 +00003997 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004000 ssl->state++;
4001 return( 0 );
4002 }
4003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004006}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004007#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004011 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004012 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004013 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004014 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4016 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004017#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004019 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004020 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004021 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004024
Hanno Becker2a831a42019-02-07 13:17:25 +00004025 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004028 ssl->state++;
4029 return( 0 );
4030 }
4031
Hanno Becker2a831a42019-02-07 13:17:25 +00004032#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4033 if( ssl->session_negotiate->peer_cert == NULL )
4034 {
4035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4036 ssl->state++;
4037 return( 0 );
4038 }
4039#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4040 if( ssl->session_negotiate->peer_cert_digest == NULL )
4041 {
4042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4043 ssl->state++;
4044 return( 0 );
4045 }
4046#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4047
Simon Butcher99000142016-10-13 17:21:01 +01004048 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004049 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004050 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004051 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004052 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 return( ret );
4054 }
4055
4056 ssl->state++;
4057
Simon Butcher99000142016-10-13 17:21:01 +01004058 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4060 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004063 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004064 }
4065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004067
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004068#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4069 peer_pk = &ssl->handshake->peer_pubkey;
4070#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4071 if( ssl->session_negotiate->peer_cert == NULL )
4072 {
4073 /* Should never happen */
4074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4075 }
4076 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4077#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4078
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004079 /*
4080 * struct {
4081 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4082 * opaque signature<0..2^16-1>;
4083 * } DigitallySigned;
4084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4086 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004087 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004088 if( i + 2 > ssl->in_hslen )
4089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004091 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004092 }
4093
Paul Bakker5121ce52009-01-03 21:22:43 +00004094 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004095 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004096 */
Simon Butcher99000142016-10-13 17:21:01 +01004097 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4098
4099 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004102 " for verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004103 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 }
4105
Simon Butcher99000142016-10-13 17:21:01 +01004106#if !defined(MBEDTLS_MD_SHA1)
4107 if( MBEDTLS_MD_SHA1 == md_alg )
4108 hash_start += 16;
4109#endif
Paul Bakker926af752012-11-23 13:38:07 +01004110
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004111 /* Info from md_alg will be used instead */
4112 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004113
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004114 i++;
4115
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004116 /*
4117 * Signature
4118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4120 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004123 " for verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004124 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004125 }
4126
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004127 /*
4128 * Check the certificate's key type matches the signature alg
4129 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004130 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004133 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004134 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004135
4136 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004137 }
4138 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004143 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004144
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004145 if( i + 2 > ssl->in_hslen )
4146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004148 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004149 }
4150
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004151 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4152 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004153
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004154 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004157 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00004158 }
4159
Simon Butcher99000142016-10-13 17:21:01 +01004160 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004161 {
4162 size_t dummy_hlen;
4163 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4164 }
Simon Butcher99000142016-10-13 17:21:01 +01004165
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004166 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004167 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004168 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004171 return( ret );
4172 }
4173
Simon Butcher99000142016-10-13 17:21:01 +01004174 mbedtls_ssl_update_handshake_status( ssl );
4175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004177
Paul Bakkered27a042013-04-18 22:46:23 +02004178 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004179}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004180#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4183static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004184{
Janos Follath865b3eb2019-12-16 11:46:15 +00004185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004186 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004187 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4192 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004193
4194 /*
4195 * struct {
4196 * uint32 ticket_lifetime_hint;
4197 * opaque ticket<0..2^16-1>;
4198 * } NewSessionTicket;
4199 *
4200 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4201 * 8 . 9 ticket_len (n)
4202 * 10 . 9+n ticket content
4203 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004204
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004205 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004206 ssl->session_negotiate,
4207 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004208 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004209 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004210 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004212 tlen = 0;
4213 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004214
Joe Subbiani6dd73642021-07-19 11:56:54 +01004215 MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 );
4216 MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004217 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004218
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004219 /*
4220 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4221 * ChangeCipherSpec share the same state.
4222 */
4223 ssl->handshake->new_session_ticket = 0;
4224
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004225 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004226 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004228 return( ret );
4229 }
4230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004232
4233 return( 0 );
4234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004236
Paul Bakker5121ce52009-01-03 21:22:43 +00004237/*
Paul Bakker1961b702013-01-25 14:49:24 +01004238 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004241{
4242 int ret = 0;
4243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004245
Paul Bakker1961b702013-01-25 14:49:24 +01004246 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 case MBEDTLS_SSL_HELLO_REQUEST:
4249 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 break;
4251
Paul Bakker1961b702013-01-25 14:49:24 +01004252 /*
4253 * <== ClientHello
4254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004256 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_SSL_PROTO_DTLS)
4260 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4261 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004262#endif
4263
Paul Bakker1961b702013-01-25 14:49:24 +01004264 /*
4265 * ==> ServerHello
4266 * Certificate
4267 * ( ServerKeyExchange )
4268 * ( CertificateRequest )
4269 * ServerHelloDone
4270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004272 ret = ssl_write_server_hello( ssl );
4273 break;
4274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4276 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004277 break;
4278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004280 ret = ssl_write_server_key_exchange( ssl );
4281 break;
4282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004284 ret = ssl_write_certificate_request( ssl );
4285 break;
4286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004288 ret = ssl_write_server_hello_done( ssl );
4289 break;
4290
4291 /*
4292 * <== ( Certificate/Alert )
4293 * ClientKeyExchange
4294 * ( CertificateVerify )
4295 * ChangeCipherSpec
4296 * Finished
4297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4299 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004300 break;
4301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004303 ret = ssl_parse_client_key_exchange( ssl );
4304 break;
4305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004307 ret = ssl_parse_certificate_verify( ssl );
4308 break;
4309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4311 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004312 break;
4313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 case MBEDTLS_SSL_CLIENT_FINISHED:
4315 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004316 break;
4317
4318 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004319 * ==> ( NewSessionTicket )
4320 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004321 * Finished
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4324#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004325 if( ssl->handshake->new_session_ticket != 0 )
4326 ret = ssl_write_new_session_ticket( ssl );
4327 else
Paul Bakkera503a632013-08-14 13:48:06 +02004328#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004330 break;
4331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 case MBEDTLS_SSL_SERVER_FINISHED:
4333 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004334 break;
4335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 case MBEDTLS_SSL_FLUSH_BUFFERS:
4337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4338 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004339 break;
4340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4342 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004343 break;
4344
4345 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 }
4349
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 return( ret );
4351}
TRodziewicz8476f2f2021-06-02 14:34:47 +02004352
TRodziewicz3946f792021-06-14 12:11:18 +02004353void mbedtls_ssl_conf_preference_order( mbedtls_ssl_config *conf, int order )
TRodziewicz8476f2f2021-06-02 14:34:47 +02004354{
TRodziewicz3946f792021-06-14 12:11:18 +02004355 conf->respect_cli_pref = order;
TRodziewicz8476f2f2021-06-02 14:34:47 +02004356}
4357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358#endif /* MBEDTLS_SSL_SRV_C */