blob: 9c1bea2bcfb7951d052d577faf0290835db9422a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
SimonBd5800b72016-04-26 07:43:27 +010024#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
27#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010030#endif
31
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020033#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050036#include "mbedtls/platform_util.h"
gabor-mezei-arm944c1072021-09-27 11:28:54 +020037#include "constant_time.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
39#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010046#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
50int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020051 const unsigned char *info,
52 size_t ilen )
53{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020054 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020058
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020060 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020061
62 memcpy( ssl->cli_id, info, ilen );
63 ssl->cli_id_len = ilen;
64
65 return( 0 );
66}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020067
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020068void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_ssl_cookie_write_t *f_cookie_write,
70 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020071 void *p_cookie )
72{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020073 conf->f_cookie_write = f_cookie_write;
74 conf->f_cookie_check = f_cookie_check;
75 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000081 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000082 size_t len )
83{
Janos Follath865b3eb2019-12-16 11:46:15 +000084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000085 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000086 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010089
Philippe Antoine747fd532018-05-30 09:13:21 +020090 if( len < 2 )
91 {
92 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
93 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
94 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
95 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
96 }
Paul Bakker5701cdc2012-09-27 21:49:42 +000097 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
98 if( servername_list_size + 2 != len )
99 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200101 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
102 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000104 }
105
106 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200107 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000108 {
109 hostname_len = ( ( p[1] << 8 ) | p[2] );
110 if( hostname_len + 3 > servername_list_size )
111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200113 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
114 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000116 }
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000119 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200120 ret = ssl->conf->f_sni( ssl->conf->p_sni,
121 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000122 if( ret != 0 )
123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
125 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
127 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000128 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000129 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000130 }
131
132 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000133 p += hostname_len + 3;
134 }
135
136 if( servername_list_size != 0 )
137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200139 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
140 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000142 }
143
144 return( 0 );
145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000147
Gilles Peskineeccd8882020-03-10 12:19:08 +0100148#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100149static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
150{
151 if( conf->f_psk != NULL )
152 return( 1 );
153
154 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
155 return( 0 );
156
157 if( conf->psk != NULL && conf->psk_len != 0 )
158 return( 1 );
159
160#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +0200161 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100162 return( 1 );
163#endif /* MBEDTLS_USE_PSA_CRYPTO */
164
165 return( 0 );
166}
167
168#if defined(MBEDTLS_USE_PSA_CRYPTO)
169static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
170{
171 if( ssl->conf->f_psk != NULL )
172 {
173 /* If we've used a callback to select the PSK,
174 * the static configuration is irrelevant. */
175
Ronald Croncf56a0a2020-08-04 09:51:30 +0200176 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100177 return( 1 );
178
179 return( 0 );
180 }
181
Ronald Croncf56a0a2020-08-04 09:51:30 +0200182 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100183 return( 1 );
184
185 return( 0 );
186}
187#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100188#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000191 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000192 size_t len )
193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_SSL_RENEGOTIATION)
195 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100196 {
197 /* Check verify-data in constant-time. The length OTOH is no secret */
198 if( len != 1 + ssl->verify_data_len ||
199 buf[0] != ssl->verify_data_len ||
gabor-mezei-arm378e7eb2021-07-19 15:19:19 +0200200 mbedtls_cf_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100201 ssl->verify_data_len ) != 0 )
202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200204 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
205 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100207 }
208 }
209 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000211 {
212 if( len != 1 || buf[0] != 0x0 )
213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200215 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
216 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +0000218 }
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000221 }
Paul Bakker48916f92012-09-16 19:57:18 +0000222
223 return( 0 );
224}
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100227 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100228
229/*
230 * Status of the implementation of signature-algorithms extension:
231 *
232 * Currently, we are only considering the signature-algorithm extension
233 * to pick a ciphersuite which allows us to send the ServerKeyExchange
234 * message with a signature-hash combination that the user allows.
235 *
236 * We do *not* check whether all certificates in our certificate
237 * chain are signed with an allowed signature-hash pair.
238 * This needs to be done at a later stage.
239 *
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000242 const unsigned char *buf,
243 size_t len )
244{
245 size_t sig_alg_list_size;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100246
Paul Bakker23f36802012-09-28 14:15:14 +0000247 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200248 const unsigned char *end = buf + len;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200249
Hanno Becker7e5437a2017-04-28 17:15:26 +0100250 mbedtls_md_type_t md_cur;
251 mbedtls_pk_type_t sig_cur;
Paul Bakker23f36802012-09-28 14:15:14 +0000252
Philippe Antoine747fd532018-05-30 09:13:21 +0200253 if ( len < 2 ) {
254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
255 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
256 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
257 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
258 }
Paul Bakker23f36802012-09-28 14:15:14 +0000259 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
260 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200261 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200264 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
265 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker23f36802012-09-28 14:15:14 +0000267 }
268
Hanno Becker7e5437a2017-04-28 17:15:26 +0100269 /* Currently we only guarantee signing the ServerKeyExchange message according
270 * to the constraints specified in this extension (see above), so it suffices
271 * to remember only one suitable hash for each possible signature algorithm.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200272 *
Hanno Becker7e5437a2017-04-28 17:15:26 +0100273 * This will change when we also consider certificate signatures,
274 * in which case we will need to remember the whole signature-hash
275 * pair list from the extension.
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200276 */
Hanno Becker7e5437a2017-04-28 17:15:26 +0100277
278 for( p = buf + 2; p < end; p += 2 )
279 {
280 /* Silently ignore unknown signature or hash algorithms. */
281
282 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
283 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
285 " unknown sig alg encoding %d", p[1] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100286 continue;
287 }
288
289 /* Check if we support the hash the user proposes */
290 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
291 if( md_cur == MBEDTLS_MD_NONE )
292 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
294 " unknown hash alg encoding %d", p[0] ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100295 continue;
296 }
297
298 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
299 {
300 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
Paul Elliott9f352112020-12-09 14:55:45 +0000302 " match sig %u and hash %u",
Paul Elliott3891caf2020-12-17 18:42:40 +0000303 (unsigned) sig_cur, (unsigned) md_cur ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100304 }
305 else
306 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
Paul Elliott3891caf2020-12-17 18:42:40 +0000308 "hash alg %u not supported", (unsigned) md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000309 }
Paul Bakker23f36802012-09-28 14:15:14 +0000310 }
311
Paul Bakker23f36802012-09-28 14:15:14 +0000312 return( 0 );
313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +0100315 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000316
Robert Cragie136884c2015-10-02 13:34:31 +0100317#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100318 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200320 const unsigned char *buf,
321 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100322{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200323 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100324 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100326
Philippe Antoine747fd532018-05-30 09:13:21 +0200327 if ( len < 2 ) {
328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
329 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
330 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
331 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
332 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100333 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
334 if( list_size + 2 != len ||
335 list_size % 2 != 0 )
336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
339 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100341 }
342
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200343 /* Should never happen unless client duplicates the extension */
344 if( ssl->handshake->curves != NULL )
345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200347 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
348 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200350 }
351
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100352 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200353 * and leave room for a final 0 */
354 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 if( our_size > MBEDTLS_ECP_DP_MAX )
356 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200357
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200358 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200359 {
360 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
361 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200362 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200363 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200364
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200365 ssl->handshake->curves = curves;
366
Paul Bakker41c83d32013-03-20 14:39:14 +0100367 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200368 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200371
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200372 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100373 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200374 *curves++ = curve_info;
375 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100376 }
377
378 list_size -= 2;
379 p += 2;
380 }
381
382 return( 0 );
383}
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200386 const unsigned char *buf,
387 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100388{
389 size_t list_size;
390 const unsigned char *p;
391
Philippe Antoine747fd532018-05-30 09:13:21 +0200392 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
396 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker41c83d32013-03-20 14:39:14 +0100398 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200399 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100400
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200401 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100402 while( list_size > 0 )
403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
405 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100406 {
Robert Cragie136884c2015-10-02 13:34:31 +0100407#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200408 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100409#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100410#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Robert Cragie136884c2015-10-02 13:34:31 +0100411 ssl->handshake->ecjpake_ctx.point_format = p[0];
412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100414 return( 0 );
415 }
416
417 list_size--;
418 p++;
419 }
420
421 return( 0 );
422}
Robert Cragieae8535d2015-10-06 17:11:18 +0100423#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
424 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100425
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200426#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
427static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
428 const unsigned char *buf,
429 size_t len )
430{
Janos Follath865b3eb2019-12-16 11:46:15 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200432
433 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
434 {
435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
436 return( 0 );
437 }
438
439 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
440 buf, len ) ) != 0 )
441 {
442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
444 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200445 return( ret );
446 }
447
448 /* Only mark the extension as OK when we're sure it is */
449 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
450
451 return( 0 );
452}
453#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
456static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200457 const unsigned char *buf,
458 size_t len )
459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200463 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
464 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200466 }
467
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200468 ssl->session_negotiate->mfl_code = buf[0];
469
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200470 return( 0 );
471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200473
Hanno Beckera0e20d02019-05-15 14:03:01 +0100474#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100475static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
476 const unsigned char *buf,
477 size_t len )
478{
479 size_t peer_cid_len;
480
481 /* CID extension only makes sense in DTLS */
482 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
483 {
484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
486 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
487 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
488 }
489
490 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100491 * Quoting draft-ietf-tls-dtls-connection-id-05
492 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100493 *
494 * struct {
495 * opaque cid<0..2^8-1>;
496 * } ConnectionId;
497 */
498
499 if( len < 1 )
500 {
501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
502 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
503 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
504 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
505 }
506
507 peer_cid_len = *buf++;
508 len--;
509
510 if( len != peer_cid_len )
511 {
512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
514 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
515 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
516 }
517
518 /* Ignore CID if the user has disabled its use. */
519 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
520 {
521 /* Leave ssl->handshake->cid_in_use in its default
522 * value of MBEDTLS_SSL_CID_DISABLED. */
523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
524 return( 0 );
525 }
526
527 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
528 {
529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
530 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
531 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
532 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
533 }
534
Hanno Becker08556bf2019-05-03 12:43:44 +0100535 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100536 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
537 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
538
539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
540 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
541
Hanno Becker89dcc882019-04-26 13:56:39 +0100542 return( 0 );
543}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
547static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200548 const unsigned char *buf,
549 size_t len )
550{
551 if( len != 0 )
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200554 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
555 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200557 }
558
559 ((void) buf);
560
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200561 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200563
564 return( 0 );
565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
569static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100570 const unsigned char *buf,
571 size_t len )
572{
573 if( len != 0 )
574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
577 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100579 }
580
581 ((void) buf);
582
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200583 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100587 }
588
589 return( 0 );
590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
594static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200595 const unsigned char *buf,
596 size_t len )
597{
598 if( len != 0 )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200601 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
602 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200604 }
605
606 ((void) buf);
607
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200608 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200612 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200613
614 return( 0 );
615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SSL_SESSION_TICKETS)
619static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200620 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200621 size_t len )
622{
Janos Follath865b3eb2019-12-16 11:46:15 +0000623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200624 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200625
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200626 mbedtls_ssl_session_init( &session );
627
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200628 if( ssl->conf->f_ticket_parse == NULL ||
629 ssl->conf->f_ticket_write == NULL )
630 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200631 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200632 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200633
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200634 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200635 ssl->handshake->new_session_ticket = 1;
636
Paul Elliottd48d5c62021-01-07 14:47:05 +0000637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200638
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200639 if( len == 0 )
640 return( 0 );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#if defined(MBEDTLS_SSL_RENEGOTIATION)
643 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200646 return( 0 );
647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200649
650 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200651 * Failures are ok: just ignore the ticket and proceed.
652 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200653 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
654 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200655 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200656 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200657
658 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
660 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
662 else
663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
664
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200666 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200667
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200668 /*
669 * Keep the session ID sent by the client, since we MUST send it back to
670 * inform them we're accepting the ticket (RFC 5077 section 3.4)
671 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200672 session.id_len = ssl->session_negotiate->id_len;
673 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200674
675 mbedtls_ssl_session_free( ssl->session_negotiate );
676 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
677
678 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500679 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200682
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200683 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200684
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200685 /* Don't send a new ticket after all, this one is OK */
686 ssl->handshake->new_session_ticket = 0;
687
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200688 return( 0 );
689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SSL_ALPN)
693static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200694 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200695{
Paul Bakker14b16c62014-05-28 11:33:54 +0200696 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200697 const unsigned char *theirs, *start, *end;
698 const char **ours;
699
700 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200701 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200702 return( 0 );
703
704 /*
705 * opaque ProtocolName<1..2^8-1>;
706 *
707 * struct {
708 * ProtocolName protocol_name_list<2..2^16-1>
709 * } ProtocolNameList;
710 */
711
712 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
713 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200714 {
715 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
716 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200718 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200719
720 list_len = ( buf[0] << 8 ) | buf[1];
721 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200722 {
723 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
724 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200726 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200727
728 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100729 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200730 */
731 start = buf + 2;
732 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100733 for( theirs = start; theirs != end; theirs += cur_len )
734 {
735 cur_len = *theirs++;
736
737 /* Current identifier must fit in list */
738 if( cur_len > (size_t)( end - theirs ) )
739 {
740 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
741 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
742 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
743 }
744
745 /* Empty strings MUST NOT be included */
746 if( cur_len == 0 )
747 {
748 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
749 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
750 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
751 }
752 }
753
754 /*
755 * Use our order of preference
756 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200757 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200758 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200759 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200760 for( theirs = start; theirs != end; theirs += cur_len )
761 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200762 cur_len = *theirs++;
763
Paul Bakker14b16c62014-05-28 11:33:54 +0200764 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200765 memcmp( theirs, *ours, cur_len ) == 0 )
766 {
767 ssl->alpn_chosen = *ours;
768 return( 0 );
769 }
770 }
771 }
772
773 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
775 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
776 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200779
Johan Pascalb62bb512015-12-03 21:56:45 +0100780#if defined(MBEDTLS_SSL_DTLS_SRTP)
781static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300782 const unsigned char *buf,
783 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100784{
Johan Pascal43f94902020-09-22 12:25:52 +0200785 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100786 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200787 size_t profile_length;
788 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200789 /*! 2 bytes for profile length and 1 byte for mki len */
790 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100791
792 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100793 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
794 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
795 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200796 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100797 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200798 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100799
800 /* RFC5764 section 4.1.1
801 * uint8 SRTPProtectionProfile[2];
802 *
803 * struct {
804 * SRTPProtectionProfiles SRTPProtectionProfiles;
805 * opaque srtp_mki<0..255>;
806 * } UseSRTPData;
807
808 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100809 */
810
Ron Eldoref72faf2018-07-12 11:54:20 +0300811 /*
812 * Min length is 5: at least one protection profile(2 bytes)
813 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200814 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200815 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300816 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200817 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200818 {
819 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
820 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Johan Pascalb62bb512015-12-03 21:56:45 +0100821 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Ron Eldor313d7b52018-12-10 14:56:21 +0200822 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100823
Johan Pascal43f94902020-09-22 12:25:52 +0200824 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200825
Ron Eldoref72faf2018-07-12 11:54:20 +0300826 /* first 2 bytes are protection profile length(in bytes) */
827 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200828 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200829
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200830 /* The profile length cannot be bigger than input buffer size - lengths fields */
831 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200832 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200833 {
834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
835 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
836 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
837 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300838 /*
839 * parse the extension list values are defined in
840 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
841 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200842 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100843 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200844 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200845 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100846
Johan Pascal43f94902020-09-22 12:25:52 +0200847 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300848 {
Johan Pascal43f94902020-09-22 12:25:52 +0200849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
850 mbedtls_ssl_get_srtp_profile_as_string(
851 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300852 }
Johan Pascal85269572020-08-25 10:01:54 +0200853 else
854 {
855 continue;
856 }
Ron Eldor591f1622018-01-22 12:30:04 +0200857 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200858 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200859 {
860 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
861 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200862 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
864 mbedtls_ssl_get_srtp_profile_as_string(
865 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200866 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100867 }
868 }
Johan Pascal43f94902020-09-22 12:25:52 +0200869 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200870 break;
871 }
Johan Pascal042d4562020-08-25 12:14:02 +0200872 buf += profile_length; /* buf points to the mki length */
873 mki_length = *buf;
874 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200875
Johan Pascal042d4562020-08-25 12:14:02 +0200876 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
877 mki_length + profile_length + size_of_lengths != len )
878 {
879 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
880 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
881 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
882 }
883
884 /* Parse the mki only if present and mki is supported locally */
885 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
886 mki_length > 0 )
887 {
888 ssl->dtls_srtp_info.mki_len = mki_length;
889
Johan Pascal5ef72d22020-10-28 17:05:47 +0100890 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300891
Ron Eldoref72faf2018-07-12 11:54:20 +0300892 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
893 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100894 }
895
Johan Pascal042d4562020-08-25 12:14:02 +0200896 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100897}
898#endif /* MBEDTLS_SSL_DTLS_SRTP */
899
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100900/*
901 * Auxiliary functions for ServerHello parsing and related actions
902 */
903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100905/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100906 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_ECDSA_C)
909static int ssl_check_key_curve( mbedtls_pk_context *pk,
910 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 const mbedtls_ecp_curve_info **crv = curves;
913 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100914
915 while( *crv != NULL )
916 {
917 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100918 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100919 crv++;
920 }
921
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100922 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100923}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100925
926/*
927 * Try picking a certificate for this ciphersuite,
928 * return 0 on success and -1 on failure.
929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930static int ssl_pick_cert( mbedtls_ssl_context *ssl,
931 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100934 mbedtls_pk_type_t pk_alg =
935 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200936 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100939 if( ssl->handshake->sni_key_cert != NULL )
940 list = ssl->handshake->sni_key_cert;
941 else
942#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200943 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100946 return( 0 );
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000949
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200950 if( list == NULL )
951 {
952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
953 return( -1 );
954 }
955
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100956 for( cur = list; cur != NULL; cur = cur->next )
957 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400958 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000960 cur->cert );
961
Gilles Peskinee198df52018-01-05 21:17:45 +0100962 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100965 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000966 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100967
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200968 /*
969 * This avoids sending the client a cert it'll reject based on
970 * keyUsage or other extensions.
971 *
972 * It also allows the user to provision different certificates for
973 * different uses based on keyUsage, eg if they want to avoid signing
974 * and decrypting with the same RSA key.
975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100977 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000980 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200981 continue;
982 }
983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_ECDSA_C)
985 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200986 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100989 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000990 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100991#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100992
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100993 /*
994 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
995 * present them a SHA-higher cert rather than failing if it's the only
996 * one we got that satisfies the other conditions.
997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
999 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001000 {
1001 if( fallback == NULL )
1002 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001006 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001007 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001008 }
1009
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001010 /* If we get there, we got a winner */
1011 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001012 }
1013
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001014 if( cur == NULL )
1015 cur = fallback;
1016
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001017 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001018 if( cur != NULL )
1019 {
1020 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001022 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001023 return( 0 );
1024 }
1025
1026 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001027}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001029
1030/*
1031 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1032 * Sets ciphersuite_info only if the suite matches.
1033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1035 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001038
Hanno Becker7e5437a2017-04-28 17:15:26 +01001039#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001040 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001041 mbedtls_pk_type_t sig_type;
1042#endif
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001045 if( suite_info == NULL )
1046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001049 }
1050
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +00001052 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001053
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001054 if( suite_info->min_minor_ver > ssl->minor_ver ||
1055 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001058 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001059 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001062 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001064 return( 0 );
1065#endif
1066
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001067#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001068 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001072 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001073 }
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02001074#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001075
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001076#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1077 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001078 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001079 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1081 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001082 return( 0 );
1083 }
1084#endif
1085
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1088 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001089 ( ssl->handshake->curves == NULL ||
1090 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001093 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001094 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001095 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001096#endif
1097
Gilles Peskineeccd8882020-03-10 12:19:08 +01001098#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001099 /* If the ciphersuite requires a pre-shared key and we don't
1100 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001102 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001105 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001106 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001107#endif
1108
Hanno Becker7e5437a2017-04-28 17:15:26 +01001109#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001110 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001111 /* If the ciphersuite requires signing, check whether
1112 * a suitable hash algorithm is present. */
1113 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1114 {
1115 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1116 if( sig_type != MBEDTLS_PK_NONE &&
1117 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1118 {
1119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001120 "for signature algorithm %u", (unsigned) sig_type ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001121 return( 0 );
1122 }
1123 }
1124
1125#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001126 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001129 /*
1130 * Final check: if ciphersuite requires us to have a
1131 * certificate/key of a particular type:
1132 * - select the appropriate certificate if we have one, or
1133 * - try the next ciphersuite if we don't
1134 * This must be done last since we modify the key_cert list.
1135 */
1136 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001139 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001140 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001141 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001142#endif
1143
1144 *ciphersuite_info = suite_info;
1145 return( 0 );
1146}
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1149static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
Paul Bakker78a8c712013-03-06 17:01:52 +01001150{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001151 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001152 unsigned int i, j;
1153 size_t n;
1154 unsigned int ciph_len, sess_len, chal_len;
1155 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001156 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_RENEGOTIATION)
1162 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001165 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1166 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001168 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001170
1171 buf = ssl->in_hdr;
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
Paul Bakker78a8c712013-03-06 17:01:52 +01001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001176 buf[2] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
Paul Bakker78a8c712013-03-06 17:01:52 +01001178 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
Paul Bakker78a8c712013-03-06 17:01:52 +01001180 buf[3], buf[4] ) );
1181
1182 /*
1183 * SSLv2 Client Hello
1184 *
1185 * Record layer:
1186 * 0 . 1 message length
1187 *
1188 * SSL layer:
1189 * 2 . 2 message type
1190 * 3 . 4 protocol version
1191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1193 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1196 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001197 }
1198
1199 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1200
1201 if( n < 17 || n > 512 )
1202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1204 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001205 }
1206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001208 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1209 ? buf[4] : ssl->conf->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001210
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001211 if( ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker78a8c712013-03-06 17:01:52 +01001212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001214 " [%d:%d] < [%d:%d]",
1215 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001216 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1219 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1220 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker78a8c712013-03-06 17:01:52 +01001221 }
1222
Paul Bakker2fbefde2013-06-29 16:01:15 +02001223 ssl->handshake->max_major_ver = buf[3];
1224 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Paul Bakker78a8c712013-03-06 17:01:52 +01001229 return( ret );
1230 }
1231
1232 ssl->handshake->update_checksum( ssl, buf + 2, n );
1233
1234 buf = ssl->in_msg;
1235 n = ssl->in_left - 5;
1236
1237 /*
1238 * 0 . 1 ciphersuitelist length
1239 * 2 . 3 session id length
1240 * 4 . 5 challenge length
1241 * 6 . .. ciphersuitelist
1242 * .. . .. session id
1243 * .. . .. challenge
1244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
Paul Bakker78a8c712013-03-06 17:01:52 +01001246
1247 ciph_len = ( buf[0] << 8 ) | buf[1];
1248 sess_len = ( buf[2] << 8 ) | buf[3];
1249 chal_len = ( buf[4] << 8 ) | buf[5];
1250
Paul Elliott9f352112020-12-09 14:55:45 +00001251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %u, sess_len: %u, chal_len: %u",
Paul Bakker78a8c712013-03-06 17:01:52 +01001252 ciph_len, sess_len, chal_len ) );
1253
1254 /*
1255 * Make sure each parameter length is valid
1256 */
1257 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1260 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001261 }
1262
1263 if( sess_len > 32 )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001267 }
1268
1269 if( chal_len < 8 || chal_len > 32 )
1270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1272 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001273 }
1274
1275 if( n != 6 + ciph_len + sess_len + chal_len )
1276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1278 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001279 }
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Paul Bakker78a8c712013-03-06 17:01:52 +01001282 buf + 6, ciph_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
Paul Bakker78a8c712013-03-06 17:01:52 +01001284 buf + 6 + ciph_len, sess_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
Paul Bakker78a8c712013-03-06 17:01:52 +01001286 buf + 6 + ciph_len + sess_len, chal_len );
1287
1288 p = buf + 6 + ciph_len;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001289 ssl->session_negotiate->id_len = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001290 memset( ssl->session_negotiate->id, 0,
1291 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001292 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
Paul Bakker78a8c712013-03-06 17:01:52 +01001293
1294 p += sess_len;
1295 memset( ssl->handshake->randbytes, 0, 64 );
1296 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1297
1298 /*
1299 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1300 */
1301 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Paul Bakker78a8c712013-03-06 17:01:52 +01001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1306#if defined(MBEDTLS_SSL_RENEGOTIATION)
1307 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001310 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001311
Gilles Peskinec94f7352017-05-10 16:37:56 +02001312 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1313 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001315 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#endif /* MBEDTLS_SSL_RENEGOTIATION */
1317 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker78a8c712013-03-06 17:01:52 +01001318 break;
1319 }
1320 }
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001323 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1324 {
1325 if( p[0] == 0 &&
Joe Subbianiefb8fae2021-08-20 12:57:09 +01001326 MBEDTLS_GET_UINT16_BE(p, 1) != MBEDTLS_SSL_FALLBACK_SCSV_VALUE )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001329
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001330 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1335 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001338 }
1339
1340 break;
1341 }
1342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001344
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001345 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001346 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001347 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001349 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001350 for( i = 0; ciphersuites[i] != 0; i++ )
1351#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001352 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001353 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001354#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001355 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001356 if( p[0] != 0 ||
Joe Subbianiefb8fae2021-08-20 12:57:09 +01001357 MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i] )
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001358 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001359
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001360 got_common_suite = 1;
1361
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001362 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1363 &ciphersuite_info ) ) != 0 )
1364 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001365
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001366 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001367 goto have_ciphersuite_v2;
1368 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001369
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001370 if( got_common_suite )
1371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001373 "but none of them usable" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001375 }
1376 else
1377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1379 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001380 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001381
1382have_ciphersuite_v2:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001384
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001385 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001386 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001387
1388 /*
1389 * SSLv2 Client Hello relevant renegotiation security checks
1390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001392 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1396 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker78a8c712013-03-06 17:01:52 +01001398 }
1399
1400 ssl->in_left = 0;
1401 ssl->state++;
1402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001404
1405 return( 0 );
1406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
Paul Bakker78a8c712013-03-06 17:01:52 +01001408
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001409/* This function doesn't alert on errors that happen early during
1410 ClientHello parsing because they might indicate that the client is
1411 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001413{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001414 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001415 size_t i, j;
1416 size_t ciph_offset, comp_offset, ext_offset;
1417 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001419 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001420#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001421 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001423 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001424#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001425 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001426 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001428 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
Hanno Becker7e5437a2017-04-28 17:15:26 +01001430 /* If there is no signature-algorithm extension present,
1431 * we need to fall back to the default values for allowed
1432 * signature-hash pairs. */
1433#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001434 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001435 int sig_hash_alg_ext_present = 0;
1436#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001437 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001442read_record_header:
1443#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001444 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001446 * otherwise read it ourselves manually in order to support SSLv2
1447 * ClientHello, which doesn't use the same record layer format.
1448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_RENEGOTIATION)
1450 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001451#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001454 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001455 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001457 return( ret );
1458 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 }
1460
1461 buf = ssl->in_hdr;
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001466#endif
1467 if( ( buf[0] & 0x80 ) != 0 )
Gilles Peskinef9828522017-05-03 12:28:43 +02001468 return( ssl_parse_client_hello_v2( ssl ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001469#endif
1470
Hanno Becker5903de42019-05-03 14:46:38 +01001471 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001472
Paul Bakkerec636f32012-09-09 19:17:02 +00001473 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001474 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001475 *
1476 * Record layer:
1477 * 0 . 0 message type
1478 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001479 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001480 * 3 . 4 message length
1481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001483 buf[0] ) );
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1488 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001489 }
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001492 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001495 buf[1], buf[2] ) );
1496
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001497 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001498
1499 /* According to RFC 5246 Appendix E.1, the version here is typically
1500 * "{03,00}, the lowest version number supported by the client, [or] the
1501 * value of ClientHello.client_version", so the only meaningful check here
1502 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1506 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001509 /* For DTLS if this is the initial handshake, remember the client sequence
1510 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_SSL_RENEGOTIATION)
1514 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001515#endif
1516 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001517 {
1518 /* Epoch should be 0 for initial handshakes */
1519 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1522 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001523 }
1524
Hanno Becker19859472018-08-06 09:40:20 +01001525 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1528 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001531 ssl->next_record_offset = 0;
1532 ssl->in_left = 0;
1533 goto read_record_header;
1534 }
1535
1536 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001538#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001541
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001542 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_SSL_RENEGOTIATION)
1545 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001548 msg_len = ssl->in_hslen;
1549 }
1550 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001551#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001552 {
Angus Grattond8213d02016-05-25 20:56:48 +10001553 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1556 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001557 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001558
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001559 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001560 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001563 return( ret );
1564 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001565
1566 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001569 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001570 else
1571#endif
1572 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001573 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001574
1575 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001578
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001579 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001580
1581 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001582 * Handshake layer:
1583 * 0 . 0 handshake type
1584 * 1 . 3 handshake length
1585 * 4 . 5 DTLS only: message seqence number
1586 * 6 . 8 DTLS only: fragment offset
1587 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1592 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001593 }
1594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1600 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001601 }
1602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001604 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1605
1606 /* We don't support fragmentation of ClientHello (yet?) */
1607 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1611 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001612 }
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001616 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001617 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001618 * Copy the client's handshake message_seq on initial handshakes,
1619 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_SSL_RENEGOTIATION)
1622 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001623 {
1624 /* This couldn't be done in ssl_prepare_handshake_record() */
1625 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1626 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001627
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001628 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001631 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001632 ssl->handshake->in_msg_seq ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001634 }
1635
1636 ssl->handshake->in_msg_seq++;
1637 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001638 else
1639#endif
1640 {
1641 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1642 ssl->in_msg[5];
1643 ssl->handshake->out_msg_seq = cli_msg_seq;
1644 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1645 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001646
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001647 /*
1648 * For now we don't support fragmentation, so make sure
1649 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001650 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001651 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1652 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1655 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001656 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 buf += mbedtls_ssl_hs_hdr_len( ssl );
1661 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001662
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001663 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001664 * ClientHello layer:
1665 * 0 . 1 protocol version
1666 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1667 * 34 . 35 session id length (1 byte)
1668 * 35 . 34+x session id
1669 * 35+x . 35+x DTLS only: cookie length (1 byte)
1670 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001671 * .. . .. ciphersuite list length (2 bytes)
1672 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001673 * .. . .. compression alg. list length (1 byte)
1674 * .. . .. compression alg. list
1675 * .. . .. extensions length (2 bytes, optional)
1676 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001677 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001678
1679 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001680 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001681 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1682 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001683 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001684 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1687 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001688 }
1689
1690 /*
1691 * Check and save the protocol version
1692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001696 ssl->conf->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001697
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001698 ssl->handshake->max_major_ver = ssl->major_ver;
1699 ssl->handshake->max_minor_ver = ssl->minor_ver;
1700
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001701 if( ssl->major_ver < ssl->conf->min_major_ver ||
1702 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001705 " [%d:%d] < [%d:%d]",
1706 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001707 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1709 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001711 }
1712
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001713 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001714 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001715 ssl->major_ver = ssl->conf->max_major_ver;
1716 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001717 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001718 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1719 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001720
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001721 /*
1722 * Save client random (inc. Unix time)
1723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001725
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001726 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001727
1728 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001729 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001730 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001731 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001732
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001733 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001734 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1738 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001740 }
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001743
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001744 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001745 memset( ssl->session_negotiate->id, 0,
1746 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001747 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001748 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001749
1750 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001751 * Check the cookie length and content
1752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001755 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001756 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001757 cookie_len = buf[cookie_offset];
1758
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001759 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001762 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1763 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001765 }
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001768 buf + cookie_offset + 1, cookie_len );
1769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001771 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_SSL_RENEGOTIATION)
1773 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001774#endif
1775 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001776 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001777 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001778 buf + cookie_offset + 1, cookie_len,
1779 ssl->cli_id, ssl->cli_id_len ) != 0 )
1780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001782 ssl->handshake->verify_cookie_len = 1;
1783 }
1784 else
1785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001787 ssl->handshake->verify_cookie_len = 0;
1788 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001789 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001792 {
1793 /* We know we didn't send a cookie, so it should be empty */
1794 if( cookie_len != 0 )
1795 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001796 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1798 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001802 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001803
1804 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001805 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001806 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001807 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001808 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001811 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001812
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001813 ciph_len = ( buf[ciph_offset + 0] << 8 )
1814 | ( buf[ciph_offset + 1] );
1815
1816 if( ciph_len < 2 ||
1817 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1818 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001821 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1822 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001824 }
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001827 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001828
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001829 /*
1830 * Check the compression algorithms length and pick one
1831 */
1832 comp_offset = ciph_offset + 2 + ciph_len;
1833
1834 comp_len = buf[comp_offset];
1835
1836 if( comp_len < 1 ||
1837 comp_len > 16 ||
1838 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001841 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1842 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakkerec636f32012-09-09 19:17:02 +00001844 }
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001847 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1850#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakkerec636f32012-09-09 19:17:02 +00001851 for( i = 0; i < comp_len; ++i )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001856 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 }
1858 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001859#endif
1860
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001861 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001865#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001866
Janos Follathc6dab2b2016-05-23 14:27:02 +01001867 /* Do not parse the extensions if the protocol is SSLv3 */
1868#if defined(MBEDTLS_SSL_PROTO_SSL3)
1869 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1870 {
1871#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001872 /*
1873 * Check the extension length
1874 */
1875 ext_offset = comp_offset + 1 + comp_len;
1876 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001877 {
Simon Butcher584a5472016-05-23 16:24:52 +01001878 if( msg_len < ext_offset + 2 )
1879 {
1880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001881 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1882 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001883 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1884 }
1885
1886 ext_len = ( buf[ext_offset + 0] << 8 )
1887 | ( buf[ext_offset + 1] );
1888
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001889 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001890 {
1891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001892 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1893 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001894 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1895 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001896 }
Simon Butcher584a5472016-05-23 16:24:52 +01001897 else
1898 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001899
Simon Butcher584a5472016-05-23 16:24:52 +01001900 ext = buf + ext_offset + 2;
1901 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001902
Simon Butcher584a5472016-05-23 16:24:52 +01001903 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001904 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001905 unsigned int ext_id;
1906 unsigned int ext_size;
1907 if ( ext_len < 4 ) {
1908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1909 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1910 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1911 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1912 }
1913 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1914 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001915
Simon Butcher584a5472016-05-23 16:24:52 +01001916 if( ext_size + 4 > ext_len )
1917 {
1918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001919 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1920 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001921 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1922 }
1923 switch( ext_id )
1924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001926 case MBEDTLS_TLS_EXT_SERVERNAME:
1927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1928 if( ssl->conf->f_sni == NULL )
1929 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001930
Simon Butcher584a5472016-05-23 16:24:52 +01001931 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1932 if( ret != 0 )
1933 return( ret );
1934 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001936
Simon Butcher584a5472016-05-23 16:24:52 +01001937 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001940 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001941#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001942
Simon Butcher584a5472016-05-23 16:24:52 +01001943 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1944 if( ret != 0 )
1945 return( ret );
1946 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001949 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001950 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1952
Simon Butcher584a5472016-05-23 16:24:52 +01001953 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1954 if( ret != 0 )
1955 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001956
1957 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001958 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01001960 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001961
Robert Cragie136884c2015-10-02 13:34:31 +01001962#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001963 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001964 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001966
Simon Butcher584a5472016-05-23 16:24:52 +01001967 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1968 if( ret != 0 )
1969 return( ret );
1970 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001971
Simon Butcher584a5472016-05-23 16:24:52 +01001972 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1974 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001975
Simon Butcher584a5472016-05-23 16:24:52 +01001976 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1977 if( ret != 0 )
1978 return( ret );
1979 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001980#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1981 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001982
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001983#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001984 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001986
Simon Butcher584a5472016-05-23 16:24:52 +01001987 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1988 if( ret != 0 )
1989 return( ret );
1990 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001991#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001994 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001996
Simon Butcher584a5472016-05-23 16:24:52 +01001997 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1998 if( ret != 0 )
1999 return( ret );
2000 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002004 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
2005 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002006
Simon Butcher584a5472016-05-23 16:24:52 +01002007 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
2008 if( ret != 0 )
2009 return( ret );
2010 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002012
Hanno Beckera0e20d02019-05-15 14:03:01 +01002013#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01002014 case MBEDTLS_TLS_EXT_CID:
2015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2016
2017 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2018 if( ret != 0 )
2019 return( ret );
2020 break;
2021#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01002024 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2025 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002026
Simon Butcher584a5472016-05-23 16:24:52 +01002027 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2028 if( ret != 0 )
2029 return( ret );
2030 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01002034 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2035 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002036
Simon Butcher584a5472016-05-23 16:24:52 +01002037 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
2038 if( ret != 0 )
2039 return( ret );
2040 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01002044 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002046
Simon Butcher584a5472016-05-23 16:24:52 +01002047 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2048 if( ret != 0 )
2049 return( ret );
2050 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01002054 case MBEDTLS_TLS_EXT_ALPN:
2055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002056
Simon Butcher584a5472016-05-23 16:24:52 +01002057 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2058 if( ret != 0 )
2059 return( ret );
2060 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002062
Johan Pascalb62bb512015-12-03 21:56:45 +01002063#if defined(MBEDTLS_SSL_DTLS_SRTP)
2064 case MBEDTLS_TLS_EXT_USE_SRTP:
2065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02002066
Johan Pascalc3ccd982020-10-28 17:18:18 +01002067 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2068 if( ret != 0 )
2069 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01002070 break;
2071#endif /* MBEDTLS_SSL_DTLS_SRTP */
2072
Simon Butcher584a5472016-05-23 16:24:52 +01002073 default:
Paul Elliott9f352112020-12-09 14:55:45 +00002074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01002075 ext_id ) );
2076 }
2077
2078 ext_len -= 4 + ext_size;
2079 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00002080 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01002081#if defined(MBEDTLS_SSL_PROTO_SSL3)
2082 }
2083#endif
2084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
Gilles Peskined50177f2017-05-16 17:53:03 +02002086 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002087 {
Joe Subbianiefb8fae2021-08-20 12:57:09 +01002088 if( MBEDTLS_GET_UINT16_BE( p, 0 ) == MBEDTLS_SSL_FALLBACK_SCSV_VALUE )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002089 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002091
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002092 if( ssl->minor_ver < ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002093 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2097 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002100 }
2101
2102 break;
2103 }
2104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01002106
Hanno Becker7e5437a2017-04-28 17:15:26 +01002107#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002108 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002109
2110 /*
2111 * Try to fall back to default hash SHA1 if the client
2112 * hasn't provided any preferred signature-hash combinations.
2113 */
2114 if( sig_hash_alg_ext_present == 0 )
2115 {
2116 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2117
2118 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2119 md_default = MBEDTLS_MD_NONE;
2120
2121 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2122 }
2123
2124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01002125 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01002126
Paul Bakker48916f92012-09-16 19:57:18 +00002127 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002128 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2129 */
2130 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2135#if defined(MBEDTLS_SSL_RENEGOTIATION)
2136 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002137 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2139 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2141 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002143 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00002144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002146 break;
2147 }
2148 }
2149
2150 /*
Paul Bakker48916f92012-09-16 19:57:18 +00002151 * Renegotiation security checks
2152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002154 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002157 handshake_failure = 1;
2158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#if defined(MBEDTLS_SSL_RENEGOTIATION)
2160 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2161 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002162 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002165 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00002166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2168 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002169 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002172 handshake_failure = 1;
2173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2175 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002176 renegotiation_info_seen == 1 )
2177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002179 handshake_failure = 1;
2180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002182
2183 if( handshake_failure == 1 )
2184 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02002185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2186 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00002188 }
Paul Bakker380da532012-04-18 16:10:25 +00002189
Paul Bakker41c83d32013-03-20 14:39:14 +01002190 /*
2191 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002192 * (At the end because we need information from the EC-based extensions
2193 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002194 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002195 got_common_suite = 0;
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002196 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002197 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002199 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002200 for( i = 0; ciphersuites[i] != 0; i++ )
2201#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002202 for( i = 0; ciphersuites[i] != 0; i++ )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002203 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002204#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002205 {
Joe Subbianiefb8fae2021-08-20 12:57:09 +01002206 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002207 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002208
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002209 got_common_suite = 1;
2210
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002211 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2212 &ciphersuite_info ) ) != 0 )
2213 return( ret );
2214
2215 if( ciphersuite_info != NULL )
2216 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002217 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002218
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002219 if( got_common_suite )
2220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002222 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002223 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2224 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002226 }
2227 else
2228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02002230 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2231 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002233 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002234
2235have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002237
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002238 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00002239 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01002240
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 ssl->state++;
2242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002246#endif
2247
Hanno Becker7e5437a2017-04-28 17:15:26 +01002248 /* Debugging-only output for testsuite */
2249#if defined(MBEDTLS_DEBUG_C) && \
2250 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01002251 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01002252 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2253 {
2254 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2255 if( sig_alg != MBEDTLS_PK_NONE )
2256 {
2257 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2258 sig_alg );
2259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2260 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2261 }
2262 else
2263 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00002265 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01002266 }
2267 }
2268#endif
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
2272 return( 0 );
2273}
2274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2276static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002277 unsigned char *buf,
2278 size_t *olen )
2279{
2280 unsigned char *p = buf;
2281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002283 {
2284 *olen = 0;
2285 return;
2286 }
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002289
Joe Subbiani2f98d792021-08-20 11:44:44 +01002290 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0 );
2291 p += 2;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002292
2293 *p++ = 0x00;
2294 *p++ = 0x00;
2295
2296 *olen = 4;
2297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002299
Hanno Beckera0e20d02019-05-15 14:03:01 +01002300#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002301static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2302 unsigned char *buf,
2303 size_t *olen )
2304{
2305 unsigned char *p = buf;
2306 size_t ext_len;
2307 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2308
2309 *olen = 0;
2310
2311 /* Skip writing the extension if we don't want to use it or if
2312 * the client hasn't offered it. */
2313 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2314 return;
2315
2316 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2317 * which is at most 255, so the increment cannot overflow. */
2318 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2319 {
2320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2321 return;
2322 }
2323
2324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2325
2326 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002327 * Quoting draft-ietf-tls-dtls-connection-id-05
2328 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002329 *
2330 * struct {
2331 * opaque cid<0..2^8-1>;
2332 * } ConnectionId;
2333 */
Joe Subbiani2f98d792021-08-20 11:44:44 +01002334 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
2335 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002336 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani2f98d792021-08-20 11:44:44 +01002337 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
2338 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002339
2340 *p++ = (uint8_t) ssl->own_cid_len;
2341 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2342
2343 *olen = ssl->own_cid_len + 5;
2344}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002345#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2348static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002349 unsigned char *buf,
2350 size_t *olen )
2351{
2352 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2354 const mbedtls_cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002355
Hanno Becker27b34d52017-10-20 14:24:51 +01002356 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002358 {
2359 *olen = 0;
2360 return;
2361 }
2362
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002363 /*
2364 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2365 * from a client and then selects a stream or Authenticated Encryption
2366 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2367 * encrypt-then-MAC response extension back to the client."
2368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002370 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2372 cipher->mode != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002373 {
2374 *olen = 0;
2375 return;
2376 }
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002379
Joe Subbiani2f98d792021-08-20 11:44:44 +01002380 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
2381 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002382
2383 *p++ = 0x00;
2384 *p++ = 0x00;
2385
2386 *olen = 4;
2387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2391static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002392 unsigned char *buf,
2393 size_t *olen )
2394{
2395 unsigned char *p = buf;
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2398 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002399 {
2400 *olen = 0;
2401 return;
2402 }
2403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002405 "extension" ) );
2406
Joe Subbiani2f98d792021-08-20 11:44:44 +01002407 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
2408 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002409
2410 *p++ = 0x00;
2411 *p++ = 0x00;
2412
2413 *olen = 4;
2414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2418static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002419 unsigned char *buf,
2420 size_t *olen )
2421{
2422 unsigned char *p = buf;
2423
2424 if( ssl->handshake->new_session_ticket == 0 )
2425 {
2426 *olen = 0;
2427 return;
2428 }
2429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002431
Joe Subbiani2f98d792021-08-20 11:44:44 +01002432 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
2433 p += 2;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002434
2435 *p++ = 0x00;
2436 *p++ = 0x00;
2437
2438 *olen = 4;
2439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002443 unsigned char *buf,
2444 size_t *olen )
2445{
2446 unsigned char *p = buf;
2447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002449 {
2450 *olen = 0;
2451 return;
2452 }
2453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002455
Joe Subbiani2f98d792021-08-20 11:44:44 +01002456 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
2457 p += 2;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#if defined(MBEDTLS_SSL_RENEGOTIATION)
2460 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002461 {
2462 *p++ = 0x00;
2463 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2464 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002465
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002466 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2467 p += ssl->verify_data_len;
2468 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2469 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002470 }
2471 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002473 {
2474 *p++ = 0x00;
2475 *p++ = 0x01;
2476 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002477 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002478
2479 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002480}
2481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2483static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002484 unsigned char *buf,
2485 size_t *olen )
2486{
2487 unsigned char *p = buf;
2488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002490 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002491 *olen = 0;
2492 return;
2493 }
2494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002496
Joe Subbiani2f98d792021-08-20 11:44:44 +01002497 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
2498 p += 2;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002499
2500 *p++ = 0x00;
2501 *p++ = 1;
2502
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002503 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002504
2505 *olen = 5;
2506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002508
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002509#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002510 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002512 unsigned char *buf,
2513 size_t *olen )
2514{
2515 unsigned char *p = buf;
2516 ((void) ssl);
2517
Paul Bakker677377f2013-10-28 12:54:26 +01002518 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002520 {
2521 *olen = 0;
2522 return;
2523 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002526
Joe Subbiani2f98d792021-08-20 11:44:44 +01002527 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
2528 p += 2;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002529
2530 *p++ = 0x00;
2531 *p++ = 2;
2532
2533 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002535
2536 *olen = 6;
2537}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002538#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002539
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002540#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2541static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2542 unsigned char *buf,
2543 size_t *olen )
2544{
Janos Follath865b3eb2019-12-16 11:46:15 +00002545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002546 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002547 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002548 size_t kkpp_len;
2549
2550 *olen = 0;
2551
2552 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002553 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002554 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2555 return;
2556
2557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2558
2559 if( end - p < 4 )
2560 {
2561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2562 return;
2563 }
2564
Joe Subbiani2f98d792021-08-20 11:44:44 +01002565 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
2566 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002567
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002568 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2569 p + 2, end - p - 2, &kkpp_len,
2570 ssl->conf->f_rng, ssl->conf->p_rng );
2571 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002572 {
2573 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2574 return;
2575 }
2576
Joe Subbiani2f98d792021-08-20 11:44:44 +01002577 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
2578 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002579
2580 *olen = kkpp_len + 4;
2581}
2582#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#if defined(MBEDTLS_SSL_ALPN )
2585static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002586 unsigned char *buf, size_t *olen )
2587{
2588 if( ssl->alpn_chosen == NULL )
2589 {
2590 *olen = 0;
2591 return;
2592 }
2593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002595
2596 /*
2597 * 0 . 1 ext identifier
2598 * 2 . 3 ext length
2599 * 4 . 5 protocol list length
2600 * 6 . 6 protocol name length
2601 * 7 . 7+n protocol name
2602 */
Joe Subbianic54e9082021-07-19 11:56:54 +01002603 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002604
2605 *olen = 7 + strlen( ssl->alpn_chosen );
2606
Joe Subbianic54e9082021-07-19 11:56:54 +01002607 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002608
Joe Subbianic54e9082021-07-19 11:56:54 +01002609 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002610
Joe Subbianic045dc12021-07-14 12:31:31 +01002611 buf[6] = MBEDTLS_BYTE_0( *olen - 7 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002612
2613 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002616
Ron Eldor3adb9922017-12-21 10:15:08 +02002617#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002618static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002619 unsigned char *buf,
2620 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002621{
Ron Eldor75870ec2018-12-06 17:31:55 +02002622 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002623 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002624 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2625
2626 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002627
Johan Pascalc3ccd982020-10-28 17:18:18 +01002628 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2629 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002630 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002631 return;
2632 }
2633
2634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2635
Johan Pascald576fdb2020-09-22 10:39:53 +02002636 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002637 {
2638 mki_len = ssl->dtls_srtp_info.mki_len;
2639 }
2640
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002641 /* The extension total size is 9 bytes :
2642 * - 2 bytes for the extension tag
2643 * - 2 bytes for the total size
2644 * - 2 bytes for the protection profile length
2645 * - 2 bytes for the protection profile
2646 * - 1 byte for the mki length
2647 * + the actual mki length
2648 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002649 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002650 {
2651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2652 return;
2653 }
2654
Johan Pascalb62bb512015-12-03 21:56:45 +01002655 /* extension */
Joe Subbianic54e9082021-07-19 11:56:54 +01002656 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 );
Ron Eldoref72faf2018-07-12 11:54:20 +03002657 /*
2658 * total length 5 and mki value: only one profile(2 bytes)
2659 * and length(2 bytes) and srtp_mki )
2660 */
Ron Eldor591f1622018-01-22 12:30:04 +02002661 ext_len = 5 + mki_len;
Joe Subbianic54e9082021-07-19 11:56:54 +01002662 MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 );
Johan Pascalb62bb512015-12-03 21:56:45 +01002663
2664 /* protection profile length: 2 */
2665 buf[4] = 0x00;
2666 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002667 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002668 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002669 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002670 {
Joe Subbianic54e9082021-07-19 11:56:54 +01002671 MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002672 }
2673 else
2674 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002676 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002677 }
2678
Ron Eldor591f1622018-01-22 12:30:04 +02002679 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002680 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002681
Ron Eldor591f1622018-01-22 12:30:04 +02002682 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002683}
2684#endif /* MBEDTLS_SSL_DTLS_SRTP */
2685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2687static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002688{
Janos Follath865b3eb2019-12-16 11:46:15 +00002689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002690 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002691 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002694
2695 /*
2696 * struct {
2697 * ProtocolVersion server_version;
2698 * opaque cookie<0..2^8-1>;
2699 * } HelloVerifyRequest;
2700 */
2701
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002702 /* The RFC is not clear on this point, but sending the actual negotiated
2703 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002705 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002707 p += 2;
2708
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002709 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002710 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002714 }
2715
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002716 /* Skip length byte until we know the length */
2717 cookie_len_byte = p++;
2718
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002719 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002720 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002721 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002724 return( ret );
2725 }
2726
2727 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002730
2731 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2733 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002736
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002737 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002738 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002740 return( ret );
2741 }
2742
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002743#if defined(MBEDTLS_SSL_PROTO_DTLS)
2744 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2745 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2746 {
2747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2748 return( ret );
2749 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002750#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002753
2754 return( 0 );
2755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002757
Hanno Becker83e36712021-04-15 08:19:40 +01002758static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
2759{
2760 int ret;
2761 mbedtls_ssl_session session_tmp;
2762 mbedtls_ssl_session * const session = ssl->session_negotiate;
2763
2764 /* Resume is 0 by default, see ssl_handshake_init().
2765 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2766 if( ssl->handshake->resume == 1 )
2767 return;
2768 if( session->id_len == 0 )
2769 return;
2770 if( ssl->conf->f_get_cache == NULL )
2771 return;
2772#if defined(MBEDTLS_SSL_RENEGOTIATION)
2773 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2774 return;
2775#endif
2776
2777 mbedtls_ssl_session_init( &session_tmp );
2778
2779 session_tmp.id_len = session->id_len;
2780 memcpy( session_tmp.id, session->id, session->id_len );
2781
2782 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
2783 &session_tmp );
2784 if( ret != 0 )
2785 goto exit;
2786
2787 if( session->ciphersuite != session_tmp.ciphersuite ||
2788 session->compression != session_tmp.compression )
2789 {
2790 /* Mismatch between cached and negotiated session */
2791 goto exit;
2792 }
2793
2794 /* Move semantics */
2795 mbedtls_ssl_session_free( session );
2796 *session = session_tmp;
2797 memset( &session_tmp, 0, sizeof( session_tmp ) );
2798
2799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2800 ssl->handshake->resume = 1;
2801
2802exit:
2803
2804 mbedtls_ssl_session_free( &session_tmp );
2805}
2806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002810 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002811#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002813 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 unsigned char *buf, *p;
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002819 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002820 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002824
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002825 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002828
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002829 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2832 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002833 }
2834
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 /*
2836 * 0 . 0 handshake type
2837 * 1 . 3 handshake length
2838 * 4 . 5 protocol version
2839 * 6 . 9 UNIX time()
2840 * 10 . 37 random bytes
2841 */
2842 buf = ssl->out_msg;
2843 p = buf + 4;
2844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002846 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002847 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002850 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002853 t = mbedtls_time( NULL );
Joe Subbiani2f98d792021-08-20 11:44:44 +01002854 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
2855 p += 4;
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Paul Elliottd48d5c62021-01-07 14:47:05 +00002857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2858 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002859#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002860 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002861 return( ret );
2862
2863 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002866 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002867 return( ret );
2868
2869 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker48916f92012-09-16 19:57:18 +00002871 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Hanno Becker83e36712021-04-15 08:19:40 +01002875 ssl_handle_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002877 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 {
2879 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002880 * New session, create a new session id,
2881 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 ssl->state++;
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002886 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002887#endif
2888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002890 if( ssl->handshake->new_session_ticket != 0 )
2891 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002892 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002893 memset( ssl->session_negotiate->id, 0, 32 );
2894 }
2895 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002897 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002898 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002899 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002900 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002901 return( ret );
2902 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 }
2904 else
2905 {
2906 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002907 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002909 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002915 return( ret );
2916 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 }
2918
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002919 /*
2920 * 38 . 38 session id length
2921 * 39 . 38+n session id
2922 * 39+n . 40+n chosen ciphersuite
2923 * 41+n . 41+n chosen compression alg.
2924 * 42+n . 43+n extensions length
2925 * 44+n . 43+n+m extensions
2926 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002927 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2928 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2929 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Paul Elliottd48d5c62021-01-07 14:47:05 +00002931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2933 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002934 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Joe Subbiani2f98d792021-08-20 11:44:44 +01002936 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
2937 p += 2;
Joe Subbianiad1115a2021-07-16 14:27:50 +01002938 *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2941 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002943 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002944
Janos Follathc6dab2b2016-05-23 14:27:02 +01002945 /* Do not write the extensions if the protocol is SSLv3 */
2946#if defined(MBEDTLS_SSL_PROTO_SSL3)
2947 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2948 {
2949#endif
2950
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002951 /*
2952 * First write extensions, then the total length
2953 */
2954 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2955 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002958 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2959 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002960#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002963 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2964 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002965#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002966
Hanno Beckera0e20d02019-05-15 14:03:01 +01002967#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002968 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2969 ext_len += olen;
2970#endif
2971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002973 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2974 ext_len += olen;
2975#endif
2976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002978 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2979 ext_len += olen;
2980#endif
2981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002983 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2984 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002985#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002986
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002987#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002988 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002989 if ( mbedtls_ssl_ciphersuite_uses_ec(
2990 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2991 {
2992 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2993 ext_len += olen;
2994 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002995#endif
2996
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002997#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2998 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2999 ext_len += olen;
3000#endif
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02003003 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
3004 ext_len += olen;
3005#endif
3006
Johan Pascalb62bb512015-12-03 21:56:45 +01003007#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01003008 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
3009 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01003010#endif
3011
Paul Elliottd48d5c62021-01-07 14:47:05 +00003012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
3013 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003014
Paul Bakkera7036632014-04-30 10:15:38 +02003015 if( ext_len > 0 )
3016 {
Joe Subbiani2f98d792021-08-20 11:44:44 +01003017 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani24647c52021-08-20 15:56:22 +01003018 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02003019 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Janos Follathc6dab2b2016-05-23 14:27:02 +01003021#if defined(MBEDTLS_SSL_PROTO_SSL3)
3022 }
3023#endif
3024
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3027 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003029 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
3033 return( ret );
3034}
3035
Gilles Peskineeccd8882020-03-10 12:19:08 +01003036#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003039 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003040 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003043
Hanno Becker77adddc2019-02-07 12:32:43 +00003044 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003047 ssl->state++;
3048 return( 0 );
3049 }
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003053}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003054#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003058 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003059 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03003060 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003061 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00003062 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10003063 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003065 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
3069 ssl->state++;
3070
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003071#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3072 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3073 authmode = ssl->handshake->sni_authmode;
3074 else
3075#endif
3076 authmode = ssl->conf->authmode;
3077
Hanno Becker77adddc2019-02-07 12:32:43 +00003078 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003079 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 {
Johan Pascal4f099262020-09-22 10:59:26 +02003081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3082 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 }
3084
3085 /*
3086 * 0 . 0 handshake type
3087 * 1 . 3 handshake length
3088 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01003089 * 5 .. m-1 cert types
3090 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02003091 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 * n .. n+1 length of all DNs
3093 * n+2 .. n+3 length of DN 1
3094 * n+4 .. ... Distinguished Name #1
3095 * ... .. ... length of DN 2, etc.
3096 */
3097 buf = ssl->out_msg;
3098 p = buf + 4;
3099
3100 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003101 * Supported certificate types
3102 *
3103 * ClientCertificateType certificate_types<1..2^8-1>;
3104 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003106 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01003107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108#if defined(MBEDTLS_RSA_C)
3109 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111#if defined(MBEDTLS_ECDSA_C)
3112 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003113#endif
3114
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003115 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003116 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01003117
Paul Bakker577e0062013-08-28 11:57:20 +02003118 sa_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01003120 /*
3121 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01003122 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003123 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3124 *
3125 * struct {
3126 * HashAlgorithm hash;
3127 * SignatureAlgorithm signature;
3128 * } SignatureAndHashAlgorithm;
3129 *
3130 * enum { (255) } HashAlgorithm;
3131 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01003132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003134 {
Simon Butcher99000142016-10-13 17:21:01 +01003135 const int *cur;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003136
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003137 /*
3138 * Supported signature algorithms
3139 */
Simon Butcher99000142016-10-13 17:21:01 +01003140 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3141 {
3142 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3143
3144 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3145 continue;
3146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#if defined(MBEDTLS_RSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003148 p[2 + sa_len++] = hash;
3149 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_ECDSA_C)
Simon Butcher99000142016-10-13 17:21:01 +01003152 p[2 + sa_len++] = hash;
3153 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003154#endif
Simon Butcher99000142016-10-13 17:21:01 +01003155 }
Paul Bakker926af752012-11-23 13:38:07 +01003156
Joe Subbianic54e9082021-07-19 11:56:54 +01003157 MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003158 sa_len += 2;
3159 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01003160 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003163 /*
3164 * DistinguishedName certificate_authorities<0..2^16-1>;
3165 * opaque DistinguishedName<1..2^16-1>;
3166 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
Paul Bakkerbc3d9842012-11-26 16:12:02 +01003169 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01003170
3171 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00003173 /* NOTE: If trusted certificates are provisioned
3174 * via a CA callback (configured through
3175 * `mbedtls_ssl_conf_ca_cb()`, then the
3176 * CertificateRequest is currently left empty. */
3177
Janos Follath088ce432017-04-10 12:42:31 +01003178#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3179 if( ssl->handshake->sni_ca_chain != NULL )
3180 crt = ssl->handshake->sni_ca_chain;
3181 else
3182#endif
3183 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003184
Janos Follath088ce432017-04-10 12:42:31 +01003185 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003186 {
irwirc9bc3002020-04-01 13:46:36 +03003187 /* It follows from RFC 5280 A.1 that this length
3188 * can be represented in at most 11 bits. */
3189 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01003190
irwirc9bc3002020-04-01 13:46:36 +03003191 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01003192 {
3193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3194 break;
3195 }
3196
Joe Subbiani2f98d792021-08-20 11:44:44 +01003197 MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 );
3198 p += 2;
Janos Follath088ce432017-04-10 12:42:31 +01003199 memcpy( p, crt->subject_raw.p, dn_size );
3200 p += dn_size;
3201
3202 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3203
3204 total_dn_size += 2 + dn_size;
3205 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02003206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 }
3208
Paul Bakker926af752012-11-23 13:38:07 +01003209 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3211 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Joe Subbianic54e9082021-07-19 11:56:54 +01003212 MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003214 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
3218 return( ret );
3219}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003220#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3223 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3224static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003225{
Janos Follath865b3eb2019-12-16 11:46:15 +00003226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3231 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003232 }
3233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3235 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3236 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003239 return( ret );
3240 }
3241
3242 return( 0 );
3243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3245 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003246
Gilles Peskineeccd8882020-03-10 12:19:08 +01003247#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003248 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003249static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003250 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01003251{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003252 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3253 * signature length which will be added in ssl_write_server_key_exchange
3254 * after the call to ssl_prepare_server_key_exchange.
3255 * ssl_write_server_key_exchange also takes care of incrementing
3256 * ssl->out_msglen. */
3257 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10003258 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003259 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02003260 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003261 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003262 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3263 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003264 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003265 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003266 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01003267 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003268 return( ret );
3269}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003270#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003271 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003272
Gilles Peskined3eb0612018-01-08 17:07:44 +01003273/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02003274 * calculating the signature if any, but excluding formatting the
3275 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003276static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3277 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00003278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003280 ssl->handshake->ciphersuite_info;
3281
Gilles Peskineeccd8882020-03-10 12:19:08 +01003282#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3283#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01003284 unsigned char *dig_signed = NULL;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003285#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3286#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003287
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003288 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003289#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02003290 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003291#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003292
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003293 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003295 /*
3296 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003297 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003298 *
3299 */
3300
3301 /*
3302 * - ECJPAKE key exchanges
3303 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003304#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3305 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3306 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003308 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003309
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003310 ret = mbedtls_ecjpake_write_round_two(
3311 &ssl->handshake->ecjpake_ctx,
3312 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003313 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003314 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003315 if( ret != 0 )
3316 {
3317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3318 return( ret );
3319 }
3320
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003321 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003322 }
3323#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3324
Hanno Becker1aa267c2017-04-28 17:08:27 +01003325 /*
3326 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3327 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3328 * we use empty support identity hints here.
3329 **/
3330#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3332 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3333 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003334 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003335 ssl->out_msg[ssl->out_msglen++] = 0x00;
3336 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003337 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3339 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003340
Hanno Becker7e5437a2017-04-28 17:15:26 +01003341 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003342 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003343 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003344#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003345 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003346 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003348 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003349
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003350 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3351 {
3352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3354 }
3355
Paul Bakker41c83d32013-03-20 14:39:14 +01003356 /*
3357 * Ephemeral DH parameters:
3358 *
3359 * struct {
3360 * opaque dh_p<1..2^16-1>;
3361 * opaque dh_g<1..2^16-1>;
3362 * opaque dh_Ys<1..2^16-1>;
3363 * } ServerDHParams;
3364 */
Hanno Beckerab740562017-10-04 13:15:37 +01003365 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3366 &ssl->conf->dhm_P,
3367 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003368 {
Hanno Beckerab740562017-10-04 13:15:37 +01003369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003370 return( ret );
3371 }
Paul Bakker48916f92012-09-16 19:57:18 +00003372
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003373 if( ( ret = mbedtls_dhm_make_params(
3374 &ssl->handshake->dhm_ctx,
3375 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3376 ssl->out_msg + ssl->out_msglen, &len,
3377 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003380 return( ret );
3381 }
3382
Gilles Peskineeccd8882020-03-10 12:19:08 +01003383#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003384 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003385#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003386
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003387 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3390 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3391 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3392 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003393 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003394#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003395
Hanno Becker1aa267c2017-04-28 17:08:27 +01003396 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003397 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003398 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003399#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003400 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003402 /*
3403 * Ephemeral ECDH parameters:
3404 *
3405 * struct {
3406 * ECParameters curve_params;
3407 * ECPoint public;
3408 * } ServerECDHParams;
3409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 const mbedtls_ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 const mbedtls_ecp_group_id *gid;
Janos Follath865b3eb2019-12-16 11:46:15 +00003412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003413 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003414
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003415 /* Match our preference list against the offered curves */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003416 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003417 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3418 if( (*curve)->grp_id == *gid )
3419 goto curve_matching_done;
3420
3421curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003422 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3425 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01003426 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003429
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003430 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3431 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003432 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003434 return( ret );
3435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003436
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003437 if( ( ret = mbedtls_ecdh_make_params(
3438 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003439 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003440 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003441 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003444 return( ret );
3445 }
3446
Gilles Peskineeccd8882020-03-10 12:19:08 +01003447#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003448 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003449#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003450
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003451 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003452
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003453 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3454 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003455 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003456#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003457
Hanno Becker1aa267c2017-04-28 17:08:27 +01003458 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003459 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003460 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003461 * exchange parameters, compute and add the signature here.
3462 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003463 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003464#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003465 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003466 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003467 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003468 size_t hashlen = 0;
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003469 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +00003470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003471
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003472 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003473 * 2.1: Choose hash algorithm:
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003474 * A: For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003475 * to choose appropriate hash.
3476 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3477 * (RFC 4492, Sec. 5.4)
3478 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003479 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003480
3481 mbedtls_md_type_t md_alg;
3482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003484 mbedtls_pk_type_t sig_alg =
3485 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003487 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003488 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3489 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003490 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003491 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3492 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003495 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003496 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003498 }
3499 }
Paul Bakker577e0062013-08-28 11:57:20 +02003500 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3502#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3503 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003504 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003505 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003506 /* B: Default hash SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003508 }
3509 else
Hanno Becker1aa267c2017-04-28 17:08:27 +01003510#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3511 MBEDTLS_SSL_PROTO_TLS1_1 */
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003512 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003513 /* C: MD5 + SHA1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003515 }
3516
Paul Elliott3891caf2020-12-17 18:42:40 +00003517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003518
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003519 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003520 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3523 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3524 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003525 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003526 hashlen = 36;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003527 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3528 dig_signed,
3529 dig_signed_len );
3530 if( ret != 0 )
3531 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003532 }
3533 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3535 MBEDTLS_SSL_PROTO_TLS1_1 */
3536#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3537 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3538 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003539 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003540 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003541 dig_signed,
3542 dig_signed_len,
3543 md_alg );
3544 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003545 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003546 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003547 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3549 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003553 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003554
Gilles Peskineebd652f2018-01-05 21:18:59 +01003555 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003556
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003557 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003558 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3561 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003562 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003563 /*
3564 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003565 * explicitly through a prefix to the signature.
3566 *
3567 * struct {
3568 * HashAlgorithm hash;
3569 * SignatureAlgorithm signature;
3570 * } SignatureAndHashAlgorithm;
3571 *
3572 * struct {
3573 * SignatureAndHashAlgorithm algorithm;
3574 * opaque signature<0..2^16-1>;
3575 * } DigitallySigned;
3576 *
3577 */
3578
Gilles Peskine1004c192018-01-08 16:59:14 +01003579 ssl->out_msg[ssl->out_msglen++] =
3580 mbedtls_ssl_hash_from_md_alg( md_alg );
3581 ssl->out_msg[ssl->out_msglen++] =
3582 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003585
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003586#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003587 if( ssl->conf->f_async_sign_start != NULL )
3588 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003589 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003590 mbedtls_ssl_own_cert( ssl ),
3591 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003592 switch( ret )
3593 {
3594 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3595 /* act as if f_async_sign was null */
3596 break;
3597 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003598 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003599 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003600 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003601 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003602 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3603 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003604 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003605 return( ret );
3606 }
3607 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003608#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003609
3610 if( mbedtls_ssl_own_key( ssl ) == NULL )
3611 {
3612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3613 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3614 }
3615
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003616 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3617 * signature length which will be added in ssl_write_server_key_exchange
3618 * after the call to ssl_prepare_server_key_exchange.
3619 * ssl_write_server_key_exchange also takes care of incrementing
3620 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003621 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3622 md_alg, hash, hashlen,
3623 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003624 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003625 ssl->conf->f_rng,
3626 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003629 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003630 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003631 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003632#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003633
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003634 return( 0 );
3635}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003636
Gilles Peskined3eb0612018-01-08 17:07:44 +01003637/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003638 * that do not include a ServerKeyExchange message, do nothing. Either
3639 * way, if successful, move on to the next step in the SSL state
3640 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003641static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3642{
Janos Follath865b3eb2019-12-16 11:46:15 +00003643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003644 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003645#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003646 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003647 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003648#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003649
Gilles Peskined3eb0612018-01-08 17:07:44 +01003650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3651
Gilles Peskineeccd8882020-03-10 12:19:08 +01003652#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003653 /* Extract static ECDH parameters and abort if ServerKeyExchange
3654 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003655 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3656 {
3657 /* For suites involving ECDH, extract DH parameters
3658 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003659#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003660 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3661 {
3662 ssl_get_ecdh_params_from_cert( ssl );
3663 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003664#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003665
3666 /* Key exchanges not involving ephemeral keys don't use
3667 * ServerKeyExchange, so end here. */
3668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3669 ssl->state++;
3670 return( 0 );
3671 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003672#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003673
Gilles Peskineeccd8882020-03-10 12:19:08 +01003674#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003675 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003676 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003677 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003678 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003679 {
3680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3681 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003682 }
3683 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003684#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003685 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003686 {
3687 /* ServerKeyExchange is needed. Prepare the message. */
3688 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003689 }
3690
3691 if( ret != 0 )
3692 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003693 /* If we're starting to write a new message, set ssl->out_msglen
3694 * to 0. But if we're resuming after an asynchronous message,
3695 * out_msglen is the amount of data written so far and mst be
3696 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003697 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3699 else
3700 ssl->out_msglen = 0;
3701 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003702 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003703
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003704 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003705 * ssl_prepare_server_key_exchange already wrote the signature
3706 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003707#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003708 if( signature_len != 0 )
3709 {
Joe Subbianiad1115a2021-07-16 14:27:50 +01003710 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len );
3711 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len );
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003712
3713 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3714 ssl->out_msg + ssl->out_msglen,
3715 signature_len );
3716
3717 /* Skip over the already-written signature */
3718 ssl->out_msglen += signature_len;
3719 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003720#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003721
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003722 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3724 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
3726 ssl->state++;
3727
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003728 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 return( ret );
3732 }
3733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003736}
3737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739{
Janos Follath865b3eb2019-12-16 11:46:15 +00003740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743
3744 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3746 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
3748 ssl->state++;
3749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003751 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003753#endif
3754
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003755 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 return( ret );
3759 }
3760
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003761#if defined(MBEDTLS_SSL_PROTO_DTLS)
3762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3763 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3764 {
3765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3766 return( ret );
3767 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003768#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003771
3772 return( 0 );
3773}
3774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3776 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3777static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003778 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003781 size_t n;
3782
3783 /*
3784 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3785 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003786 if( *p + 2 > end )
3787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3789 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003790 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003791
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003792 n = ( (*p)[0] << 8 ) | (*p)[1];
3793 *p += 2;
3794
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003795 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3798 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003799 }
3800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3804 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003805 }
3806
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003807 *p += n;
3808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003810
Paul Bakker70df2fb2013-04-17 17:19:09 +02003811 return( ret );
3812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3814 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3817 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003818
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003819#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003820static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3821 unsigned char *peer_pms,
3822 size_t *peer_pmslen,
3823 size_t peer_pmssize )
3824{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003825 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003826 peer_pms, peer_pmslen, peer_pmssize );
3827 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3828 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003829 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003830 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003831 }
3832 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3833 return( ret );
3834}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003835#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003836
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003837static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3838 const unsigned char *p,
3839 const unsigned char *end,
3840 unsigned char *peer_pms,
3841 size_t *peer_pmslen,
3842 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003843{
Janos Follath865b3eb2019-12-16 11:46:15 +00003844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003845 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3846 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3847 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003848
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003849#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003850 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003851 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003852 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003853 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3855 return( ssl_resume_decrypt_pms( ssl,
3856 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003857 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003858#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003859
3860 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003861 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3864 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3865 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003866 {
Joe Subbiania724ef92021-08-18 12:06:57 +01003867 if ( p + 2 > end )
3868 {
Philippe Antoine747fd532018-05-30 09:13:21 +02003869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3870 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3871 }
Joe Subbianif15da892021-08-03 16:10:38 +01003872 if( *p++ != MBEDTLS_BYTE_1( len ) ||
Joe Subbiania724ef92021-08-18 12:06:57 +01003873 *p++ != MBEDTLS_BYTE_0( len ) )
3874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3876 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003877 }
3878 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003879#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003880
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003881 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3884 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003885 }
3886
Gilles Peskine422ccab2018-01-11 18:29:01 +01003887 /*
3888 * Decrypt the premaster secret
3889 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003890#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003891 if( ssl->conf->f_async_decrypt_start != NULL )
3892 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003893 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003894 mbedtls_ssl_own_cert( ssl ),
3895 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003896 switch( ret )
3897 {
3898 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3899 /* act as if f_async_decrypt_start was null */
3900 break;
3901 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003902 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003903 return( ssl_resume_decrypt_pms( ssl,
3904 peer_pms,
3905 peer_pmslen,
3906 peer_pmssize ) );
3907 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003908 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003909 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3910 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003911 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003912 return( ret );
3913 }
3914 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003915#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003916
Gilles Peskine422ccab2018-01-11 18:29:01 +01003917 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3918 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3920 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3921 }
3922
3923 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003924 peer_pms, peer_pmslen, peer_pmssize,
3925 ssl->conf->f_rng, ssl->conf->p_rng );
3926 return( ret );
3927}
3928
3929static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3930 const unsigned char *p,
3931 const unsigned char *end,
3932 size_t pms_offset )
3933{
Janos Follath865b3eb2019-12-16 11:46:15 +00003934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003935 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3936 unsigned char ver[2];
3937 unsigned char fake_pms[48], peer_pms[48];
3938 unsigned char mask;
3939 size_t i, peer_pmslen;
3940 unsigned int diff;
3941
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003942 /* In case of a failure in decryption, the decryption may write less than
3943 * 2 bytes of output, but we always read the first two bytes. It doesn't
3944 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003945 * ret being nonzero, and we only care whether diff is 0.
3946 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3947 * also makes memory analyzers happy (don't access uninitialized memory,
3948 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003949 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003950 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003951
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003952 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3953 peer_pms,
3954 &peer_pmslen,
3955 sizeof( peer_pms ) );
3956
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003957#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003958 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3959 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003960#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003963 ssl->handshake->max_minor_ver,
3964 ssl->conf->transport, ver );
3965
3966 /* Avoid data-dependent branches while checking for invalid
3967 * padding, to protect against timing-based Bleichenbacher-type
3968 * attacks. */
3969 diff = (unsigned int) ret;
3970 diff |= peer_pmslen ^ 48;
3971 diff |= peer_pms[0] ^ ver[0];
3972 diff |= peer_pms[1] ^ ver[1];
3973
3974 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3975 /* MSVC has a warning about unary minus on unsigned, but this is
3976 * well-defined and precisely what we want to do here */
3977#if defined(_MSC_VER)
3978#pragma warning( push )
3979#pragma warning( disable : 4146 )
3980#endif
3981 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3982#if defined(_MSC_VER)
3983#pragma warning( pop )
3984#endif
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003985
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003986 /*
3987 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3988 * must not cause the connection to end immediately; instead, send a
3989 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003990 * To protect against timing-based variants of the attack, we must
3991 * not have any branch that depends on whether the decryption was
3992 * successful. In particular, always generate the fake premaster secret,
3993 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003994 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003995 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003996 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003997 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003998 /* It's ok to abort on an RNG failure, since this does not reveal
3999 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004000 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01004001 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004002
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01004003#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02004004 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004006#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02004007
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004008 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
4009 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
4010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004013 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004014 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004015
Gilles Peskine422ccab2018-01-11 18:29:01 +01004016 /* Set pms to either the true or the fake PMS, without
4017 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00004018 for( i = 0; i < ssl->handshake->pmslen; i++ )
4019 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
4020
4021 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4024 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02004025
Gilles Peskineeccd8882020-03-10 12:19:08 +01004026#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004028 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004029{
Paul Bakker6db455e2013-09-18 17:29:31 +02004030 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03004031 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004032
Hanno Becker845b9462018-10-26 12:07:29 +01004033 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4036 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004037 }
4038
4039 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004040 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02004041 */
Hanno Becker83c9f492017-06-26 13:52:14 +01004042 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4045 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004046 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004047
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004048 n = ( (*p)[0] << 8 ) | (*p)[1];
4049 *p += 2;
4050
irwir6527bd62019-09-21 18:51:25 +03004051 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4054 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004055 }
4056
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004057 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004058 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004059 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004061 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004062 else
Paul Bakker6db455e2013-09-18 17:29:31 +02004063 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01004064 /* Identity is not a big secret since clients send it in the clear,
4065 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004066 if( n != ssl->conf->psk_identity_len ||
gabor-mezei-arm378e7eb2021-07-19 15:19:19 +02004067 mbedtls_cf_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02004068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02004070 }
4071 }
4072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02004076 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4077 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004079 }
4080
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004081 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02004082
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02004083 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004084}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004085#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02004086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004088{
Janos Follath865b3eb2019-12-16 11:46:15 +00004089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004091 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02004092
Hanno Beckere694c3e2017-12-27 21:34:08 +00004093 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004096
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004097#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004098 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4099 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4100 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4101 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004102 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004103 {
4104 /* We've already read a record and there is an asynchronous
4105 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02004106 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4108 }
4109 else
4110#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01004111 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 return( ret );
4115 }
4116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004118 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00004119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4123 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 }
4125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4129 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 }
4131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4133 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004135 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004138 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004139 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004140
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004141 if( p != end )
4142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4144 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004145 }
4146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004148 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01004149 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02004150 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004151 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4154 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004155 }
4156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004158 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4161#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4162 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4163 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4164 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4165 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4166 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4167 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4168 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004171 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4174 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004175 }
4176
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004177 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4178 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004181 &ssl->handshake->pmslen,
4182 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004184 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4187 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004188 }
4189
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004190 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4191 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004193 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4195 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4196 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4197 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4198#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4199 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004200 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004201 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02004204 return( ret );
4205 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004206
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004207 if( p != end )
4208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4210 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004211 }
4212
Hanno Becker845b9462018-10-26 12:07:29 +01004213#if defined(MBEDTLS_USE_PSA_CRYPTO)
4214 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
4215 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00004216 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01004217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4218 else
4219#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004221 ciphersuite_info->key_exchange ) ) != 0 )
4222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004224 return( ret );
4225 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02004226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4229#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4230 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004231 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004232#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004233 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01004234 {
4235 /* There is an asynchronous operation in progress to
4236 * decrypt the encrypted premaster secret, so skip
4237 * directly to resuming this operation. */
4238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4239 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4240 * won't actually use it, but maintain p anyway for robustness. */
4241 p += ssl->conf->psk_identity_len + 2;
4242 }
4243 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02004244#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004245 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004248 return( ret );
4249 }
4250
Hanno Becker845b9462018-10-26 12:07:29 +01004251#if defined(MBEDTLS_USE_PSA_CRYPTO)
4252 /* Opaque PSKs are currently only supported for PSK-only. */
4253 if( ssl_use_opaque_psk( ssl ) == 1 )
4254 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4255#endif
4256
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004257 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004260 return( ret );
4261 }
4262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004264 ciphersuite_info->key_exchange ) ) != 0 )
4265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02004267 return( ret );
4268 }
4269 }
4270 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4272#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4273 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004274 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004275 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004278 return( ret );
4279 }
4280 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004283 return( ret );
4284 }
4285
Hanno Becker845b9462018-10-26 12:07:29 +01004286#if defined(MBEDTLS_USE_PSA_CRYPTO)
4287 /* Opaque PSKs are currently only supported for PSK-only. */
4288 if( ssl_use_opaque_psk( ssl ) == 1 )
4289 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4290#endif
4291
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004292 if( p != end )
4293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4295 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004296 }
4297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004299 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004302 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004303 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004304 }
4305 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4307#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4308 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004309 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004310 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004313 return( ret );
4314 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004317 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4320 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004321 }
4322
Hanno Becker845b9462018-10-26 12:07:29 +01004323#if defined(MBEDTLS_USE_PSA_CRYPTO)
4324 /* Opaque PSKs are currently only supported for PSK-only. */
4325 if( ssl_use_opaque_psk( ssl ) == 1 )
4326 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4327#endif
4328
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004329 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4330 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004333 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004336 return( ret );
4337 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004338 }
4339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4341#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4342 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004343 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004344 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004347 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 }
4349 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004352#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4353 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4354 {
4355 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4356 p, end - p );
4357 if( ret != 0 )
4358 {
4359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4360 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4361 }
4362
4363 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4364 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4365 ssl->conf->f_rng, ssl->conf->p_rng );
4366 if( ret != 0 )
4367 {
4368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4369 return( ret );
4370 }
4371 }
4372 else
4373#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004382 return( ret );
4383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004384
Paul Bakker5121ce52009-01-03 21:22:43 +00004385 ssl->state++;
4386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004388
4389 return( 0 );
4390}
4391
Gilles Peskineeccd8882020-03-10 12:19:08 +01004392#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004395 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004396 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004399
Hanno Becker77adddc2019-02-07 12:32:43 +00004400 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004403 ssl->state++;
4404 return( 0 );
4405 }
4406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004409}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004410#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004414 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004415 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004416 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004417 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4419 mbedtls_pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02004420#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004422 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004423 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004424 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004427
Hanno Becker2a831a42019-02-07 13:17:25 +00004428 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004431 ssl->state++;
4432 return( 0 );
4433 }
4434
Hanno Becker2a831a42019-02-07 13:17:25 +00004435#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4436 if( ssl->session_negotiate->peer_cert == NULL )
4437 {
4438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4439 ssl->state++;
4440 return( 0 );
4441 }
4442#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4443 if( ssl->session_negotiate->peer_cert_digest == NULL )
4444 {
4445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4446 ssl->state++;
4447 return( 0 );
4448 }
4449#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4450
Simon Butcher99000142016-10-13 17:21:01 +01004451 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004452 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004453 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004455 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 return( ret );
4457 }
4458
4459 ssl->state++;
4460
Simon Butcher99000142016-10-13 17:21:01 +01004461 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4463 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4466 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 }
4468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004470
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004471#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4472 peer_pk = &ssl->handshake->peer_pubkey;
4473#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4474 if( ssl->session_negotiate->peer_cert == NULL )
4475 {
4476 /* Should never happen */
4477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4478 }
4479 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4480#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4481
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004482 /*
4483 * struct {
4484 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4485 * opaque signature<0..2^16-1>;
4486 * } DigitallySigned;
4487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4489 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4490 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01004491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004493 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004494
4495 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004496 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004497 {
4498 hash_start += 16;
4499 hashlen -= 16;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 md_alg = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004501 }
Paul Bakker926af752012-11-23 13:38:07 +01004502 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004503 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4505 MBEDTLS_SSL_PROTO_TLS1_1 */
4506#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4507 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004508 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004509 if( i + 2 > ssl->in_hslen )
4510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4512 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004513 }
4514
Paul Bakker5121ce52009-01-03 21:22:43 +00004515 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004516 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 */
Simon Butcher99000142016-10-13 17:21:01 +01004518 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4519
4520 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004523 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker926af752012-11-23 13:38:07 +01004525 }
4526
Simon Butcher99000142016-10-13 17:21:01 +01004527#if !defined(MBEDTLS_MD_SHA1)
4528 if( MBEDTLS_MD_SHA1 == md_alg )
4529 hash_start += 16;
4530#endif
Paul Bakker926af752012-11-23 13:38:07 +01004531
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004532 /* Info from md_alg will be used instead */
4533 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004534
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004535 i++;
4536
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004537 /*
4538 * Signature
4539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4541 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004544 " for verify message" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004546 }
4547
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004548 /*
4549 * Check the certificate's key type matches the signature alg
4550 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004551 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4554 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004555 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004556
4557 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004558 }
4559 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004564 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004565
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004566 if( i + 2 > ssl->in_hslen )
4567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4569 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004570 }
4571
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004572 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4573 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004574
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004575 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4578 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 }
4580
Simon Butcher99000142016-10-13 17:21:01 +01004581 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004582 {
4583 size_t dummy_hlen;
4584 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4585 }
Simon Butcher99000142016-10-13 17:21:01 +01004586
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004587 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004588 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004589 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004592 return( ret );
4593 }
4594
Simon Butcher99000142016-10-13 17:21:01 +01004595 mbedtls_ssl_update_handshake_status( ssl );
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Paul Bakkered27a042013-04-18 22:46:23 +02004599 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004600}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004601#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4604static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004605{
Janos Follath865b3eb2019-12-16 11:46:15 +00004606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004607 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004608 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4613 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004614
4615 /*
4616 * struct {
4617 * uint32 ticket_lifetime_hint;
4618 * opaque ticket<0..2^16-1>;
4619 * } NewSessionTicket;
4620 *
4621 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4622 * 8 . 9 ticket_len (n)
4623 * 10 . 9+n ticket content
4624 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004625
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004626 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004627 ssl->session_negotiate,
4628 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004629 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004630 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004631 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004633 tlen = 0;
4634 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004635
Joe Subbianic54e9082021-07-19 11:56:54 +01004636 MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 );
4637 MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004638 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004639
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004640 /*
4641 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4642 * ChangeCipherSpec share the same state.
4643 */
4644 ssl->handshake->new_session_ticket = 0;
4645
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004646 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004647 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004649 return( ret );
4650 }
4651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004653
4654 return( 0 );
4655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004657
Paul Bakker5121ce52009-01-03 21:22:43 +00004658/*
Paul Bakker1961b702013-01-25 14:49:24 +01004659 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004662{
4663 int ret = 0;
4664
Manuel Pégourié-Gonnarddba460f2015-06-24 22:59:30 +02004665 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01004671 return( ret );
4672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004673#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004674 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004676 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004677 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004678 return( ret );
4679 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01004680#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004681
Paul Bakker1961b702013-01-25 14:49:24 +01004682 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684 case MBEDTLS_SSL_HELLO_REQUEST:
4685 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 break;
4687
Paul Bakker1961b702013-01-25 14:49:24 +01004688 /*
4689 * <== ClientHello
4690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004692 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004695#if defined(MBEDTLS_SSL_PROTO_DTLS)
4696 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4697 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004698#endif
4699
Paul Bakker1961b702013-01-25 14:49:24 +01004700 /*
4701 * ==> ServerHello
4702 * Certificate
4703 * ( ServerKeyExchange )
4704 * ( CertificateRequest )
4705 * ServerHelloDone
4706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004708 ret = ssl_write_server_hello( ssl );
4709 break;
4710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4712 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004713 break;
4714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004716 ret = ssl_write_server_key_exchange( ssl );
4717 break;
4718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004720 ret = ssl_write_certificate_request( ssl );
4721 break;
4722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004724 ret = ssl_write_server_hello_done( ssl );
4725 break;
4726
4727 /*
4728 * <== ( Certificate/Alert )
4729 * ClientKeyExchange
4730 * ( CertificateVerify )
4731 * ChangeCipherSpec
4732 * Finished
4733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4735 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004736 break;
4737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004739 ret = ssl_parse_client_key_exchange( ssl );
4740 break;
4741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004743 ret = ssl_parse_certificate_verify( ssl );
4744 break;
4745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4747 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004748 break;
4749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 case MBEDTLS_SSL_CLIENT_FINISHED:
4751 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004752 break;
4753
4754 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004755 * ==> ( NewSessionTicket )
4756 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004757 * Finished
4758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4760#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004761 if( ssl->handshake->new_session_ticket != 0 )
4762 ret = ssl_write_new_session_ticket( ssl );
4763 else
Paul Bakkera503a632013-08-14 13:48:06 +02004764#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004766 break;
4767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768 case MBEDTLS_SSL_SERVER_FINISHED:
4769 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004770 break;
4771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 case MBEDTLS_SSL_FLUSH_BUFFERS:
4773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4774 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004775 break;
4776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4778 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004779 break;
4780
4781 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004784 }
4785
Paul Bakker5121ce52009-01-03 21:22:43 +00004786 return( ret );
4787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788#endif /* MBEDTLS_SSL_SRV_C */