blob: bd0982c3c919b13f849dc9b9f4c611bd489d304e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS server-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
SimonBd5800b72016-04-26 07:43:27 +010024#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
27#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010030#endif
31
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Chris Jones84a773f2021-03-05 18:38:47 +000033#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Andres Amaya Garcia84914062018-04-24 08:40:46 -050036#include "mbedtls/platform_util.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020037#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020038#include "mbedtls/constant_time.h"
Rich Evans00ab4702015-02-06 13:43:58 +000039
40#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010047#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
51int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020052 const unsigned char *info,
53 size_t ilen )
54{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020055 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020059
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020061 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020062
63 memcpy( ssl->cli_id, info, ilen );
64 ssl->cli_id_len = ilen;
65
66 return( 0 );
67}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020068
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020069void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_ssl_cookie_write_t *f_cookie_write,
71 mbedtls_ssl_cookie_check_t *f_cookie_check,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020072 void *p_cookie )
73{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +020074 conf->f_cookie_write = f_cookie_write;
75 conf->f_cookie_check = f_cookie_check;
76 conf->p_cookie = p_cookie;
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +020077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +000082 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +000083 size_t len )
84{
Janos Follath865b3eb2019-12-16 11:46:15 +000085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5701cdc2012-09-27 21:49:42 +000086 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +000087 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +010090
Philippe Antoine747fd532018-05-30 09:13:21 +020091 if( len < 2 )
92 {
93 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
94 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
95 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +010096 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +020097 }
Paul Bakker5701cdc2012-09-27 21:49:42 +000098 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
99 if( servername_list_size + 2 != len )
100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
103 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100104 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000105 }
106
107 p = buf + 2;
Philippe Antoine747fd532018-05-30 09:13:21 +0200108 while( servername_list_size > 2 )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000109 {
110 hostname_len = ( ( p[1] << 8 ) | p[2] );
111 if( hostname_len + 3 > servername_list_size )
112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200114 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
115 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100116 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000117 }
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
Paul Bakker5701cdc2012-09-27 21:49:42 +0000120 {
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200121 ret = ssl->conf->f_sni( ssl->conf->p_sni,
122 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000123 if( ret != 0 )
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
126 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
127 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100128 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000129 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000130 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000131 }
132
133 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000134 p += hostname_len + 3;
135 }
136
137 if( servername_list_size != 0 )
138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100141 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
142 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000143 }
144
145 return( 0 );
146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000148
Gilles Peskineeccd8882020-03-10 12:19:08 +0100149#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100150static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
151{
152 if( conf->f_psk != NULL )
153 return( 1 );
154
155 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
156 return( 0 );
157
158 if( conf->psk != NULL && conf->psk_len != 0 )
159 return( 1 );
160
161#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +0200162 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100163 return( 1 );
164#endif /* MBEDTLS_USE_PSA_CRYPTO */
165
166 return( 0 );
167}
168
169#if defined(MBEDTLS_USE_PSA_CRYPTO)
170static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
171{
172 if( ssl->conf->f_psk != NULL )
173 {
174 /* If we've used a callback to select the PSK,
175 * the static configuration is irrelevant. */
176
Ronald Croncf56a0a2020-08-04 09:51:30 +0200177 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100178 return( 1 );
179
180 return( 0 );
181 }
182
Ronald Croncf56a0a2020-08-04 09:51:30 +0200183 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100184 return( 1 );
185
186 return( 0 );
187}
188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100189#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000192 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000193 size_t len )
194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_SSL_RENEGOTIATION)
196 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100197 {
198 /* Check verify-data in constant-time. The length OTOH is no secret */
199 if( len != 1 + ssl->verify_data_len ||
200 buf[0] != ssl->verify_data_len ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200201 mbedtls_ct_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100202 ssl->verify_data_len ) != 0 )
203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200205 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
206 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100207 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100208 }
209 }
210 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000212 {
213 if( len != 1 || buf[0] != 0x0 )
214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200216 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
217 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100218 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000219 }
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000222 }
Paul Bakker48916f92012-09-16 19:57:18 +0000223
224 return( 0 );
225}
226
Jerry Yue7541932022-01-28 10:21:24 +0800227#if 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 */
Jerry Yu18cd4392021-12-17 17:44:24 +0800241static int ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
242 const unsigned char *buf,
243 size_t len )
Paul Bakker23f36802012-09-28 14:15:14 +0000244{
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100257 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +0200258 }
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 );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100266 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
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
Jerry Yu24811fb2022-01-19 18:02:15 +0800298 if( mbedtls_ssl_sig_alg_is_offered(
299 ssl, MBEDTLS_GET_UINT16_BE( p, 0 ) ) )
Hanno Becker7e5437a2017-04-28 17:15:26 +0100300 {
301 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
Paul Elliott9f352112020-12-09 14:55:45 +0000303 " match sig %u and hash %u",
Paul Elliott3891caf2020-12-17 18:42:40 +0000304 (unsigned) sig_cur, (unsigned) md_cur ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100305 }
306 else
307 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
Paul Elliott3891caf2020-12-17 18:42:40 +0000309 "hash alg %u not supported", (unsigned) md_cur ) );
Paul Bakker23f36802012-09-28 14:15:14 +0000310 }
Paul Bakker23f36802012-09-28 14:15:14 +0000311 }
312
Paul Bakker23f36802012-09-28 14:15:14 +0000313 return( 0 );
314}
Jerry Yue7541932022-01-28 10:21:24 +0800315#endif /* 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)
Jerry Yub925f212022-01-12 11:17:02 +0800319/*
Jerry Yud491ea42022-01-13 16:15:25 +0800320 * Function for parsing a supported groups (TLS 1.3) or supported elliptic
321 * curves (TLS 1.2) extension.
322 *
323 * The "extension_data" field of a supported groups extension contains a
324 * "NamedGroupList" value (TLS 1.3 RFC8446):
325 * enum {
326 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
327 * x25519(0x001D), x448(0x001E),
328 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
329 * ffdhe6144(0x0103), ffdhe8192(0x0104),
330 * ffdhe_private_use(0x01FC..0x01FF),
331 * ecdhe_private_use(0xFE00..0xFEFF),
332 * (0xFFFF)
333 * } NamedGroup;
334 * struct {
335 * NamedGroup named_group_list<2..2^16-1>;
336 * } NamedGroupList;
337 *
338 * The "extension_data" field of a supported elliptic curves extension contains
339 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
340 * enum {
341 * deprecated(1..22),
342 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
343 * x25519(29), x448(30),
344 * reserved (0xFE00..0xFEFF),
345 * deprecated(0xFF01..0xFF02),
346 * (0xFFFF)
347 * } NamedCurve;
348 * struct {
349 * NamedCurve named_curve_list<2..2^16-1>
350 * } NamedCurveList;
351 *
Jerry Yub925f212022-01-12 11:17:02 +0800352 * The TLS 1.3 supported groups extension was defined to be a compatible
353 * generalization of the TLS 1.2 supported elliptic curves extension. They both
354 * share the same extension identifier.
Jerry Yud491ea42022-01-13 16:15:25 +0800355 *
356 * DHE groups are not supported yet.
Jerry Yub925f212022-01-12 11:17:02 +0800357 */
Jerry Yub47d0f82021-12-20 17:34:40 +0800358static int ssl_parse_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yuffef9c52021-12-24 22:31:08 +0800359 const unsigned char *buf,
360 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100361{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200362 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100363 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100365
Philippe Antoine747fd532018-05-30 09:13:21 +0200366 if ( len < 2 ) {
367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
368 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
369 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100370 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +0200371 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100372 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
373 if( list_size + 2 != len ||
374 list_size % 2 != 0 )
375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200377 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
378 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100379 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +0100380 }
381
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200382 /* Should never happen unless client duplicates the extension */
383 if( ssl->handshake->curves != NULL )
384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200386 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100387 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
388 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200389 }
390
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100391 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200392 * and leave room for a final 0 */
393 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( our_size > MBEDTLS_ECP_DP_MAX )
395 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200396
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200397 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200398 {
399 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
400 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200401 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200402 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200403
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200404 ssl->handshake->curves = curves;
405
Paul Bakker41c83d32013-03-20 14:39:14 +0100406 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200407 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200410
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200411 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100412 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200413 *curves++ = curve_info;
414 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100415 }
416
417 list_size -= 2;
418 p += 2;
419 }
420
421 return( 0 );
422}
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200425 const unsigned char *buf,
426 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100427{
428 size_t list_size;
429 const unsigned char *p;
430
Philippe Antoine747fd532018-05-30 09:13:21 +0200431 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200434 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
435 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100436 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +0100437 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200438 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100439
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200440 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100441 while( list_size > 0 )
442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
444 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100445 {
Robert Cragie136884c2015-10-02 13:34:31 +0100446#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200447 ssl->handshake->ecdh_ctx.point_format = p[0];
Robert Cragie136884c2015-10-02 13:34:31 +0100448#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100449#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200450 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
451 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100454 return( 0 );
455 }
456
457 list_size--;
458 p++;
459 }
460
461 return( 0 );
462}
Robert Cragieae8535d2015-10-06 17:11:18 +0100463#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
464 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100465
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200466#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
467static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
468 const unsigned char *buf,
469 size_t len )
470{
Janos Follath865b3eb2019-12-16 11:46:15 +0000471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200472
473 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
474 {
475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
476 return( 0 );
477 }
478
479 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
480 buf, len ) ) != 0 )
481 {
482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200483 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
484 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200485 return( ret );
486 }
487
488 /* Only mark the extension as OK when we're sure it is */
489 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
490
491 return( 0 );
492}
493#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
496static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200497 const unsigned char *buf,
498 size_t len )
499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
504 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100505 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200506 }
507
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200508 ssl->session_negotiate->mfl_code = buf[0];
509
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200510 return( 0 );
511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200513
Hanno Beckera0e20d02019-05-15 14:03:01 +0100514#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100515static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
516 const unsigned char *buf,
517 size_t len )
518{
519 size_t peer_cid_len;
520
521 /* CID extension only makes sense in DTLS */
522 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
523 {
524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
525 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
526 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100527 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100528 }
529
530 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100531 * Quoting draft-ietf-tls-dtls-connection-id-05
532 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100533 *
534 * struct {
535 * opaque cid<0..2^8-1>;
536 * } ConnectionId;
537 */
538
539 if( len < 1 )
540 {
541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
542 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100543 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
544 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100545 }
546
547 peer_cid_len = *buf++;
548 len--;
549
550 if( len != peer_cid_len )
551 {
552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100554 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
555 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100556 }
557
558 /* Ignore CID if the user has disabled its use. */
559 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
560 {
561 /* Leave ssl->handshake->cid_in_use in its default
562 * value of MBEDTLS_SSL_CID_DISABLED. */
563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
564 return( 0 );
565 }
566
567 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
568 {
569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
570 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
571 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100572 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100573 }
574
Hanno Becker08556bf2019-05-03 12:43:44 +0100575 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100576 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
577 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
578
579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
580 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
581
Hanno Becker89dcc882019-04-26 13:56:39 +0100582 return( 0 );
583}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100584#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
587static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100588 const unsigned char *buf,
589 size_t len )
590{
591 if( len != 0 )
592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200594 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
595 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100596 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100597 }
598
599 ((void) buf);
600
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100601 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100604 }
605
606 return( 0 );
607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
611static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200612 const unsigned char *buf,
613 size_t len )
614{
615 if( len != 0 )
616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200618 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
619 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100620 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200621 }
622
623 ((void) buf);
624
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100625 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200628 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200629
630 return( 0 );
631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_SSL_SESSION_TICKETS)
635static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200636 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200637 size_t len )
638{
Janos Follath865b3eb2019-12-16 11:46:15 +0000639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200640 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200641
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200642 mbedtls_ssl_session_init( &session );
643
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200644 if( ssl->conf->f_ticket_parse == NULL ||
645 ssl->conf->f_ticket_write == NULL )
646 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200647 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200648 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200649
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200650 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200651 ssl->handshake->new_session_ticket = 1;
652
Paul Elliottd48d5c62021-01-07 14:47:05 +0000653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200654
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200655 if( len == 0 )
656 return( 0 );
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_SSL_RENEGOTIATION)
659 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200662 return( 0 );
663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665
666 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200667 * Failures are ok: just ignore the ticket and proceed.
668 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200669 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
670 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200671 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200672 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200673
674 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
676 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
678 else
679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
680
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200682 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200683
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200684 /*
685 * Keep the session ID sent by the client, since we MUST send it back to
686 * inform them we're accepting the ticket (RFC 5077 section 3.4)
687 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200688 session.id_len = ssl->session_negotiate->id_len;
689 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200690
691 mbedtls_ssl_session_free( ssl->session_negotiate );
692 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
693
694 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500695 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200698
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200699 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200700
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200701 /* Don't send a new ticket after all, this one is OK */
702 ssl->handshake->new_session_ticket = 0;
703
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200704 return( 0 );
705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#if defined(MBEDTLS_SSL_ALPN)
709static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200710 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200711{
Paul Bakker14b16c62014-05-28 11:33:54 +0200712 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200713 const unsigned char *theirs, *start, *end;
714 const char **ours;
715
716 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200717 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718 return( 0 );
719
720 /*
721 * opaque ProtocolName<1..2^8-1>;
722 *
723 * struct {
724 * ProtocolName protocol_name_list<2..2^16-1>
725 * } ProtocolNameList;
726 */
727
728 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
729 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200730 {
731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
732 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100733 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200734 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200735
736 list_len = ( buf[0] << 8 ) | buf[1];
737 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200738 {
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100741 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200742 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200743
744 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100745 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200746 */
747 start = buf + 2;
748 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100749 for( theirs = start; theirs != end; theirs += cur_len )
750 {
751 cur_len = *theirs++;
752
753 /* Current identifier must fit in list */
754 if( cur_len > (size_t)( end - theirs ) )
755 {
756 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
757 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100758 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100759 }
760
761 /* Empty strings MUST NOT be included */
762 if( cur_len == 0 )
763 {
764 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
765 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100766 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100767 }
768 }
769
770 /*
771 * Use our order of preference
772 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200773 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200774 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200775 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200776 for( theirs = start; theirs != end; theirs += cur_len )
777 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200778 cur_len = *theirs++;
779
Paul Bakker14b16c62014-05-28 11:33:54 +0200780 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200781 memcmp( theirs, *ours, cur_len ) == 0 )
782 {
783 ssl->alpn_chosen = *ours;
784 return( 0 );
785 }
786 }
787 }
788
789 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
791 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
Dave Rodgman53c86892021-06-29 10:02:06 +0100792 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200795
Johan Pascalb62bb512015-12-03 21:56:45 +0100796#if defined(MBEDTLS_SSL_DTLS_SRTP)
797static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300798 const unsigned char *buf,
799 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100800{
Johan Pascal43f94902020-09-22 12:25:52 +0200801 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100802 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200803 size_t profile_length;
804 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200805 /*! 2 bytes for profile length and 1 byte for mki len */
806 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100807
808 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100809 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
810 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
811 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200812 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100813 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200814 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100815
816 /* RFC5764 section 4.1.1
817 * uint8 SRTPProtectionProfile[2];
818 *
819 * struct {
820 * SRTPProtectionProfiles SRTPProtectionProfiles;
821 * opaque srtp_mki<0..255>;
822 * } UseSRTPData;
823
824 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100825 */
826
Ron Eldoref72faf2018-07-12 11:54:20 +0300827 /*
828 * Min length is 5: at least one protection profile(2 bytes)
829 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200830 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200831 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300832 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200833 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200834 {
835 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100836 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
837 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200838 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100839
Johan Pascal43f94902020-09-22 12:25:52 +0200840 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200841
Ron Eldoref72faf2018-07-12 11:54:20 +0300842 /* first 2 bytes are protection profile length(in bytes) */
843 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200844 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200845
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200846 /* The profile length cannot be bigger than input buffer size - lengths fields */
847 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200848 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200849 {
850 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100851 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
852 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200853 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300854 /*
855 * parse the extension list values are defined in
856 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
857 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200858 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100859 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200860 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200861 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100862
Johan Pascal43f94902020-09-22 12:25:52 +0200863 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300864 {
Johan Pascal43f94902020-09-22 12:25:52 +0200865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
866 mbedtls_ssl_get_srtp_profile_as_string(
867 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300868 }
Johan Pascal85269572020-08-25 10:01:54 +0200869 else
870 {
871 continue;
872 }
Ron Eldor591f1622018-01-22 12:30:04 +0200873 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200874 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200875 {
876 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
877 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200878 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
880 mbedtls_ssl_get_srtp_profile_as_string(
881 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200882 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100883 }
884 }
Johan Pascal43f94902020-09-22 12:25:52 +0200885 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200886 break;
887 }
Johan Pascal042d4562020-08-25 12:14:02 +0200888 buf += profile_length; /* buf points to the mki length */
889 mki_length = *buf;
890 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200891
Johan Pascal042d4562020-08-25 12:14:02 +0200892 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
893 mki_length + profile_length + size_of_lengths != len )
894 {
895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100896 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
897 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascal042d4562020-08-25 12:14:02 +0200898 }
899
900 /* Parse the mki only if present and mki is supported locally */
901 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
902 mki_length > 0 )
903 {
904 ssl->dtls_srtp_info.mki_len = mki_length;
905
Johan Pascal5ef72d22020-10-28 17:05:47 +0100906 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300907
Ron Eldoref72faf2018-07-12 11:54:20 +0300908 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
909 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100910 }
911
Johan Pascal042d4562020-08-25 12:14:02 +0200912 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100913}
914#endif /* MBEDTLS_SSL_DTLS_SRTP */
915
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100916/*
917 * Auxiliary functions for ServerHello parsing and related actions
918 */
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100921/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100922 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#if defined(MBEDTLS_ECDSA_C)
925static int ssl_check_key_curve( mbedtls_pk_context *pk,
926 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 const mbedtls_ecp_curve_info **crv = curves;
929 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930
931 while( *crv != NULL )
932 {
933 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100934 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100935 crv++;
936 }
937
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100938 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100941
942/*
943 * Try picking a certificate for this ciphersuite,
944 * return 0 on success and -1 on failure.
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946static int ssl_pick_cert( mbedtls_ssl_context *ssl,
947 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100948{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100950 mbedtls_pk_type_t pk_alg =
951 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200952 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100955 if( ssl->handshake->sni_key_cert != NULL )
956 list = ssl->handshake->sni_key_cert;
957 else
958#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200959 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100962 return( 0 );
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000965
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200966 if( list == NULL )
967 {
968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
969 return( -1 );
970 }
971
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100972 for( cur = list; cur != NULL; cur = cur->next )
973 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400974 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000976 cur->cert );
977
Gilles Peskinee198df52018-01-05 21:17:45 +0100978 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100981 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000982 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100983
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200984 /*
985 * This avoids sending the client a cert it'll reject based on
986 * keyUsage or other extensions.
987 *
988 * It also allows the user to provision different certificates for
989 * different uses based on keyUsage, eg if they want to avoid signing
990 * and decrypting with the same RSA key.
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100993 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000996 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200997 continue;
998 }
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#if defined(MBEDTLS_ECDSA_C)
1001 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +02001002 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
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 mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001005 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001006 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001007#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001008
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001009 /*
1010 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
1011 * present them a SHA-higher cert rather than failing if it's the only
1012 * one we got that satisfies the other conditions.
1013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
1015 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001016 {
1017 if( fallback == NULL )
1018 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001021 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001022 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001023 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001024 }
1025
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +01001026 /* If we get there, we got a winner */
1027 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001028 }
1029
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001030 if( cur == NULL )
1031 cur = fallback;
1032
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001033 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001034 if( cur != NULL )
1035 {
1036 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001038 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +01001039 return( 0 );
1040 }
1041
1042 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001045
1046/*
1047 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1048 * Sets ciphersuite_info only if the suite matches.
1049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1051 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001054
Jerry Yue7541932022-01-28 10:21:24 +08001055#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001056 mbedtls_pk_type_t sig_type;
1057#endif
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001060 if( suite_info == NULL )
1061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001064 }
1065
Hanno Becker5c5efdf2019-01-28 14:59:35 +00001066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +00001067 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001068
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001069 if( suite_info->min_minor_ver > ssl->minor_ver ||
1070 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001073 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001074 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +01001079 return( 0 );
1080#endif
1081
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001082#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1083 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001084 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001085 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1087 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +02001088 return( 0 );
1089 }
1090#endif
1091
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1094 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001095 ( ssl->handshake->curves == NULL ||
1096 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001099 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001100 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001101 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001102#endif
1103
Gilles Peskineeccd8882020-03-10 12:19:08 +01001104#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001105 /* If the ciphersuite requires a pre-shared key and we don't
1106 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +01001108 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001111 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001112 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001113#endif
1114
Jerry Yue7541932022-01-28 10:21:24 +08001115#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001116 /* If the ciphersuite requires signing, check whether
1117 * a suitable hash algorithm is present. */
1118 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1119 {
1120 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1121 if( sig_type != MBEDTLS_PK_NONE &&
1122 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1123 {
1124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001125 "for signature algorithm %u", (unsigned) sig_type ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001126 return( 0 );
1127 }
1128 }
1129
Jerry Yue7541932022-01-28 10:21:24 +08001130#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001133 /*
1134 * Final check: if ciphersuite requires us to have a
1135 * certificate/key of a particular type:
1136 * - select the appropriate certificate if we have one, or
1137 * - try the next ciphersuite if we don't
1138 * This must be done last since we modify the key_cert list.
1139 */
1140 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001143 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001144 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001145 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001146#endif
1147
1148 *ciphersuite_info = suite_info;
1149 return( 0 );
1150}
1151
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001152/* This function doesn't alert on errors that happen early during
1153 ClientHello parsing because they might indicate that the client is
1154 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001157 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001158 size_t i, j;
1159 size_t ciph_offset, comp_offset, ext_offset;
1160 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001162 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001163#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001164 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001166 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001167#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001168 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001169 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001171 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Hanno Becker7e5437a2017-04-28 17:15:26 +01001173 /* If there is no signature-algorithm extension present,
1174 * we need to fall back to the default values for allowed
1175 * signature-hash pairs. */
Jerry Yue7541932022-01-28 10:21:24 +08001176#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001177 int sig_hash_alg_ext_present = 0;
Jerry Yue7541932022-01-28 10:21:24 +08001178#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001183read_record_header:
1184#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001185 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001187 * otherwise read it ourselves manually in order to support SSLv2
1188 * ClientHello, which doesn't use the same record layer format.
1189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_RENEGOTIATION)
1191 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001195 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001196 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001198 return( ret );
1199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 }
1201
1202 buf = ssl->in_hdr;
1203
Hanno Becker5903de42019-05-03 14:46:38 +01001204 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001205
Paul Bakkerec636f32012-09-09 19:17:02 +00001206 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001207 * TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001208 *
1209 * Record layer:
1210 * 0 . 0 message type
1211 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001212 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001213 * 3 . 4 message length
1214 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001216 buf[0] ) );
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001221 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001222 }
1223
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001225 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1226
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001228 buf[1], buf[2] ) );
1229
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001230 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001231
1232 /* According to RFC 5246 Appendix E.1, the version here is typically
1233 * "{03,00}, the lowest version number supported by the client, [or] the
1234 * value of ClientHello.client_version", so the only meaningful check here
1235 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001239 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakkerec636f32012-09-09 19:17:02 +00001240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001242 /* For DTLS if this is the initial handshake, remember the client sequence
1243 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#if defined(MBEDTLS_SSL_RENEGOTIATION)
1247 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001248#endif
1249 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001250 {
1251 /* Epoch should be 0 for initial handshakes */
1252 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001255 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001256 }
1257
Jerry Yud9a94fe2021-09-28 18:58:59 +08001258 memcpy( &ssl->cur_out_ctr[2], ssl->in_ctr + 2,
Jerry Yud96a5c22021-09-29 17:46:51 +08001259 sizeof( ssl->cur_out_ctr ) - 2 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1262 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001265 ssl->next_record_offset = 0;
1266 ssl->in_left = 0;
1267 goto read_record_header;
1268 }
1269
1270 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001272#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001275
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001276 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_SSL_RENEGOTIATION)
1279 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001282 msg_len = ssl->in_hslen;
1283 }
1284 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001285#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001286 {
Angus Grattond8213d02016-05-25 20:56:48 +10001287 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001290 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001291 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001292
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001293 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001294 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001297 return( ret );
1298 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001299
1300 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001303 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001304 else
1305#endif
1306 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001307 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001308
1309 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001312
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001313 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001314
1315 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001316 * Handshake layer:
1317 * 0 . 0 handshake type
1318 * 1 . 3 handshake length
1319 * 4 . 5 DTLS only: message seqence number
1320 * 6 . 8 DTLS only: fragment offset
1321 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001326 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001327 }
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001334 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001335 }
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001338 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1339
1340 /* We don't support fragmentation of ClientHello (yet?) */
1341 if( buf[1] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001345 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001346 }
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001350 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001351 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001352 * Copy the client's handshake message_seq on initial handshakes,
1353 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_SSL_RENEGOTIATION)
1356 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001357 {
1358 /* This couldn't be done in ssl_prepare_handshake_record() */
1359 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1360 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001361
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001362 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001365 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001366 ssl->handshake->in_msg_seq ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001367 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001368 }
1369
1370 ssl->handshake->in_msg_seq++;
1371 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001372 else
1373#endif
1374 {
1375 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1376 ssl->in_msg[5];
1377 ssl->handshake->out_msg_seq = cli_msg_seq;
1378 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1379 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001380
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001381 /*
1382 * For now we don't support fragmentation, so make sure
1383 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001384 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001385 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1386 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1389 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001390 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001391 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 buf += mbedtls_ssl_hs_hdr_len( ssl );
1395 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001396
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001397 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001398 * ClientHello layer:
1399 * 0 . 1 protocol version
1400 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1401 * 34 . 35 session id length (1 byte)
1402 * 35 . 34+x session id
1403 * 35+x . 35+x DTLS only: cookie length (1 byte)
1404 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001405 * .. . .. ciphersuite list length (2 bytes)
1406 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001407 * .. . .. compression alg. list length (1 byte)
1408 * .. . .. compression alg. list
1409 * .. . .. extensions length (2 bytes, optional)
1410 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001411 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001412
1413 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001414 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001415 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1416 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001417 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001418 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001421 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001422 }
1423
1424 /*
1425 * Check and save the protocol version
1426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001430 ssl->conf->transport, buf );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001431 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001432
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001433 ssl->handshake->max_major_ver = ssl->major_ver;
1434 ssl->handshake->max_minor_ver = ssl->minor_ver;
1435
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001436 if( ssl->major_ver < ssl->conf->min_major_ver ||
1437 ssl->minor_ver < ssl->conf->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001440 " [%d:%d] < [%d:%d]",
1441 ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001442 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1444 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Hanno Beckerbc000442021-06-24 09:18:19 +01001445 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001446 }
1447
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001448 if( ssl->major_ver > ssl->conf->max_major_ver )
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001449 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001450 ssl->major_ver = ssl->conf->max_major_ver;
1451 ssl->minor_ver = ssl->conf->max_minor_ver;
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001452 }
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001453 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1454 ssl->minor_ver = ssl->conf->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001455
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001456 /*
1457 * Save client random (inc. Unix time)
1458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001460
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001461 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001462
1463 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001464 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001465 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001466 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001467
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001468 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001469 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001472 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1473 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001474 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001475 }
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001478
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001479 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001480 memset( ssl->session_negotiate->id, 0,
1481 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001482 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001483 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001484
1485 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001486 * Check the cookie length and content
1487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001490 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001491 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001492 cookie_len = buf[cookie_offset];
1493
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001494 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001497 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +01001498 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1499 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001500 }
1501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001503 buf + cookie_offset + 1, cookie_len );
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001506 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_SSL_RENEGOTIATION)
1508 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001509#endif
1510 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001511 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001512 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001513 buf + cookie_offset + 1, cookie_len,
1514 ssl->cli_id, ssl->cli_id_len ) != 0 )
1515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001517 ssl->handshake->verify_cookie_len = 1;
1518 }
1519 else
1520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001522 ssl->handshake->verify_cookie_len = 0;
1523 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001524 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001525 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001527 {
1528 /* We know we didn't send a cookie, so it should be empty */
1529 if( cookie_len != 0 )
1530 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001531 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001533 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001534 }
1535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001537 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001538
1539 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001540 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001541 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001542 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001543 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001544 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001546 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001547
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001548 ciph_len = ( buf[ciph_offset + 0] << 8 )
1549 | ( buf[ciph_offset + 1] );
1550
1551 if( ciph_len < 2 ||
1552 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1553 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001556 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1557 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001558 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001559 }
1560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001562 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001563
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001564 /*
1565 * Check the compression algorithms length and pick one
1566 */
1567 comp_offset = ciph_offset + 2 + ciph_len;
1568
1569 comp_len = buf[comp_offset];
1570
1571 if( comp_len < 1 ||
1572 comp_len > 16 ||
1573 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1577 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001578 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001579 }
1580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001582 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001585 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001589#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001590 /*
1591 * Check the extension length
1592 */
1593 ext_offset = comp_offset + 1 + comp_len;
1594 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001595 {
Simon Butcher584a5472016-05-23 16:24:52 +01001596 if( msg_len < ext_offset + 2 )
1597 {
1598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001599 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1600 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001601 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001602 }
1603
1604 ext_len = ( buf[ext_offset + 0] << 8 )
1605 | ( buf[ext_offset + 1] );
1606
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001607 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001608 {
1609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1611 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001612 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001613 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001614 }
Simon Butcher584a5472016-05-23 16:24:52 +01001615 else
1616 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001617
Simon Butcher584a5472016-05-23 16:24:52 +01001618 ext = buf + ext_offset + 2;
1619 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001620
Simon Butcher584a5472016-05-23 16:24:52 +01001621 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001622 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001623 unsigned int ext_id;
1624 unsigned int ext_size;
1625 if ( ext_len < 4 ) {
1626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1627 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1628 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001629 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02001630 }
1631 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1632 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001633
Simon Butcher584a5472016-05-23 16:24:52 +01001634 if( ext_size + 4 > ext_len )
1635 {
1636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001637 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1638 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001639 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001640 }
1641 switch( ext_id )
1642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001644 case MBEDTLS_TLS_EXT_SERVERNAME:
1645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1646 if( ssl->conf->f_sni == NULL )
1647 break;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001648
Simon Butcher584a5472016-05-23 16:24:52 +01001649 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1650 if( ret != 0 )
1651 return( ret );
1652 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001654
Simon Butcher584a5472016-05-23 16:24:52 +01001655 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001658 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001659#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001660
Simon Butcher584a5472016-05-23 16:24:52 +01001661 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1662 if( ret != 0 )
1663 return( ret );
1664 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001665
Jerry Yue7541932022-01-28 10:21:24 +08001666#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001667 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1669
Jerry Yu18cd4392021-12-17 17:44:24 +08001670 ret = ssl_parse_sig_alg_ext( ssl, ext + 4, ext_size );
Simon Butcher584a5472016-05-23 16:24:52 +01001671 if( ret != 0 )
1672 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001673
1674 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001675 break;
Jerry Yue7541932022-01-28 10:21:24 +08001676#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001677
Robert Cragie136884c2015-10-02 13:34:31 +01001678#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001679 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yub47d0f82021-12-20 17:34:40 +08001680 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Simon Butcher584a5472016-05-23 16:24:52 +01001681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001682
Jerry Yub47d0f82021-12-20 17:34:40 +08001683 ret = ssl_parse_supported_groups_ext( ssl, ext + 4, ext_size );
Simon Butcher584a5472016-05-23 16:24:52 +01001684 if( ret != 0 )
1685 return( ret );
1686 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001687
Simon Butcher584a5472016-05-23 16:24:52 +01001688 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1690 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001691
Simon Butcher584a5472016-05-23 16:24:52 +01001692 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1693 if( ret != 0 )
1694 return( ret );
1695 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001696#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1697 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001698
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001699#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001700 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001702
Simon Butcher584a5472016-05-23 16:24:52 +01001703 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1704 if( ret != 0 )
1705 return( ret );
1706 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001707#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001710 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001712
Simon Butcher584a5472016-05-23 16:24:52 +01001713 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1714 if( ret != 0 )
1715 return( ret );
1716 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001718
Hanno Beckera0e20d02019-05-15 14:03:01 +01001719#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001720 case MBEDTLS_TLS_EXT_CID:
1721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1722
1723 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1724 if( ret != 0 )
1725 return( ret );
1726 break;
Thomas Daubneye1c9a402021-06-15 11:26:43 +01001727#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +01001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001730 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001732
Simon Butcher584a5472016-05-23 16:24:52 +01001733 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1734 if( ret != 0 )
1735 return( ret );
1736 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001740 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001742
Simon Butcher584a5472016-05-23 16:24:52 +01001743 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1744 if( ret != 0 )
1745 return( ret );
1746 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001750 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001752
Simon Butcher584a5472016-05-23 16:24:52 +01001753 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1754 if( ret != 0 )
1755 return( ret );
1756 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001760 case MBEDTLS_TLS_EXT_ALPN:
1761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001762
Simon Butcher584a5472016-05-23 16:24:52 +01001763 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1764 if( ret != 0 )
1765 return( ret );
1766 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001768
Johan Pascalb62bb512015-12-03 21:56:45 +01001769#if defined(MBEDTLS_SSL_DTLS_SRTP)
1770 case MBEDTLS_TLS_EXT_USE_SRTP:
1771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02001772
Johan Pascalc3ccd982020-10-28 17:18:18 +01001773 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
1774 if( ret != 0 )
1775 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001776 break;
1777#endif /* MBEDTLS_SSL_DTLS_SRTP */
1778
Simon Butcher584a5472016-05-23 16:24:52 +01001779 default:
Paul Elliott9f352112020-12-09 14:55:45 +00001780 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01001781 ext_id ) );
1782 }
1783
1784 ext_len -= 4 + ext_size;
1785 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00001786 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01001787
Jerry Yue7541932022-01-28 10:21:24 +08001788#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001789
1790 /*
1791 * Try to fall back to default hash SHA1 if the client
1792 * hasn't provided any preferred signature-hash combinations.
1793 */
1794 if( sig_hash_alg_ext_present == 0 )
1795 {
Jerry Yueb821c62022-01-19 18:35:56 +08001796 uint16_t sig_algs[] = { MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA1) };
1797 mbedtls_md_type_t md_default = MBEDTLS_MD_NONE;
Jerry Yu97198852022-01-21 16:16:01 +08001798 for( i = 0; i < sizeof( sig_algs ) / sizeof( sig_algs[0] ) ; i++ )
Jerry Yueb821c62022-01-19 18:35:56 +08001799 {
1800 if( mbedtls_ssl_sig_alg_is_offered( ssl, sig_algs[ i ] ) )
1801 md_default = MBEDTLS_MD_SHA1;
1802 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01001803
Jerry Yu97198852022-01-21 16:16:01 +08001804 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs,
1805 md_default );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001806 }
1807
Jerry Yue7541932022-01-28 10:21:24 +08001808#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001809
Paul Bakker48916f92012-09-16 19:57:18 +00001810 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001811 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1812 */
1813 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1818#if defined(MBEDTLS_SSL_RENEGOTIATION)
1819 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001820 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1822 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001823 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1824 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01001825 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001826 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001827#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001829 break;
1830 }
1831 }
1832
1833 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001834 * Renegotiation security checks
1835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001837 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001840 handshake_failure = 1;
1841 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#if defined(MBEDTLS_SSL_RENEGOTIATION)
1843 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1844 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001845 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001848 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1851 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001852 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001855 handshake_failure = 1;
1856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1858 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001859 renegotiation_info_seen == 1 )
1860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001862 handshake_failure = 1;
1863 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001865
1866 if( handshake_failure == 1 )
1867 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02001868 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1869 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001870 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001871 }
Paul Bakker380da532012-04-18 16:10:25 +00001872
Paul Bakker41c83d32013-03-20 14:39:14 +01001873 /*
Glenn Strauss2ed95272022-01-21 18:02:17 -05001874 * Server certification selection (after processing TLS extensions)
1875 */
1876 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
1877 {
1878 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
1879 return( ret );
1880 }
1881
1882 /*
Paul Bakker41c83d32013-03-20 14:39:14 +01001883 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001884 * (At the end because we need information from the EC-based extensions
Glenn Strauss2ed95272022-01-21 18:02:17 -05001885 * and certificate from the SNI callback triggered by the SNI extension
1886 * or certificate from server certificate selection callback.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001887 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001888 got_common_suite = 0;
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001889 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001890 ciphersuite_info = NULL;
TRodziewicz8476f2f2021-06-02 14:34:47 +02001891
TRodziewicz3946f792021-06-14 12:11:18 +02001892 if (ssl->conf->respect_cli_pref == MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT)
TRodziewicz8476f2f2021-06-02 14:34:47 +02001893 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001894 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001895 for( i = 0; ciphersuites[i] != 0; i++ )
1896 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001897 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001898 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001899
TRodziewicz8476f2f2021-06-02 14:34:47 +02001900 got_common_suite = 1;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001901
TRodziewicz8476f2f2021-06-02 14:34:47 +02001902 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1903 &ciphersuite_info ) ) != 0 )
1904 return( ret );
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001905
TRodziewicz8476f2f2021-06-02 14:34:47 +02001906 if( ciphersuite_info != NULL )
1907 goto have_ciphersuite;
1908 }
1909 } else {
1910 for( i = 0; ciphersuites[i] != 0; i++ )
1911 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
1912 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001913 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001914 continue;
1915
1916 got_common_suite = 1;
1917
1918 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1919 &ciphersuite_info ) ) != 0 )
1920 return( ret );
1921
1922 if( ciphersuite_info != NULL )
1923 goto have_ciphersuite;
1924 }
1925 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001926
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001927 if( got_common_suite )
1928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001930 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001931 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1932 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman096c4112021-06-29 09:52:06 +01001933 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001934 }
1935 else
1936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001938 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1939 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01001940 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001941 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001942
1943have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001945
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001946 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001947 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001948
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 ssl->state++;
1950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001954#endif
1955
Hanno Becker7e5437a2017-04-28 17:15:26 +01001956 /* Debugging-only output for testsuite */
1957#if defined(MBEDTLS_DEBUG_C) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001958 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001959 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1960 {
1961 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
1962 if( sig_alg != MBEDTLS_PK_NONE )
1963 {
1964 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
1965 sig_alg );
1966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
1967 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
1968 }
1969 else
1970 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Paul Elliott3891caf2020-12-17 18:42:40 +00001972 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001973 }
1974 }
1975#endif
1976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
1979 return( 0 );
1980}
1981
Hanno Beckera0e20d02019-05-15 14:03:01 +01001982#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01001983static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
1984 unsigned char *buf,
1985 size_t *olen )
1986{
1987 unsigned char *p = buf;
1988 size_t ext_len;
1989 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1990
1991 *olen = 0;
1992
1993 /* Skip writing the extension if we don't want to use it or if
1994 * the client hasn't offered it. */
1995 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
1996 return;
1997
1998 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
1999 * which is at most 255, so the increment cannot overflow. */
2000 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2001 {
2002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2003 return;
2004 }
2005
2006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2007
2008 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01002009 * Quoting draft-ietf-tls-dtls-connection-id-05
2010 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01002011 *
2012 * struct {
2013 * opaque cid<0..2^8-1>;
2014 * } ConnectionId;
2015 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002016 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
2017 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002018 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002019 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
2020 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01002021
2022 *p++ = (uint8_t) ssl->own_cid_len;
2023 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2024
2025 *olen = ssl->own_cid_len + 5;
2026}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002027#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01002028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2030static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002031 unsigned char *buf,
2032 size_t *olen )
2033{
2034 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 const mbedtls_ssl_ciphersuite_t *suite = NULL;
Przemyslaw Stekiel2c87a202022-01-31 10:59:30 +01002036#if defined(MBEDTLS_USE_PSA_CRYPTO)
2037 psa_key_type_t key_type;
2038 psa_algorithm_t alg;
2039 size_t key_bits;
2040#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 const mbedtls_cipher_info_t *cipher = NULL;
Przemyslaw Stekiel2c87a202022-01-31 10:59:30 +01002042#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002043
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002044 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002045 {
2046 *olen = 0;
2047 return;
2048 }
2049
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002050 /*
2051 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2052 * from a client and then selects a stream or Authenticated Encryption
2053 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2054 * encrypt-then-MAC response extension back to the client."
2055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002057 ssl->session_negotiate->ciphersuite ) ) == NULL ||
Przemyslaw Stekiel2c87a202022-01-31 10:59:30 +01002058#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01002059 ( mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg,
2060 &key_type, &key_bits ) != PSA_SUCCESS ) ||
Przemyslaw Stekiel2c87a202022-01-31 10:59:30 +01002061 alg != PSA_ALG_CBC_NO_PADDING )
2062#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2064 cipher->mode != MBEDTLS_MODE_CBC )
Przemyslaw Stekiel2c87a202022-01-31 10:59:30 +01002065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002066 {
2067 *olen = 0;
2068 return;
2069 }
2070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002072
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002073 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
2074 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002075
2076 *p++ = 0x00;
2077 *p++ = 0x00;
2078
2079 *olen = 4;
2080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2084static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002085 unsigned char *buf,
2086 size_t *olen )
2087{
2088 unsigned char *p = buf;
2089
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002090 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002091 {
2092 *olen = 0;
2093 return;
2094 }
2095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002097 "extension" ) );
2098
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002099 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
2100 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002101
2102 *p++ = 0x00;
2103 *p++ = 0x00;
2104
2105 *olen = 4;
2106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2110static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002111 unsigned char *buf,
2112 size_t *olen )
2113{
2114 unsigned char *p = buf;
2115
2116 if( ssl->handshake->new_session_ticket == 0 )
2117 {
2118 *olen = 0;
2119 return;
2120 }
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002123
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002124 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
2125 p += 2;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002126
2127 *p++ = 0x00;
2128 *p++ = 0x00;
2129
2130 *olen = 4;
2131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002135 unsigned char *buf,
2136 size_t *olen )
2137{
2138 unsigned char *p = buf;
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002141 {
2142 *olen = 0;
2143 return;
2144 }
2145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002147
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002148 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
2149 p += 2;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_SSL_RENEGOTIATION)
2152 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002153 {
2154 *p++ = 0x00;
2155 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2156 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002157
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002158 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2159 p += ssl->verify_data_len;
2160 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2161 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002162 }
2163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002165 {
2166 *p++ = 0x00;
2167 *p++ = 0x01;
2168 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002169 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002170
2171 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002172}
2173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2175static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002176 unsigned char *buf,
2177 size_t *olen )
2178{
2179 unsigned char *p = buf;
2180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002182 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002183 *olen = 0;
2184 return;
2185 }
2186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002188
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002189 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
2190 p += 2;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002191
2192 *p++ = 0x00;
2193 *p++ = 1;
2194
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002195 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002196
2197 *olen = 5;
2198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002200
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002201#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002202 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002204 unsigned char *buf,
2205 size_t *olen )
2206{
2207 unsigned char *p = buf;
2208 ((void) ssl);
2209
Paul Bakker677377f2013-10-28 12:54:26 +01002210 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002212 {
2213 *olen = 0;
2214 return;
2215 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002218
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002219 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
2220 p += 2;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002221
2222 *p++ = 0x00;
2223 *p++ = 2;
2224
2225 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002227
2228 *olen = 6;
2229}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002230#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002231
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002232#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2233static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2234 unsigned char *buf,
2235 size_t *olen )
2236{
Janos Follath865b3eb2019-12-16 11:46:15 +00002237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002238 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002239 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002240 size_t kkpp_len;
2241
2242 *olen = 0;
2243
2244 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002245 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002246 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2247 return;
2248
2249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2250
2251 if( end - p < 4 )
2252 {
2253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2254 return;
2255 }
2256
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002257 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
2258 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002259
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002260 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2261 p + 2, end - p - 2, &kkpp_len,
2262 ssl->conf->f_rng, ssl->conf->p_rng );
2263 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002264 {
2265 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2266 return;
2267 }
2268
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002269 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
2270 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002271
2272 *olen = kkpp_len + 4;
2273}
2274#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_SSL_ALPN )
2277static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002278 unsigned char *buf, size_t *olen )
2279{
2280 if( ssl->alpn_chosen == NULL )
2281 {
2282 *olen = 0;
2283 return;
2284 }
2285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002287
2288 /*
2289 * 0 . 1 ext identifier
2290 * 2 . 3 ext length
2291 * 4 . 5 protocol list length
2292 * 6 . 6 protocol name length
2293 * 7 . 7+n protocol name
2294 */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002295 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002296
2297 *olen = 7 + strlen( ssl->alpn_chosen );
2298
Joe Subbiani6dd73642021-07-19 11:56:54 +01002299 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002300
Joe Subbiani6dd73642021-07-19 11:56:54 +01002301 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002302
Joe Subbiani2194dc42021-07-14 12:31:31 +01002303 buf[6] = MBEDTLS_BYTE_0( *olen - 7 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002304
2305 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002308
Ron Eldor3adb9922017-12-21 10:15:08 +02002309#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002310static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002311 unsigned char *buf,
2312 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002313{
Ron Eldor75870ec2018-12-06 17:31:55 +02002314 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002315 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002316 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2317
2318 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002319
Johan Pascalc3ccd982020-10-28 17:18:18 +01002320 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2321 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002322 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002323 return;
2324 }
2325
2326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2327
Johan Pascald576fdb2020-09-22 10:39:53 +02002328 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002329 {
2330 mki_len = ssl->dtls_srtp_info.mki_len;
2331 }
2332
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002333 /* The extension total size is 9 bytes :
2334 * - 2 bytes for the extension tag
2335 * - 2 bytes for the total size
2336 * - 2 bytes for the protection profile length
2337 * - 2 bytes for the protection profile
2338 * - 1 byte for the mki length
2339 * + the actual mki length
2340 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002341 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002342 {
2343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2344 return;
2345 }
2346
Johan Pascalb62bb512015-12-03 21:56:45 +01002347 /* extension */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002348 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 );
Ron Eldoref72faf2018-07-12 11:54:20 +03002349 /*
2350 * total length 5 and mki value: only one profile(2 bytes)
2351 * and length(2 bytes) and srtp_mki )
2352 */
Ron Eldor591f1622018-01-22 12:30:04 +02002353 ext_len = 5 + mki_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002354 MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 );
Johan Pascalb62bb512015-12-03 21:56:45 +01002355
2356 /* protection profile length: 2 */
2357 buf[4] = 0x00;
2358 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002359 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002360 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002361 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002362 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002363 MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002364 }
2365 else
2366 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002368 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002369 }
2370
Ron Eldor591f1622018-01-22 12:30:04 +02002371 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002372 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002373
Ron Eldor591f1622018-01-22 12:30:04 +02002374 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002375}
2376#endif /* MBEDTLS_SSL_DTLS_SRTP */
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2379static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002380{
Janos Follath865b3eb2019-12-16 11:46:15 +00002381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002382 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002383 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002386
2387 /*
2388 * struct {
2389 * ProtocolVersion server_version;
2390 * opaque cookie<0..2^8-1>;
2391 * } HelloVerifyRequest;
2392 */
2393
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002394 /* The RFC is not clear on this point, but sending the actual negotiated
2395 * version looks like the most interoperable thing to do. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002397 ssl->conf->transport, p );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002399 p += 2;
2400
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002401 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002402 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002406 }
2407
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002408 /* Skip length byte until we know the length */
2409 cookie_len_byte = p++;
2410
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002411 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002412 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002413 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002416 return( ret );
2417 }
2418
2419 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002422
2423 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2425 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002428
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002429 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002430 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002432 return( ret );
2433 }
2434
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002435#if defined(MBEDTLS_SSL_PROTO_DTLS)
2436 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2437 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2438 {
2439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2440 return( ret );
2441 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002442#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002445
2446 return( 0 );
2447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002449
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002450static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
Hanno Becker64ce9742021-04-15 08:19:40 +01002451{
2452 int ret;
Hanno Beckera5b1a392021-04-15 16:48:01 +01002453 mbedtls_ssl_session session_tmp;
Hanno Becker64ce9742021-04-15 08:19:40 +01002454 mbedtls_ssl_session * const session = ssl->session_negotiate;
2455
2456 /* Resume is 0 by default, see ssl_handshake_init().
2457 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2458 if( ssl->handshake->resume == 1 )
2459 return;
Hanno Becker7ad77962021-05-13 06:13:34 +01002460 if( session->id_len == 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +01002461 return;
2462 if( ssl->conf->f_get_cache == NULL )
2463 return;
2464#if defined(MBEDTLS_SSL_RENEGOTIATION)
2465 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2466 return;
2467#endif
2468
Hanno Beckera5b1a392021-04-15 16:48:01 +01002469 mbedtls_ssl_session_init( &session_tmp );
2470
Hanno Becker64ce9742021-04-15 08:19:40 +01002471 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
Hanno Beckerccdaf6e2021-04-15 09:26:17 +01002472 session->id,
2473 session->id_len,
Hanno Becker64ce9742021-04-15 08:19:40 +01002474 &session_tmp );
2475 if( ret != 0 )
2476 goto exit;
2477
2478 if( session->ciphersuite != session_tmp.ciphersuite ||
2479 session->compression != session_tmp.compression )
2480 {
2481 /* Mismatch between cached and negotiated session */
2482 goto exit;
2483 }
2484
2485 /* Move semantics */
Hanno Becker64ce9742021-04-15 08:19:40 +01002486 mbedtls_ssl_session_free( session );
2487 *session = session_tmp;
Hanno Beckerfc1f4132021-05-14 14:57:54 +01002488 memset( &session_tmp, 0, sizeof( session_tmp ) );
Hanno Becker64ce9742021-04-15 08:19:40 +01002489
2490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2491 ssl->handshake->resume = 1;
2492
2493exit:
2494
Hanno Beckerdf564022021-05-13 06:26:37 +01002495 mbedtls_ssl_session_free( &session_tmp );
Hanno Becker64ce9742021-04-15 08:19:40 +01002496}
2497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002501 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002502#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002504 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 unsigned char *buf, *p;
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002511 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002515
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002516 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002519
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002520 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2523 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002524 }
2525
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 /*
2527 * 0 . 0 handshake type
2528 * 1 . 3 handshake length
2529 * 4 . 5 protocol version
2530 * 6 . 9 UNIX time()
2531 * 10 . 37 random bytes
2532 */
2533 buf = ssl->out_msg;
2534 p = buf + 4;
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002537 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002538 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002541 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002544 t = mbedtls_time( NULL );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002545 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
2546 p += 4;
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Paul Elliottd48d5c62021-01-07 14:47:05 +00002548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2549 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002550#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002551 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002552 return( ret );
2553
2554 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002557 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002558 return( ret );
2559
2560 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Paul Bakker48916f92012-09-16 19:57:18 +00002562 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002566 ssl_handle_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002568 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 {
2570 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002571 * New session, create a new session id,
2572 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 ssl->state++;
2575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002577 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002578#endif
2579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002581 if( ssl->handshake->new_session_ticket != 0 )
2582 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002583 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002584 memset( ssl->session_negotiate->id, 0, 32 );
2585 }
2586 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002588 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002589 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002590 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002591 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002592 return( ret );
2593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
2595 else
2596 {
2597 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002598 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002600 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002606 return( ret );
2607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
2609
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002610 /*
2611 * 38 . 38 session id length
2612 * 39 . 38+n session id
2613 * 39+n . 40+n chosen ciphersuite
2614 * 41+n . 41+n chosen compression alg.
2615 * 42+n . 43+n extensions length
2616 * 44+n . 43+n+m extensions
2617 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002618 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2619 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2620 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Paul Elliottd48d5c62021-01-07 14:47:05 +00002622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002625 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002627 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
2628 p += 2;
Joe Subbianifbeb6922021-07-16 14:27:50 +01002629 *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2632 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002634 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002635
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002636 /*
2637 * First write extensions, then the total length
2638 */
2639 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2640 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002643 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2644 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002645#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002646
Hanno Beckera0e20d02019-05-15 14:03:01 +01002647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002648 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2649 ext_len += olen;
2650#endif
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002653 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2654 ext_len += olen;
2655#endif
2656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002658 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2659 ext_len += olen;
2660#endif
2661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002663 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2664 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002665#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002666
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002667#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002668 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02002669 if ( mbedtls_ssl_ciphersuite_uses_ec(
2670 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2671 {
2672 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2673 ext_len += olen;
2674 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002675#endif
2676
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002677#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2678 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2679 ext_len += olen;
2680#endif
2681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002683 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2684 ext_len += olen;
2685#endif
2686
Johan Pascalb62bb512015-12-03 21:56:45 +01002687#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01002688 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
2689 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002690#endif
2691
Paul Elliottd48d5c62021-01-07 14:47:05 +00002692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2693 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002694
Paul Bakkera7036632014-04-30 10:15:38 +02002695 if( ext_len > 0 )
2696 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002697 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani94180e72021-08-20 16:20:44 +01002698 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02002699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
2701 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2703 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002705 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 return( ret );
2710}
2711
Gilles Peskineeccd8882020-03-10 12:19:08 +01002712#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002715 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002716 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002719
Hanno Becker77adddc2019-02-07 12:32:43 +00002720 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002723 ssl->state++;
2724 return( 0 );
2725 }
2726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002729}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002730#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002734 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002735 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002736 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002737 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002739 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002741 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
2745 ssl->state++;
2746
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002747#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2748 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2749 authmode = ssl->handshake->sni_authmode;
2750 else
2751#endif
2752 authmode = ssl->conf->authmode;
2753
Hanno Becker77adddc2019-02-07 12:32:43 +00002754 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002755 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 {
Johan Pascal4f099262020-09-22 10:59:26 +02002757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2758 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
2761 /*
2762 * 0 . 0 handshake type
2763 * 1 . 3 handshake length
2764 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002765 * 5 .. m-1 cert types
2766 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002767 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 * n .. n+1 length of all DNs
2769 * n+2 .. n+3 length of DN 1
2770 * n+4 .. ... Distinguished Name #1
2771 * ... .. ... length of DN 2, etc.
2772 */
2773 buf = ssl->out_msg;
2774 p = buf + 4;
2775
2776 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002777 * Supported certificate types
2778 *
2779 * ClientCertificateType certificate_types<1..2^8-1>;
2780 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002782 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_RSA_C)
2785 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#if defined(MBEDTLS_ECDSA_C)
2788 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002789#endif
2790
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002791 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002792 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002793
Paul Bakker577e0062013-08-28 11:57:20 +02002794 sa_len = 0;
Jerry Yue7541932022-01-28 10:21:24 +08002795
Paul Bakker926af752012-11-23 13:38:07 +01002796 /*
2797 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002798 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002799 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2800 *
2801 * struct {
2802 * HashAlgorithm hash;
2803 * SignatureAlgorithm signature;
2804 * } SignatureAndHashAlgorithm;
2805 *
2806 * enum { (255) } HashAlgorithm;
2807 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002810 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002811 /*
2812 * Supported signature algorithms
2813 */
Jerry Yu6106fdc2022-01-12 16:36:14 +08002814 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
2815 if( sig_alg == NULL )
2816 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
2817
2818 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Simon Butcher99000142016-10-13 17:21:01 +01002819 {
Jerry Yu713013f2022-01-17 18:16:35 +08002820 unsigned char hash = MBEDTLS_BYTE_1( *sig_alg );
Simon Butcher99000142016-10-13 17:21:01 +01002821
Jerry Yu713013f2022-01-17 18:16:35 +08002822 if( mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
Simon Butcher99000142016-10-13 17:21:01 +01002823 continue;
Jerry Yu1bab3012022-01-19 17:43:22 +08002824 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
Jerry Yu1abd1bc2021-12-21 21:27:48 +08002825 continue;
Simon Butcher99000142016-10-13 17:21:01 +01002826
Jerry Yu1abd1bc2021-12-21 21:27:48 +08002827 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, sa_len );
2828 sa_len += 2;
Simon Butcher99000142016-10-13 17:21:01 +01002829 }
Paul Bakker926af752012-11-23 13:38:07 +01002830
Joe Subbiani6dd73642021-07-19 11:56:54 +01002831 MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002832 sa_len += 2;
2833 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002836 /*
2837 * DistinguishedName certificate_authorities<0..2^16-1>;
2838 * opaque DistinguishedName<1..2^16-1>;
2839 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002842 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01002843
2844 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00002846 /* NOTE: If trusted certificates are provisioned
2847 * via a CA callback (configured through
2848 * `mbedtls_ssl_conf_ca_cb()`, then the
2849 * CertificateRequest is currently left empty. */
2850
Janos Follath088ce432017-04-10 12:42:31 +01002851#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2852 if( ssl->handshake->sni_ca_chain != NULL )
2853 crt = ssl->handshake->sni_ca_chain;
2854 else
2855#endif
2856 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002857
Janos Follath088ce432017-04-10 12:42:31 +01002858 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002859 {
irwirc9bc3002020-04-01 13:46:36 +03002860 /* It follows from RFC 5280 A.1 that this length
2861 * can be represented in at most 11 bits. */
2862 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01002863
irwirc9bc3002020-04-01 13:46:36 +03002864 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01002865 {
2866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2867 break;
2868 }
2869
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002870 MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 );
2871 p += 2;
Janos Follath088ce432017-04-10 12:42:31 +01002872 memcpy( p, crt->subject_raw.p, dn_size );
2873 p += dn_size;
2874
2875 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2876
2877 total_dn_size += 2 + dn_size;
2878 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 }
2881
Paul Bakker926af752012-11-23 13:38:07 +01002882 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2884 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002885 MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002887 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
2891 return( ret );
2892}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002893#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2896 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2897static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002898{
Janos Follath865b3eb2019-12-16 11:46:15 +00002899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2904 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002905 }
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2908 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2909 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002912 return( ret );
2913 }
2914
2915 return( 0 );
2916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2918 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002919
Gilles Peskineeccd8882020-03-10 12:19:08 +01002920#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002921 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002922static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002923 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002924{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002925 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2926 * signature length which will be added in ssl_write_server_key_exchange
2927 * after the call to ssl_prepare_server_key_exchange.
2928 * ssl_write_server_key_exchange also takes care of incrementing
2929 * ssl->out_msglen. */
2930 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10002931 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002932 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02002933 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002934 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002935 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
2936 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002937 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02002938 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002939 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01002940 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002941 return( ret );
2942}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002943#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002944 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002945
Gilles Peskined3eb0612018-01-08 17:07:44 +01002946/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02002947 * calculating the signature if any, but excluding formatting the
2948 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01002949static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
2950 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00002951{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002953 ssl->handshake->ciphersuite_info;
2954
Gilles Peskineeccd8882020-03-10 12:19:08 +01002955#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
Jerry Yuc5aef882021-12-23 20:15:02 +08002956#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01002957 unsigned char *dig_signed = NULL;
Jerry Yuc5aef882021-12-23 20:15:02 +08002958#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002959#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002960
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002961 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002962#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02002963 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002964#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002965
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002966#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef00f1522021-06-22 00:09:00 +02002967#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2968 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
2969#else
2970 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
2971#endif
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002972#endif
Gilles Peskinef00f1522021-06-22 00:09:00 +02002973
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002974 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002976 /*
2977 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002978 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002979 *
2980 */
2981
2982 /*
2983 * - ECJPAKE key exchanges
2984 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002985#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2986 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2987 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002989 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002990
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002991 ret = mbedtls_ecjpake_write_round_two(
2992 &ssl->handshake->ecjpake_ctx,
2993 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10002994 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002995 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002996 if( ret != 0 )
2997 {
2998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
2999 return( ret );
3000 }
3001
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003002 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003003 }
3004#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3005
Hanno Becker1aa267c2017-04-28 17:08:27 +01003006 /*
3007 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3008 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3009 * we use empty support identity hints here.
3010 **/
3011#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3013 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3014 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003015 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003016 ssl->out_msg[ssl->out_msglen++] = 0x00;
3017 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003018 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3020 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003021
Hanno Becker7e5437a2017-04-28 17:15:26 +01003022 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003023 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003024 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003025#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003026 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003027 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003029 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003030
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01003031 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3032 {
3033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3035 }
3036
Paul Bakker41c83d32013-03-20 14:39:14 +01003037 /*
3038 * Ephemeral DH parameters:
3039 *
3040 * struct {
3041 * opaque dh_p<1..2^16-1>;
3042 * opaque dh_g<1..2^16-1>;
3043 * opaque dh_Ys<1..2^16-1>;
3044 * } ServerDHParams;
3045 */
Hanno Beckerab740562017-10-04 13:15:37 +01003046 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3047 &ssl->conf->dhm_P,
3048 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003049 {
Hanno Beckerab740562017-10-04 13:15:37 +01003050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003051 return( ret );
3052 }
Paul Bakker48916f92012-09-16 19:57:18 +00003053
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003054 if( ( ret = mbedtls_dhm_make_params(
3055 &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003056 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003057 ssl->out_msg + ssl->out_msglen, &len,
3058 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003061 return( ret );
3062 }
3063
Jerry Yuc5aef882021-12-23 20:15:02 +08003064#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003065 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003066#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003067
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003068 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3071 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3072 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3073 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003074 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003075#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003076
Hanno Becker1aa267c2017-04-28 17:08:27 +01003077 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003078 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003079 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003080#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003081 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003083 /*
3084 * Ephemeral ECDH parameters:
3085 *
3086 * struct {
3087 * ECParameters curve_params;
3088 * ECPoint public;
3089 * } ServerECDHParams;
3090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 const mbedtls_ecp_curve_info **curve = NULL;
Brett Warren01f3dae2021-08-17 13:50:51 +01003092 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Janos Follath865b3eb2019-12-16 11:46:15 +00003093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003094 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003095
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003096 /* Match our preference list against the offered curves */
Brett Warren01f3dae2021-08-17 13:50:51 +01003097 if( group_list == NULL )
3098 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
3099 for( ; *group_list != 0; group_list++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003100 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
Brett Warren01f3dae2021-08-17 13:50:51 +01003101 if( (*curve)->tls_id == *group_list )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003102 goto curve_matching_done;
3103
3104curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003105 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01003108 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Gergely Budai987bfb52014-01-19 21:48:42 +01003109 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003112
Andrzej Kurekf093a3d2019-02-01 02:50:36 -05003113 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3114 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003115 {
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02003116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003117 return( ret );
3118 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003120 if( ( ret = mbedtls_ecdh_make_params(
3121 &ssl->handshake->ecdh_ctx, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003122 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10003123 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
Gilles Peskinefe1c0932017-11-23 13:35:02 +01003124 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003127 return( ret );
3128 }
3129
Jerry Yuc5aef882021-12-23 20:15:02 +08003130#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003131 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003132#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003133
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003134 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003136 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3137 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003138 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003139#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Hanno Becker1aa267c2017-04-28 17:08:27 +01003141 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003142 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003143 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003144 * exchange parameters, compute and add the signature here.
3145 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003146 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003147#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003148 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149 {
Gilles Peskine1004c192018-01-08 16:59:14 +01003150 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003151 size_t hashlen = 0;
Ronald Cron69a63422021-10-18 09:47:58 +02003152#if defined(MBEDTLS_USE_PSA_CRYPTO)
3153 unsigned char hash[PSA_HASH_MAX_SIZE];
3154#else
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003155 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Ronald Cron69a63422021-10-18 09:47:58 +02003156#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00003157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003158
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003159 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003160 * 2.1: Choose hash algorithm:
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003161 * For TLS 1.2, obey signature-hash-algorithm extension
3162 * to choose appropriate hash.
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003163 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003164
Jerry Yuc5aef882021-12-23 20:15:02 +08003165 mbedtls_md_type_t md_alg;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003166
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003167 mbedtls_pk_type_t sig_alg =
3168 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003170 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003171 /* For TLS 1.2, obey signature-hash-algorithm extension
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003172 * (RFC 5246, Sec. 7.4.1.4.1). */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003173 if( sig_alg == MBEDTLS_PK_NONE ||
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003174 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3175 sig_alg ) ) == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker4cb1f4d2017-10-10 15:59:57 +01003178 /* (... because we choose a cipher suite
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003179 * only if there is a matching hash.) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003181 }
3182 }
Paul Bakker577e0062013-08-28 11:57:20 +02003183 else
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003184 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003187 }
3188
Paul Elliott3891caf2020-12-17 18:42:40 +00003189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003190
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003191 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003192 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003195 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003196 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003197 dig_signed,
3198 dig_signed_len,
3199 md_alg );
3200 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003201 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003202 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003203 else
Paul Bakker577e0062013-08-28 11:57:20 +02003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003207 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003208
Gilles Peskineebd652f2018-01-05 21:18:59 +01003209 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003210
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003211 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003212 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker23f36802012-09-28 14:15:14 +00003215 {
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003216 /*
3217 * For TLS 1.2, we need to specify signature and hash algorithm
Hanno Becker7e5437a2017-04-28 17:15:26 +01003218 * explicitly through a prefix to the signature.
3219 *
3220 * struct {
3221 * HashAlgorithm hash;
3222 * SignatureAlgorithm signature;
3223 * } SignatureAndHashAlgorithm;
3224 *
3225 * struct {
3226 * SignatureAndHashAlgorithm algorithm;
3227 * opaque signature<0..2^16-1>;
3228 * } DigitallySigned;
3229 *
3230 */
3231
Gilles Peskine1004c192018-01-08 16:59:14 +01003232 ssl->out_msg[ssl->out_msglen++] =
3233 mbedtls_ssl_hash_from_md_alg( md_alg );
3234 ssl->out_msg[ssl->out_msglen++] =
3235 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003236 }
3237
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003238#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003239 if( ssl->conf->f_async_sign_start != NULL )
3240 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003241 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003242 mbedtls_ssl_own_cert( ssl ),
3243 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003244 switch( ret )
3245 {
3246 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3247 /* act as if f_async_sign was null */
3248 break;
3249 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003250 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003251 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003252 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003253 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003254 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3255 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003256 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003257 return( ret );
3258 }
3259 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003260#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003261
3262 if( mbedtls_ssl_own_key( ssl ) == NULL )
3263 {
3264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3265 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3266 }
3267
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003268 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3269 * signature length which will be added in ssl_write_server_key_exchange
3270 * after the call to ssl_prepare_server_key_exchange.
3271 * ssl_write_server_key_exchange also takes care of incrementing
3272 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003273 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3274 md_alg, hash, hashlen,
3275 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003276 out_buf_len - ssl->out_msglen - 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003277 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003278 ssl->conf->f_rng,
3279 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003282 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003283 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003284 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003285#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003286
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003287 return( 0 );
3288}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003289
Gilles Peskined3eb0612018-01-08 17:07:44 +01003290/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003291 * that do not include a ServerKeyExchange message, do nothing. Either
3292 * way, if successful, move on to the next step in the SSL state
3293 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003294static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3295{
Janos Follath865b3eb2019-12-16 11:46:15 +00003296 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003297 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003298#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003299 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003300 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003301#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003302
Gilles Peskined3eb0612018-01-08 17:07:44 +01003303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3304
Gilles Peskineeccd8882020-03-10 12:19:08 +01003305#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003306 /* Extract static ECDH parameters and abort if ServerKeyExchange
3307 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003308 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3309 {
3310 /* For suites involving ECDH, extract DH parameters
3311 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003312#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003313 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3314 {
3315 ssl_get_ecdh_params_from_cert( ssl );
3316 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003317#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003318
3319 /* Key exchanges not involving ephemeral keys don't use
3320 * ServerKeyExchange, so end here. */
3321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3322 ssl->state++;
3323 return( 0 );
3324 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003325#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003326
Gilles Peskineeccd8882020-03-10 12:19:08 +01003327#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003328 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003329 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003330 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003331 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003332 {
3333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3334 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003335 }
3336 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003337#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003338 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003339 {
3340 /* ServerKeyExchange is needed. Prepare the message. */
3341 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003342 }
3343
3344 if( ret != 0 )
3345 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003346 /* If we're starting to write a new message, set ssl->out_msglen
3347 * to 0. But if we're resuming after an asynchronous message,
3348 * out_msglen is the amount of data written so far and mst be
3349 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003350 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3352 else
3353 ssl->out_msglen = 0;
3354 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003355 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003356
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003357 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003358 * ssl_prepare_server_key_exchange already wrote the signature
3359 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003360#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003361 if( signature_len != 0 )
3362 {
Joe Subbianifbeb6922021-07-16 14:27:50 +01003363 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len );
3364 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len );
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003365
3366 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3367 ssl->out_msg + ssl->out_msglen,
3368 signature_len );
3369
3370 /* Skip over the already-written signature */
3371 ssl->out_msglen += signature_len;
3372 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003373#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003374
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003375 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3377 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003378
3379 ssl->state++;
3380
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003381 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003382 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 return( ret );
3385 }
3386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003389}
3390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003392{
Janos Follath865b3eb2019-12-16 11:46:15 +00003393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003396
3397 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3399 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003400
3401 ssl->state++;
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003404 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003406#endif
3407
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003408 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 return( ret );
3412 }
3413
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003414#if defined(MBEDTLS_SSL_PROTO_DTLS)
3415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3416 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3417 {
3418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3419 return( ret );
3420 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003421#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003424
3425 return( 0 );
3426}
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3429 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3430static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003431 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003434 size_t n;
3435
3436 /*
3437 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3438 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003439 if( *p + 2 > end )
3440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003442 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003443 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003444
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003445 n = ( (*p)[0] << 8 ) | (*p)[1];
3446 *p += 2;
3447
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003448 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003451 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003452 }
3453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003457 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003458 }
3459
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003460 *p += n;
3461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003463
Paul Bakker70df2fb2013-04-17 17:19:09 +02003464 return( ret );
3465}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3467 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3470 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003471
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003472#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003473static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3474 unsigned char *peer_pms,
3475 size_t *peer_pmslen,
3476 size_t peer_pmssize )
3477{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003478 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003479 peer_pms, peer_pmslen, peer_pmssize );
3480 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3481 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003482 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003483 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003484 }
3485 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3486 return( ret );
3487}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003488#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003489
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003490static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3491 const unsigned char *p,
3492 const unsigned char *end,
3493 unsigned char *peer_pms,
3494 size_t *peer_pmslen,
3495 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003496{
Janos Follath865b3eb2019-12-16 11:46:15 +00003497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003498 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3499 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3500 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003501
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003502#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003503 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003504 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003505 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003506 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3508 return( ssl_resume_decrypt_pms( ssl,
3509 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003510 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003511#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003512
3513 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003514 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003515 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003516 if ( p + 2 > end ) {
3517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003518 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003519 }
Joe Subbiani2194dc42021-07-14 12:31:31 +01003520 if( *p++ != MBEDTLS_BYTE_1( len ) ||
3521 *p++ != MBEDTLS_BYTE_0( len ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003522 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003524 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003525 }
3526
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003527 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003530 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003531 }
3532
Gilles Peskine422ccab2018-01-11 18:29:01 +01003533 /*
3534 * Decrypt the premaster secret
3535 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003536#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003537 if( ssl->conf->f_async_decrypt_start != NULL )
3538 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003539 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003540 mbedtls_ssl_own_cert( ssl ),
3541 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003542 switch( ret )
3543 {
3544 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3545 /* act as if f_async_decrypt_start was null */
3546 break;
3547 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003548 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003549 return( ssl_resume_decrypt_pms( ssl,
3550 peer_pms,
3551 peer_pmslen,
3552 peer_pmssize ) );
3553 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003554 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003555 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3556 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003557 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003558 return( ret );
3559 }
3560 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003561#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003562
Gilles Peskine422ccab2018-01-11 18:29:01 +01003563 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3564 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3566 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3567 }
3568
3569 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003570 peer_pms, peer_pmslen, peer_pmssize,
3571 ssl->conf->f_rng, ssl->conf->p_rng );
3572 return( ret );
3573}
3574
3575static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3576 const unsigned char *p,
3577 const unsigned char *end,
3578 size_t pms_offset )
3579{
Janos Follath865b3eb2019-12-16 11:46:15 +00003580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003581 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3582 unsigned char ver[2];
3583 unsigned char fake_pms[48], peer_pms[48];
3584 unsigned char mask;
3585 size_t i, peer_pmslen;
3586 unsigned int diff;
3587
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003588 /* In case of a failure in decryption, the decryption may write less than
3589 * 2 bytes of output, but we always read the first two bytes. It doesn't
3590 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003591 * ret being nonzero, and we only care whether diff is 0.
3592 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3593 * also makes memory analyzers happy (don't access uninitialized memory,
3594 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003595 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003596 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003597
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003598 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3599 peer_pms,
3600 &peer_pmslen,
3601 sizeof( peer_pms ) );
3602
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003603#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003604 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3605 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003606#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Gilles Peskine2e333372018-04-24 13:22:10 +02003609 ssl->handshake->max_minor_ver,
3610 ssl->conf->transport, ver );
3611
3612 /* Avoid data-dependent branches while checking for invalid
3613 * padding, to protect against timing-based Bleichenbacher-type
3614 * attacks. */
3615 diff = (unsigned int) ret;
3616 diff |= peer_pmslen ^ 48;
3617 diff |= peer_pms[0] ^ ver[0];
3618 diff |= peer_pms[1] ^ ver[1];
3619
3620 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
Gabor Mezei90437e32021-10-20 11:59:27 +02003621 mask = mbedtls_ct_uint_mask( diff );
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003622
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003623 /*
3624 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3625 * must not cause the connection to end immediately; instead, send a
3626 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003627 * To protect against timing-based variants of the attack, we must
3628 * not have any branch that depends on whether the decryption was
3629 * successful. In particular, always generate the fake premaster secret,
3630 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003631 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003632 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003633 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003634 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003635 /* It's ok to abort on an RNG failure, since this does not reveal
3636 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003637 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003638 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003639
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003640#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003641 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003643#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003644
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003645 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3646 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003650 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003651 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003652
Gilles Peskine422ccab2018-01-11 18:29:01 +01003653 /* Set pms to either the true or the fake PMS, without
3654 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003655 for( i = 0; i < ssl->handshake->pmslen; i++ )
3656 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3657
3658 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3661 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003662
Gilles Peskineeccd8882020-03-10 12:19:08 +01003663#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003665 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003666{
Paul Bakker6db455e2013-09-18 17:29:31 +02003667 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003668 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003669
Hanno Becker845b9462018-10-26 12:07:29 +01003670 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3673 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003674 }
3675
3676 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003677 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003678 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003679 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003682 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003683 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003684
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003685 n = ( (*p)[0] << 8 ) | (*p)[1];
3686 *p += 2;
3687
irwir6527bd62019-09-21 18:51:25 +03003688 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003691 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003692 }
3693
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003694 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003695 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003696 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003698 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003699 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003700 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003701 /* Identity is not a big secret since clients send it in the clear,
3702 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003703 if( n != ssl->conf->psk_identity_len ||
Gabor Mezei90437e32021-10-20 11:59:27 +02003704 mbedtls_ct_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003707 }
3708 }
3709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003713 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3714 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003716 }
3717
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003718 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003719
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003720 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003721}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003722#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003725{
Janos Follath865b3eb2019-12-16 11:46:15 +00003726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003728 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003729
Hanno Beckere694c3e2017-12-27 21:34:08 +00003730 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003734#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003735 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3736 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3737 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3738 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003739 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003740 {
3741 /* We've already read a record and there is an asynchronous
3742 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003743 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3745 }
3746 else
3747#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01003748 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003751 return( ret );
3752 }
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003755 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003760 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 }
3762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003766 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 }
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3770 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003772 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003775 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003777
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003778 if( p != end )
3779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003781 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003782 }
3783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003785 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01003786 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003787 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003788 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003791 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003792 }
3793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003795 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003796 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3798#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3799 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3800 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3801 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3802 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3803 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3804 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3805 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003808 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003811 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003812 }
3813
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003814 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3815 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003818 &ssl->handshake->pmslen,
3819 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003821 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003824 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003825 }
3826
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003827 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3828 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003829 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003830 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3832 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3833 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3834 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3835#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3836 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003837 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003838 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003841 return( ret );
3842 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003843
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003844 if( p != end )
3845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003847 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003848 }
3849
Hanno Becker845b9462018-10-26 12:07:29 +01003850#if defined(MBEDTLS_USE_PSA_CRYPTO)
3851 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
3852 * and skip the intermediate PMS. */
Hanno Beckerc1385c12018-11-05 12:44:27 +00003853 if( ssl_use_opaque_psk( ssl ) == 1 )
Hanno Becker845b9462018-10-26 12:07:29 +01003854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
3855 else
3856#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003858 ciphersuite_info->key_exchange ) ) != 0 )
3859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003861 return( ret );
3862 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003863 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003864 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3866#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3867 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003868 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003869#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003870 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003871 {
3872 /* There is an asynchronous operation in progress to
3873 * decrypt the encrypted premaster secret, so skip
3874 * directly to resuming this operation. */
3875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
3876 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3877 * won't actually use it, but maintain p anyway for robustness. */
3878 p += ssl->conf->psk_identity_len + 2;
3879 }
3880 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003881#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003882 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003885 return( ret );
3886 }
3887
Hanno Becker845b9462018-10-26 12:07:29 +01003888#if defined(MBEDTLS_USE_PSA_CRYPTO)
3889 /* Opaque PSKs are currently only supported for PSK-only. */
3890 if( ssl_use_opaque_psk( ssl ) == 1 )
3891 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3892#endif
3893
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003894 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003897 return( ret );
3898 }
3899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003901 ciphersuite_info->key_exchange ) ) != 0 )
3902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003904 return( ret );
3905 }
3906 }
3907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3909#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3910 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003911 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003912 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003915 return( ret );
3916 }
3917 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003920 return( ret );
3921 }
3922
Hanno Becker845b9462018-10-26 12:07:29 +01003923#if defined(MBEDTLS_USE_PSA_CRYPTO)
3924 /* Opaque PSKs are currently only supported for PSK-only. */
3925 if( ssl_use_opaque_psk( ssl ) == 1 )
3926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3927#endif
3928
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003929 if( p != end )
3930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003932 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003933 }
3934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003936 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003939 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003940 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003941 }
3942 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3944#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3945 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003946 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003947 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003950 return( ret );
3951 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003954 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003957 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003958 }
3959
Hanno Becker845b9462018-10-26 12:07:29 +01003960#if defined(MBEDTLS_USE_PSA_CRYPTO)
3961 /* Opaque PSKs are currently only supported for PSK-only. */
3962 if( ssl_use_opaque_psk( ssl ) == 1 )
3963 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3964#endif
3965
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003966 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3967 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003970 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003973 return( ret );
3974 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003975 }
3976 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3978#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3979 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003980 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003981 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003984 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003985 }
3986 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003987 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003989#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3990 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3991 {
3992 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3993 p, end - p );
3994 if( ret != 0 )
3995 {
3996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01003997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003998 }
3999
4000 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4001 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4002 ssl->conf->f_rng, ssl->conf->p_rng );
4003 if( ret != 0 )
4004 {
4005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4006 return( ret );
4007 }
4008 }
4009 else
4010#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4013 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004014 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004019 return( ret );
4020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004021
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 ssl->state++;
4023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004025
4026 return( 0 );
4027}
4028
Gilles Peskineeccd8882020-03-10 12:19:08 +01004029#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004031{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004032 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004033 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Hanno Becker77adddc2019-02-07 12:32:43 +00004037 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004040 ssl->state++;
4041 return( 0 );
4042 }
4043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004046}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004047#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004051 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004052 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004053 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004054 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 mbedtls_pk_type_t pk_alg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004057 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004058 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004059 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004062
Hanno Becker2a831a42019-02-07 13:17:25 +00004063 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004066 ssl->state++;
4067 return( 0 );
4068 }
4069
Hanno Becker2a831a42019-02-07 13:17:25 +00004070#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4071 if( ssl->session_negotiate->peer_cert == NULL )
4072 {
4073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4074 ssl->state++;
4075 return( 0 );
4076 }
4077#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4078 if( ssl->session_negotiate->peer_cert_digest == NULL )
4079 {
4080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4081 ssl->state++;
4082 return( 0 );
4083 }
4084#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4085
Simon Butcher99000142016-10-13 17:21:01 +01004086 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004087 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004088 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004089 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004090 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 return( ret );
4092 }
4093
4094 ssl->state++;
4095
Simon Butcher99000142016-10-13 17:21:01 +01004096 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4098 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004101 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004102 }
4103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004105
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004106#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4107 peer_pk = &ssl->handshake->peer_pubkey;
4108#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4109 if( ssl->session_negotiate->peer_cert == NULL )
4110 {
4111 /* Should never happen */
4112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4113 }
4114 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4115#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4116
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004117 /*
4118 * struct {
4119 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4120 * opaque signature<0..2^16-1>;
4121 * } DigitallySigned;
4122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004124 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004125 if( i + 2 > ssl->in_hslen )
4126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004128 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004129 }
4130
Paul Bakker5121ce52009-01-03 21:22:43 +00004131 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004132 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 */
Simon Butcher99000142016-10-13 17:21:01 +01004134 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4135
4136 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004139 " for verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004140 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00004141 }
4142
Simon Butcher99000142016-10-13 17:21:01 +01004143#if !defined(MBEDTLS_MD_SHA1)
4144 if( MBEDTLS_MD_SHA1 == md_alg )
4145 hash_start += 16;
4146#endif
Paul Bakker926af752012-11-23 13:38:07 +01004147
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02004148 /* Info from md_alg will be used instead */
4149 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004150
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004151 i++;
4152
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004153 /*
4154 * Signature
4155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4157 == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004160 " for verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004161 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004162 }
4163
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004164 /*
4165 * Check the certificate's key type matches the signature alg
4166 */
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004167 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004170 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004171 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004172
4173 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02004174 }
4175 else
Paul Bakker577e0062013-08-28 11:57:20 +02004176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004179 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004180
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004181 if( i + 2 > ssl->in_hslen )
4182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004184 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004185 }
4186
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004187 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4188 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004189
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004190 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004193 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00004194 }
4195
Simon Butcher99000142016-10-13 17:21:01 +01004196 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004197 {
4198 size_t dummy_hlen;
4199 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4200 }
Simon Butcher99000142016-10-13 17:21:01 +01004201
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004202 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004203 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004204 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 return( ret );
4208 }
4209
Simon Butcher99000142016-10-13 17:21:01 +01004210 mbedtls_ssl_update_handshake_status( ssl );
4211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004213
Paul Bakkered27a042013-04-18 22:46:23 +02004214 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004215}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004216#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4219static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004220{
Janos Follath865b3eb2019-12-16 11:46:15 +00004221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004222 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004223 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4228 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004229
4230 /*
4231 * struct {
4232 * uint32 ticket_lifetime_hint;
4233 * opaque ticket<0..2^16-1>;
4234 * } NewSessionTicket;
4235 *
4236 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4237 * 8 . 9 ticket_len (n)
4238 * 10 . 9+n ticket content
4239 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004240
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004241 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004242 ssl->session_negotiate,
4243 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004244 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004245 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004246 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004248 tlen = 0;
4249 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004250
Joe Subbiani6dd73642021-07-19 11:56:54 +01004251 MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 );
4252 MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004253 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004254
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004255 /*
4256 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4257 * ChangeCipherSpec share the same state.
4258 */
4259 ssl->handshake->new_session_ticket = 0;
4260
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004261 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004262 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004264 return( ret );
4265 }
4266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004268
4269 return( 0 );
4270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004272
Paul Bakker5121ce52009-01-03 21:22:43 +00004273/*
Paul Bakker1961b702013-01-25 14:49:24 +01004274 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004277{
4278 int ret = 0;
4279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004281
Paul Bakker1961b702013-01-25 14:49:24 +01004282 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 case MBEDTLS_SSL_HELLO_REQUEST:
4285 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004286 break;
4287
Paul Bakker1961b702013-01-25 14:49:24 +01004288 /*
4289 * <== ClientHello
4290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004292 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004293 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295#if defined(MBEDTLS_SSL_PROTO_DTLS)
4296 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4297 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004298#endif
4299
Paul Bakker1961b702013-01-25 14:49:24 +01004300 /*
4301 * ==> ServerHello
4302 * Certificate
4303 * ( ServerKeyExchange )
4304 * ( CertificateRequest )
4305 * ServerHelloDone
4306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004307 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004308 ret = ssl_write_server_hello( ssl );
4309 break;
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4312 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004313 break;
4314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004316 ret = ssl_write_server_key_exchange( ssl );
4317 break;
4318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004320 ret = ssl_write_certificate_request( ssl );
4321 break;
4322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004324 ret = ssl_write_server_hello_done( ssl );
4325 break;
4326
4327 /*
4328 * <== ( Certificate/Alert )
4329 * ClientKeyExchange
4330 * ( CertificateVerify )
4331 * ChangeCipherSpec
4332 * Finished
4333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4335 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004336 break;
4337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004339 ret = ssl_parse_client_key_exchange( ssl );
4340 break;
4341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004343 ret = ssl_parse_certificate_verify( ssl );
4344 break;
4345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4347 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004348 break;
4349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 case MBEDTLS_SSL_CLIENT_FINISHED:
4351 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004352 break;
4353
4354 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004355 * ==> ( NewSessionTicket )
4356 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004357 * Finished
4358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4360#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004361 if( ssl->handshake->new_session_ticket != 0 )
4362 ret = ssl_write_new_session_ticket( ssl );
4363 else
Paul Bakkera503a632013-08-14 13:48:06 +02004364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004366 break;
4367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 case MBEDTLS_SSL_SERVER_FINISHED:
4369 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004370 break;
4371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 case MBEDTLS_SSL_FLUSH_BUFFERS:
4373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4374 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004375 break;
4376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4378 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004379 break;
4380
4381 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 }
4385
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 return( ret );
4387}
TRodziewicz8476f2f2021-06-02 14:34:47 +02004388
TRodziewicz3946f792021-06-14 12:11:18 +02004389void mbedtls_ssl_conf_preference_order( mbedtls_ssl_config *conf, int order )
TRodziewicz8476f2f2021-06-02 14:34:47 +02004390{
TRodziewicz3946f792021-06-14 12:11:18 +02004391 conf->respect_cli_pref = order;
TRodziewicz8476f2f2021-06-02 14:34:47 +02004392}
4393
Jerry Yufb4b6472022-01-27 15:03:26 +08004394#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_2 */