blob: 977ecc8f3366ca7a2e6bb233cc93c66a176ddd50 [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"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020037#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020038#include "mbedtls/constant_time.h"
Rich Evans00ab4702015-02-06 13:43:58 +000039
40#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010047#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
51int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020052 const unsigned char *info,
53 size_t ilen )
54{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020055 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020059
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020061 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020062
63 memcpy( ssl->cli_id, info, ilen );
64 ssl->cli_id_len = ilen;
65
66 return( 0 );
67}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020068
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020069void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_ssl_cookie_write_t *f_cookie_write,
71 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020072 void *p_cookie )
73{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020074 conf->f_cookie_write = f_cookie_write;
75 conf->f_cookie_check = f_cookie_check;
76 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000082 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000083 size_t len )
84{
Janos Follath865b3eb2019-12-16 11:46:15 +000085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000086 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000087 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010090
Philippe Antoine747fd532018-05-30 09:13:21 +020091 if( len < 2 )
92 {
93 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
94 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
95 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +010096 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +020097 }
Paul Bakker5701cdc2012-09-27 21:49:42 +000098 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
99 if( servername_list_size + 2 != len )
100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
103 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100104 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000105 }
106
107 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200108 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000109 {
110 hostname_len = ( ( p[1] << 8 ) | p[2] );
111 if( hostname_len + 3 > servername_list_size )
112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200114 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
115 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100116 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000117 }
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000120 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200121 ret = ssl->conf->f_sni( ssl->conf->p_sni,
122 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000123 if( ret != 0 )
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
126 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
127 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100128 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000129 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000130 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000131 }
132
133 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000134 p += hostname_len + 3;
135 }
136
137 if( servername_list_size != 0 )
138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100141 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
142 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000143 }
144
145 return( 0 );
146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000148
Gilles Peskineeccd8882020-03-10 12:19:08 +0100149#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100150static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
151{
152 if( conf->f_psk != NULL )
153 return( 1 );
154
155 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
156 return( 0 );
157
158 if( conf->psk != NULL && conf->psk_len != 0 )
159 return( 1 );
160
161#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +0200162 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100163 return( 1 );
164#endif /* MBEDTLS_USE_PSA_CRYPTO */
165
166 return( 0 );
167}
168
169#if defined(MBEDTLS_USE_PSA_CRYPTO)
170static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
171{
172 if( ssl->conf->f_psk != NULL )
173 {
174 /* If we've used a callback to select the PSK,
175 * the static configuration is irrelevant. */
176
Ronald Croncf56a0a2020-08-04 09:51:30 +0200177 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100178 return( 1 );
179
180 return( 0 );
181 }
182
Ronald Croncf56a0a2020-08-04 09:51:30 +0200183 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100184 return( 1 );
185
186 return( 0 );
187}
188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100189#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000192 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000193 size_t len )
194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_SSL_RENEGOTIATION)
196 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100197 {
198 /* Check verify-data in constant-time. The length OTOH is no secret */
199 if( len != 1 + ssl->verify_data_len ||
200 buf[0] != ssl->verify_data_len ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200201 mbedtls_ct_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100202 ssl->verify_data_len ) != 0 )
203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200205 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
206 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100207 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100208 }
209 }
210 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000212 {
213 if( len != 1 || buf[0] != 0x0 )
214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200216 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
217 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100218 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000219 }
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000222 }
Paul Bakker48916f92012-09-16 19:57:18 +0000223
224 return( 0 );
225}
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100228 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100229
230/*
231 * Status of the implementation of signature-algorithms extension:
232 *
233 * Currently, we are only considering the signature-algorithm extension
234 * to pick a ciphersuite which allows us to send the ServerKeyExchange
235 * message with a signature-hash combination that the user allows.
236 *
237 * We do *not* check whether all certificates in our certificate
238 * chain are signed with an allowed signature-hash pair.
239 * This needs to be done at a later stage.
240 *
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000243 const unsigned char *buf,
244 size_t len )
245{
246 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100247
Paul Bakker23f36802012-09-28 14:15:14 +0000248 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200249 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200250
Hanno Becker7e5437a2017-04-28 17:15:26 +0100251 mbedtls_md_type_t md_cur;
252 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000253
Philippe Antoine747fd532018-05-30 09:13:21 +0200254 if ( len < 2 ) {
255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
256 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
257 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100258 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +0200259 }
Paul Bakker23f36802012-09-28 14:15:14 +0000260 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
261 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200262 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
266 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100267 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker23f36802012-09-28 14:15:14 +0000268 }
269
Hanno Becker7e5437a2017-04-28 17:15:26 +0100270 /* Currently we only guarantee signing the ServerKeyExchange message according
271 * to the constraints specified in this extension (see above), so it suffices
272 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200273 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100274 * This will change when we also consider certificate signatures,
275 * in which case we will need to remember the whole signature-hash
276 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200277 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100278
279 for( p = buf + 2; p < end; p += 2 )
280 {
281 /* Silently ignore unknown signature or hash algorithms. */
282
283 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
284 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
286 " unknown sig alg encoding %d", p[1] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100287 continue;
288 }
289
290 /* Check if we support the hash the user proposes */
291 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
292 if( md_cur == MBEDTLS_MD_NONE )
293 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
295 " unknown hash alg encoding %d", p[0] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100296 continue;
297 }
298
299 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
300 {
301 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
Paul Elliott9f352112020-12-09 14:55:45 +0000303 " match sig %u and hash %u",
Paul Elliott3891caf2020-12-17 18:42:40 +0000304 (unsigned) sig_cur, (unsigned) md_cur ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100305 }
306 else
307 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
Paul Elliott3891caf2020-12-17 18:42:40 +0000309 "hash alg %u not supported", (unsigned) md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000310 }
Paul Bakker23f36802012-09-28 14:15:14 +0000311 }
312
Paul Bakker23f36802012-09-28 14:15:14 +0000313 return( 0 );
314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100316 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000317
Robert Cragie136884c2015-10-02 13:34:31 +0100318#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100319 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yub47d0f82021-12-20 17:34:40 +0800320static int ssl_parse_supported_groups_ext( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200321 const unsigned char *buf,
322 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100323{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200324 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100325 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100327
Philippe Antoine747fd532018-05-30 09:13:21 +0200328 if ( len < 2 ) {
329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
330 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
331 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100332 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +0200333 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100334 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
335 if( list_size + 2 != len ||
336 list_size % 2 != 0 )
337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
340 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100341 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +0100342 }
343
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200344 /* Should never happen unless client duplicates the extension */
345 if( ssl->handshake->curves != NULL )
346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200348 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100349 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
350 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200351 }
352
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100353 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200354 * and leave room for a final 0 */
355 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( our_size > MBEDTLS_ECP_DP_MAX )
357 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200358
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200359 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200360 {
361 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
362 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200363 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200364 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200365
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200366 ssl->handshake->curves = curves;
367
Paul Bakker41c83d32013-03-20 14:39:14 +0100368 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200369 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200372
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200373 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100374 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200375 *curves++ = curve_info;
376 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100377 }
378
379 list_size -= 2;
380 p += 2;
381 }
382
383 return( 0 );
384}
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200387 const unsigned char *buf,
388 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100389{
390 size_t list_size;
391 const unsigned char *p;
392
Philippe Antoine747fd532018-05-30 09:13:21 +0200393 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200396 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
397 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100398 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +0100399 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200400 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100401
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200402 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100403 while( list_size > 0 )
404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
406 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100407 {
Robert Cragie136884c2015-10-02 13:34:31 +0100408#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200409 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100410#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100411#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200412 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
413 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100416 return( 0 );
417 }
418
419 list_size--;
420 p++;
421 }
422
423 return( 0 );
424}
Robert Cragieae8535d2015-10-06 17:11:18 +0100425#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
426 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100427
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200428#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
429static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
430 const unsigned char *buf,
431 size_t len )
432{
Janos Follath865b3eb2019-12-16 11:46:15 +0000433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200434
435 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
436 {
437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
438 return( 0 );
439 }
440
441 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
442 buf, len ) ) != 0 )
443 {
444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200445 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
446 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200447 return( ret );
448 }
449
450 /* Only mark the extension as OK when we're sure it is */
451 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
452
453 return( 0 );
454}
455#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
458static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200459 const unsigned char *buf,
460 size_t len )
461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200465 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
466 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100467 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200468 }
469
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200470 ssl->session_negotiate->mfl_code = buf[0];
471
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200472 return( 0 );
473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200475
Hanno Beckera0e20d02019-05-15 14:03:01 +0100476#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100477static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
478 const unsigned char *buf,
479 size_t len )
480{
481 size_t peer_cid_len;
482
483 /* CID extension only makes sense in DTLS */
484 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
485 {
486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
487 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
488 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100489 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100490 }
491
492 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100493 * Quoting draft-ietf-tls-dtls-connection-id-05
494 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100495 *
496 * struct {
497 * opaque cid<0..2^8-1>;
498 * } ConnectionId;
499 */
500
501 if( len < 1 )
502 {
503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
504 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100505 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
506 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100507 }
508
509 peer_cid_len = *buf++;
510 len--;
511
512 if( len != peer_cid_len )
513 {
514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
515 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100516 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
517 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100518 }
519
520 /* Ignore CID if the user has disabled its use. */
521 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
522 {
523 /* Leave ssl->handshake->cid_in_use in its default
524 * value of MBEDTLS_SSL_CID_DISABLED. */
525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
526 return( 0 );
527 }
528
529 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
530 {
531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
532 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
533 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100534 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100535 }
536
Hanno Becker08556bf2019-05-03 12:43:44 +0100537 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100538 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
539 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
540
541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
542 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
543
Hanno Becker89dcc882019-04-26 13:56:39 +0100544 return( 0 );
545}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100546#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
549static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100550 const unsigned char *buf,
551 size_t len )
552{
553 if( len != 0 )
554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200556 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
557 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100558 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100559 }
560
561 ((void) buf);
562
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100563 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100566 }
567
568 return( 0 );
569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
573static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200574 const unsigned char *buf,
575 size_t len )
576{
577 if( len != 0 )
578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200580 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
581 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100582 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200583 }
584
585 ((void) buf);
586
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100587 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200590 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200591
592 return( 0 );
593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
597static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200598 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200599 size_t len )
600{
Janos Follath865b3eb2019-12-16 11:46:15 +0000601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200602 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200603
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200604 mbedtls_ssl_session_init( &session );
605
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200606 if( ssl->conf->f_ticket_parse == NULL ||
607 ssl->conf->f_ticket_write == NULL )
608 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200609 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200610 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200611
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200612 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200613 ssl->handshake->new_session_ticket = 1;
614
Paul Elliottd48d5c62021-01-07 14:47:05 +0000615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200616
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200617 if( len == 0 )
618 return( 0 );
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SSL_RENEGOTIATION)
621 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200624 return( 0 );
625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200627
628 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200629 * Failures are ok: just ignore the ticket and proceed.
630 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200631 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
632 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200633 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200634 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200635
636 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
638 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
640 else
641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
642
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200643 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200644 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200645
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200646 /*
647 * Keep the session ID sent by the client, since we MUST send it back to
648 * inform them we're accepting the ticket (RFC 5077 section 3.4)
649 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200650 session.id_len = ssl->session_negotiate->id_len;
651 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200652
653 mbedtls_ssl_session_free( ssl->session_negotiate );
654 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
655
656 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500657 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200660
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200661 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200662
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200663 /* Don't send a new ticket after all, this one is OK */
664 ssl->handshake->new_session_ticket = 0;
665
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200666 return( 0 );
667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SSL_ALPN)
671static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200672 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200673{
Paul Bakker14b16c62014-05-28 11:33:54 +0200674 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200675 const unsigned char *theirs, *start, *end;
676 const char **ours;
677
678 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200679 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200680 return( 0 );
681
682 /*
683 * opaque ProtocolName<1..2^8-1>;
684 *
685 * struct {
686 * ProtocolName protocol_name_list<2..2^16-1>
687 * } ProtocolNameList;
688 */
689
690 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
691 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200692 {
693 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
694 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100695 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200696 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200697
698 list_len = ( buf[0] << 8 ) | buf[1];
699 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200700 {
701 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100703 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200704 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200705
706 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100707 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200708 */
709 start = buf + 2;
710 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100711 for( theirs = start; theirs != end; theirs += cur_len )
712 {
713 cur_len = *theirs++;
714
715 /* Current identifier must fit in list */
716 if( cur_len > (size_t)( end - theirs ) )
717 {
718 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
719 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100720 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100721 }
722
723 /* Empty strings MUST NOT be included */
724 if( cur_len == 0 )
725 {
726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
727 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100728 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100729 }
730 }
731
732 /*
733 * Use our order of preference
734 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200735 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200736 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200737 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200738 for( theirs = start; theirs != end; theirs += cur_len )
739 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200740 cur_len = *theirs++;
741
Paul Bakker14b16c62014-05-28 11:33:54 +0200742 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200743 memcmp( theirs, *ours, cur_len ) == 0 )
744 {
745 ssl->alpn_chosen = *ours;
746 return( 0 );
747 }
748 }
749 }
750
751 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
753 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
Dave Rodgman53c86892021-06-29 10:02:06 +0100754 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200757
Johan Pascalb62bb512015-12-03 21:56:45 +0100758#if defined(MBEDTLS_SSL_DTLS_SRTP)
759static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300760 const unsigned char *buf,
761 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100762{
Johan Pascal43f94902020-09-22 12:25:52 +0200763 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100764 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200765 size_t profile_length;
766 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200767 /*! 2 bytes for profile length and 1 byte for mki len */
768 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100769
770 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100771 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
772 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
773 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200774 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100775 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200776 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100777
778 /* RFC5764 section 4.1.1
779 * uint8 SRTPProtectionProfile[2];
780 *
781 * struct {
782 * SRTPProtectionProfiles SRTPProtectionProfiles;
783 * opaque srtp_mki<0..255>;
784 * } UseSRTPData;
785
786 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100787 */
788
Ron Eldoref72faf2018-07-12 11:54:20 +0300789 /*
790 * Min length is 5: at least one protection profile(2 bytes)
791 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200792 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200793 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300794 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200795 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200796 {
797 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100798 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
799 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200800 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100801
Johan Pascal43f94902020-09-22 12:25:52 +0200802 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200803
Ron Eldoref72faf2018-07-12 11:54:20 +0300804 /* first 2 bytes are protection profile length(in bytes) */
805 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200806 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200807
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200808 /* The profile length cannot be bigger than input buffer size - lengths fields */
809 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200810 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200811 {
812 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100813 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
814 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200815 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300816 /*
817 * parse the extension list values are defined in
818 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
819 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200820 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100821 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200822 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200823 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100824
Johan Pascal43f94902020-09-22 12:25:52 +0200825 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300826 {
Johan Pascal43f94902020-09-22 12:25:52 +0200827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
828 mbedtls_ssl_get_srtp_profile_as_string(
829 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300830 }
Johan Pascal85269572020-08-25 10:01:54 +0200831 else
832 {
833 continue;
834 }
Ron Eldor591f1622018-01-22 12:30:04 +0200835 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200836 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200837 {
838 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
839 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200840 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
842 mbedtls_ssl_get_srtp_profile_as_string(
843 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200844 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100845 }
846 }
Johan Pascal43f94902020-09-22 12:25:52 +0200847 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200848 break;
849 }
Johan Pascal042d4562020-08-25 12:14:02 +0200850 buf += profile_length; /* buf points to the mki length */
851 mki_length = *buf;
852 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200853
Johan Pascal042d4562020-08-25 12:14:02 +0200854 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
855 mki_length + profile_length + size_of_lengths != len )
856 {
857 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100858 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
859 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascal042d4562020-08-25 12:14:02 +0200860 }
861
862 /* Parse the mki only if present and mki is supported locally */
863 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
864 mki_length > 0 )
865 {
866 ssl->dtls_srtp_info.mki_len = mki_length;
867
Johan Pascal5ef72d22020-10-28 17:05:47 +0100868 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300869
Ron Eldoref72faf2018-07-12 11:54:20 +0300870 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
871 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100872 }
873
Johan Pascal042d4562020-08-25 12:14:02 +0200874 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100875}
876#endif /* MBEDTLS_SSL_DTLS_SRTP */
877
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100878/*
879 * Auxiliary functions for ServerHello parsing and related actions
880 */
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100884 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_ECDSA_C)
887static int ssl_check_key_curve( mbedtls_pk_context *pk,
888 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 const mbedtls_ecp_curve_info **crv = curves;
891 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100892
893 while( *crv != NULL )
894 {
895 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100896 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100897 crv++;
898 }
899
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100900 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100903
904/*
905 * Try picking a certificate for this ciphersuite,
906 * return 0 on success and -1 on failure.
907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908static int ssl_pick_cert( mbedtls_ssl_context *ssl,
909 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100912 mbedtls_pk_type_t pk_alg =
913 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200914 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100917 if( ssl->handshake->sni_key_cert != NULL )
918 list = ssl->handshake->sni_key_cert;
919 else
920#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200921 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100924 return( 0 );
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000927
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200928 if( list == NULL )
929 {
930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
931 return( -1 );
932 }
933
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100934 for( cur = list; cur != NULL; cur = cur->next )
935 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400936 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000938 cur->cert );
939
Gilles Peskinee198df52018-01-05 21:17:45 +0100940 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100943 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000944 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100945
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200946 /*
947 * This avoids sending the client a cert it'll reject based on
948 * keyUsage or other extensions.
949 *
950 * It also allows the user to provision different certificates for
951 * different uses based on keyUsage, eg if they want to avoid signing
952 * and decrypting with the same RSA key.
953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100955 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000958 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200959 continue;
960 }
961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_ECDSA_C)
963 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200964 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100967 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000968 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100969#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100970
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100971 /*
972 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
973 * present them a SHA-higher cert rather than failing if it's the only
974 * one we got that satisfies the other conditions.
975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
977 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100978 {
979 if( fallback == NULL )
980 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000983 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100984 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000985 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100986 }
987
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100988 /* If we get there, we got a winner */
989 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100990 }
991
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000992 if( cur == NULL )
993 cur = fallback;
994
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200995 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100996 if( cur != NULL )
997 {
998 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001000 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001001 return( 0 );
1002 }
1003
1004 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001007
1008/*
1009 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1010 * Sets ciphersuite_info only if the suite matches.
1011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1013 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001016
Hanno Becker7e5437a2017-04-28 17:15:26 +01001017#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001018 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001019 mbedtls_pk_type_t sig_type;
1020#endif
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001023 if( suite_info == NULL )
1024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001027 }
1028
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +00001030 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001031
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001032 if( suite_info->min_minor_ver > ssl->minor_ver ||
1033 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001036 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001037 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001040 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001042 return( 0 );
1043#endif
1044
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001045#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1046 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001047 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001048 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1050 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001051 return( 0 );
1052 }
1053#endif
1054
1055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1057 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001058 ( ssl->handshake->curves == NULL ||
1059 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001062 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001063 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001064 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001065#endif
1066
Gilles Peskineeccd8882020-03-10 12:19:08 +01001067#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001068 /* If the ciphersuite requires a pre-shared key and we don't
1069 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001071 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001074 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001075 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001076#endif
1077
Hanno Becker7e5437a2017-04-28 17:15:26 +01001078#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001079 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001080 /* If the ciphersuite requires signing, check whether
1081 * a suitable hash algorithm is present. */
1082 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1083 {
1084 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1085 if( sig_type != MBEDTLS_PK_NONE &&
1086 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1087 {
1088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001089 "for signature algorithm %u", (unsigned) sig_type ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001090 return( 0 );
1091 }
1092 }
1093
1094#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001095 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001098 /*
1099 * Final check: if ciphersuite requires us to have a
1100 * certificate/key of a particular type:
1101 * - select the appropriate certificate if we have one, or
1102 * - try the next ciphersuite if we don't
1103 * This must be done last since we modify the key_cert list.
1104 */
1105 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001108 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001109 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001110 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001111#endif
1112
1113 *ciphersuite_info = suite_info;
1114 return( 0 );
1115}
1116
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001117/* This function doesn't alert on errors that happen early during
1118 ClientHello parsing because they might indicate that the client is
1119 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001122 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001123 size_t i, j;
1124 size_t ciph_offset, comp_offset, ext_offset;
1125 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001127 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001128#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001129 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001131 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001132#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001133 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001134 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001136 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Hanno Becker7e5437a2017-04-28 17:15:26 +01001138 /* If there is no signature-algorithm extension present,
1139 * we need to fall back to the default values for allowed
1140 * signature-hash pairs. */
1141#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001142 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001143 int sig_hash_alg_ext_present = 0;
1144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001145 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001150read_record_header:
1151#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001152 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001154 * otherwise read it ourselves manually in order to support SSLv2
1155 * ClientHello, which doesn't use the same record layer format.
1156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_SSL_RENEGOTIATION)
1158 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001159#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001162 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001163 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001165 return( ret );
1166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
1168
1169 buf = ssl->in_hdr;
1170
Hanno Becker5903de42019-05-03 14:46:38 +01001171 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001172
Paul Bakkerec636f32012-09-09 19:17:02 +00001173 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001174 * TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001175 *
1176 * Record layer:
1177 * 0 . 0 message type
1178 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001179 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001180 * 3 . 4 message length
1181 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001182 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001183 buf[0] ) );
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001188 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001189 }
1190
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001192 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1193
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001195 buf[1], buf[2] ) );
1196
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001197 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001198
1199 /* According to RFC 5246 Appendix E.1, the version here is typically
1200 * "{03,00}, the lowest version number supported by the client, [or] the
1201 * value of ClientHello.client_version", so the only meaningful check here
1202 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001206 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakkerec636f32012-09-09 19:17:02 +00001207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001209 /* For DTLS if this is the initial handshake, remember the client sequence
1210 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_SSL_RENEGOTIATION)
1214 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001215#endif
1216 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001217 {
1218 /* Epoch should be 0 for initial handshakes */
1219 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001222 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001223 }
1224
Jerry Yud9a94fe2021-09-28 18:58:59 +08001225 memcpy( &ssl->cur_out_ctr[2], ssl->in_ctr + 2,
Jerry Yud96a5c22021-09-29 17:46:51 +08001226 sizeof( ssl->cur_out_ctr ) - 2 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1229 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001232 ssl->next_record_offset = 0;
1233 ssl->in_left = 0;
1234 goto read_record_header;
1235 }
1236
1237 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001239#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001240 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001242
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001243 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_SSL_RENEGOTIATION)
1246 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001249 msg_len = ssl->in_hslen;
1250 }
1251 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001252#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001253 {
Angus Grattond8213d02016-05-25 20:56:48 +10001254 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001257 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001258 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001259
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001260 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001261 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001264 return( ret );
1265 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001266
1267 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001270 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001271 else
1272#endif
1273 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001274 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001275
1276 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001279
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001280 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001281
1282 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001283 * Handshake layer:
1284 * 0 . 0 handshake type
1285 * 1 . 3 handshake length
1286 * 4 . 5 DTLS only: message seqence number
1287 * 6 . 8 DTLS only: fragment offset
1288 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001293 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001294 }
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001301 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001302 }
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001305 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1306
1307 /* We don't support fragmentation of ClientHello (yet?) */
1308 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001312 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001313 }
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001316 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001317 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001318 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001319 * Copy the client's handshake message_seq on initial handshakes,
1320 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_SSL_RENEGOTIATION)
1323 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001324 {
1325 /* This couldn't be done in ssl_prepare_handshake_record() */
1326 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1327 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001328
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001329 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001332 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001333 ssl->handshake->in_msg_seq ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001334 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001335 }
1336
1337 ssl->handshake->in_msg_seq++;
1338 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001339 else
1340#endif
1341 {
1342 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1343 ssl->in_msg[5];
1344 ssl->handshake->out_msg_seq = cli_msg_seq;
1345 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1346 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001347
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001348 /*
1349 * For now we don't support fragmentation, so make sure
1350 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001351 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001352 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1353 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1356 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001357 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 buf += mbedtls_ssl_hs_hdr_len( ssl );
1362 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001363
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001364 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001365 * ClientHello layer:
1366 * 0 . 1 protocol version
1367 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1368 * 34 . 35 session id length (1 byte)
1369 * 35 . 34+x session id
1370 * 35+x . 35+x DTLS only: cookie length (1 byte)
1371 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001372 * .. . .. ciphersuite list length (2 bytes)
1373 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001374 * .. . .. compression alg. list length (1 byte)
1375 * .. . .. compression alg. list
1376 * .. . .. extensions length (2 bytes, optional)
1377 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001378 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001379
1380 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001381 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001382 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1383 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001384 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001385 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001388 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001389 }
1390
1391 /*
1392 * Check and save the protocol version
1393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001397 ssl->conf->transport, buf );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001398 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001399
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001400 ssl->handshake->max_major_ver = ssl->major_ver;
1401 ssl->handshake->max_minor_ver = ssl->minor_ver;
1402
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001403 if( ssl->major_ver < ssl->conf->min_major_ver ||
1404 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001407 " [%d:%d] < [%d:%d]",
1408 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001409 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1411 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Hanno Beckerbc000442021-06-24 09:18:19 +01001412 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001413 }
1414
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001415 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001416 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001417 ssl->major_ver = ssl->conf->max_major_ver;
1418 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001419 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001420 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1421 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001422
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001423 /*
1424 * Save client random (inc. Unix time)
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001427
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001428 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001429
1430 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001431 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001432 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001433 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001434
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001435 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001436 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1440 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001441 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001442 }
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001445
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001446 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001447 memset( ssl->session_negotiate->id, 0,
1448 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001449 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001450 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001451
1452 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001453 * Check the cookie length and content
1454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001456 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001457 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001458 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001459 cookie_len = buf[cookie_offset];
1460
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001461 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001464 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +01001465 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1466 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001467 }
1468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001470 buf + cookie_offset + 1, cookie_len );
1471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001473 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_SSL_RENEGOTIATION)
1475 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001476#endif
1477 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001478 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001479 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001480 buf + cookie_offset + 1, cookie_len,
1481 ssl->cli_id, ssl->cli_id_len ) != 0 )
1482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001484 ssl->handshake->verify_cookie_len = 1;
1485 }
1486 else
1487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001489 ssl->handshake->verify_cookie_len = 0;
1490 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001491 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001494 {
1495 /* We know we didn't send a cookie, so it should be empty */
1496 if( cookie_len != 0 )
1497 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001498 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001500 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001501 }
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001504 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001505
1506 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001507 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001508 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001509 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001510 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001513 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001514
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001515 ciph_len = ( buf[ciph_offset + 0] << 8 )
1516 | ( buf[ciph_offset + 1] );
1517
1518 if( ciph_len < 2 ||
1519 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1520 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001523 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1524 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001525 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001526 }
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001529 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001530
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001531 /*
1532 * Check the compression algorithms length and pick one
1533 */
1534 comp_offset = ciph_offset + 2 + ciph_len;
1535
1536 comp_len = buf[comp_offset];
1537
1538 if( comp_len < 1 ||
1539 comp_len > 16 ||
1540 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001543 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1544 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001545 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001546 }
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001549 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001552 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001554 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001556#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001557 /*
1558 * Check the extension length
1559 */
1560 ext_offset = comp_offset + 1 + comp_len;
1561 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001562 {
Simon Butcher584a5472016-05-23 16:24:52 +01001563 if( msg_len < ext_offset + 2 )
1564 {
1565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001566 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1567 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001568 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001569 }
1570
1571 ext_len = ( buf[ext_offset + 0] << 8 )
1572 | ( buf[ext_offset + 1] );
1573
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001574 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001575 {
1576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001577 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1578 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001579 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001580 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001581 }
Simon Butcher584a5472016-05-23 16:24:52 +01001582 else
1583 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001584
Simon Butcher584a5472016-05-23 16:24:52 +01001585 ext = buf + ext_offset + 2;
1586 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001587
Simon Butcher584a5472016-05-23 16:24:52 +01001588 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001589 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001590 unsigned int ext_id;
1591 unsigned int ext_size;
1592 if ( ext_len < 4 ) {
1593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1594 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1595 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001596 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02001597 }
1598 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1599 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001600
Simon Butcher584a5472016-05-23 16:24:52 +01001601 if( ext_size + 4 > ext_len )
1602 {
1603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001604 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1605 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001606 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001607 }
1608 switch( ext_id )
1609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001611 case MBEDTLS_TLS_EXT_SERVERNAME:
1612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1613 if( ssl->conf->f_sni == NULL )
1614 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001615
Simon Butcher584a5472016-05-23 16:24:52 +01001616 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1617 if( ret != 0 )
1618 return( ret );
1619 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001621
Simon Butcher584a5472016-05-23 16:24:52 +01001622 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001625 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001626#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001627
Simon Butcher584a5472016-05-23 16:24:52 +01001628 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1629 if( ret != 0 )
1630 return( ret );
1631 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001634 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001635 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1637
Simon Butcher584a5472016-05-23 16:24:52 +01001638 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1639 if( ret != 0 )
1640 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001641
1642 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001643 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001645 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001646
Robert Cragie136884c2015-10-02 13:34:31 +01001647#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001648 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yub47d0f82021-12-20 17:34:40 +08001649 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Simon Butcher584a5472016-05-23 16:24:52 +01001650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001651
Jerry Yub47d0f82021-12-20 17:34:40 +08001652 ret = ssl_parse_supported_groups_ext( ssl, ext + 4, ext_size );
Simon Butcher584a5472016-05-23 16:24:52 +01001653 if( ret != 0 )
1654 return( ret );
1655 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001656
Simon Butcher584a5472016-05-23 16:24:52 +01001657 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1659 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001660
Simon Butcher584a5472016-05-23 16:24:52 +01001661 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1662 if( ret != 0 )
1663 return( ret );
1664 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001665#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1666 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001667
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001668#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001669 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001671
Simon Butcher584a5472016-05-23 16:24:52 +01001672 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1673 if( ret != 0 )
1674 return( ret );
1675 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001676#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001679 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001681
Simon Butcher584a5472016-05-23 16:24:52 +01001682 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1683 if( ret != 0 )
1684 return( ret );
1685 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001687
Hanno Beckera0e20d02019-05-15 14:03:01 +01001688#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001689 case MBEDTLS_TLS_EXT_CID:
1690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1691
1692 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1693 if( ret != 0 )
1694 return( ret );
1695 break;
Thomas Daubneye1c9a402021-06-15 11:26:43 +01001696#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +01001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001699 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001701
Simon Butcher584a5472016-05-23 16:24:52 +01001702 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1703 if( ret != 0 )
1704 return( ret );
1705 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001709 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001711
Simon Butcher584a5472016-05-23 16:24:52 +01001712 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1713 if( ret != 0 )
1714 return( ret );
1715 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001719 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001721
Simon Butcher584a5472016-05-23 16:24:52 +01001722 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1723 if( ret != 0 )
1724 return( ret );
1725 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001729 case MBEDTLS_TLS_EXT_ALPN:
1730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001731
Simon Butcher584a5472016-05-23 16:24:52 +01001732 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1733 if( ret != 0 )
1734 return( ret );
1735 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001737
Johan Pascalb62bb512015-12-03 21:56:45 +01001738#if defined(MBEDTLS_SSL_DTLS_SRTP)
1739 case MBEDTLS_TLS_EXT_USE_SRTP:
1740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02001741
Johan Pascalc3ccd982020-10-28 17:18:18 +01001742 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
1743 if( ret != 0 )
1744 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001745 break;
1746#endif /* MBEDTLS_SSL_DTLS_SRTP */
1747
Simon Butcher584a5472016-05-23 16:24:52 +01001748 default:
Paul Elliott9f352112020-12-09 14:55:45 +00001749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01001750 ext_id ) );
1751 }
1752
1753 ext_len -= 4 + ext_size;
1754 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00001755 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01001756
Hanno Becker7e5437a2017-04-28 17:15:26 +01001757#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001758 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001759
1760 /*
1761 * Try to fall back to default hash SHA1 if the client
1762 * hasn't provided any preferred signature-hash combinations.
1763 */
1764 if( sig_hash_alg_ext_present == 0 )
1765 {
1766 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
1767
1768 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
1769 md_default = MBEDTLS_MD_NONE;
1770
1771 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
1772 }
1773
1774#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001775 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001776
Paul Bakker48916f92012-09-16 19:57:18 +00001777 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001778 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1779 */
1780 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1785#if defined(MBEDTLS_SSL_RENEGOTIATION)
1786 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001787 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1789 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001790 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1791 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01001792 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001793 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001796 break;
1797 }
1798 }
1799
1800 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001801 * Renegotiation security checks
1802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001804 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001807 handshake_failure = 1;
1808 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#if defined(MBEDTLS_SSL_RENEGOTIATION)
1810 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1811 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001812 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001815 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1818 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001819 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001822 handshake_failure = 1;
1823 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1825 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001826 renegotiation_info_seen == 1 )
1827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001829 handshake_failure = 1;
1830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001832
1833 if( handshake_failure == 1 )
1834 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02001835 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1836 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001837 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001838 }
Paul Bakker380da532012-04-18 16:10:25 +00001839
Paul Bakker41c83d32013-03-20 14:39:14 +01001840 /*
1841 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001842 * (At the end because we need information from the EC-based extensions
1843 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001844 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001845 got_common_suite = 0;
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001846 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001847 ciphersuite_info = NULL;
TRodziewicz8476f2f2021-06-02 14:34:47 +02001848
TRodziewicz3946f792021-06-14 12:11:18 +02001849 if (ssl->conf->respect_cli_pref == MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT)
TRodziewicz8476f2f2021-06-02 14:34:47 +02001850 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001851 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001852 for( i = 0; ciphersuites[i] != 0; i++ )
1853 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001854 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001855 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001856
TRodziewicz8476f2f2021-06-02 14:34:47 +02001857 got_common_suite = 1;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001858
TRodziewicz8476f2f2021-06-02 14:34:47 +02001859 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1860 &ciphersuite_info ) ) != 0 )
1861 return( ret );
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001862
TRodziewicz8476f2f2021-06-02 14:34:47 +02001863 if( ciphersuite_info != NULL )
1864 goto have_ciphersuite;
1865 }
1866 } else {
1867 for( i = 0; ciphersuites[i] != 0; i++ )
1868 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
1869 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001870 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001871 continue;
1872
1873 got_common_suite = 1;
1874
1875 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1876 &ciphersuite_info ) ) != 0 )
1877 return( ret );
1878
1879 if( ciphersuite_info != NULL )
1880 goto have_ciphersuite;
1881 }
1882 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001883
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001884 if( got_common_suite )
1885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001887 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001888 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1889 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman096c4112021-06-29 09:52:06 +01001890 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001891 }
1892 else
1893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1896 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01001897 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001898 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001899
1900have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001902
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001903 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001904 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001905
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 ssl->state++;
1907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001911#endif
1912
Hanno Becker7e5437a2017-04-28 17:15:26 +01001913 /* Debugging-only output for testsuite */
1914#if defined(MBEDTLS_DEBUG_C) && \
1915 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001916 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001917 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1918 {
1919 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
1920 if( sig_alg != MBEDTLS_PK_NONE )
1921 {
1922 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1923 sig_alg );
1924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1925 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
1926 }
1927 else
1928 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001930 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001931 }
1932 }
1933#endif
1934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
1937 return( 0 );
1938}
1939
Hanno Beckera0e20d02019-05-15 14:03:01 +01001940#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01001941static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
1942 unsigned char *buf,
1943 size_t *olen )
1944{
1945 unsigned char *p = buf;
1946 size_t ext_len;
1947 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1948
1949 *olen = 0;
1950
1951 /* Skip writing the extension if we don't want to use it or if
1952 * the client hasn't offered it. */
1953 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
1954 return;
1955
1956 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
1957 * which is at most 255, so the increment cannot overflow. */
1958 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
1959 {
1960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1961 return;
1962 }
1963
1964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
1965
1966 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01001967 * Quoting draft-ietf-tls-dtls-connection-id-05
1968 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01001969 *
1970 * struct {
1971 * opaque cid<0..2^8-1>;
1972 * } ConnectionId;
1973 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001974 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
1975 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01001976 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001977 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
1978 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01001979
1980 *p++ = (uint8_t) ssl->own_cid_len;
1981 memcpy( p, ssl->own_cid, ssl->own_cid_len );
1982
1983 *olen = ssl->own_cid_len + 5;
1984}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001985#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1988static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001989 unsigned char *buf,
1990 size_t *olen )
1991{
1992 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 const mbedtls_ssl_ciphersuite_t *suite = NULL;
1994 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001995
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001996 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001997 {
1998 *olen = 0;
1999 return;
2000 }
2001
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002002 /*
2003 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2004 * from a client and then selects a stream or Authenticated Encryption
2005 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2006 * encrypt-then-MAC response extension back to the client."
2007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002009 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2011 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002012 {
2013 *olen = 0;
2014 return;
2015 }
2016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002018
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002019 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
2020 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002021
2022 *p++ = 0x00;
2023 *p++ = 0x00;
2024
2025 *olen = 4;
2026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2030static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002031 unsigned char *buf,
2032 size_t *olen )
2033{
2034 unsigned char *p = buf;
2035
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002036 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002037 {
2038 *olen = 0;
2039 return;
2040 }
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002043 "extension" ) );
2044
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002045 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
2046 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002047
2048 *p++ = 0x00;
2049 *p++ = 0x00;
2050
2051 *olen = 4;
2052}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2056static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002057 unsigned char *buf,
2058 size_t *olen )
2059{
2060 unsigned char *p = buf;
2061
2062 if( ssl->handshake->new_session_ticket == 0 )
2063 {
2064 *olen = 0;
2065 return;
2066 }
2067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002069
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002070 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
2071 p += 2;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002072
2073 *p++ = 0x00;
2074 *p++ = 0x00;
2075
2076 *olen = 4;
2077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002081 unsigned char *buf,
2082 size_t *olen )
2083{
2084 unsigned char *p = buf;
2085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002087 {
2088 *olen = 0;
2089 return;
2090 }
2091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002093
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002094 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
2095 p += 2;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_SSL_RENEGOTIATION)
2098 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002099 {
2100 *p++ = 0x00;
2101 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2102 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002103
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002104 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2105 p += ssl->verify_data_len;
2106 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2107 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002108 }
2109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002111 {
2112 *p++ = 0x00;
2113 *p++ = 0x01;
2114 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002115 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002116
2117 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002118}
2119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2121static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002122 unsigned char *buf,
2123 size_t *olen )
2124{
2125 unsigned char *p = buf;
2126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002128 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002129 *olen = 0;
2130 return;
2131 }
2132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002134
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002135 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
2136 p += 2;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002137
2138 *p++ = 0x00;
2139 *p++ = 1;
2140
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002141 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002142
2143 *olen = 5;
2144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002146
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002147#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002148 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002150 unsigned char *buf,
2151 size_t *olen )
2152{
2153 unsigned char *p = buf;
2154 ((void) ssl);
2155
Paul Bakker677377f2013-10-28 12:54:26 +01002156 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002158 {
2159 *olen = 0;
2160 return;
2161 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002164
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002165 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
2166 p += 2;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002167
2168 *p++ = 0x00;
2169 *p++ = 2;
2170
2171 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002173
2174 *olen = 6;
2175}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002176#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002177
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002178#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2179static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2180 unsigned char *buf,
2181 size_t *olen )
2182{
Janos Follath865b3eb2019-12-16 11:46:15 +00002183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002184 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002185 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002186 size_t kkpp_len;
2187
2188 *olen = 0;
2189
2190 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002191 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002192 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2193 return;
2194
2195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2196
2197 if( end - p < 4 )
2198 {
2199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2200 return;
2201 }
2202
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002203 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
2204 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002205
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002206 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2207 p + 2, end - p - 2, &kkpp_len,
2208 ssl->conf->f_rng, ssl->conf->p_rng );
2209 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002210 {
2211 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2212 return;
2213 }
2214
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002215 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
2216 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002217
2218 *olen = kkpp_len + 4;
2219}
2220#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222#if defined(MBEDTLS_SSL_ALPN )
2223static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002224 unsigned char *buf, size_t *olen )
2225{
2226 if( ssl->alpn_chosen == NULL )
2227 {
2228 *olen = 0;
2229 return;
2230 }
2231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002233
2234 /*
2235 * 0 . 1 ext identifier
2236 * 2 . 3 ext length
2237 * 4 . 5 protocol list length
2238 * 6 . 6 protocol name length
2239 * 7 . 7+n protocol name
2240 */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002241 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002242
2243 *olen = 7 + strlen( ssl->alpn_chosen );
2244
Joe Subbiani6dd73642021-07-19 11:56:54 +01002245 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002246
Joe Subbiani6dd73642021-07-19 11:56:54 +01002247 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002248
Joe Subbiani2194dc42021-07-14 12:31:31 +01002249 buf[6] = MBEDTLS_BYTE_0( *olen - 7 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002250
2251 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002254
Ron Eldor3adb9922017-12-21 10:15:08 +02002255#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002256static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002257 unsigned char *buf,
2258 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002259{
Ron Eldor75870ec2018-12-06 17:31:55 +02002260 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002261 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002262 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2263
2264 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002265
Johan Pascalc3ccd982020-10-28 17:18:18 +01002266 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2267 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002268 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002269 return;
2270 }
2271
2272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2273
Johan Pascald576fdb2020-09-22 10:39:53 +02002274 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002275 {
2276 mki_len = ssl->dtls_srtp_info.mki_len;
2277 }
2278
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002279 /* The extension total size is 9 bytes :
2280 * - 2 bytes for the extension tag
2281 * - 2 bytes for the total size
2282 * - 2 bytes for the protection profile length
2283 * - 2 bytes for the protection profile
2284 * - 1 byte for the mki length
2285 * + the actual mki length
2286 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002287 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002288 {
2289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2290 return;
2291 }
2292
Johan Pascalb62bb512015-12-03 21:56:45 +01002293 /* extension */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002294 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 );
Ron Eldoref72faf2018-07-12 11:54:20 +03002295 /*
2296 * total length 5 and mki value: only one profile(2 bytes)
2297 * and length(2 bytes) and srtp_mki )
2298 */
Ron Eldor591f1622018-01-22 12:30:04 +02002299 ext_len = 5 + mki_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002300 MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 );
Johan Pascalb62bb512015-12-03 21:56:45 +01002301
2302 /* protection profile length: 2 */
2303 buf[4] = 0x00;
2304 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002305 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002306 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002307 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002308 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002309 MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002310 }
2311 else
2312 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002314 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002315 }
2316
Ron Eldor591f1622018-01-22 12:30:04 +02002317 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002318 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002319
Ron Eldor591f1622018-01-22 12:30:04 +02002320 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002321}
2322#endif /* MBEDTLS_SSL_DTLS_SRTP */
2323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2325static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002326{
Janos Follath865b3eb2019-12-16 11:46:15 +00002327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002328 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002329 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002332
2333 /*
2334 * struct {
2335 * ProtocolVersion server_version;
2336 * opaque cookie<0..2^8-1>;
2337 * } HelloVerifyRequest;
2338 */
2339
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002340 /* The RFC is not clear on this point, but sending the actual negotiated
2341 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002343 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002345 p += 2;
2346
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002347 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002348 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002352 }
2353
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002354 /* Skip length byte until we know the length */
2355 cookie_len_byte = p++;
2356
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002357 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002358 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002359 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002362 return( ret );
2363 }
2364
2365 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002368
2369 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2371 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002374
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002375 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002376 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002378 return( ret );
2379 }
2380
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002381#if defined(MBEDTLS_SSL_PROTO_DTLS)
2382 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2383 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2384 {
2385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2386 return( ret );
2387 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002388#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002391
2392 return( 0 );
2393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002395
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002396static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
Hanno Becker64ce9742021-04-15 08:19:40 +01002397{
2398 int ret;
Hanno Beckera5b1a392021-04-15 16:48:01 +01002399 mbedtls_ssl_session session_tmp;
Hanno Becker64ce9742021-04-15 08:19:40 +01002400 mbedtls_ssl_session * const session = ssl->session_negotiate;
2401
2402 /* Resume is 0 by default, see ssl_handshake_init().
2403 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2404 if( ssl->handshake->resume == 1 )
2405 return;
Hanno Becker7ad77962021-05-13 06:13:34 +01002406 if( session->id_len == 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +01002407 return;
2408 if( ssl->conf->f_get_cache == NULL )
2409 return;
2410#if defined(MBEDTLS_SSL_RENEGOTIATION)
2411 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2412 return;
2413#endif
2414
Hanno Beckera5b1a392021-04-15 16:48:01 +01002415 mbedtls_ssl_session_init( &session_tmp );
2416
Hanno Becker64ce9742021-04-15 08:19:40 +01002417 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
Hanno Beckerccdaf6e2021-04-15 09:26:17 +01002418 session->id,
2419 session->id_len,
Hanno Becker64ce9742021-04-15 08:19:40 +01002420 &session_tmp );
2421 if( ret != 0 )
2422 goto exit;
2423
2424 if( session->ciphersuite != session_tmp.ciphersuite ||
2425 session->compression != session_tmp.compression )
2426 {
2427 /* Mismatch between cached and negotiated session */
2428 goto exit;
2429 }
2430
2431 /* Move semantics */
Hanno Becker64ce9742021-04-15 08:19:40 +01002432 mbedtls_ssl_session_free( session );
2433 *session = session_tmp;
Hanno Beckerfc1f4132021-05-14 14:57:54 +01002434 memset( &session_tmp, 0, sizeof( session_tmp ) );
Hanno Becker64ce9742021-04-15 08:19:40 +01002435
2436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2437 ssl->handshake->resume = 1;
2438
2439exit:
2440
Hanno Beckerdf564022021-05-13 06:26:37 +01002441 mbedtls_ssl_session_free( &session_tmp );
Hanno Becker64ce9742021-04-15 08:19:40 +01002442}
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002447 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002448#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002450 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 unsigned char *buf, *p;
2452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002456 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002457 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002461
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002462 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002465
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002466 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2469 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002470 }
2471
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 /*
2473 * 0 . 0 handshake type
2474 * 1 . 3 handshake length
2475 * 4 . 5 protocol version
2476 * 6 . 9 UNIX time()
2477 * 10 . 37 random bytes
2478 */
2479 buf = ssl->out_msg;
2480 p = buf + 4;
2481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002483 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002484 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002487 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002490 t = mbedtls_time( NULL );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002491 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
2492 p += 4;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Paul Elliottd48d5c62021-01-07 14:47:05 +00002494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2495 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002496#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002497 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002498 return( ret );
2499
2500 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002503 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002504 return( ret );
2505
2506 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Paul Bakker48916f92012-09-16 19:57:18 +00002508 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002512 ssl_handle_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002514 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 {
2516 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002517 * New session, create a new session id,
2518 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 ssl->state++;
2521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002523 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002524#endif
2525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002527 if( ssl->handshake->new_session_ticket != 0 )
2528 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002529 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002530 memset( ssl->session_negotiate->id, 0, 32 );
2531 }
2532 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002534 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002535 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002536 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002537 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002538 return( ret );
2539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541 else
2542 {
2543 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002544 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002546 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002552 return( ret );
2553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002556 /*
2557 * 38 . 38 session id length
2558 * 39 . 38+n session id
2559 * 39+n . 40+n chosen ciphersuite
2560 * 41+n . 41+n chosen compression alg.
2561 * 42+n . 43+n extensions length
2562 * 44+n . 43+n+m extensions
2563 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002564 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2565 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2566 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Paul Elliottd48d5c62021-01-07 14:47:05 +00002568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002571 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002573 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
2574 p += 2;
Joe Subbianifbeb6922021-07-16 14:27:50 +01002575 *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2578 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002580 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002581
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002582 /*
2583 * First write extensions, then the total length
2584 */
2585 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2586 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002589 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2590 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002591#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002592
Hanno Beckera0e20d02019-05-15 14:03:01 +01002593#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002594 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2595 ext_len += olen;
2596#endif
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002599 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2600 ext_len += olen;
2601#endif
2602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002604 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2605 ext_len += olen;
2606#endif
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002609 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2610 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002611#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002612
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002613#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002614 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002615 if ( mbedtls_ssl_ciphersuite_uses_ec(
2616 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2617 {
2618 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2619 ext_len += olen;
2620 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002621#endif
2622
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002623#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2624 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2625 ext_len += olen;
2626#endif
2627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002629 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2630 ext_len += olen;
2631#endif
2632
Johan Pascalb62bb512015-12-03 21:56:45 +01002633#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01002634 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
2635 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002636#endif
2637
Paul Elliottd48d5c62021-01-07 14:47:05 +00002638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2639 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002640
Paul Bakkera7036632014-04-30 10:15:38 +02002641 if( ext_len > 0 )
2642 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002643 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani94180e72021-08-20 16:20:44 +01002644 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02002645 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2649 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002651 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 return( ret );
2656}
2657
Gilles Peskineeccd8882020-03-10 12:19:08 +01002658#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002661 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002662 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002665
Hanno Becker77adddc2019-02-07 12:32:43 +00002666 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 ssl->state++;
2670 return( 0 );
2671 }
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002675}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002676#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002680 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002681 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002682 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002683 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002685 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002687 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
2691 ssl->state++;
2692
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002693#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2694 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2695 authmode = ssl->handshake->sni_authmode;
2696 else
2697#endif
2698 authmode = ssl->conf->authmode;
2699
Hanno Becker77adddc2019-02-07 12:32:43 +00002700 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002701 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 {
Johan Pascal4f099262020-09-22 10:59:26 +02002703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2704 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 }
2706
2707 /*
2708 * 0 . 0 handshake type
2709 * 1 . 3 handshake length
2710 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002711 * 5 .. m-1 cert types
2712 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002713 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 * n .. n+1 length of all DNs
2715 * n+2 .. n+3 length of DN 1
2716 * n+4 .. ... Distinguished Name #1
2717 * ... .. ... length of DN 2, etc.
2718 */
2719 buf = ssl->out_msg;
2720 p = buf + 4;
2721
2722 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002723 * Supported certificate types
2724 *
2725 * ClientCertificateType certificate_types<1..2^8-1>;
2726 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002728 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#if defined(MBEDTLS_RSA_C)
2731 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#if defined(MBEDTLS_ECDSA_C)
2734 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002735#endif
2736
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002737 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002738 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002739
Paul Bakker577e0062013-08-28 11:57:20 +02002740 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002742 /*
2743 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002744 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002745 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2746 *
2747 * struct {
2748 * HashAlgorithm hash;
2749 * SignatureAlgorithm signature;
2750 * } SignatureAndHashAlgorithm;
2751 *
2752 * enum { (255) } HashAlgorithm;
2753 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002756 {
Simon Butcher99000142016-10-13 17:21:01 +01002757 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002758
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002759 /*
2760 * Supported signature algorithms
2761 */
Simon Butcher99000142016-10-13 17:21:01 +01002762 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
2763 {
2764 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
2765
2766 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
2767 continue;
2768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002770 p[2 + sa_len++] = hash;
2771 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01002774 p[2 + sa_len++] = hash;
2775 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002776#endif
Simon Butcher99000142016-10-13 17:21:01 +01002777 }
Paul Bakker926af752012-11-23 13:38:07 +01002778
Joe Subbiani6dd73642021-07-19 11:56:54 +01002779 MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002780 sa_len += 2;
2781 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002785 /*
2786 * DistinguishedName certificate_authorities<0..2^16-1>;
2787 * opaque DistinguishedName<1..2^16-1>;
2788 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002791 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01002792
2793 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00002795 /* NOTE: If trusted certificates are provisioned
2796 * via a CA callback (configured through
2797 * `mbedtls_ssl_conf_ca_cb()`, then the
2798 * CertificateRequest is currently left empty. */
2799
Janos Follath088ce432017-04-10 12:42:31 +01002800#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2801 if( ssl->handshake->sni_ca_chain != NULL )
2802 crt = ssl->handshake->sni_ca_chain;
2803 else
2804#endif
2805 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002806
Janos Follath088ce432017-04-10 12:42:31 +01002807 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002808 {
irwirc9bc3002020-04-01 13:46:36 +03002809 /* It follows from RFC 5280 A.1 that this length
2810 * can be represented in at most 11 bits. */
2811 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01002812
irwirc9bc3002020-04-01 13:46:36 +03002813 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01002814 {
2815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2816 break;
2817 }
2818
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002819 MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 );
2820 p += 2;
Janos Follath088ce432017-04-10 12:42:31 +01002821 memcpy( p, crt->subject_raw.p, dn_size );
2822 p += dn_size;
2823
2824 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2825
2826 total_dn_size += 2 + dn_size;
2827 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 }
2830
Paul Bakker926af752012-11-23 13:38:07 +01002831 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2833 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002834 MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002836 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
2840 return( ret );
2841}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002842#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2845 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2846static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002847{
Janos Follath865b3eb2019-12-16 11:46:15 +00002848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2853 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002854 }
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2857 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2858 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002861 return( ret );
2862 }
2863
2864 return( 0 );
2865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2867 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002868
Gilles Peskineeccd8882020-03-10 12:19:08 +01002869#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002870 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002871static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002872 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002873{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002874 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2875 * signature length which will be added in ssl_write_server_key_exchange
2876 * after the call to ssl_prepare_server_key_exchange.
2877 * ssl_write_server_key_exchange also takes care of incrementing
2878 * ssl->out_msglen. */
2879 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10002880 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002881 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02002882 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002883 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002884 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
2885 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002886 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02002887 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002888 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01002889 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002890 return( ret );
2891}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002892#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002893 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002894
Gilles Peskined3eb0612018-01-08 17:07:44 +01002895/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02002896 * calculating the signature if any, but excluding formatting the
2897 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01002898static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
2899 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00002900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002902 ssl->handshake->ciphersuite_info;
2903
Gilles Peskineeccd8882020-03-10 12:19:08 +01002904#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
2905#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01002906 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002907#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
2908#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002909
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002910 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002911#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02002912 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002913#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002914
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002915#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef00f1522021-06-22 00:09:00 +02002916#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2917 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
2918#else
2919 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
2920#endif
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002921#endif
Gilles Peskinef00f1522021-06-22 00:09:00 +02002922
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002923 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002925 /*
2926 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002927 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002928 *
2929 */
2930
2931 /*
2932 * - ECJPAKE key exchanges
2933 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002934#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2935 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2936 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002938 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002939
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002940 ret = mbedtls_ecjpake_write_round_two(
2941 &ssl->handshake->ecjpake_ctx,
2942 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10002943 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002944 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002945 if( ret != 0 )
2946 {
2947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
2948 return( ret );
2949 }
2950
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002951 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002952 }
2953#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2954
Hanno Becker1aa267c2017-04-28 17:08:27 +01002955 /*
2956 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
2957 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
2958 * we use empty support identity hints here.
2959 **/
2960#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2962 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2963 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002964 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002965 ssl->out_msg[ssl->out_msglen++] = 0x00;
2966 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002967 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2969 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970
Hanno Becker7e5437a2017-04-28 17:15:26 +01002971 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002972 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01002973 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002974#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002975 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00002976 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002978 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002979
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002980 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
2981 {
2982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
2983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2984 }
2985
Paul Bakker41c83d32013-03-20 14:39:14 +01002986 /*
2987 * Ephemeral DH parameters:
2988 *
2989 * struct {
2990 * opaque dh_p<1..2^16-1>;
2991 * opaque dh_g<1..2^16-1>;
2992 * opaque dh_Ys<1..2^16-1>;
2993 * } ServerDHParams;
2994 */
Hanno Beckerab740562017-10-04 13:15:37 +01002995 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
2996 &ssl->conf->dhm_P,
2997 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002998 {
Hanno Beckerab740562017-10-04 13:15:37 +01002999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003000 return( ret );
3001 }
Paul Bakker48916f92012-09-16 19:57:18 +00003002
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003003 if( ( ret = mbedtls_dhm_make_params(
3004 &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003005 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003006 ssl->out_msg + ssl->out_msglen, &len,
3007 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003010 return( ret );
3011 }
3012
Gilles Peskineeccd8882020-03-10 12:19:08 +01003013#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003014 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003015#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003016
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003017 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3020 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3021 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3022 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003023 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003024#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003025
Hanno Becker1aa267c2017-04-28 17:08:27 +01003026 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003027 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003028 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003029#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003030 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003032 /*
3033 * Ephemeral ECDH parameters:
3034 *
3035 * struct {
3036 * ECParameters curve_params;
3037 * ECPoint public;
3038 * } ServerECDHParams;
3039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 const mbedtls_ecp_curve_info **curve = NULL;
Brett Warren01f3dae2021-08-17 13:50:51 +01003041 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Janos Follath865b3eb2019-12-16 11:46:15 +00003042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003043 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003044
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003045 /* Match our preference list against the offered curves */
Brett Warren01f3dae2021-08-17 13:50:51 +01003046 if( group_list == NULL )
3047 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
3048 for( ; *group_list != 0; group_list++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003049 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
Brett Warren01f3dae2021-08-17 13:50:51 +01003050 if( (*curve)->tls_id == *group_list )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003051 goto curve_matching_done;
3052
3053curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003054 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01003057 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Gergely Budai987bfb52014-01-19 21:48:42 +01003058 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003061
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003062 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3063 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003064 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003066 return( ret );
3067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003069 if( ( ret = mbedtls_ecdh_make_params(
3070 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003071 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003072 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003073 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003076 return( ret );
3077 }
3078
Gilles Peskineeccd8882020-03-10 12:19:08 +01003079#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003080 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003081#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003082
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003083 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003084
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003085 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3086 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003087 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003088#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Hanno Becker1aa267c2017-04-28 17:08:27 +01003090 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003091 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003092 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003093 * exchange parameters, compute and add the signature here.
3094 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003095 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003096#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003097 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003099 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003100 size_t hashlen = 0;
Ronald Cron69a63422021-10-18 09:47:58 +02003101#if defined(MBEDTLS_USE_PSA_CRYPTO)
3102 unsigned char hash[PSA_HASH_MAX_SIZE];
3103#else
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003104 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Ronald Cron69a63422021-10-18 09:47:58 +02003105#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00003106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003107
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003108 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003109 * 2.1: Choose hash algorithm:
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003110 * For TLS 1.2, obey signature-hash-algorithm extension
3111 * to choose appropriate hash.
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003112 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003113
3114 mbedtls_md_type_t md_alg;
3115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003117 mbedtls_pk_type_t sig_alg =
3118 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003120 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003121 /* For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003122 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003123 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003124 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3125 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003128 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003129 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003131 }
3132 }
Paul Bakker577e0062013-08-28 11:57:20 +02003133 else
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003134 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003137 }
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003138#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003139
Paul Elliott3891caf2020-12-17 18:42:40 +00003140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003141
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003142 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003143 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003144 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003147 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003148 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003149 dig_signed,
3150 dig_signed_len,
3151 md_alg );
3152 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003153 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003154 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003155 else
TRodziewicz0f82ec62021-05-12 17:49:18 +02003156#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003160 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003161
Gilles Peskineebd652f2018-01-05 21:18:59 +01003162 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003163
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003164 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003165 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3168 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003169 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003170 /*
3171 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003172 * explicitly through a prefix to the signature.
3173 *
3174 * struct {
3175 * HashAlgorithm hash;
3176 * SignatureAlgorithm signature;
3177 * } SignatureAndHashAlgorithm;
3178 *
3179 * struct {
3180 * SignatureAndHashAlgorithm algorithm;
3181 * opaque signature<0..2^16-1>;
3182 * } DigitallySigned;
3183 *
3184 */
3185
Gilles Peskine1004c192018-01-08 16:59:14 +01003186 ssl->out_msg[ssl->out_msglen++] =
3187 mbedtls_ssl_hash_from_md_alg( md_alg );
3188 ssl->out_msg[ssl->out_msglen++] =
3189 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003192
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003193#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003194 if( ssl->conf->f_async_sign_start != NULL )
3195 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003196 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003197 mbedtls_ssl_own_cert( ssl ),
3198 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003199 switch( ret )
3200 {
3201 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3202 /* act as if f_async_sign was null */
3203 break;
3204 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003205 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003206 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003207 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003208 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003209 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3210 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003211 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003212 return( ret );
3213 }
3214 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003215#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003216
3217 if( mbedtls_ssl_own_key( ssl ) == NULL )
3218 {
3219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3220 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3221 }
3222
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003223 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3224 * signature length which will be added in ssl_write_server_key_exchange
3225 * after the call to ssl_prepare_server_key_exchange.
3226 * ssl_write_server_key_exchange also takes care of incrementing
3227 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003228 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3229 md_alg, hash, hashlen,
3230 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003231 out_buf_len - ssl->out_msglen - 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003232 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003233 ssl->conf->f_rng,
3234 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003237 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003238 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003239 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003240#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003241
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003242 return( 0 );
3243}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003244
Gilles Peskined3eb0612018-01-08 17:07:44 +01003245/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003246 * that do not include a ServerKeyExchange message, do nothing. Either
3247 * way, if successful, move on to the next step in the SSL state
3248 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003249static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3250{
Janos Follath865b3eb2019-12-16 11:46:15 +00003251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003252 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003253#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003254 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003255 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003256#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003257
Gilles Peskined3eb0612018-01-08 17:07:44 +01003258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3259
Gilles Peskineeccd8882020-03-10 12:19:08 +01003260#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003261 /* Extract static ECDH parameters and abort if ServerKeyExchange
3262 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003263 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3264 {
3265 /* For suites involving ECDH, extract DH parameters
3266 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003267#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003268 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3269 {
3270 ssl_get_ecdh_params_from_cert( ssl );
3271 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003272#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003273
3274 /* Key exchanges not involving ephemeral keys don't use
3275 * ServerKeyExchange, so end here. */
3276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3277 ssl->state++;
3278 return( 0 );
3279 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003280#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003281
Gilles Peskineeccd8882020-03-10 12:19:08 +01003282#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003283 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003284 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003285 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003286 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003287 {
3288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3289 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003290 }
3291 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003292#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003293 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003294 {
3295 /* ServerKeyExchange is needed. Prepare the message. */
3296 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003297 }
3298
3299 if( ret != 0 )
3300 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003301 /* If we're starting to write a new message, set ssl->out_msglen
3302 * to 0. But if we're resuming after an asynchronous message,
3303 * out_msglen is the amount of data written so far and mst be
3304 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003305 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3307 else
3308 ssl->out_msglen = 0;
3309 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003310 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003311
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003312 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003313 * ssl_prepare_server_key_exchange already wrote the signature
3314 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003315#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003316 if( signature_len != 0 )
3317 {
Joe Subbianifbeb6922021-07-16 14:27:50 +01003318 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len );
3319 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len );
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003320
3321 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3322 ssl->out_msg + ssl->out_msglen,
3323 signature_len );
3324
3325 /* Skip over the already-written signature */
3326 ssl->out_msglen += signature_len;
3327 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003328#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003329
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003330 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3332 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
3334 ssl->state++;
3335
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003336 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 return( ret );
3340 }
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003344}
3345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003347{
Janos Follath865b3eb2019-12-16 11:46:15 +00003348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351
3352 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3354 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
3356 ssl->state++;
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003361#endif
3362
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003363 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 return( ret );
3367 }
3368
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003369#if defined(MBEDTLS_SSL_PROTO_DTLS)
3370 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3371 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3372 {
3373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3374 return( ret );
3375 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003376#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
3380 return( 0 );
3381}
3382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3384 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3385static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003386 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003389 size_t n;
3390
3391 /*
3392 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3393 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003394 if( *p + 2 > end )
3395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003397 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003399
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003400 n = ( (*p)[0] << 8 ) | (*p)[1];
3401 *p += 2;
3402
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003403 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003406 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003407 }
3408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003412 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003413 }
3414
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003415 *p += n;
3416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003418
Paul Bakker70df2fb2013-04-17 17:19:09 +02003419 return( ret );
3420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3422 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3425 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003426
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003427#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003428static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3429 unsigned char *peer_pms,
3430 size_t *peer_pmslen,
3431 size_t peer_pmssize )
3432{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003433 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003434 peer_pms, peer_pmslen, peer_pmssize );
3435 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3436 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003437 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003438 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003439 }
3440 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3441 return( ret );
3442}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003443#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003444
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003445static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3446 const unsigned char *p,
3447 const unsigned char *end,
3448 unsigned char *peer_pms,
3449 size_t *peer_pmslen,
3450 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003451{
Janos Follath865b3eb2019-12-16 11:46:15 +00003452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003453 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3454 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3455 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003456
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003457#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003458 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003459 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003460 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003461 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3463 return( ssl_resume_decrypt_pms( ssl,
3464 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003465 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003466#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003467
3468 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003469 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003470 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02003471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003472 if ( p + 2 > end ) {
3473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003474 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003475 }
Joe Subbiani2194dc42021-07-14 12:31:31 +01003476 if( *p++ != MBEDTLS_BYTE_1( len ) ||
3477 *p++ != MBEDTLS_BYTE_0( len ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003478 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003480 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003481 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003482#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003483
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003484 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003487 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003488 }
3489
Gilles Peskine422ccab2018-01-11 18:29:01 +01003490 /*
3491 * Decrypt the premaster secret
3492 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003493#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003494 if( ssl->conf->f_async_decrypt_start != NULL )
3495 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003496 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003497 mbedtls_ssl_own_cert( ssl ),
3498 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003499 switch( ret )
3500 {
3501 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3502 /* act as if f_async_decrypt_start was null */
3503 break;
3504 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003505 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003506 return( ssl_resume_decrypt_pms( ssl,
3507 peer_pms,
3508 peer_pmslen,
3509 peer_pmssize ) );
3510 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003511 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003512 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3513 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003514 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003515 return( ret );
3516 }
3517 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003518#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003519
Gilles Peskine422ccab2018-01-11 18:29:01 +01003520 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3521 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3523 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3524 }
3525
3526 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003527 peer_pms, peer_pmslen, peer_pmssize,
3528 ssl->conf->f_rng, ssl->conf->p_rng );
3529 return( ret );
3530}
3531
3532static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3533 const unsigned char *p,
3534 const unsigned char *end,
3535 size_t pms_offset )
3536{
Janos Follath865b3eb2019-12-16 11:46:15 +00003537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003538 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3539 unsigned char ver[2];
3540 unsigned char fake_pms[48], peer_pms[48];
3541 unsigned char mask;
3542 size_t i, peer_pmslen;
3543 unsigned int diff;
3544
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003545 /* In case of a failure in decryption, the decryption may write less than
3546 * 2 bytes of output, but we always read the first two bytes. It doesn't
3547 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003548 * ret being nonzero, and we only care whether diff is 0.
3549 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3550 * also makes memory analyzers happy (don't access uninitialized memory,
3551 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003552 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003553 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003554
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003555 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3556 peer_pms,
3557 &peer_pmslen,
3558 sizeof( peer_pms ) );
3559
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003560#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003561 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3562 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003563#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003566 ssl->handshake->max_minor_ver,
3567 ssl->conf->transport, ver );
3568
3569 /* Avoid data-dependent branches while checking for invalid
3570 * padding, to protect against timing-based Bleichenbacher-type
3571 * attacks. */
3572 diff = (unsigned int) ret;
3573 diff |= peer_pmslen ^ 48;
3574 diff |= peer_pms[0] ^ ver[0];
3575 diff |= peer_pms[1] ^ ver[1];
3576
3577 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
Gabor Mezei90437e32021-10-20 11:59:27 +02003578 mask = mbedtls_ct_uint_mask( diff );
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003579
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003580 /*
3581 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3582 * must not cause the connection to end immediately; instead, send a
3583 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003584 * To protect against timing-based variants of the attack, we must
3585 * not have any branch that depends on whether the decryption was
3586 * successful. In particular, always generate the fake premaster secret,
3587 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003588 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003589 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003590 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003591 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003592 /* It's ok to abort on an RNG failure, since this does not reveal
3593 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003594 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003595 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003596
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003597#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003598 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003600#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003601
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003602 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3603 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003607 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003608 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003609
Gilles Peskine422ccab2018-01-11 18:29:01 +01003610 /* Set pms to either the true or the fake PMS, without
3611 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003612 for( i = 0; i < ssl->handshake->pmslen; i++ )
3613 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3614
3615 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3618 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003619
Gilles Peskineeccd8882020-03-10 12:19:08 +01003620#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003622 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003623{
Paul Bakker6db455e2013-09-18 17:29:31 +02003624 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003625 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003626
Hanno Becker845b9462018-10-26 12:07:29 +01003627 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3630 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003631 }
3632
3633 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003634 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003635 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003636 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003639 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003640 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003641
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003642 n = ( (*p)[0] << 8 ) | (*p)[1];
3643 *p += 2;
3644
irwir6527bd62019-09-21 18:51:25 +03003645 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003648 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003649 }
3650
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003651 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003652 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003653 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003655 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003656 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003657 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003658 /* Identity is not a big secret since clients send it in the clear,
3659 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003660 if( n != ssl->conf->psk_identity_len ||
Gabor Mezei90437e32021-10-20 11:59:27 +02003661 mbedtls_ct_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003664 }
3665 }
3666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003670 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3671 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003673 }
3674
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003675 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003676
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003677 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003678}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003679#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003682{
Janos Follath865b3eb2019-12-16 11:46:15 +00003683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003685 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003686
Hanno Beckere694c3e2017-12-27 21:34:08 +00003687 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003691#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003692 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3693 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3694 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3695 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003696 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003697 {
3698 /* We've already read a record and there is an asynchronous
3699 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003700 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3702 }
3703 else
3704#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01003705 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 return( ret );
3709 }
3710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003712 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003717 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 }
3719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003723 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724 }
3725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3727 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003729 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003732 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003734
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003735 if( p != end )
3736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003738 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003739 }
3740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003742 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01003743 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003744 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003745 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003748 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003749 }
3750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003752 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003753 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3755#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3756 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3757 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3758 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3759 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3760 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3761 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3762 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003765 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003768 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003769 }
3770
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003771 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3772 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003775 &ssl->handshake->pmslen,
3776 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003778 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003781 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003782 }
3783
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003784 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3785 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003787 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3789 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3790 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3791 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3792#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3793 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003794 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003795 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003798 return( ret );
3799 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003800
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003801 if( p != end )
3802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003804 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003805 }
3806
Hanno Becker845b9462018-10-26 12:07:29 +01003807#if defined(MBEDTLS_USE_PSA_CRYPTO)
3808 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
3809 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00003810 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01003811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
3812 else
3813#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003815 ciphersuite_info->key_exchange ) ) != 0 )
3816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003818 return( ret );
3819 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003821 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3823#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3824 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003825 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003826#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003827 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003828 {
3829 /* There is an asynchronous operation in progress to
3830 * decrypt the encrypted premaster secret, so skip
3831 * directly to resuming this operation. */
3832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
3833 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3834 * won't actually use it, but maintain p anyway for robustness. */
3835 p += ssl->conf->psk_identity_len + 2;
3836 }
3837 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003838#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003839 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003842 return( ret );
3843 }
3844
Hanno Becker845b9462018-10-26 12:07:29 +01003845#if defined(MBEDTLS_USE_PSA_CRYPTO)
3846 /* Opaque PSKs are currently only supported for PSK-only. */
3847 if( ssl_use_opaque_psk( ssl ) == 1 )
3848 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3849#endif
3850
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003851 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003854 return( ret );
3855 }
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003858 ciphersuite_info->key_exchange ) ) != 0 )
3859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003861 return( ret );
3862 }
3863 }
3864 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3866#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3867 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003868 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003869 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003872 return( ret );
3873 }
3874 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003877 return( ret );
3878 }
3879
Hanno Becker845b9462018-10-26 12:07:29 +01003880#if defined(MBEDTLS_USE_PSA_CRYPTO)
3881 /* Opaque PSKs are currently only supported for PSK-only. */
3882 if( ssl_use_opaque_psk( ssl ) == 1 )
3883 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3884#endif
3885
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003886 if( p != end )
3887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003889 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003890 }
3891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003893 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003896 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003897 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003898 }
3899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3901#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3902 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003903 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003904 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003907 return( ret );
3908 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003911 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003914 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003915 }
3916
Hanno Becker845b9462018-10-26 12:07:29 +01003917#if defined(MBEDTLS_USE_PSA_CRYPTO)
3918 /* Opaque PSKs are currently only supported for PSK-only. */
3919 if( ssl_use_opaque_psk( ssl ) == 1 )
3920 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3921#endif
3922
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003923 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3924 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003927 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003930 return( ret );
3931 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003932 }
3933 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3935#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3936 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003937 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003938 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003941 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 }
3943 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003944 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003946#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3947 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3948 {
3949 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3950 p, end - p );
3951 if( ret != 0 )
3952 {
3953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003955 }
3956
3957 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3958 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3959 ssl->conf->f_rng, ssl->conf->p_rng );
3960 if( ret != 0 )
3961 {
3962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3963 return( ret );
3964 }
3965 }
3966 else
3967#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00003974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00003976 return( ret );
3977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003978
Paul Bakker5121ce52009-01-03 21:22:43 +00003979 ssl->state++;
3980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982
3983 return( 0 );
3984}
3985
Gilles Peskineeccd8882020-03-10 12:19:08 +01003986#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003989 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003990 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003993
Hanno Becker77adddc2019-02-07 12:32:43 +00003994 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003997 ssl->state++;
3998 return( 0 );
3999 }
4000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004003}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004004#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004008 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004009 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004010 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004011 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4013 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004016 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004017 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004018 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004021
Hanno Becker2a831a42019-02-07 13:17:25 +00004022 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004025 ssl->state++;
4026 return( 0 );
4027 }
4028
Hanno Becker2a831a42019-02-07 13:17:25 +00004029#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4030 if( ssl->session_negotiate->peer_cert == NULL )
4031 {
4032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4033 ssl->state++;
4034 return( 0 );
4035 }
4036#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4037 if( ssl->session_negotiate->peer_cert_digest == NULL )
4038 {
4039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4040 ssl->state++;
4041 return( 0 );
4042 }
4043#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4044
Simon Butcher99000142016-10-13 17:21:01 +01004045 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004046 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004047 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004049 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 return( ret );
4051 }
4052
4053 ssl->state++;
4054
Simon Butcher99000142016-10-13 17:21:01 +01004055 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4057 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004060 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004061 }
4062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004064
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004065#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4066 peer_pk = &ssl->handshake->peer_pubkey;
4067#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4068 if( ssl->session_negotiate->peer_cert == NULL )
4069 {
4070 /* Should never happen */
4071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4072 }
4073 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4074#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4075
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004076 /*
4077 * struct {
4078 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4079 * opaque signature<0..2^16-1>;
4080 * } DigitallySigned;
4081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4083 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004084 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004085 if( i + 2 > ssl->in_hslen )
4086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004088 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004089 }
4090
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004092 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004093 */
Simon Butcher99000142016-10-13 17:21:01 +01004094 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4095
4096 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004099 " for verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004100 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00004101 }
4102
Simon Butcher99000142016-10-13 17:21:01 +01004103#if !defined(MBEDTLS_MD_SHA1)
4104 if( MBEDTLS_MD_SHA1 == md_alg )
4105 hash_start += 16;
4106#endif
Paul Bakker926af752012-11-23 13:38:07 +01004107
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004108 /* Info from md_alg will be used instead */
4109 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004110
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004111 i++;
4112
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004113 /*
4114 * Signature
4115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4117 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004120 " for verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004121 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004122 }
4123
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004124 /*
4125 * Check the certificate's key type matches the signature alg
4126 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004127 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004130 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004131 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004132
4133 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004134 }
4135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4139 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004140 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004141
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004142 if( i + 2 > ssl->in_hslen )
4143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004145 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004146 }
4147
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004148 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4149 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004150
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004151 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004154 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 }
4156
Simon Butcher99000142016-10-13 17:21:01 +01004157 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004158 {
4159 size_t dummy_hlen;
4160 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4161 }
Simon Butcher99000142016-10-13 17:21:01 +01004162
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004163 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004164 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004165 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004168 return( ret );
4169 }
4170
Simon Butcher99000142016-10-13 17:21:01 +01004171 mbedtls_ssl_update_handshake_status( ssl );
4172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004174
Paul Bakkered27a042013-04-18 22:46:23 +02004175 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004176}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004177#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4180static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004181{
Janos Follath865b3eb2019-12-16 11:46:15 +00004182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004183 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004184 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004188 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4189 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004190
4191 /*
4192 * struct {
4193 * uint32 ticket_lifetime_hint;
4194 * opaque ticket<0..2^16-1>;
4195 * } NewSessionTicket;
4196 *
4197 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4198 * 8 . 9 ticket_len (n)
4199 * 10 . 9+n ticket content
4200 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004201
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004202 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004203 ssl->session_negotiate,
4204 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004205 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004206 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004207 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004209 tlen = 0;
4210 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004211
Joe Subbiani6dd73642021-07-19 11:56:54 +01004212 MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 );
4213 MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004214 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004215
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004216 /*
4217 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4218 * ChangeCipherSpec share the same state.
4219 */
4220 ssl->handshake->new_session_ticket = 0;
4221
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004222 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004223 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004225 return( ret );
4226 }
4227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004229
4230 return( 0 );
4231}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004233
Paul Bakker5121ce52009-01-03 21:22:43 +00004234/*
Paul Bakker1961b702013-01-25 14:49:24 +01004235 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004238{
4239 int ret = 0;
4240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004242
Paul Bakker1961b702013-01-25 14:49:24 +01004243 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 case MBEDTLS_SSL_HELLO_REQUEST:
4246 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 break;
4248
Paul Bakker1961b702013-01-25 14:49:24 +01004249 /*
4250 * <== ClientHello
4251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004253 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004254 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256#if defined(MBEDTLS_SSL_PROTO_DTLS)
4257 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4258 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004259#endif
4260
Paul Bakker1961b702013-01-25 14:49:24 +01004261 /*
4262 * ==> ServerHello
4263 * Certificate
4264 * ( ServerKeyExchange )
4265 * ( CertificateRequest )
4266 * ServerHelloDone
4267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004269 ret = ssl_write_server_hello( ssl );
4270 break;
4271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4273 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004274 break;
4275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004277 ret = ssl_write_server_key_exchange( ssl );
4278 break;
4279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004281 ret = ssl_write_certificate_request( ssl );
4282 break;
4283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004285 ret = ssl_write_server_hello_done( ssl );
4286 break;
4287
4288 /*
4289 * <== ( Certificate/Alert )
4290 * ClientKeyExchange
4291 * ( CertificateVerify )
4292 * ChangeCipherSpec
4293 * Finished
4294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4296 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004297 break;
4298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004300 ret = ssl_parse_client_key_exchange( ssl );
4301 break;
4302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004304 ret = ssl_parse_certificate_verify( ssl );
4305 break;
4306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004307 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4308 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004309 break;
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 case MBEDTLS_SSL_CLIENT_FINISHED:
4312 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004313 break;
4314
4315 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004316 * ==> ( NewSessionTicket )
4317 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004318 * Finished
4319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4321#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004322 if( ssl->handshake->new_session_ticket != 0 )
4323 ret = ssl_write_new_session_ticket( ssl );
4324 else
Paul Bakkera503a632013-08-14 13:48:06 +02004325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004327 break;
4328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 case MBEDTLS_SSL_SERVER_FINISHED:
4330 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004331 break;
4332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 case MBEDTLS_SSL_FLUSH_BUFFERS:
4334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4335 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004336 break;
4337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4339 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004340 break;
4341
4342 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004345 }
4346
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 return( ret );
4348}
TRodziewicz8476f2f2021-06-02 14:34:47 +02004349
TRodziewicz3946f792021-06-14 12:11:18 +02004350void mbedtls_ssl_conf_preference_order( mbedtls_ssl_config *conf, int order )
TRodziewicz8476f2f2021-06-02 14:34:47 +02004351{
TRodziewicz3946f792021-06-14 12:11:18 +02004352 conf->respect_cli_pref = order;
TRodziewicz8476f2f2021-06-02 14:34:47 +02004353}
4354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355#endif /* MBEDTLS_SSL_SRV_C */