blob: 04121c1bd8b6b42640f4d48d9830dda011b2a5ee [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 {
Glenn Strauss69894072022-01-24 12:58:00 -0500121 ssl->handshake->sni_name = p + 3;
122 ssl->handshake->sni_name_len = hostname_len;
123 if( ssl->conf->f_sni == NULL )
124 return( 0 );
125
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +0200126 ret = ssl->conf->f_sni( ssl->conf->p_sni,
127 ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000128 if( ret != 0 )
129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
131 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
132 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100133 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000134 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000135 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000136 }
137
138 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000139 p += hostname_len + 3;
140 }
141
142 if( servername_list_size != 0 )
143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200145 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100146 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
147 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000148 }
149
150 return( 0 );
151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000153
Gilles Peskineeccd8882020-03-10 12:19:08 +0100154#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker845b9462018-10-26 12:07:29 +0100155static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
156{
157 if( conf->f_psk != NULL )
158 return( 1 );
159
160 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
161 return( 0 );
162
Hanno Becker845b9462018-10-26 12:07:29 +0100163
164#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +0200165 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker845b9462018-10-26 12:07:29 +0100166 return( 1 );
Neil Armstrong8ecd6682022-05-05 11:40:35 +0200167#endif /* MBEDTLS_USE_PSA_CRYPTO */
168
Neil Armstronge952a302022-05-03 10:22:14 +0200169 if( conf->psk != NULL && conf->psk_len != 0 )
170 return( 1 );
Hanno Becker845b9462018-10-26 12:07:29 +0100171
172 return( 0 );
173}
Gilles Peskineeccd8882020-03-10 12:19:08 +0100174#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker845b9462018-10-26 12:07:29 +0100175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000177 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000178 size_t len )
179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_SSL_RENEGOTIATION)
181 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100182 {
183 /* Check verify-data in constant-time. The length OTOH is no secret */
184 if( len != 1 + ssl->verify_data_len ||
185 buf[0] != ssl->verify_data_len ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200186 mbedtls_ct_memcmp( buf + 1, ssl->peer_verify_data,
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100187 ssl->verify_data_len ) != 0 )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200190 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
191 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100192 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100193 }
194 }
195 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000197 {
198 if( len != 1 || buf[0] != 0x0 )
199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200201 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
202 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100203 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000204 }
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +0000207 }
Paul Bakker48916f92012-09-16 19:57:18 +0000208
209 return( 0 );
210}
211
Robert Cragie136884c2015-10-02 13:34:31 +0100212#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100213 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yub925f212022-01-12 11:17:02 +0800214/*
Jerry Yud491ea42022-01-13 16:15:25 +0800215 * Function for parsing a supported groups (TLS 1.3) or supported elliptic
216 * curves (TLS 1.2) extension.
217 *
218 * The "extension_data" field of a supported groups extension contains a
219 * "NamedGroupList" value (TLS 1.3 RFC8446):
220 * enum {
221 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
222 * x25519(0x001D), x448(0x001E),
223 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
224 * ffdhe6144(0x0103), ffdhe8192(0x0104),
225 * ffdhe_private_use(0x01FC..0x01FF),
226 * ecdhe_private_use(0xFE00..0xFEFF),
227 * (0xFFFF)
228 * } NamedGroup;
229 * struct {
230 * NamedGroup named_group_list<2..2^16-1>;
231 * } NamedGroupList;
232 *
233 * The "extension_data" field of a supported elliptic curves extension contains
234 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
235 * enum {
236 * deprecated(1..22),
237 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
238 * x25519(29), x448(30),
239 * reserved (0xFE00..0xFEFF),
240 * deprecated(0xFF01..0xFF02),
241 * (0xFFFF)
242 * } NamedCurve;
243 * struct {
244 * NamedCurve named_curve_list<2..2^16-1>
245 * } NamedCurveList;
246 *
Jerry Yub925f212022-01-12 11:17:02 +0800247 * The TLS 1.3 supported groups extension was defined to be a compatible
248 * generalization of the TLS 1.2 supported elliptic curves extension. They both
249 * share the same extension identifier.
Jerry Yud491ea42022-01-13 16:15:25 +0800250 *
251 * DHE groups are not supported yet.
Jerry Yub925f212022-01-12 11:17:02 +0800252 */
Jerry Yub47d0f82021-12-20 17:34:40 +0800253static int ssl_parse_supported_groups_ext( mbedtls_ssl_context *ssl,
Jerry Yuffef9c52021-12-24 22:31:08 +0800254 const unsigned char *buf,
255 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100256{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200257 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100258 const unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 const mbedtls_ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100260
Philippe Antoine747fd532018-05-30 09:13:21 +0200261 if ( len < 2 ) {
262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100265 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +0200266 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100267 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
268 if( list_size + 2 != len ||
269 list_size % 2 != 0 )
270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200272 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
273 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100274 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +0100275 }
276
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200277 /* Should never happen unless client duplicates the extension */
278 if( ssl->handshake->curves != NULL )
279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200281 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100282 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
283 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200284 }
285
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100286 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200287 * and leave room for a final 0 */
288 our_size = list_size / 2 + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( our_size > MBEDTLS_ECP_DP_MAX )
290 our_size = MBEDTLS_ECP_DP_MAX;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200291
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200293 {
294 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
295 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200296 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200297 }
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200298
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200299 ssl->handshake->curves = curves;
300
Paul Bakker41c83d32013-03-20 14:39:14 +0100301 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200302 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200305
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200306 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100307 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200308 *curves++ = curve_info;
309 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100310 }
311
312 list_size -= 2;
313 p += 2;
314 }
315
316 return( 0 );
317}
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200320 const unsigned char *buf,
321 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100322{
323 size_t list_size;
324 const unsigned char *p;
325
Philippe Antoine747fd532018-05-30 09:13:21 +0200326 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200329 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
330 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100331 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +0100332 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200333 list_size = buf[0];
Paul Bakker41c83d32013-03-20 14:39:14 +0100334
Manuel Pégourié-Gonnardc1b46d02015-09-16 11:18:32 +0200335 p = buf + 1;
Paul Bakker41c83d32013-03-20 14:39:14 +0100336 while( list_size > 0 )
337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
339 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Paul Bakker41c83d32013-03-20 14:39:14 +0100340 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200341#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
342 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200343 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200344#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
345 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100346#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200347 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
348 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100349#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100351 return( 0 );
352 }
353
354 list_size--;
355 p++;
356 }
357
358 return( 0 );
359}
Robert Cragieae8535d2015-10-06 17:11:18 +0100360#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
361 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +0100362
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200363#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
364static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
365 const unsigned char *buf,
366 size_t len )
367{
Janos Follath865b3eb2019-12-16 11:46:15 +0000368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200369
370 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
371 {
372 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
373 return( 0 );
374 }
375
376 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
377 buf, len ) ) != 0 )
378 {
379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200380 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
381 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200382 return( ret );
383 }
384
385 /* Only mark the extension as OK when we're sure it is */
386 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
387
388 return( 0 );
389}
390#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
393static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200394 const unsigned char *buf,
395 size_t len )
396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200400 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
401 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100402 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200403 }
404
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200405 ssl->session_negotiate->mfl_code = buf[0];
406
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200407 return( 0 );
408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200410
Hanno Beckera0e20d02019-05-15 14:03:01 +0100411#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +0100412static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
413 const unsigned char *buf,
414 size_t len )
415{
416 size_t peer_cid_len;
417
418 /* CID extension only makes sense in DTLS */
419 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
420 {
421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
422 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
423 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100424 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100425 }
426
427 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100428 * Quoting draft-ietf-tls-dtls-connection-id-05
429 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker89dcc882019-04-26 13:56:39 +0100430 *
431 * struct {
432 * opaque cid<0..2^8-1>;
433 * } ConnectionId;
434 */
435
436 if( len < 1 )
437 {
438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100440 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
441 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100442 }
443
444 peer_cid_len = *buf++;
445 len--;
446
447 if( len != peer_cid_len )
448 {
449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
450 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100451 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
452 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Becker89dcc882019-04-26 13:56:39 +0100453 }
454
455 /* Ignore CID if the user has disabled its use. */
456 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
457 {
458 /* Leave ssl->handshake->cid_in_use in its default
459 * value of MBEDTLS_SSL_CID_DISABLED. */
460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
461 return( 0 );
462 }
463
464 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
465 {
466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
467 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
468 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100469 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Becker89dcc882019-04-26 13:56:39 +0100470 }
471
Hanno Becker08556bf2019-05-03 12:43:44 +0100472 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Becker89dcc882019-04-26 13:56:39 +0100473 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
474 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
475
476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
477 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
478
Hanno Becker89dcc882019-04-26 13:56:39 +0100479 return( 0 );
480}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100481#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +0100482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
484static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100485 const unsigned char *buf,
486 size_t len )
487{
488 if( len != 0 )
489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200491 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
492 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100493 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100494 }
495
496 ((void) buf);
497
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100498 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100501 }
502
503 return( 0 );
504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
508static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200509 const unsigned char *buf,
510 size_t len )
511{
512 if( len != 0 )
513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200515 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
516 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100517 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200518 }
519
520 ((void) buf);
521
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100522 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200525 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200526
527 return( 0 );
528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_SSL_SESSION_TICKETS)
532static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200533 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200534 size_t len )
535{
Janos Follath865b3eb2019-12-16 11:46:15 +0000536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200537 mbedtls_ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200538
Manuel Pégourié-Gonnardbae389b2015-06-24 10:45:58 +0200539 mbedtls_ssl_session_init( &session );
540
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200541 if( ssl->conf->f_ticket_parse == NULL ||
542 ssl->conf->f_ticket_write == NULL )
543 {
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200544 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200545 }
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200546
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200547 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200548 ssl->handshake->new_session_ticket = 1;
549
Paul Elliottd48d5c62021-01-07 14:47:05 +0000550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200551
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200552 if( len == 0 )
553 return( 0 );
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_SSL_RENEGOTIATION)
556 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200559 return( 0 );
560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200562
563 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200564 * Failures are ok: just ignore the ticket and proceed.
565 */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200566 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
567 buf, len ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200568 {
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200569 mbedtls_ssl_session_free( &session );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200570
571 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
573 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
575 else
576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
577
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200578 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200579 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200580
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200581 /*
582 * Keep the session ID sent by the client, since we MUST send it back to
583 * inform them we're accepting the ticket (RFC 5077 section 3.4)
584 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200585 session.id_len = ssl->session_negotiate->id_len;
586 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200587
588 mbedtls_ssl_session_free( ssl->session_negotiate );
589 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
590
591 /* Zeroize instead of free as we copied the content */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500592 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200595
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200596 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200597
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200598 /* Don't send a new ticket after all, this one is OK */
599 ssl->handshake->new_session_ticket = 0;
600
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200601 return( 0 );
602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_SSL_ALPN)
606static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200607 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200608{
Paul Bakker14b16c62014-05-28 11:33:54 +0200609 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200610 const unsigned char *theirs, *start, *end;
611 const char **ours;
612
613 /* If ALPN not configured, just ignore the extension */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200614 if( ssl->conf->alpn_list == NULL )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200615 return( 0 );
616
617 /*
618 * opaque ProtocolName<1..2^8-1>;
619 *
620 * struct {
621 * ProtocolName protocol_name_list<2..2^16-1>
622 * } ProtocolNameList;
623 */
624
625 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
626 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200627 {
628 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
629 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100630 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200631 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200632
633 list_len = ( buf[0] << 8 ) | buf[1];
634 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200635 {
636 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
637 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100638 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200639 }
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200640
641 /*
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100642 * Validate peer's list (lengths)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200643 */
644 start = buf + 2;
645 end = buf + len;
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100646 for( theirs = start; theirs != end; theirs += cur_len )
647 {
648 cur_len = *theirs++;
649
650 /* Current identifier must fit in list */
651 if( cur_len > (size_t)( end - theirs ) )
652 {
653 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
654 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100655 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100656 }
657
658 /* Empty strings MUST NOT be included */
659 if( cur_len == 0 )
660 {
661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
662 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Becker90d59dd2021-06-24 11:17:13 +0100663 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard239987f2018-01-09 10:43:43 +0100664 }
665 }
666
667 /*
668 * Use our order of preference
669 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200670 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200671 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200672 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200673 for( theirs = start; theirs != end; theirs += cur_len )
674 {
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200675 cur_len = *theirs++;
676
Paul Bakker14b16c62014-05-28 11:33:54 +0200677 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200678 memcmp( theirs, *ours, cur_len ) == 0 )
679 {
680 ssl->alpn_chosen = *ours;
681 return( 0 );
682 }
683 }
684 }
685
686 /* If we get there, no match was found */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
688 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
Dave Rodgman53c86892021-06-29 10:02:06 +0100689 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200692
Johan Pascalb62bb512015-12-03 21:56:45 +0100693#if defined(MBEDTLS_SSL_DTLS_SRTP)
694static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +0300695 const unsigned char *buf,
696 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +0100697{
Johan Pascal43f94902020-09-22 12:25:52 +0200698 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
Johan Pascalb62bb512015-12-03 21:56:45 +0100699 size_t i,j;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200700 size_t profile_length;
701 uint16_t mki_length;
Ron Eldor313d7b52018-12-10 14:56:21 +0200702 /*! 2 bytes for profile length and 1 byte for mki len */
703 const size_t size_of_lengths = 3;
Johan Pascalb62bb512015-12-03 21:56:45 +0100704
705 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +0100706 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
707 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
708 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascal85269572020-08-25 10:01:54 +0200709 {
Johan Pascalb62bb512015-12-03 21:56:45 +0100710 return( 0 );
Johan Pascal85269572020-08-25 10:01:54 +0200711 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100712
713 /* RFC5764 section 4.1.1
714 * uint8 SRTPProtectionProfile[2];
715 *
716 * struct {
717 * SRTPProtectionProfiles SRTPProtectionProfiles;
718 * opaque srtp_mki<0..255>;
719 * } UseSRTPData;
720
721 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100722 */
723
Ron Eldoref72faf2018-07-12 11:54:20 +0300724 /*
725 * Min length is 5: at least one protection profile(2 bytes)
726 * and length(2 bytes) + srtp_mki length(1 byte)
Johan Pascal042d4562020-08-25 12:14:02 +0200727 * Check here that we have at least 2 bytes of protection profiles length
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200728 * and one of srtp_mki length
Ron Eldoref72faf2018-07-12 11:54:20 +0300729 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200730 if( len < size_of_lengths )
Ron Eldor313d7b52018-12-10 14:56:21 +0200731 {
732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100733 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
734 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200735 }
Johan Pascalb62bb512015-12-03 21:56:45 +0100736
Johan Pascal43f94902020-09-22 12:25:52 +0200737 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +0200738
Ron Eldoref72faf2018-07-12 11:54:20 +0300739 /* first 2 bytes are protection profile length(in bytes) */
740 profile_length = ( buf[0] << 8 ) | buf[1];
Johan Pascal042d4562020-08-25 12:14:02 +0200741 buf += 2;
Ron Eldor591f1622018-01-22 12:30:04 +0200742
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200743 /* The profile length cannot be bigger than input buffer size - lengths fields */
744 if( profile_length > len - size_of_lengths ||
Johan Pascal042d4562020-08-25 12:14:02 +0200745 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
Ron Eldor313d7b52018-12-10 14:56:21 +0200746 {
747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100748 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
749 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor313d7b52018-12-10 14:56:21 +0200750 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300751 /*
752 * parse the extension list values are defined in
753 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
754 */
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200755 for( j = 0; j < profile_length; j += 2 )
Johan Pascalb62bb512015-12-03 21:56:45 +0100756 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200757 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
Johan Pascal43f94902020-09-22 12:25:52 +0200758 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
Johan Pascalb62bb512015-12-03 21:56:45 +0100759
Johan Pascal43f94902020-09-22 12:25:52 +0200760 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldorb4655392018-07-05 18:25:39 +0300761 {
Johan Pascal43f94902020-09-22 12:25:52 +0200762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
763 mbedtls_ssl_get_srtp_profile_as_string(
764 client_protection ) ) );
Ron Eldorb4655392018-07-05 18:25:39 +0300765 }
Johan Pascal85269572020-08-25 10:01:54 +0200766 else
767 {
768 continue;
769 }
Ron Eldor591f1622018-01-22 12:30:04 +0200770 /* check if suggested profile is in our list */
Ron Eldora9788042018-12-05 11:04:31 +0200771 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Ron Eldor591f1622018-01-22 12:30:04 +0200772 {
773 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
774 {
Ron Eldor3adb9922017-12-21 10:15:08 +0200775 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +0200776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
777 mbedtls_ssl_get_srtp_profile_as_string(
778 client_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +0200779 break;
Johan Pascalb62bb512015-12-03 21:56:45 +0100780 }
781 }
Johan Pascal43f94902020-09-22 12:25:52 +0200782 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +0200783 break;
784 }
Johan Pascal042d4562020-08-25 12:14:02 +0200785 buf += profile_length; /* buf points to the mki length */
786 mki_length = *buf;
787 buf++;
Ron Eldor591f1622018-01-22 12:30:04 +0200788
Johan Pascal042d4562020-08-25 12:14:02 +0200789 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
790 mki_length + profile_length + size_of_lengths != len )
791 {
792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +0100793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
794 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascal042d4562020-08-25 12:14:02 +0200795 }
796
797 /* Parse the mki only if present and mki is supported locally */
798 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
799 mki_length > 0 )
800 {
801 ssl->dtls_srtp_info.mki_len = mki_length;
802
Johan Pascal5ef72d22020-10-28 17:05:47 +0100803 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
Ron Eldorb4655392018-07-05 18:25:39 +0300804
Ron Eldoref72faf2018-07-12 11:54:20 +0300805 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
806 ssl->dtls_srtp_info.mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100807 }
808
Johan Pascal042d4562020-08-25 12:14:02 +0200809 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100810}
811#endif /* MBEDTLS_SSL_DTLS_SRTP */
812
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100813/*
814 * Auxiliary functions for ServerHello parsing and related actions
815 */
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100818/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100819 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#if defined(MBEDTLS_ECDSA_C)
822static int ssl_check_key_curve( mbedtls_pk_context *pk,
823 const mbedtls_ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100824{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 const mbedtls_ecp_curve_info **crv = curves;
826 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100827
828 while( *crv != NULL )
829 {
830 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100831 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100832 crv++;
833 }
834
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100835 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100838
839/*
840 * Try picking a certificate for this ciphersuite,
841 * return 0 on success and -1 on failure.
842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843static int ssl_pick_cert( mbedtls_ssl_context *ssl,
844 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100845{
Glenn Strauss041a3762022-03-15 06:08:29 -0400846 mbedtls_ssl_key_cert *cur, *list;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +0100847 mbedtls_pk_type_t pk_alg =
848 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200849 uint32_t flags;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100852 if( ssl->handshake->sni_key_cert != NULL )
853 list = ssl->handshake->sni_key_cert;
854 else
855#endif
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200856 list = ssl->conf->key_cert;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( pk_alg == MBEDTLS_PK_NONE )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100859 return( 0 );
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000862
Manuel Pégourié-Gonnarde540b492015-07-07 12:44:38 +0200863 if( list == NULL )
864 {
865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
866 return( -1 );
867 }
868
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100869 for( cur = list; cur != NULL; cur = cur->next )
870 {
Andrzej Kurek7ed01e82020-03-18 11:51:59 -0400871 flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000873 cur->cert );
874
Gilles Peskinee198df52018-01-05 21:17:45 +0100875 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100878 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000879 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100880
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200881 /*
882 * This avoids sending the client a cert it'll reject based on
883 * keyUsage or other extensions.
884 *
885 * It also allows the user to provision different certificates for
886 * different uses based on keyUsage, eg if they want to avoid signing
887 * and decrypting with the same RSA key.
888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +0100890 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000893 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200894 continue;
895 }
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_ECDSA_C)
898 if( pk_alg == MBEDTLS_PK_ECDSA &&
Gilles Peskine81d4e892017-10-27 10:18:44 +0200899 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100902 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000903 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100904#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100905
906 /* If we get there, we got a winner */
907 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100908 }
909
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +0200910 /* Do not update ssl->handshake->key_cert unless there is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100911 if( cur != NULL )
912 {
913 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000915 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100916 return( 0 );
917 }
918
919 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922
923/*
924 * Check if a given ciphersuite is suitable for use with our config/keys/etc
925 * Sets ciphersuite_info only if the suite matches.
926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
928 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 const mbedtls_ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100931
Jerry Yue7541932022-01-28 10:21:24 +0800932#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100933 mbedtls_pk_type_t sig_type;
934#endif
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100937 if( suite_info == NULL )
938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
940 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100941 }
942
Hanno Becker5c5efdf2019-01-28 14:59:35 +0000943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
Paul Elliott3891caf2020-12-17 18:42:40 +0000944 (unsigned int) suite_id, suite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000945
Glenn Strauss60bfe602022-03-14 19:04:24 -0400946 if( suite_info->min_tls_version > ssl->tls_version ||
947 suite_info->max_tls_version < ssl->tls_version )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100950 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000951 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100952
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +0200953#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
954 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200955 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +0200956 {
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +0200957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
958 "not configured or ext missing" ) );
Manuel Pégourié-Gonnarde511b4e2015-09-16 14:11:09 +0200959 return( 0 );
960 }
961#endif
962
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
965 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100966 ( ssl->handshake->curves == NULL ||
967 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000970 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100971 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000972 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100973#endif
974
Gilles Peskineeccd8882020-03-10 12:19:08 +0100975#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100976 /* If the ciphersuite requires a pre-shared key and we don't
977 * have one, skip it now rather than failing later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker845b9462018-10-26 12:07:29 +0100979 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100982 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000983 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100984#endif
985
Jerry Yue7541932022-01-28 10:21:24 +0800986#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100987 /* If the ciphersuite requires signing, check whether
988 * a suitable hash algorithm is present. */
Ronald Cron8457c122022-03-07 11:32:54 +0100989 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
990 if( sig_type != MBEDTLS_PK_NONE &&
Gabor Mezeia3d016c2022-05-10 12:44:09 +0200991 mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
992 ssl, mbedtls_ssl_sig_from_pk_alg( sig_type ) ) == MBEDTLS_SSL_HASH_NONE )
Hanno Becker7e5437a2017-04-28 17:15:26 +0100993 {
Ronald Cron8457c122022-03-07 11:32:54 +0100994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
995 "for signature algorithm %u", (unsigned) sig_type ) );
996 return( 0 );
Hanno Becker7e5437a2017-04-28 17:15:26 +0100997 }
998
Jerry Yue7541932022-01-28 10:21:24 +0800999#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001002 /*
1003 * Final check: if ciphersuite requires us to have a
1004 * certificate/key of a particular type:
1005 * - select the appropriate certificate if we have one, or
1006 * - try the next ciphersuite if we don't
1007 * This must be done last since we modify the key_cert list.
1008 */
1009 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001012 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001013 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001014 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001015#endif
1016
1017 *ciphersuite_info = suite_info;
1018 return( 0 );
1019}
1020
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001021/* This function doesn't alert on errors that happen early during
1022 ClientHello parsing because they might indicate that the client is
1023 not talking SSL/TLS at all and would not understand our alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001025{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001026 int ret, got_common_suite;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001027 size_t i, j;
1028 size_t ciph_offset, comp_offset, ext_offset;
1029 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001031 size_t cookie_offset, cookie_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001032#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001033 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001035 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001036#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001037 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001038 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker7e5437a2017-04-28 17:15:26 +01001041 /* If there is no signature-algorithm extension present,
1042 * we need to fall back to the default values for allowed
1043 * signature-hash pairs. */
Jerry Yue7541932022-01-28 10:21:24 +08001044#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001045 int sig_hash_alg_ext_present = 0;
Jerry Yue7541932022-01-28 10:21:24 +08001046#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001051read_record_header:
1052#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001053 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001055 * otherwise read it ourselves manually in order to support SSLv2
1056 * ClientHello, which doesn't use the same record layer format.
1057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_SSL_RENEGOTIATION)
1059 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001063 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001064 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001066 return( ret );
1067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 }
1069
1070 buf = ssl->in_hdr;
1071
Hanno Becker5903de42019-05-03 14:46:38 +01001072 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001073
Paul Bakkerec636f32012-09-09 19:17:02 +00001074 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001075 * TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001076 *
1077 * Record layer:
1078 * 0 . 0 message type
1079 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001080 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001081 * 3 . 4 message length
1082 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message type: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001084 buf[0] ) );
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001089 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001090 }
1091
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, message len.: %d",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001093 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1094
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, protocol version: [%d:%d]",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001096 buf[1], buf[2] ) );
1097
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001098 /* For DTLS if this is the initial handshake, remember the client sequence
1099 * number to use it in our next message (RFC 6347 4.2.1) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001101 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_SSL_RENEGOTIATION)
1103 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001104#endif
1105 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001106 {
1107 /* Epoch should be 0 for initial handshakes */
1108 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001111 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001112 }
1113
Jerry Yud9a94fe2021-09-28 18:58:59 +08001114 memcpy( &ssl->cur_out_ctr[2], ssl->in_ctr + 2,
Jerry Yud96a5c22021-09-29 17:46:51 +08001115 sizeof( ssl->cur_out_ctr ) - 2 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1118 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001121 ssl->next_record_offset = 0;
1122 ssl->in_left = 0;
1123 goto read_record_header;
1124 }
1125
1126 /* No MAC to check yet, so we can update right now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001128#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001131
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001132 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_SSL_RENEGOTIATION)
1135 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 /* Set by mbedtls_ssl_read_record() */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001138 msg_len = ssl->in_hslen;
1139 }
1140 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001141#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001142 {
Angus Grattond8213d02016-05-25 20:56:48 +10001143 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001146 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001147 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001148
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001149 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01001150 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001153 return( ret );
1154 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001155
1156 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker5903de42019-05-03 14:46:38 +01001159 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001160 else
1161#endif
1162 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001163 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001164
1165 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001168
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001169 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001170
1171 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001172 * Handshake layer:
1173 * 0 . 0 handshake type
1174 * 1 . 3 handshake length
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001175 * 4 . 5 DTLS only: message sequence number
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001176 * 6 . 8 DTLS only: fragment offset
1177 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001182 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001183 }
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001190 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001191 }
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001192 {
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04001193 size_t handshake_len = MBEDTLS_GET_UINT24_BE( buf, 1 );
Andrzej Kurek755ddff2022-06-15 07:31:40 -04001194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %u",
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04001195 ( unsigned ) handshake_len ) );
1196
1197 /* The record layer has a record size limit of 2^14 - 1 and
1198 * fragmentation is not supported, so buf[1] should be zero. */
1199 if( buf[1] != 0 )
1200 {
1201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message: %u != 0",
1202 (unsigned) buf[1] ) );
1203 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1204 }
1205
1206 /* We don't support fragmentation of ClientHello (yet?) */
1207 if( msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + handshake_len )
1208 {
1209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message: %u != %u + %u",
Gilles Peskine364fd8b2022-02-15 23:53:36 +01001210 (unsigned) msg_len,
1211 (unsigned) mbedtls_ssl_hs_hdr_len( ssl ),
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04001212 (unsigned) handshake_len ) );
1213 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1214 }
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001215 }
1216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001219 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001220 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001221 * Copy the client's handshake message_seq on initial handshakes,
1222 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_SSL_RENEGOTIATION)
1225 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001226 {
1227 /* This couldn't be done in ssl_prepare_handshake_record() */
1228 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1229 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001230
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001231 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Paul Elliott9f352112020-12-09 14:55:45 +00001234 "%u (expected %u)", cli_msg_seq,
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001235 ssl->handshake->in_msg_seq ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001236 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001237 }
1238
1239 ssl->handshake->in_msg_seq++;
1240 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001241 else
1242#endif
1243 {
1244 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1245 ssl->in_msg[5];
1246 ssl->handshake->out_msg_seq = cli_msg_seq;
1247 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1248 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001249 {
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04001250 /*
1251 * For now we don't support fragmentation, so make sure
1252 * fragment_offset == 0 and fragment_length == length
1253 */
1254 size_t fragment_offset, fragment_length, length;
1255 fragment_offset = MBEDTLS_GET_UINT24_BE( ssl->in_msg, 6 );
1256 fragment_length = MBEDTLS_GET_UINT24_BE( ssl->in_msg, 9 );
1257 length = MBEDTLS_GET_UINT24_BE( ssl->in_msg, 1 );
1258 MBEDTLS_SSL_DEBUG_MSG(
1259 4, ( "fragment_offset=%u fragment_length=%u length=%u",
1260 (unsigned) fragment_offset, (unsigned) fragment_length,
1261 (unsigned) length ) );
1262 if( fragment_offset != 0 || length != fragment_length )
1263 {
1264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1265 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1266 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001267 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 buf += mbedtls_ssl_hs_hdr_len( ssl );
1272 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001273
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001274 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001275 * ClientHello layer:
1276 * 0 . 1 protocol version
1277 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1278 * 34 . 35 session id length (1 byte)
1279 * 35 . 34+x session id
1280 * 35+x . 35+x DTLS only: cookie length (1 byte)
1281 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001282 * .. . .. ciphersuite list length (2 bytes)
1283 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001284 * .. . .. compression alg. list length (1 byte)
1285 * .. . .. compression alg. list
1286 * .. . .. extensions length (2 bytes, optional)
1287 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001288 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001289
1290 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001291 * Minimal length (with everything empty and extensions omitted) is
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001292 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1293 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001294 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001295 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001298 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001299 }
1300
1301 /*
1302 * Check and save the protocol version
1303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001305
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001306 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001307 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakkerec636f32012-09-09 19:17:02 +00001308
Glenn Strauss60bfe602022-03-14 19:04:24 -04001309 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001310 {
Ronald Cron90f01202022-03-15 15:37:13 +01001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server only supports TLS 1.2" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1313 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Hanno Beckerbc000442021-06-24 09:18:19 +01001314 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001315 }
1316
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001317 /*
1318 * Save client random (inc. Unix time)
1319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001321
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001322 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001323
1324 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001325 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001326 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001327 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001328
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001329 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001330 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001333 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1334 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001335 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001336 }
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001339
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001340 ssl->session_negotiate->id_len = sess_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001341 memset( ssl->session_negotiate->id, 0,
1342 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001343 memcpy( ssl->session_negotiate->id, buf + 35,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001344 ssl->session_negotiate->id_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001345
1346 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001347 * Check the cookie length and content
1348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001351 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001352 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001353 cookie_len = buf[cookie_offset];
1354
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001355 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001358 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker90d59dd2021-06-24 11:17:13 +01001359 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1360 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001361 }
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001364 buf + cookie_offset + 1, cookie_len );
1365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001367 if( ssl->conf->f_cookie_check != NULL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_SSL_RENEGOTIATION)
1369 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001370#endif
1371 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001372 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001373 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001374 buf + cookie_offset + 1, cookie_len,
1375 ssl->cli_id, ssl->cli_id_len ) != 0 )
1376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001378 ssl->handshake->verify_cookie_len = 1;
1379 }
1380 else
1381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001383 ssl->handshake->verify_cookie_len = 0;
1384 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001385 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001388 {
1389 /* We know we didn't send a cookie, so it should be empty */
1390 if( cookie_len != 0 )
1391 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001392 /* This may be an attacker's probe, so don't send an alert */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001394 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001395 }
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001398 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001399
1400 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001401 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001402 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001403 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001404 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001405 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001407 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001408
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001409 ciph_len = ( buf[ciph_offset + 0] << 8 )
1410 | ( buf[ciph_offset + 1] );
1411
1412 if( ciph_len < 2 ||
1413 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1414 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001417 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1418 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001419 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001420 }
1421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001423 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001424
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001425 /*
1426 * Check the compression algorithms length and pick one
1427 */
1428 comp_offset = ciph_offset + 2 + ciph_len;
1429
1430 comp_len = buf[comp_offset];
1431
1432 if( comp_len < 1 ||
1433 comp_len > 16 ||
1434 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001437 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1438 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001439 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerec636f32012-09-09 19:17:02 +00001440 }
1441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001443 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001446 /* See comments in ssl_write_client_hello() */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001448 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001450#endif
Simon Butcher584a5472016-05-23 16:24:52 +01001451 /*
1452 * Check the extension length
1453 */
1454 ext_offset = comp_offset + 1 + comp_len;
1455 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001456 {
Simon Butcher584a5472016-05-23 16:24:52 +01001457 if( msg_len < ext_offset + 2 )
1458 {
1459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001460 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1461 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001462 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001463 }
1464
1465 ext_len = ( buf[ext_offset + 0] << 8 )
1466 | ( buf[ext_offset + 1] );
1467
Glenn Strauss8a8a83b2021-02-02 02:21:23 -05001468 if( msg_len != ext_offset + 2 + ext_len )
Simon Butcher584a5472016-05-23 16:24:52 +01001469 {
1470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001471 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1472 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001473 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001474 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001475 }
Simon Butcher584a5472016-05-23 16:24:52 +01001476 else
1477 ext_len = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001478
Simon Butcher584a5472016-05-23 16:24:52 +01001479 ext = buf + ext_offset + 2;
1480 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001481
Simon Butcher584a5472016-05-23 16:24:52 +01001482 while( ext_len != 0 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001483 {
Philippe Antoine747fd532018-05-30 09:13:21 +02001484 unsigned int ext_id;
1485 unsigned int ext_size;
1486 if ( ext_len < 4 ) {
1487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1488 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1489 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001490 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02001491 }
1492 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1493 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001494
Simon Butcher584a5472016-05-23 16:24:52 +01001495 if( ext_size + 4 > ext_len )
1496 {
1497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001498 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1499 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001500 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Simon Butcher584a5472016-05-23 16:24:52 +01001501 }
1502 switch( ext_id )
1503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001505 case MBEDTLS_TLS_EXT_SERVERNAME:
1506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
Simon Butcher584a5472016-05-23 16:24:52 +01001507 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1508 if( ret != 0 )
1509 return( ret );
1510 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001512
Simon Butcher584a5472016-05-23 16:24:52 +01001513 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_SSL_RENEGOTIATION)
Simon Butcher584a5472016-05-23 16:24:52 +01001516 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001517#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001518
Simon Butcher584a5472016-05-23 16:24:52 +01001519 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1520 if( ret != 0 )
1521 return( ret );
1522 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001523
Jerry Yue7541932022-01-28 10:21:24 +08001524#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001525 case MBEDTLS_TLS_EXT_SIG_ALG:
Ron Eldor73a38172017-10-03 15:58:26 +03001526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1527
Gabor Mezei078e8032022-04-27 21:17:56 +02001528 ret = mbedtls_ssl_parse_sig_alg_ext( ssl, ext + 4, ext + 4 + ext_size );
Simon Butcher584a5472016-05-23 16:24:52 +01001529 if( ret != 0 )
1530 return( ret );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001531
1532 sig_hash_alg_ext_present = 1;
Simon Butcher584a5472016-05-23 16:24:52 +01001533 break;
Jerry Yue7541932022-01-28 10:21:24 +08001534#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001535
Robert Cragie136884c2015-10-02 13:34:31 +01001536#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001537 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yub47d0f82021-12-20 17:34:40 +08001538 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Simon Butcher584a5472016-05-23 16:24:52 +01001539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001540
Jerry Yub47d0f82021-12-20 17:34:40 +08001541 ret = ssl_parse_supported_groups_ext( ssl, ext + 4, ext_size );
Simon Butcher584a5472016-05-23 16:24:52 +01001542 if( ret != 0 )
1543 return( ret );
1544 break;
Paul Bakker41c83d32013-03-20 14:39:14 +01001545
Simon Butcher584a5472016-05-23 16:24:52 +01001546 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1548 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001549
Simon Butcher584a5472016-05-23 16:24:52 +01001550 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1551 if( ret != 0 )
1552 return( ret );
1553 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001554#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1555 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001556
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001557#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Simon Butcher584a5472016-05-23 16:24:52 +01001558 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001560
Simon Butcher584a5472016-05-23 16:24:52 +01001561 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1562 if( ret != 0 )
1563 return( ret );
1564 break;
Manuel Pégourié-Gonnardbf57be62015-09-16 15:04:01 +02001565#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Simon Butcher584a5472016-05-23 16:24:52 +01001568 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001570
Simon Butcher584a5472016-05-23 16:24:52 +01001571 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1572 if( ret != 0 )
1573 return( ret );
1574 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001576
Hanno Beckera0e20d02019-05-15 14:03:01 +01001577#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89dcc882019-04-26 13:56:39 +01001578 case MBEDTLS_TLS_EXT_CID:
1579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1580
1581 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1582 if( ret != 0 )
1583 return( ret );
1584 break;
Thomas Daubneye1c9a402021-06-15 11:26:43 +01001585#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker89dcc882019-04-26 13:56:39 +01001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Simon Butcher584a5472016-05-23 16:24:52 +01001588 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001590
Simon Butcher584a5472016-05-23 16:24:52 +01001591 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1592 if( ret != 0 )
1593 return( ret );
1594 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Simon Butcher584a5472016-05-23 16:24:52 +01001598 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001600
Simon Butcher584a5472016-05-23 16:24:52 +01001601 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1602 if( ret != 0 )
1603 return( ret );
1604 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Simon Butcher584a5472016-05-23 16:24:52 +01001608 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001610
Simon Butcher584a5472016-05-23 16:24:52 +01001611 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1612 if( ret != 0 )
1613 return( ret );
1614 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#if defined(MBEDTLS_SSL_ALPN)
Simon Butcher584a5472016-05-23 16:24:52 +01001618 case MBEDTLS_TLS_EXT_ALPN:
1619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001620
Simon Butcher584a5472016-05-23 16:24:52 +01001621 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1622 if( ret != 0 )
1623 return( ret );
1624 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001626
Johan Pascalb62bb512015-12-03 21:56:45 +01001627#if defined(MBEDTLS_SSL_DTLS_SRTP)
1628 case MBEDTLS_TLS_EXT_USE_SRTP:
1629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
Johan Pascald576fdb2020-09-22 10:39:53 +02001630
Johan Pascalc3ccd982020-10-28 17:18:18 +01001631 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
1632 if( ret != 0 )
1633 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001634 break;
1635#endif /* MBEDTLS_SSL_DTLS_SRTP */
1636
Simon Butcher584a5472016-05-23 16:24:52 +01001637 default:
Paul Elliott9f352112020-12-09 14:55:45 +00001638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Simon Butcher584a5472016-05-23 16:24:52 +01001639 ext_id ) );
1640 }
1641
1642 ext_len -= 4 + ext_size;
1643 ext += 4 + ext_size;
Paul Bakker48916f92012-09-16 19:57:18 +00001644 }
Janos Follathc6dab2b2016-05-23 14:27:02 +01001645
Jerry Yue7541932022-01-28 10:21:24 +08001646#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01001647
1648 /*
1649 * Try to fall back to default hash SHA1 if the client
1650 * hasn't provided any preferred signature-hash combinations.
1651 */
Gabor Mezei86acf052022-05-10 13:29:02 +02001652 if( ! sig_hash_alg_ext_present )
Hanno Becker7e5437a2017-04-28 17:15:26 +01001653 {
Gabor Mezei86acf052022-05-10 13:29:02 +02001654 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
1655 const uint16_t default_sig_algs[] = {
Gabor Mezeic1051b62022-05-10 13:13:58 +02001656#if defined(MBEDTLS_ECDSA_C)
1657 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA,
1658 MBEDTLS_SSL_HASH_SHA1 ),
1659#endif
1660#if defined(MBEDTLS_RSA_C)
1661 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA,
1662 MBEDTLS_SSL_HASH_SHA1 ),
1663#endif
Gabor Mezei86acf052022-05-10 13:29:02 +02001664 MBEDTLS_TLS_SIG_NONE
Gabor Mezei078e8032022-04-27 21:17:56 +02001665 };
Hanno Becker7e5437a2017-04-28 17:15:26 +01001666
Gabor Mezei86acf052022-05-10 13:29:02 +02001667#if defined(static_assert)
1668 static_assert( sizeof( default_sig_algs ) / sizeof( default_sig_algs[0] ) <=
1669 MBEDTLS_RECEIVED_SIG_ALGS_SIZE, "default_sig_algs is too big" );
1670#endif
Gabor Mezei078e8032022-04-27 21:17:56 +02001671
Gabor Mezei86acf052022-05-10 13:29:02 +02001672 memcpy( received_sig_algs, default_sig_algs, sizeof( default_sig_algs ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001673 }
1674
Jerry Yue7541932022-01-28 10:21:24 +08001675#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01001676
Paul Bakker48916f92012-09-16 19:57:18 +00001677 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001678 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1679 */
1680 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1685#if defined(MBEDTLS_SSL_RENEGOTIATION)
1686 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001687 {
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01001688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1689 "during renegotiation" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001690 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1691 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01001692 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001693 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001696 break;
1697 }
1698 }
1699
1700 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001701 * Renegotiation security checks
1702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001704 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001707 handshake_failure = 1;
1708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#if defined(MBEDTLS_SSL_RENEGOTIATION)
1710 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1711 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001712 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001715 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1718 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001719 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001722 handshake_failure = 1;
1723 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1725 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001726 renegotiation_info_seen == 1 )
1727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001729 handshake_failure = 1;
1730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001732
1733 if( handshake_failure == 1 )
1734 {
Gilles Peskinec94f7352017-05-10 16:37:56 +02001735 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1736 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Becker90d59dd2021-06-24 11:17:13 +01001737 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001738 }
Paul Bakker380da532012-04-18 16:10:25 +00001739
Paul Bakker41c83d32013-03-20 14:39:14 +01001740 /*
Glenn Strauss2ed95272022-01-21 18:02:17 -05001741 * Server certification selection (after processing TLS extensions)
1742 */
1743 if( ssl->conf->f_cert_cb && ( ret = ssl->conf->f_cert_cb( ssl ) ) != 0 )
1744 {
1745 MBEDTLS_SSL_DEBUG_RET( 1, "f_cert_cb", ret );
1746 return( ret );
1747 }
Glenn Strauss69894072022-01-24 12:58:00 -05001748#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1749 ssl->handshake->sni_name = NULL;
1750 ssl->handshake->sni_name_len = 0;
1751#endif
Glenn Strauss2ed95272022-01-21 18:02:17 -05001752
1753 /*
Paul Bakker41c83d32013-03-20 14:39:14 +01001754 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001755 * (At the end because we need information from the EC-based extensions
Glenn Strauss2ed95272022-01-21 18:02:17 -05001756 * and certificate from the SNI callback triggered by the SNI extension
1757 * or certificate from server certificate selection callback.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001758 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001759 got_common_suite = 0;
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001760 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001761 ciphersuite_info = NULL;
TRodziewicz8476f2f2021-06-02 14:34:47 +02001762
TRodziewicz3946f792021-06-14 12:11:18 +02001763 if (ssl->conf->respect_cli_pref == MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT)
TRodziewicz8476f2f2021-06-02 14:34:47 +02001764 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001765 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001766 for( i = 0; ciphersuites[i] != 0; i++ )
1767 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001768 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001769 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001770
TRodziewicz8476f2f2021-06-02 14:34:47 +02001771 got_common_suite = 1;
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001772
TRodziewicz8476f2f2021-06-02 14:34:47 +02001773 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1774 &ciphersuite_info ) ) != 0 )
1775 return( ret );
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001776
TRodziewicz8476f2f2021-06-02 14:34:47 +02001777 if( ciphersuite_info != NULL )
1778 goto have_ciphersuite;
1779 }
1780 } else {
1781 for( i = 0; ciphersuites[i] != 0; i++ )
1782 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
1783 {
Joe Subbianie4603ee2021-08-20 13:05:30 +01001784 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
TRodziewicz8476f2f2021-06-02 14:34:47 +02001785 continue;
1786
1787 got_common_suite = 1;
1788
1789 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1790 &ciphersuite_info ) ) != 0 )
1791 return( ret );
1792
1793 if( ciphersuite_info != NULL )
1794 goto have_ciphersuite;
1795 }
1796 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001797
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001798 if( got_common_suite )
1799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001801 "but none of them usable" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001802 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1803 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman096c4112021-06-29 09:52:06 +01001804 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001805 }
1806 else
1807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001809 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1810 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01001811 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001812 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001813
1814have_ciphersuite:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001816
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001817 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Hanno Beckere694c3e2017-12-27 21:34:08 +00001818 ssl->handshake->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001819
Paul Bakker5121ce52009-01-03 21:22:43 +00001820 ssl->state++;
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001823 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001825#endif
1826
Hanno Becker7e5437a2017-04-28 17:15:26 +01001827 /* Debugging-only output for testsuite */
1828#if defined(MBEDTLS_DEBUG_C) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01001829 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Ronald Cron8457c122022-03-07 11:32:54 +01001830 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
1831 if( sig_alg != MBEDTLS_PK_NONE )
Hanno Becker7e5437a2017-04-28 17:15:26 +01001832 {
Gabor Mezeia3d016c2022-05-10 12:44:09 +02001833 unsigned int sig_hash = mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
1834 ssl, mbedtls_ssl_sig_from_pk_alg( sig_alg ) );
Gabor Mezei0a4298b2022-05-13 16:25:35 +02001835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %u",
1836 sig_hash ) );
Ronald Cron8457c122022-03-07 11:32:54 +01001837 }
1838 else
1839 {
1840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
1841 "%u - should not happen", (unsigned) sig_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01001842 }
1843#endif
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
1847 return( 0 );
1848}
1849
Hanno Beckera0e20d02019-05-15 14:03:01 +01001850#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01001851static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
1852 unsigned char *buf,
1853 size_t *olen )
1854{
1855 unsigned char *p = buf;
1856 size_t ext_len;
1857 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1858
1859 *olen = 0;
1860
1861 /* Skip writing the extension if we don't want to use it or if
1862 * the client hasn't offered it. */
1863 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
1864 return;
1865
1866 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
1867 * which is at most 255, so the increment cannot overflow. */
1868 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
1869 {
1870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
1871 return;
1872 }
1873
1874 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
1875
1876 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +01001877 * Quoting draft-ietf-tls-dtls-connection-id-05
1878 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker51de2d32019-04-26 15:46:55 +01001879 *
1880 * struct {
1881 * opaque cid<0..2^8-1>;
1882 * } ConnectionId;
1883 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001884 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
1885 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01001886 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001887 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
1888 p += 2;
Hanno Becker51de2d32019-04-26 15:46:55 +01001889
1890 *p++ = (uint8_t) ssl->own_cid_len;
1891 memcpy( p, ssl->own_cid, ssl->own_cid_len );
1892
1893 *olen = ssl->own_cid_len + 5;
1894}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001895#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker51de2d32019-04-26 15:46:55 +01001896
Neil Armstrong76b74072022-04-06 13:43:54 +02001897#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001899 unsigned char *buf,
1900 size_t *olen )
1901{
1902 unsigned char *p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 const mbedtls_ssl_ciphersuite_t *suite = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001904
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001905 /*
1906 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1907 * from a client and then selects a stream or Authenticated Encryption
1908 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1909 * encrypt-then-MAC response extension back to the client."
1910 */
Neil Armstrongfe635e42022-04-01 10:36:09 +02001911 suite = mbedtls_ssl_ciphersuite_from_id(
1912 ssl->session_negotiate->ciphersuite );
1913 if( suite == NULL )
Ronald Cron862902d2022-03-24 14:15:28 +01001914 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
Neil Armstrongfe635e42022-04-01 10:36:09 +02001915 else
1916 {
1917 mbedtls_ssl_mode_t ssl_mode =
Neil Armstrongab555e02022-04-04 11:07:59 +02001918 mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongfe635e42022-04-01 10:36:09 +02001919 ssl->session_negotiate->encrypt_then_mac,
1920 suite );
1921
1922 if( ssl_mode != MBEDTLS_SSL_MODE_CBC_ETM )
1923 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
Ronald Cron862902d2022-03-24 14:15:28 +01001924 }
1925
1926 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
1927 {
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001928 *olen = 0;
1929 return;
1930 }
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001933
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001934 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
1935 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001936
1937 *p++ = 0x00;
1938 *p++ = 0x00;
1939
1940 *olen = 4;
1941}
Neil Armstrong76b74072022-04-06 13:43:54 +02001942#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1945static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001946 unsigned char *buf,
1947 size_t *olen )
1948{
1949 unsigned char *p = buf;
1950
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001951 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001952 {
1953 *olen = 0;
1954 return;
1955 }
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001958 "extension" ) );
1959
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001960 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
1961 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001962
1963 *p++ = 0x00;
1964 *p++ = 0x00;
1965
1966 *olen = 4;
1967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1971static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001972 unsigned char *buf,
1973 size_t *olen )
1974{
1975 unsigned char *p = buf;
1976
1977 if( ssl->handshake->new_session_ticket == 0 )
1978 {
1979 *olen = 0;
1980 return;
1981 }
1982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001984
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001985 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
1986 p += 2;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001987
1988 *p++ = 0x00;
1989 *p++ = 0x00;
1990
1991 *olen = 4;
1992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001996 unsigned char *buf,
1997 size_t *olen )
1998{
1999 unsigned char *p = buf;
2000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002002 {
2003 *olen = 0;
2004 return;
2005 }
2006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002008
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002009 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
2010 p += 2;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#if defined(MBEDTLS_SSL_RENEGOTIATION)
2013 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002014 {
2015 *p++ = 0x00;
2016 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2017 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002018
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002019 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2020 p += ssl->verify_data_len;
2021 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2022 p += ssl->verify_data_len;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002023 }
2024 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002026 {
2027 *p++ = 0x00;
2028 *p++ = 0x01;
2029 *p++ = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002030 }
Manuel Pégourié-Gonnard19389752015-06-23 13:46:44 +02002031
2032 *olen = p - buf;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002033}
2034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2036static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002037 unsigned char *buf,
2038 size_t *olen )
2039{
2040 unsigned char *p = buf;
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002043 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002044 *olen = 0;
2045 return;
2046 }
2047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002049
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002050 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
2051 p += 2;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002052
2053 *p++ = 0x00;
2054 *p++ = 1;
2055
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002056 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002057
2058 *olen = 5;
2059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002061
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002062#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002063 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002065 unsigned char *buf,
2066 size_t *olen )
2067{
2068 unsigned char *p = buf;
2069 ((void) ssl);
2070
Paul Bakker677377f2013-10-28 12:54:26 +01002071 if( ( ssl->handshake->cli_exts &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
Paul Bakker677377f2013-10-28 12:54:26 +01002073 {
2074 *olen = 0;
2075 return;
2076 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002079
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002080 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
2081 p += 2;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002082
2083 *p++ = 0x00;
2084 *p++ = 2;
2085
2086 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002088
2089 *olen = 6;
2090}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002091#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002092
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002093#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2094static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2095 unsigned char *buf,
2096 size_t *olen )
2097{
Janos Follath865b3eb2019-12-16 11:46:15 +00002098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002099 unsigned char *p = buf;
Angus Grattond8213d02016-05-25 20:56:48 +10002100 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002101 size_t kkpp_len;
2102
2103 *olen = 0;
2104
2105 /* Skip costly computation if not needed */
Hanno Beckere694c3e2017-12-27 21:34:08 +00002106 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002107 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2108 return;
2109
2110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2111
2112 if( end - p < 4 )
2113 {
2114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2115 return;
2116 }
2117
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002118 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
2119 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002120
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02002121 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2122 p + 2, end - p - 2, &kkpp_len,
2123 ssl->conf->f_rng, ssl->conf->p_rng );
2124 if( ret != 0 )
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002125 {
2126 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2127 return;
2128 }
2129
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002130 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
2131 p += 2;
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002132
2133 *olen = kkpp_len + 4;
2134}
2135#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if defined(MBEDTLS_SSL_ALPN )
2138static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002139 unsigned char *buf, size_t *olen )
2140{
2141 if( ssl->alpn_chosen == NULL )
2142 {
2143 *olen = 0;
2144 return;
2145 }
2146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002148
2149 /*
2150 * 0 . 1 ext identifier
2151 * 2 . 3 ext length
2152 * 4 . 5 protocol list length
2153 * 6 . 6 protocol name length
2154 * 7 . 7+n protocol name
2155 */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002156 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0);
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002157
2158 *olen = 7 + strlen( ssl->alpn_chosen );
2159
Joe Subbiani6dd73642021-07-19 11:56:54 +01002160 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002161
Joe Subbiani6dd73642021-07-19 11:56:54 +01002162 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002163
Joe Subbiani2194dc42021-07-14 12:31:31 +01002164 buf[6] = MBEDTLS_BYTE_0( *olen - 7 );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002165
2166 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002169
Ron Eldor3adb9922017-12-21 10:15:08 +02002170#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
Johan Pascalb62bb512015-12-03 21:56:45 +01002171static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03002172 unsigned char *buf,
2173 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +01002174{
Ron Eldor75870ec2018-12-06 17:31:55 +02002175 size_t mki_len = 0, ext_len = 0;
Ron Eldor089c9fe2018-12-06 17:12:49 +02002176 uint16_t profile_value = 0;
Johan Pascal8f70fba2020-09-02 10:32:06 +02002177 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2178
2179 *olen = 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002180
Johan Pascalc3ccd982020-10-28 17:18:18 +01002181 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2182 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01002183 {
Johan Pascalb62bb512015-12-03 21:56:45 +01002184 return;
2185 }
2186
2187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2188
Johan Pascald576fdb2020-09-22 10:39:53 +02002189 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02002190 {
2191 mki_len = ssl->dtls_srtp_info.mki_len;
2192 }
2193
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002194 /* The extension total size is 9 bytes :
2195 * - 2 bytes for the extension tag
2196 * - 2 bytes for the total size
2197 * - 2 bytes for the protection profile length
2198 * - 2 bytes for the protection profile
2199 * - 1 byte for the mki length
2200 * + the actual mki length
2201 * Check we have enough room in the output buffer */
Johan Pascald576fdb2020-09-22 10:39:53 +02002202 if( (size_t)( end - buf ) < mki_len + 9 )
Johan Pascal8f70fba2020-09-02 10:32:06 +02002203 {
2204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2205 return;
2206 }
2207
Johan Pascalb62bb512015-12-03 21:56:45 +01002208 /* extension */
Joe Subbiani6dd73642021-07-19 11:56:54 +01002209 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 );
Ron Eldoref72faf2018-07-12 11:54:20 +03002210 /*
2211 * total length 5 and mki value: only one profile(2 bytes)
2212 * and length(2 bytes) and srtp_mki )
2213 */
Ron Eldor591f1622018-01-22 12:30:04 +02002214 ext_len = 5 + mki_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002215 MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 );
Johan Pascalb62bb512015-12-03 21:56:45 +01002216
2217 /* protection profile length: 2 */
2218 buf[4] = 0x00;
2219 buf[5] = 0x02;
Johan Pascal43f94902020-09-22 12:25:52 +02002220 profile_value = mbedtls_ssl_check_srtp_profile_value(
Johan Pascald576fdb2020-09-22 10:39:53 +02002221 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
Johan Pascal43f94902020-09-22 12:25:52 +02002222 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +02002223 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002224 MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002225 }
2226 else
2227 {
Johan Pascal8f70fba2020-09-02 10:32:06 +02002228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
Ron Eldor089c9fe2018-12-06 17:12:49 +02002229 return;
Johan Pascalb62bb512015-12-03 21:56:45 +01002230 }
2231
Ron Eldor591f1622018-01-22 12:30:04 +02002232 buf[8] = mki_len & 0xFF;
Ron Eldor75870ec2018-12-06 17:31:55 +02002233 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
Johan Pascalb62bb512015-12-03 21:56:45 +01002234
Ron Eldor591f1622018-01-22 12:30:04 +02002235 *olen = 9 + mki_len;
Johan Pascalb62bb512015-12-03 21:56:45 +01002236}
2237#endif /* MBEDTLS_SSL_DTLS_SRTP */
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2240static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002241{
Janos Follath865b3eb2019-12-16 11:46:15 +00002242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002243 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002244 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002247
2248 /*
2249 * struct {
2250 * ProtocolVersion server_version;
2251 * opaque cookie<0..2^8-1>;
2252 * } HelloVerifyRequest;
2253 */
2254
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002255 /* The RFC is not clear on this point, but sending the actual negotiated
2256 * version looks like the most interoperable thing to do. */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04002257 mbedtls_ssl_write_version( p, ssl->conf->transport, ssl->tls_version );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002259 p += 2;
2260
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002261 /* If we get here, f_cookie_check is not null */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002262 if( ssl->conf->f_cookie_write == NULL )
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002266 }
2267
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002268 /* Skip length byte until we know the length */
2269 cookie_len_byte = p++;
2270
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002271 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Angus Grattond8213d02016-05-25 20:56:48 +10002272 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002273 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002276 return( ret );
2277 }
2278
2279 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002282
2283 ssl->out_msglen = p - ssl->out_msg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2285 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002288
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002289 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002290 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002292 return( ret );
2293 }
2294
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002295#if defined(MBEDTLS_SSL_PROTO_DTLS)
2296 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2297 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2298 {
2299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2300 return( ret );
2301 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01002302#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002305
2306 return( 0 );
2307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002309
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002310static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
Hanno Becker64ce9742021-04-15 08:19:40 +01002311{
2312 int ret;
Hanno Beckera5b1a392021-04-15 16:48:01 +01002313 mbedtls_ssl_session session_tmp;
Hanno Becker64ce9742021-04-15 08:19:40 +01002314 mbedtls_ssl_session * const session = ssl->session_negotiate;
2315
2316 /* Resume is 0 by default, see ssl_handshake_init().
2317 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2318 if( ssl->handshake->resume == 1 )
2319 return;
Hanno Becker7ad77962021-05-13 06:13:34 +01002320 if( session->id_len == 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +01002321 return;
2322 if( ssl->conf->f_get_cache == NULL )
2323 return;
2324#if defined(MBEDTLS_SSL_RENEGOTIATION)
2325 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2326 return;
2327#endif
2328
Hanno Beckera5b1a392021-04-15 16:48:01 +01002329 mbedtls_ssl_session_init( &session_tmp );
2330
Hanno Becker64ce9742021-04-15 08:19:40 +01002331 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
Hanno Beckerccdaf6e2021-04-15 09:26:17 +01002332 session->id,
2333 session->id_len,
Hanno Becker64ce9742021-04-15 08:19:40 +01002334 &session_tmp );
2335 if( ret != 0 )
2336 goto exit;
2337
2338 if( session->ciphersuite != session_tmp.ciphersuite ||
2339 session->compression != session_tmp.compression )
2340 {
2341 /* Mismatch between cached and negotiated session */
2342 goto exit;
2343 }
2344
2345 /* Move semantics */
Hanno Becker64ce9742021-04-15 08:19:40 +01002346 mbedtls_ssl_session_free( session );
2347 *session = session_tmp;
Hanno Beckerfc1f4132021-05-14 14:57:54 +01002348 memset( &session_tmp, 0, sizeof( session_tmp ) );
Hanno Becker64ce9742021-04-15 08:19:40 +01002349
2350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2351 ssl->handshake->resume = 1;
2352
2353exit:
2354
Hanno Beckerdf564022021-05-13 06:26:37 +01002355 mbedtls_ssl_session_free( &session_tmp );
Hanno Becker64ce9742021-04-15 08:19:40 +01002356}
2357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002361 mbedtls_time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002362#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00002363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002364 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 unsigned char *buf, *p;
2366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002370 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002371 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002375
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002376 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002379
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002380 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +01002381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2383 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +01002384 }
2385
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 /*
2387 * 0 . 0 handshake type
2388 * 1 . 3 handshake length
2389 * 4 . 5 protocol version
2390 * 6 . 9 UNIX time()
2391 * 10 . 37 random bytes
2392 */
2393 buf = ssl->out_msg;
2394 p = buf + 4;
2395
Glenn Strausse3af4cb2022-03-15 03:23:42 -04002396 mbedtls_ssl_write_version( p, ssl->conf->transport, ssl->tls_version );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002397 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002400 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002403 t = mbedtls_time( NULL );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002404 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
2405 p += 4;
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Paul Elliottd48d5c62021-01-07 14:47:05 +00002407 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2408 (long long) t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002409#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002410 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002411 return( ret );
2412
2413 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002416 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002417 return( ret );
2418
2419 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Paul Bakker48916f92012-09-16 19:57:18 +00002421 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
Hanno Beckerf6e09c62021-05-13 06:12:38 +01002425 ssl_handle_id_based_session_resumption( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002427 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 {
2429 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002430 * New session, create a new session id,
2431 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 ssl->state++;
2434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01002436 ssl->session_negotiate->start = mbedtls_time( NULL );
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002437#endif
2438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002440 if( ssl->handshake->new_session_ticket != 0 )
2441 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002442 ssl->session_negotiate->id_len = n = 0;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002443 memset( ssl->session_negotiate->id, 0, 32 );
2444 }
2445 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002447 {
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002448 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002449 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002450 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002451 return( ret );
2452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454 else
2455 {
2456 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002457 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002459 n = ssl->session_negotiate->id_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002465 return( ret );
2466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 }
2468
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002469 /*
2470 * 38 . 38 session id length
2471 * 39 . 38+n session id
2472 * 39+n . 40+n chosen ciphersuite
2473 * 41+n . 41+n chosen compression alg.
2474 * 42+n . 43+n extensions length
2475 * 44+n . 43+n+m extensions
2476 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02002477 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2478 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2479 p += ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Paul Elliottd48d5c62021-01-07 14:47:05 +00002481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002484 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002486 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
2487 p += 2;
Joe Subbianifbeb6922021-07-16 14:27:50 +01002488 *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2491 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Elliott3891caf2020-12-17 18:42:40 +00002493 (unsigned int) ssl->session_negotiate->compression ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002494
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002495 /*
2496 * First write extensions, then the total length
2497 */
2498 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2499 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002502 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2503 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002504#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002505
Hanno Beckera0e20d02019-05-15 14:03:01 +01002506#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker51de2d32019-04-26 15:46:55 +01002507 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2508 ext_len += olen;
2509#endif
2510
Neil Armstrong76b74072022-04-06 13:43:54 +02002511#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002512 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2513 ext_len += olen;
2514#endif
2515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002517 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2518 ext_len += olen;
2519#endif
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002522 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2523 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002524#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002525
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02002526#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002527 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Leonid Rozenboim28752702022-04-21 18:00:52 -07002528 const mbedtls_ssl_ciphersuite_t *suite =
2529 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
2530 if ( suite != NULL && mbedtls_ssl_ciphersuite_uses_ec( suite) )
Ron Eldor755bb6a2018-02-14 19:30:48 +02002531 {
2532 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2533 ext_len += olen;
2534 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002535#endif
2536
Manuel Pégourié-Gonnard55c7f992015-09-16 15:35:27 +02002537#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2538 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2539 ext_len += olen;
2540#endif
2541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002543 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2544 ext_len += olen;
2545#endif
2546
Johan Pascalb62bb512015-12-03 21:56:45 +01002547#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascalc3ccd982020-10-28 17:18:18 +01002548 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
2549 ext_len += olen;
Johan Pascalb62bb512015-12-03 21:56:45 +01002550#endif
2551
Paul Elliottd48d5c62021-01-07 14:47:05 +00002552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2553 ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002554
Paul Bakkera7036632014-04-30 10:15:38 +02002555 if( ext_len > 0 )
2556 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002557 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani94180e72021-08-20 16:20:44 +01002558 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02002559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
2561 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2563 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002565 ret = mbedtls_ssl_write_handshake_msg( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 return( ret );
2570}
2571
Gilles Peskineeccd8882020-03-10 12:19:08 +01002572#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002575 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002576 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002579
Hanno Becker77adddc2019-02-07 12:32:43 +00002580 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002583 ssl->state++;
2584 return( 0 );
2585 }
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002590#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002592{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002594 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002595 ssl->handshake->ciphersuite_info;
irwirc9bc3002020-04-01 13:46:36 +03002596 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002597 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 unsigned char *buf, *p;
Angus Grattond8213d02016-05-25 20:56:48 +10002599 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 const mbedtls_x509_crt *crt;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002601 int authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 ssl->state++;
2606
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002607#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2608 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2609 authmode = ssl->handshake->sni_authmode;
2610 else
2611#endif
2612 authmode = ssl->conf->authmode;
2613
Hanno Becker77adddc2019-02-07 12:32:43 +00002614 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02002615 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 {
Johan Pascal4f099262020-09-22 10:59:26 +02002617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2618 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620
2621 /*
2622 * 0 . 0 handshake type
2623 * 1 . 3 handshake length
2624 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002625 * 5 .. m-1 cert types
2626 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002627 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 * n .. n+1 length of all DNs
2629 * n+2 .. n+3 length of DN 1
2630 * n+4 .. ... Distinguished Name #1
2631 * ... .. ... length of DN 2, etc.
2632 */
2633 buf = ssl->out_msg;
2634 p = buf + 4;
2635
2636 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002637 * Supported certificate types
2638 *
2639 * ClientCertificateType certificate_types<1..2^8-1>;
2640 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002642 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_RSA_C)
2645 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002646#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#if defined(MBEDTLS_ECDSA_C)
2648 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002649#endif
2650
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002651 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002652 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002653
Paul Bakker577e0062013-08-28 11:57:20 +02002654 sa_len = 0;
Jerry Yue7541932022-01-28 10:21:24 +08002655
Paul Bakker926af752012-11-23 13:38:07 +01002656 /*
2657 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002658 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002659 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2660 *
2661 * struct {
2662 * HashAlgorithm hash;
2663 * SignatureAlgorithm signature;
2664 * } SignatureAndHashAlgorithm;
2665 *
2666 * enum { (255) } HashAlgorithm;
2667 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002668 */
Ronald Cron8457c122022-03-07 11:32:54 +01002669 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
2670 if( sig_alg == NULL )
2671 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
2672
Gabor Mezei15b95a62022-05-09 16:37:58 +02002673 for( ; *sig_alg != MBEDTLS_TLS_SIG_NONE; sig_alg++ )
Paul Bakker926af752012-11-23 13:38:07 +01002674 {
Ronald Cron8457c122022-03-07 11:32:54 +01002675 unsigned char hash = MBEDTLS_BYTE_1( *sig_alg );
Jerry Yu6106fdc2022-01-12 16:36:14 +08002676
Ronald Cron8457c122022-03-07 11:32:54 +01002677 if( mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
2678 continue;
2679 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
2680 continue;
Simon Butcher99000142016-10-13 17:21:01 +01002681
Ronald Cron8457c122022-03-07 11:32:54 +01002682 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, sa_len );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002683 sa_len += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Ronald Cron8457c122022-03-07 11:32:54 +01002686 MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 );
2687 sa_len += 2;
2688 p += sa_len;
2689
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002690 /*
2691 * DistinguishedName certificate_authorities<0..2^16-1>;
2692 * opaque DistinguishedName<1..2^16-1>;
2693 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002696 total_dn_size = 0;
Janos Follath088ce432017-04-10 12:42:31 +01002697
2698 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 {
Hanno Becker8bf74f32019-03-27 11:01:30 +00002700 /* NOTE: If trusted certificates are provisioned
2701 * via a CA callback (configured through
2702 * `mbedtls_ssl_conf_ca_cb()`, then the
2703 * CertificateRequest is currently left empty. */
2704
Janos Follath088ce432017-04-10 12:42:31 +01002705#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2706 if( ssl->handshake->sni_ca_chain != NULL )
2707 crt = ssl->handshake->sni_ca_chain;
2708 else
2709#endif
2710 crt = ssl->conf->ca_chain;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002711
Janos Follath088ce432017-04-10 12:42:31 +01002712 while( crt != NULL && crt->version != 0 )
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002713 {
irwirc9bc3002020-04-01 13:46:36 +03002714 /* It follows from RFC 5280 A.1 that this length
2715 * can be represented in at most 11 bits. */
2716 dn_size = (uint16_t) crt->subject_raw.len;
Janos Follath088ce432017-04-10 12:42:31 +01002717
irwirc9bc3002020-04-01 13:46:36 +03002718 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Janos Follath088ce432017-04-10 12:42:31 +01002719 {
2720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2721 break;
2722 }
2723
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002724 MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 );
2725 p += 2;
Janos Follath088ce432017-04-10 12:42:31 +01002726 memcpy( p, crt->subject_raw.p, dn_size );
2727 p += dn_size;
2728
2729 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2730
2731 total_dn_size += 2 + dn_size;
2732 crt = crt->next;
Manuel Pégourié-Gonnardbc1babb2015-10-02 11:16:47 +02002733 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
Paul Bakker926af752012-11-23 13:38:07 +01002736 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2738 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002739 MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002741 ret = mbedtls_ssl_write_handshake_msg( ssl );
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 return( ret );
2746}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002747#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002749#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2750 ( defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2751 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
2752static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
2753{
2754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong4f33fbc2022-03-22 16:30:01 +01002756 unsigned char buf[
2757 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
Neil Armstrong104a7c12022-03-23 10:58:03 +01002758 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002759 size_t ecdh_bits = 0;
2760 size_t key_len;
Neil Armstrong104a7c12022-03-23 10:58:03 +01002761 mbedtls_pk_context *pk;
2762 mbedtls_ecp_keypair *key;
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002763
Neil Armstrong104a7c12022-03-23 10:58:03 +01002764 pk = mbedtls_ssl_own_key( ssl );
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002765
Neil Armstrong104a7c12022-03-23 10:58:03 +01002766 if( pk == NULL )
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002767 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2768
Neil Armstrong104a7c12022-03-23 10:58:03 +01002769 switch( mbedtls_pk_get_type( pk ) )
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002770 {
Neil Armstrong104a7c12022-03-23 10:58:03 +01002771 case MBEDTLS_PK_OPAQUE:
2772 if( ! mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
2773 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2774
2775 ssl->handshake->ecdh_psa_privkey =
2776 *( (mbedtls_svc_key_id_t*) pk->pk_ctx );
2777
Neil Armstronge88d1902022-04-04 11:25:23 +02002778 /* Key should not be destroyed in the TLS library */
2779 ssl->handshake->ecdh_psa_privkey_is_external = 1;
2780
Neil Armstrong104a7c12022-03-23 10:58:03 +01002781 status = psa_get_key_attributes( ssl->handshake->ecdh_psa_privkey,
2782 &key_attributes );
2783 if( status != PSA_SUCCESS)
Neil Armstronge88d1902022-04-04 11:25:23 +02002784 {
2785 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong13352222022-03-25 15:08:11 +01002786 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstronge88d1902022-04-04 11:25:23 +02002787 }
Neil Armstrong104a7c12022-03-23 10:58:03 +01002788
2789 ssl->handshake->ecdh_psa_type = psa_get_key_type( &key_attributes );
2790 ssl->handshake->ecdh_bits = psa_get_key_bits( &key_attributes );
2791
2792 psa_reset_key_attributes( &key_attributes );
2793
Neil Armstrong104a7c12022-03-23 10:58:03 +01002794 ret = 0;
2795 break;
2796 case MBEDTLS_PK_ECKEY:
2797 case MBEDTLS_PK_ECKEY_DH:
2798 case MBEDTLS_PK_ECDSA:
2799 key = mbedtls_pk_ec( *pk );
2800 if( key == NULL )
2801 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2802
2803 /* Convert EC group to PSA key type. */
2804 if( ( ssl->handshake->ecdh_psa_type =
2805 mbedtls_ecc_group_to_psa( key->grp.id,
2806 &ecdh_bits ) ) == 0 )
2807 {
2808 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2809 }
2810
Neil Armstrong91477a72022-03-25 15:42:20 +01002811 ssl->handshake->ecdh_bits = ecdh_bits;
Neil Armstrong104a7c12022-03-23 10:58:03 +01002812
2813 key_attributes = psa_key_attributes_init();
2814 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2815 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
2816 psa_set_key_type( &key_attributes,
2817 PSA_KEY_TYPE_ECC_KEY_PAIR( ssl->handshake->ecdh_psa_type ) );
2818 psa_set_key_bits( &key_attributes, ssl->handshake->ecdh_bits );
2819
2820 key_len = PSA_BITS_TO_BYTES( key->grp.pbits );
2821 ret = mbedtls_ecp_write_key( key, buf, key_len );
2822 if( ret != 0 )
2823 goto cleanup;
2824
2825 status = psa_import_key( &key_attributes, buf, key_len,
2826 &ssl->handshake->ecdh_psa_privkey );
2827 if( status != PSA_SUCCESS )
2828 {
2829 ret = psa_ssl_status_to_mbedtls( status );
2830 goto cleanup;
2831 }
2832
2833 ret = 0;
2834 break;
2835 default:
2836 ret = MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002837 }
2838
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002839cleanup:
Neil Armstrong5cd5f762022-03-22 17:28:51 +01002840 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrong1f4b3962022-03-09 14:54:29 +01002841
2842 return( ret );
2843}
2844#elif defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2846static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002847{
Janos Follath865b3eb2019-12-16 11:46:15 +00002848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002849
Leonid Rozenboim28752702022-04-21 18:00:52 -07002850 const mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
2851 if( private_key == NULL)
2852 {
Paul Elliottdd428d32022-05-13 17:43:16 +01002853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no server private key" ) );
Leonid Rozenboim28752702022-04-21 18:00:52 -07002854 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
2855 }
2856
2857 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2860 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002861 }
2862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2864 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2865 MBEDTLS_ECDH_OURS ) ) != 0 )
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002868 return( ret );
2869 }
2870
2871 return( 0 );
2872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2874 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002875
Gilles Peskineeccd8882020-03-10 12:19:08 +01002876#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002877 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002878static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002879 size_t *signature_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002880{
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002881 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2882 * signature length which will be added in ssl_write_server_key_exchange
2883 * after the call to ssl_prepare_server_key_exchange.
2884 * ssl_write_server_key_exchange also takes care of incrementing
2885 * ssl->out_msglen. */
2886 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
Angus Grattond8213d02016-05-25 20:56:48 +10002887 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002888 - sig_start );
Gilles Peskine8f97af72018-04-26 11:46:10 +02002889 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02002890 sig_start, signature_len, sig_max_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002891 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
2892 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002893 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02002894 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002895 }
Gilles Peskined3eb0612018-01-08 17:07:44 +01002896 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002897 return( ret );
2898}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002899#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002900 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01002901
Gilles Peskined3eb0612018-01-08 17:07:44 +01002902/* Prepare the ServerKeyExchange message, up to and including
Gilles Peskine168dae82018-04-25 23:35:42 +02002903 * calculating the signature if any, but excluding formatting the
2904 * signature and sending the message. */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01002905static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
2906 size_t *signature_len )
Paul Bakker5690efc2011-05-26 13:16:06 +00002907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002909 ssl->handshake->ciphersuite_info;
2910
Gilles Peskineeccd8882020-03-10 12:19:08 +01002911#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
Jerry Yuc5aef882021-12-23 20:15:02 +08002912#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine3ce9b902018-01-06 01:34:21 +01002913 unsigned char *dig_signed = NULL;
Jerry Yuc5aef882021-12-23 20:15:02 +08002914#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002915#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002916
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002917 (void) ciphersuite_info; /* unused in some configurations */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002918#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine22e695f2018-04-26 00:22:50 +02002919 (void) signature_len;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002920#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002921
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002922#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef00f1522021-06-22 00:09:00 +02002923#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2924 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
2925#else
2926 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
2927#endif
Gilles Peskine16fe8fc2021-06-22 09:45:56 +02002928#endif
Gilles Peskinef00f1522021-06-22 00:09:00 +02002929
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002930 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002932 /*
2933 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002934 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002935 *
2936 */
2937
2938 /*
2939 * - ECJPAKE key exchanges
2940 */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002941#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2942 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2943 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002945 size_t len = 0;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002946
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002947 ret = mbedtls_ecjpake_write_round_two(
2948 &ssl->handshake->ecjpake_ctx,
2949 ssl->out_msg + ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10002950 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002951 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002952 if( ret != 0 )
2953 {
2954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
2955 return( ret );
2956 }
2957
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002958 ssl->out_msglen += len;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002959 }
2960#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2961
Hanno Becker1aa267c2017-04-28 17:08:27 +01002962 /*
2963 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
2964 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
2965 * we use empty support identity hints here.
2966 **/
2967#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2969 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2970 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002971 {
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01002972 ssl->out_msg[ssl->out_msglen++] = 0x00;
2973 ssl->out_msg[ssl->out_msglen++] = 0x00;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002974 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2976 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002977
Hanno Becker7e5437a2017-04-28 17:15:26 +01002978 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01002979 * - DHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01002980 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002981#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002982 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
Paul Bakker48916f92012-09-16 19:57:18 +00002983 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01002985 size_t len = 0;
Gilles Peskine184a3fa2018-01-06 01:46:17 +01002986
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002987 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
2988 {
2989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
2990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2991 }
2992
Paul Bakker41c83d32013-03-20 14:39:14 +01002993 /*
2994 * Ephemeral DH parameters:
2995 *
2996 * struct {
2997 * opaque dh_p<1..2^16-1>;
2998 * opaque dh_g<1..2^16-1>;
2999 * opaque dh_Ys<1..2^16-1>;
3000 * } ServerDHParams;
3001 */
Hanno Beckerab740562017-10-04 13:15:37 +01003002 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3003 &ssl->conf->dhm_P,
3004 &ssl->conf->dhm_G ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003005 {
Hanno Beckerab740562017-10-04 13:15:37 +01003006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003007 return( ret );
3008 }
Paul Bakker48916f92012-09-16 19:57:18 +00003009
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003010 if( ( ret = mbedtls_dhm_make_params(
3011 &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003012 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003013 ssl->out_msg + ssl->out_msglen, &len,
3014 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01003017 return( ret );
3018 }
3019
Jerry Yuc5aef882021-12-23 20:15:02 +08003020#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003021 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003022#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003023
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003024 ssl->out_msglen += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3027 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3028 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3029 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker41c83d32013-03-20 14:39:14 +01003030 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003031#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01003032
Hanno Becker1aa267c2017-04-28 17:08:27 +01003033 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003034 * - ECDHE key exchanges
Hanno Becker1aa267c2017-04-28 17:08:27 +01003035 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003036#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003037 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 {
Paul Bakker41c83d32013-03-20 14:39:14 +01003039 /*
3040 * Ephemeral ECDH parameters:
3041 *
3042 * struct {
3043 * ECParameters curve_params;
3044 * ECPoint public;
3045 * } ServerECDHParams;
3046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 const mbedtls_ecp_curve_info **curve = NULL;
Brett Warren01f3dae2021-08-17 13:50:51 +01003048 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Janos Follath865b3eb2019-12-16 11:46:15 +00003049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher600c5e62018-06-14 08:58:59 +01003050 size_t len = 0;
Gergely Budai987bfb52014-01-19 21:48:42 +01003051
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003052 /* Match our preference list against the offered curves */
Brett Warren01f3dae2021-08-17 13:50:51 +01003053 if( group_list == NULL )
3054 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
3055 for( ; *group_list != 0; group_list++ )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003056 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
Brett Warren01f3dae2021-08-17 13:50:51 +01003057 if( (*curve)->tls_id == *group_list )
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01003058 goto curve_matching_done;
3059
3060curve_matching_done:
Manuel Pégourié-Gonnardb86145e2015-06-23 14:11:39 +02003061 if( curve == NULL || *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01003062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
Dave Rodgmanbb05cd02021-06-29 10:37:43 +01003064 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Gergely Budai987bfb52014-01-19 21:48:42 +01003065 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01003068
Przemek Stekielb6ce0b62022-03-09 15:38:24 +01003069#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd91526c2022-04-12 14:38:52 +02003070 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3071 psa_key_attributes_t key_attributes;
3072 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3073 size_t ecdh_bits = 0;
3074 uint8_t *p = ssl->out_msg + ssl->out_msglen;
3075 const size_t header_size = 4; // curve_type(1), namedcurve(2),
3076 // data length(1)
3077 const size_t data_length_size = 1;
3078
3079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3080
3081 /* Convert EC group to PSA key type. */
3082 handshake->ecdh_psa_type = mbedtls_psa_parse_tls_ecc_group(
3083 (*curve)->tls_id, &ecdh_bits );
3084
3085 if( handshake->ecdh_psa_type == 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003086 {
Neil Armstrongd91526c2022-04-12 14:38:52 +02003087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group parse." ) );
3088 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Przemek Stekielb6ce0b62022-03-09 15:38:24 +01003089 }
Neil Armstrongd91526c2022-04-12 14:38:52 +02003090 handshake->ecdh_bits = ecdh_bits;
3091
3092 key_attributes = psa_key_attributes_init();
3093 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3094 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3095 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3096 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3097
3098 /*
3099 * ECParameters curve_params
3100 *
3101 * First byte is curve_type, always named_curve
3102 */
3103 *p++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
3104
3105 /*
3106 * Next two bytes are the namedcurve value
3107 */
3108 MBEDTLS_PUT_UINT16_BE( (*curve)->tls_id, p, 0 );
3109 p += 2;
3110
3111 /* Generate ECDH private key. */
3112 status = psa_generate_key( &key_attributes,
3113 &handshake->ecdh_psa_privkey );
3114 if( status != PSA_SUCCESS )
3115 {
3116 ret = psa_ssl_status_to_mbedtls( status );
3117 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
3118 return( ret );
3119 }
3120
3121 /*
3122 * ECPoint public
3123 *
3124 * First byte is data length.
3125 * It will be filled later. p holds now the data length location.
3126 */
3127
3128 /* Export the public part of the ECDH private key from PSA.
3129 * Make one byte space for the length.
3130 */
3131 unsigned char *own_pubkey = p + data_length_size;
3132
3133 size_t own_pubkey_max_len = (size_t)( MBEDTLS_SSL_OUT_CONTENT_LEN
3134 - ( own_pubkey - ssl->out_msg ) );
3135
3136 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3137 own_pubkey, own_pubkey_max_len,
3138 &len );
3139 if( status != PSA_SUCCESS )
3140 {
3141 ret = psa_ssl_status_to_mbedtls( status );
3142 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
3143 (void) psa_destroy_key( handshake->ecdh_psa_privkey );
3144 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3145 return( ret );
3146 }
3147
3148 /* Store the length of the exported public key. */
3149 *p = (uint8_t) len;
3150
3151 /* Determine full message length. */
3152 len += header_size;
3153#else
3154 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3155 (*curve)->grp_id ) ) != 0 )
3156 {
3157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
3158 return( ret );
3159 }
3160
3161 if( ( ret = mbedtls_ecdh_make_params(
3162 &ssl->handshake->ecdh_ctx, &len,
3163 ssl->out_msg + ssl->out_msglen,
3164 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
3165 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3166 {
3167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
3168 return( ret );
3169 }
3170
3171 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3172 MBEDTLS_DEBUG_ECDH_Q );
Przemek Stekielce1d7922022-03-14 16:16:25 +01003173#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01003174
Jerry Yuc5aef882021-12-23 20:15:02 +08003175#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003176 dig_signed = ssl->out_msg + ssl->out_msglen;
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003177#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003178
Gilles Peskinef9f15ae2018-01-08 17:13:01 +01003179 ssl->out_msglen += len;
Paul Bakker41c83d32013-03-20 14:39:14 +01003180 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003181#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Hanno Becker1aa267c2017-04-28 17:08:27 +01003183 /*
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003184 *
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003185 * Part 2: For key exchanges involving the server signing the
Hanno Beckercf7ae7e2017-05-11 14:07:25 +01003186 * exchange parameters, compute and add the signature here.
3187 *
Hanno Becker1aa267c2017-04-28 17:08:27 +01003188 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003189#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003190 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191 {
Paul Elliott11420382022-05-13 17:43:47 +01003192 if( dig_signed == NULL )
3193 {
3194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3196 }
3197
Gilles Peskine1004c192018-01-08 16:59:14 +01003198 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
Gilles Peskineca1d7422018-04-24 11:53:22 +02003199 size_t hashlen = 0;
Ronald Cron69a63422021-10-18 09:47:58 +02003200#if defined(MBEDTLS_USE_PSA_CRYPTO)
3201 unsigned char hash[PSA_HASH_MAX_SIZE];
3202#else
Gilles Peskinee1efdf92018-01-05 21:18:37 +01003203 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Ronald Cron69a63422021-10-18 09:47:58 +02003204#endif
Janos Follath865b3eb2019-12-16 11:46:15 +00003205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23f36802012-09-28 14:15:14 +00003206
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003207 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003208 * 2.1: Choose hash algorithm:
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003209 * For TLS 1.2, obey signature-hash-algorithm extension
3210 * to choose appropriate hash.
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003211 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003212
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003213 mbedtls_pk_type_t sig_alg =
3214 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Ronald Cron8457c122022-03-07 11:32:54 +01003215
Gabor Mezeia3d016c2022-05-10 12:44:09 +02003216 unsigned int sig_hash =
3217 mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
3218 ssl, mbedtls_ssl_sig_from_pk_alg( sig_alg ) );
3219
3220 mbedtls_md_type_t md_alg = mbedtls_ssl_md_alg_from_hash( sig_hash );
3221
Ronald Cron8457c122022-03-07 11:32:54 +01003222 /* For TLS 1.2, obey signature-hash-algorithm extension
3223 * (RFC 5246, Sec. 7.4.1.4.1). */
Gabor Mezei696956d2022-05-13 16:27:29 +02003224 if( sig_alg == MBEDTLS_PK_NONE || md_alg == MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003225 {
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ronald Cron8457c122022-03-07 11:32:54 +01003227 /* (... because we choose a cipher suite
3228 * only if there is a matching hash.) */
TRodziewicz4ca18aa2021-05-20 14:46:20 +02003229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003230 }
3231
Paul Elliott3891caf2020-12-17 18:42:40 +00003232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Hanno Becker7e5437a2017-04-28 17:15:26 +01003233
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003234 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003235 * 2.2: Compute the hash to be signed
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003238 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02003239 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01003240 dig_signed,
3241 dig_signed_len,
3242 md_alg );
3243 if( ret != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003244 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003245 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003246 else
Paul Bakker577e0062013-08-28 11:57:20 +02003247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003250 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003251
Gilles Peskineebd652f2018-01-05 21:18:59 +01003252 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003253
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003254 /*
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003255 * 2.3: Compute and add the signature
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003256 */
Ronald Cron8457c122022-03-07 11:32:54 +01003257 /*
3258 * We need to specify signature and hash algorithm explicitly through
3259 * a prefix to the signature.
3260 *
3261 * struct {
3262 * HashAlgorithm hash;
3263 * SignatureAlgorithm signature;
3264 * } SignatureAndHashAlgorithm;
3265 *
3266 * struct {
3267 * SignatureAndHashAlgorithm algorithm;
3268 * opaque signature<0..2^16-1>;
3269 * } DigitallySigned;
3270 *
3271 */
Hanno Becker7e5437a2017-04-28 17:15:26 +01003272
Ronald Cron8457c122022-03-07 11:32:54 +01003273 ssl->out_msg[ssl->out_msglen++] = mbedtls_ssl_hash_from_md_alg( md_alg );
3274 ssl->out_msg[ssl->out_msglen++] = mbedtls_ssl_sig_from_pk_alg( sig_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003275
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003276#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003277 if( ssl->conf->f_async_sign_start != NULL )
3278 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003279 ret = ssl->conf->f_async_sign_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003280 mbedtls_ssl_own_cert( ssl ),
3281 md_alg, hash, hashlen );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003282 switch( ret )
3283 {
3284 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3285 /* act as if f_async_sign was null */
3286 break;
3287 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003288 ssl->handshake->async_in_progress = 1;
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003289 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003290 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003291 ssl->handshake->async_in_progress = 1;
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003292 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3293 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003294 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003295 return( ret );
3296 }
3297 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003298#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine4bf9a282018-01-05 21:20:50 +01003299
3300 if( mbedtls_ssl_own_key( ssl ) == NULL )
3301 {
3302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3303 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3304 }
3305
Gilles Peskine0fd90dd2018-04-26 07:41:09 +02003306 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3307 * signature length which will be added in ssl_write_server_key_exchange
3308 * after the call to ssl_prepare_server_key_exchange.
3309 * ssl_write_server_key_exchange also takes care of incrementing
3310 * ssl->out_msglen. */
Gilles Peskine1004c192018-01-08 16:59:14 +01003311 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3312 md_alg, hash, hashlen,
3313 ssl->out_msg + ssl->out_msglen + 2,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003314 out_buf_len - ssl->out_msglen - 2,
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003315 signature_len,
Gilles Peskine1004c192018-01-08 16:59:14 +01003316 ssl->conf->f_rng,
3317 ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003320 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003321 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003322 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003323#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003324
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003325 return( 0 );
3326}
Paul Bakker1ef83d62012-04-11 12:09:53 +00003327
Gilles Peskined3eb0612018-01-08 17:07:44 +01003328/* Prepare the ServerKeyExchange message and send it. For ciphersuites
Gilles Peskine168dae82018-04-25 23:35:42 +02003329 * that do not include a ServerKeyExchange message, do nothing. Either
3330 * way, if successful, move on to the next step in the SSL state
3331 * machine. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003332static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3333{
Janos Follath865b3eb2019-12-16 11:46:15 +00003334 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003335 size_t signature_len = 0;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003336#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003337 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003338 ssl->handshake->ciphersuite_info;
Gilles Peskineeccd8882020-03-10 12:19:08 +01003339#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003340
Gilles Peskined3eb0612018-01-08 17:07:44 +01003341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3342
Gilles Peskineeccd8882020-03-10 12:19:08 +01003343#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003344 /* Extract static ECDH parameters and abort if ServerKeyExchange
3345 * is not needed. */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003346 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3347 {
3348 /* For suites involving ECDH, extract DH parameters
3349 * from certificate at this point. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003350#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003351 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3352 {
3353 ssl_get_ecdh_params_from_cert( ssl );
3354 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003355#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003356
3357 /* Key exchanges not involving ephemeral keys don't use
3358 * ServerKeyExchange, so end here. */
3359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3360 ssl->state++;
3361 return( 0 );
3362 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003363#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003364
Gilles Peskineeccd8882020-03-10 12:19:08 +01003365#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003366 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskined3eb0612018-01-08 17:07:44 +01003367 /* If we have already prepared the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003368 * signature operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003369 if( ssl->handshake->async_in_progress != 0 )
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003370 {
3371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3372 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003373 }
3374 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003375#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003376 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003377 {
3378 /* ServerKeyExchange is needed. Prepare the message. */
3379 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
Gilles Peskined3eb0612018-01-08 17:07:44 +01003380 }
3381
3382 if( ret != 0 )
3383 {
Gilles Peskinead28bf02018-04-26 00:19:16 +02003384 /* If we're starting to write a new message, set ssl->out_msglen
3385 * to 0. But if we're resuming after an asynchronous message,
3386 * out_msglen is the amount of data written so far and mst be
3387 * preserved. */
Gilles Peskined3eb0612018-01-08 17:07:44 +01003388 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3390 else
3391 ssl->out_msglen = 0;
3392 return( ret );
Gilles Peskineebd30ae2018-01-06 03:34:20 +01003393 }
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003394
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003395 /* If there is a signature, write its length.
Gilles Peskine168dae82018-04-25 23:35:42 +02003396 * ssl_prepare_server_key_exchange already wrote the signature
3397 * itself at its proper place in the output buffer. */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003398#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003399 if( signature_len != 0 )
3400 {
Joe Subbianifbeb6922021-07-16 14:27:50 +01003401 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len );
3402 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len );
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003403
3404 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3405 ssl->out_msg + ssl->out_msglen,
3406 signature_len );
3407
3408 /* Skip over the already-written signature */
3409 ssl->out_msglen += signature_len;
3410 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003411#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Gilles Peskine7ab013a2018-01-08 17:04:16 +01003412
Gilles Peskine184a3fa2018-01-06 01:46:17 +01003413 /* Add header and send. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3415 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003416
3417 ssl->state++;
3418
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003419 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003422 return( ret );
3423 }
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003427}
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003430{
Janos Follath865b3eb2019-12-16 11:46:15 +00003431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
3435 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3437 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003438
3439 ssl->state++;
3440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003442 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003444#endif
3445
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003446 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 return( ret );
3450 }
3451
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003452#if defined(MBEDTLS_SSL_PROTO_DTLS)
3453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3454 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3455 {
3456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3457 return( ret );
3458 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01003459#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003462
3463 return( 0 );
3464}
3465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3467 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3468static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003469 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003472 size_t n;
3473
3474 /*
3475 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3476 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003477 if( *p + 2 > end )
3478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003480 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003481 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003482
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003483 n = ( (*p)[0] << 8 ) | (*p)[1];
3484 *p += 2;
3485
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003486 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003489 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003490 }
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003495 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003496 }
3497
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003498 *p += n;
3499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003501
Paul Bakker70df2fb2013-04-17 17:19:09 +02003502 return( ret );
3503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3505 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3508 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003509
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003510#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003511static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3512 unsigned char *peer_pms,
3513 size_t *peer_pmslen,
3514 size_t peer_pmssize )
3515{
Gilles Peskine8f97af72018-04-26 11:46:10 +02003516 int ret = ssl->conf->f_async_resume( ssl,
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003517 peer_pms, peer_pmslen, peer_pmssize );
3518 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3519 {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003520 ssl->handshake->async_in_progress = 0;
Gilles Peskine1febfef2018-04-30 11:54:39 +02003521 mbedtls_ssl_set_async_operation_data( ssl, NULL );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003522 }
3523 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3524 return( ret );
3525}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003526#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003527
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003528static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3529 const unsigned char *p,
3530 const unsigned char *end,
3531 unsigned char *peer_pms,
3532 size_t *peer_pmslen,
3533 size_t peer_pmssize )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003534{
Janos Follath865b3eb2019-12-16 11:46:15 +00003535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine422ccab2018-01-11 18:29:01 +01003536 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3537 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3538 size_t len = mbedtls_pk_get_len( public_key );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003539
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003540#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003541 /* If we have already started decoding the message and there is an ongoing
Gilles Peskine168dae82018-04-25 23:35:42 +02003542 * decryption operation, resume signing. */
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003543 if( ssl->handshake->async_in_progress != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003544 {
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3546 return( ssl_resume_decrypt_pms( ssl,
3547 peer_pms, peer_pmslen, peer_pmssize ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003548 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003549#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003550
3551 /*
Gilles Peskine422ccab2018-01-11 18:29:01 +01003552 * Prepare to decrypt the premaster using own private RSA key
Paul Bakker70df2fb2013-04-17 17:19:09 +02003553 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003554 if ( p + 2 > end ) {
3555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003556 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003557 }
Joe Subbiani2194dc42021-07-14 12:31:31 +01003558 if( *p++ != MBEDTLS_BYTE_1( len ) ||
3559 *p++ != MBEDTLS_BYTE_0( len ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003560 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003562 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003563 }
3564
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003565 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003568 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003569 }
3570
Gilles Peskine422ccab2018-01-11 18:29:01 +01003571 /*
3572 * Decrypt the premaster secret
3573 */
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003574#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003575 if( ssl->conf->f_async_decrypt_start != NULL )
3576 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003577 ret = ssl->conf->f_async_decrypt_start( ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003578 mbedtls_ssl_own_cert( ssl ),
3579 p, len );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003580 switch( ret )
3581 {
3582 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3583 /* act as if f_async_decrypt_start was null */
3584 break;
3585 case 0:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003586 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003587 return( ssl_resume_decrypt_pms( ssl,
3588 peer_pms,
3589 peer_pmslen,
3590 peer_pmssize ) );
3591 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003592 ssl->handshake->async_in_progress = 1;
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003593 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3594 default:
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003595 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003596 return( ret );
3597 }
3598 }
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003599#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003600
Gilles Peskine422ccab2018-01-11 18:29:01 +01003601 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3602 {
Gilles Peskine422ccab2018-01-11 18:29:01 +01003603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3604 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3605 }
3606
3607 ret = mbedtls_pk_decrypt( private_key, p, len,
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003608 peer_pms, peer_pmslen, peer_pmssize,
3609 ssl->conf->f_rng, ssl->conf->p_rng );
3610 return( ret );
3611}
3612
3613static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3614 const unsigned char *p,
3615 const unsigned char *end,
3616 size_t pms_offset )
3617{
Janos Follath865b3eb2019-12-16 11:46:15 +00003618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003619 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3620 unsigned char ver[2];
3621 unsigned char fake_pms[48], peer_pms[48];
3622 unsigned char mask;
3623 size_t i, peer_pmslen;
3624 unsigned int diff;
3625
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003626 /* In case of a failure in decryption, the decryption may write less than
3627 * 2 bytes of output, but we always read the first two bytes. It doesn't
3628 * matter in the end because diff will be nonzero in that case due to
André Maroneze79533292020-11-12 09:37:42 +01003629 * ret being nonzero, and we only care whether diff is 0.
3630 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3631 * also makes memory analyzers happy (don't access uninitialized memory,
3632 * even if it's an unsigned char). */
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003633 peer_pms[0] = peer_pms[1] = ~0;
André Maroneze79533292020-11-12 09:37:42 +01003634 peer_pmslen = 0;
Gilles Peskine0a8352b2018-06-13 18:16:41 +02003635
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003636 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3637 peer_pms,
3638 &peer_pmslen,
3639 sizeof( peer_pms ) );
3640
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003641#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003642 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3643 return( ret );
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003644#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003645
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003646 mbedtls_ssl_write_version( ver, ssl->conf->transport,
3647 ssl->session_negotiate->tls_version );
Gilles Peskine2e333372018-04-24 13:22:10 +02003648
3649 /* Avoid data-dependent branches while checking for invalid
3650 * padding, to protect against timing-based Bleichenbacher-type
3651 * attacks. */
3652 diff = (unsigned int) ret;
3653 diff |= peer_pmslen ^ 48;
3654 diff |= peer_pms[0] ^ ver[0];
3655 diff |= peer_pms[1] ^ ver[1];
3656
3657 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
Gabor Mezei90437e32021-10-20 11:59:27 +02003658 mask = mbedtls_ct_uint_mask( diff );
Manuel Pégourié-Gonnardb9c93d02015-06-23 13:53:15 +02003659
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003660 /*
3661 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3662 * must not cause the connection to end immediately; instead, send a
3663 * bad_record_mac later in the handshake.
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003664 * To protect against timing-based variants of the attack, we must
3665 * not have any branch that depends on whether the decryption was
3666 * successful. In particular, always generate the fake premaster secret,
3667 * regardless of whether it will ultimately influence the output or not.
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003668 */
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003669 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003670 if( ret != 0 )
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003671 {
Gilles Peskinee1416382018-04-26 10:23:21 +02003672 /* It's ok to abort on an RNG failure, since this does not reveal
3673 * anything about the RSA decryption. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003674 return( ret );
Gilles Peskinebcd98a52018-01-11 21:30:40 +01003675 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003676
Manuel Pégourié-Gonnard331ba572015-04-20 12:33:57 +01003677#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnardce60fbe2015-04-15 16:45:52 +02003678 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003680#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003681
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003682 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3683 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3686 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003687 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003688 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003689
Gilles Peskine422ccab2018-01-11 18:29:01 +01003690 /* Set pms to either the true or the fake PMS, without
3691 * data-dependent branches. */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003692 for( i = 0; i < ssl->handshake->pmslen; i++ )
3693 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3694
3695 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3698 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003699
Gilles Peskineeccd8882020-03-10 12:19:08 +01003700#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003702 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003703{
Paul Bakker6db455e2013-09-18 17:29:31 +02003704 int ret = 0;
irwir6527bd62019-09-21 18:51:25 +03003705 uint16_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003706
Hanno Becker845b9462018-10-26 12:07:29 +01003707 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3710 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003711 }
3712
3713 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003714 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003715 */
Hanno Becker83c9f492017-06-26 13:52:14 +01003716 if( end - *p < 2 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003719 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003720 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003721
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003722 n = ( (*p)[0] << 8 ) | (*p)[1];
3723 *p += 2;
3724
irwir6527bd62019-09-21 18:51:25 +03003725 if( n == 0 || n > end - *p )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003728 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003729 }
3730
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003731 if( ssl->conf->f_psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02003732 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003733 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003735 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003736 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003737 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003738 /* Identity is not a big secret since clients send it in the clear,
3739 * but treat it carefully anyway, just in case */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003740 if( n != ssl->conf->psk_identity_len ||
Gabor Mezei90437e32021-10-20 11:59:27 +02003741 mbedtls_ct_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Paul Bakker6db455e2013-09-18 17:29:31 +02003744 }
3745 }
3746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Gilles Peskinec94f7352017-05-10 16:37:56 +02003750 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3751 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003753 }
3754
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003755 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003756
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003757 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003758}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003759#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003762{
Janos Follath865b3eb2019-12-16 11:46:15 +00003763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003765 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003766
Hanno Beckere694c3e2017-12-27 21:34:08 +00003767 ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003770
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003771#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003772 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3773 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3774 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3775 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003776 ( ssl->handshake->async_in_progress != 0 ) )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003777 {
3778 /* We've already read a record and there is an asynchronous
3779 * operation in progress to decrypt it. So skip reading the
Gilles Peskine168dae82018-04-25 23:35:42 +02003780 * record. */
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3782 }
3783 else
3784#endif
Hanno Becker327c93b2018-08-15 13:56:18 +01003785 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 return( ret );
3789 }
3790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003792 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003797 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 }
3799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003803 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 }
3805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3807 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003808 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003809 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003812 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003814
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003815 if( p != end )
3816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003818 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003819 }
3820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003822 ssl->handshake->premaster,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01003823 MBEDTLS_PREMASTER_SIZE,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003824 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003825 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003828 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003829 }
3830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003832 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003833 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd91526c2022-04-12 14:38:52 +02003835#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3836 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3837 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3838 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Przemek Stekielce1d7922022-03-14 16:16:25 +01003839 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Neil Armstrong1f4b3962022-03-09 14:54:29 +01003840 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3841 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3842 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Przemek Stekielce1d7922022-03-14 16:16:25 +01003843 {
Neil Armstrong913b3642022-04-13 14:59:48 +02003844#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielce1d7922022-03-14 16:16:25 +01003845 size_t data_len = (size_t)( *p++ );
3846 size_t buf_len = (size_t)( end - p );
3847 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3848 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3849
3850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Read the peer's public key." ) );
3851
3852 /*
Przemek Stekiel338b61d2022-03-15 08:03:43 +01003853 * We must have at least two bytes (1 for length, at least 1 for data)
3854 */
Przemek Stekielce1d7922022-03-14 16:16:25 +01003855 if( buf_len < 2 )
3856 {
3857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid buffer length" ) );
3858 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
3859 }
3860
3861 if( data_len < 1 || data_len > buf_len )
3862 {
3863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid data length" ) );
3864 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
3865 }
3866
3867 /* Store peer's ECDH public key. */
3868 memcpy( handshake->ecdh_psa_peerkey, p, data_len );
3869 handshake->ecdh_psa_peerkey_len = data_len;
3870
3871 /* Compute ECDH shared secret. */
3872 status = psa_raw_key_agreement(
3873 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
3874 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
3875 handshake->premaster, sizeof( handshake->premaster ),
3876 &handshake->pmslen );
3877 if( status != PSA_SUCCESS )
3878 {
3879 ret = psa_ssl_status_to_mbedtls( status );
3880 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
Neil Armstrongf716a702022-04-04 11:23:46 +02003881 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003882 (void) psa_destroy_key( handshake->ecdh_psa_privkey );
Przemek Stekielce1d7922022-03-14 16:16:25 +01003883 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3884 return( ret );
3885 }
3886
Neil Armstrongf716a702022-04-04 11:23:46 +02003887 if( handshake->ecdh_psa_privkey_is_external == 0 )
Przemek Stekielce1d7922022-03-14 16:16:25 +01003888 {
Neil Armstrong8113d252022-03-23 10:57:04 +01003889 status = psa_destroy_key( handshake->ecdh_psa_privkey );
3890
3891 if( status != PSA_SUCCESS )
3892 {
3893 ret = psa_ssl_status_to_mbedtls( status );
3894 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
3895 return( ret );
3896 }
Przemek Stekielce1d7922022-03-14 16:16:25 +01003897 }
3898 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongd91526c2022-04-12 14:38:52 +02003899#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003901 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01003904 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003905 }
3906
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003907 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3908 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003911 &ssl->handshake->pmslen,
3912 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 MBEDTLS_MPI_MAX_SIZE,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003914 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Hanno Beckerd3eec782021-06-24 10:21:46 +01003917 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003918 }
3919
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003920 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3921 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong913b3642022-04-13 14:59:48 +02003922#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003924 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3926 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3927 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3928 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3929#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3930 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003931 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003932 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003935 return( ret );
3936 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003937
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003938 if( p != end )
3939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01003941 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003942 }
3943
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003944#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003946 ciphersuite_info->key_exchange ) ) != 0 )
3947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003949 return( ret );
3950 }
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003951#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3955#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3956 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003957 {
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003958#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003959 if ( ssl->handshake->async_in_progress != 0 )
Gilles Peskine2c6078e2018-01-12 13:46:43 +01003960 {
3961 /* There is an asynchronous operation in progress to
3962 * decrypt the encrypted premaster secret, so skip
3963 * directly to resuming this operation. */
3964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
3965 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3966 * won't actually use it, but maintain p anyway for robustness. */
3967 p += ssl->conf->psk_identity_len + 2;
3968 }
3969 else
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003970#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003971 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003974 return( ret );
3975 }
3976
3977 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003980 return( ret );
3981 }
3982
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003983#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003985 ciphersuite_info->key_exchange ) ) != 0 )
3986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003988 return( ret );
3989 }
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003990#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003991 }
3992 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3994#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3995 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003996 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003997 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004000 return( ret );
4001 }
4002 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004005 return( ret );
4006 }
4007
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004008 if( p != end )
4009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
Hanno Becker666b5b42021-06-24 10:13:31 +01004011 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01004012 }
4013
Neil Armstrong80f6f322022-05-03 17:56:38 +02004014#if defined(MBEDTLS_USE_PSA_CRYPTO)
4015 unsigned char *pms = ssl->handshake->premaster;
4016 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
4017 size_t pms_len;
4018
4019 /* Write length only when we know the actual value */
4020 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
4021 pms + 2, pms_end - ( pms + 2 ), &pms_len,
4022 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
4023 {
4024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4025 return( ret );
4026 }
4027 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
4028 pms += 2 + pms_len;
4029
4030 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
4031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004033 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004036 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004037 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02004038#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004039 }
4040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd91526c2022-04-12 14:38:52 +02004042#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Neil Armstrong039db292022-03-09 11:38:34 +01004043 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
4044 {
Neil Armstrong913b3642022-04-13 14:59:48 +02004045#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong039db292022-03-09 11:38:34 +01004046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4047 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
4048 uint8_t ecpoint_len;
4049
4050 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4051
4052 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4053 {
4054 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4055 psa_destroy_key( handshake->ecdh_psa_privkey );
4056 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
4057 return( ret );
4058 }
4059
4060 /* Keep a copy of the peer's public key */
Neil Armstrong3cae1672022-04-05 10:01:15 +02004061 if( p >= end )
4062 {
4063 psa_destroy_key( handshake->ecdh_psa_privkey );
4064 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
4065 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
4066 }
4067
Neil Armstrong039db292022-03-09 11:38:34 +01004068 ecpoint_len = *(p++);
Neil Armstrong3cae1672022-04-05 10:01:15 +02004069 if( (size_t)( end - p ) < ecpoint_len ) {
Neil Armstrong039db292022-03-09 11:38:34 +01004070 psa_destroy_key( handshake->ecdh_psa_privkey );
4071 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
4072 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
4073 }
4074
4075 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) ) {
4076 psa_destroy_key( handshake->ecdh_psa_privkey );
4077 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
4078 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
4079 }
4080
4081 memcpy( handshake->ecdh_psa_peerkey, p, ecpoint_len );
4082 handshake->ecdh_psa_peerkey_len = ecpoint_len;
4083 p += ecpoint_len;
4084
Neil Armstrong3bcef082022-03-23 18:16:54 +01004085 /* As RFC 5489 section 2, the premaster secret is formed as follows:
Neil Armstrongfdf20cb2022-03-24 09:43:02 +01004086 * - a uint16 containing the length (in octets) of the ECDH computation
4087 * - the octet string produced by the ECDH computation
4088 * - a uint16 containing the length (in octets) of the PSK
4089 * - the PSK itself
4090 */
Neil Armstrong039db292022-03-09 11:38:34 +01004091 unsigned char *psm = ssl->handshake->premaster;
Neil Armstrongd6e27592022-03-23 18:17:24 +01004092 const unsigned char* const psm_end =
4093 psm + sizeof( ssl->handshake->premaster );
Neil Armstrong2d63da92022-03-23 18:17:31 +01004094 /* uint16 to store length (in octets) of the ECDH computation */
4095 const size_t zlen_size = 2;
Neil Armstrong549a3e42022-03-23 18:16:24 +01004096 size_t zlen = 0;
Neil Armstrong039db292022-03-09 11:38:34 +01004097
4098 /* Compute ECDH shared secret. */
4099 status = psa_raw_key_agreement( PSA_ALG_ECDH,
4100 handshake->ecdh_psa_privkey,
4101 handshake->ecdh_psa_peerkey,
4102 handshake->ecdh_psa_peerkey_len,
Neil Armstrong2d63da92022-03-23 18:17:31 +01004103 psm + zlen_size,
4104 psm_end - ( psm + zlen_size ),
Neil Armstrong039db292022-03-09 11:38:34 +01004105 &zlen );
4106
4107 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
4108 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
4109
Neil Armstrongfb0a81e2022-03-23 18:17:11 +01004110 if( status != PSA_SUCCESS )
4111 return( psa_ssl_status_to_mbedtls( status ) );
4112 else if( destruction_status != PSA_SUCCESS )
4113 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong039db292022-03-09 11:38:34 +01004114
Neil Armstrong3bcef082022-03-23 18:16:54 +01004115 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrong039db292022-03-09 11:38:34 +01004116 MBEDTLS_PUT_UINT16_BE( zlen, psm, 0 );
Neil Armstrong2d63da92022-03-23 18:17:31 +01004117 psm += zlen_size + zlen;
Neil Armstrong039db292022-03-09 11:38:34 +01004118
Przemek Stekiel14d11b02022-04-14 08:33:29 +02004119#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004120 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004123 return( ret );
4124 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004127 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
Hanno Beckerb24e74b2021-06-24 09:52:01 +01004130 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004131 }
4132
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004133 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4134 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02004135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02004137 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004140 return( ret );
4141 }
Neil Armstrong913b3642022-04-13 14:59:48 +02004142#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02004143 }
4144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4146#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4147 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01004148 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00004149 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01004150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02004152 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004153 }
4154 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004155 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004157#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4158 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4159 {
4160 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4161 p, end - p );
4162 if( ret != 0 )
4163 {
4164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01004165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02004166 }
4167
4168 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4169 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4170 ssl->conf->f_rng, ssl->conf->p_rng );
4171 if( ret != 0 )
4172 {
4173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4174 return( ret );
4175 }
4176 }
4177 else
4178#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00004187 return( ret );
4188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004189
Paul Bakker5121ce52009-01-03 21:22:43 +00004190 ssl->state++;
4191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004193
4194 return( 0 );
4195}
4196
Gilles Peskineeccd8882020-03-10 12:19:08 +01004197#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004199{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004200 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004201 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004204
Hanno Becker77adddc2019-02-07 12:32:43 +00004205 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02004206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02004208 ssl->state++;
4209 return( 0 );
4210 }
4211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4213 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004214}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004215#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004219 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004220 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004221 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004222 size_t hashlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 mbedtls_pk_type_t pk_alg;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 mbedtls_md_type_t md_alg;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01004225 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00004226 ssl->handshake->ciphersuite_info;
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004227 mbedtls_pk_context * peer_pk;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004230
Hanno Becker2a831a42019-02-07 13:17:25 +00004231 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004234 ssl->state++;
4235 return( 0 );
4236 }
4237
Hanno Becker2a831a42019-02-07 13:17:25 +00004238#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4239 if( ssl->session_negotiate->peer_cert == NULL )
4240 {
4241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4242 ssl->state++;
4243 return( 0 );
4244 }
4245#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4246 if( ssl->session_negotiate->peer_cert_digest == NULL )
4247 {
4248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4249 ssl->state++;
4250 return( 0 );
4251 }
4252#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4253
Simon Butcher99000142016-10-13 17:21:01 +01004254 /* Read the message without adding it to the checksum */
Hanno Becker327c93b2018-08-15 13:56:18 +01004255 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Simon Butcher99000142016-10-13 17:21:01 +01004256 if( 0 != ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 {
Hanno Becker327c93b2018-08-15 13:56:18 +01004258 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004259 return( ret );
4260 }
4261
4262 ssl->state++;
4263
Simon Butcher99000142016-10-13 17:21:01 +01004264 /* Process the message contents */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4266 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004269 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 }
4271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 i = mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004274#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4275 peer_pk = &ssl->handshake->peer_pubkey;
4276#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4277 if( ssl->session_negotiate->peer_cert == NULL )
4278 {
4279 /* Should never happen */
4280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4281 }
4282 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4283#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4284
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004285 /*
4286 * struct {
4287 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4288 * opaque signature<0..2^16-1>;
4289 * } DigitallySigned;
4290 */
Ronald Cron8457c122022-03-07 11:32:54 +01004291 if( i + 2 > ssl->in_hslen )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004292 {
Ronald Cron8457c122022-03-07 11:32:54 +01004293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4294 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
4295 }
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004296
Ronald Cron8457c122022-03-07 11:32:54 +01004297 /*
4298 * Hash
4299 */
4300 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
Simon Butcher99000142016-10-13 17:21:01 +01004301
Ronald Cron8457c122022-03-07 11:32:54 +01004302 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
4303 {
4304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
4305 " for verify message" ) );
4306 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
4307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004308
Simon Butcher99000142016-10-13 17:21:01 +01004309#if !defined(MBEDTLS_MD_SHA1)
Ronald Cron8457c122022-03-07 11:32:54 +01004310 if( MBEDTLS_MD_SHA1 == md_alg )
4311 hash_start += 16;
Simon Butcher99000142016-10-13 17:21:01 +01004312#endif
Paul Bakker926af752012-11-23 13:38:07 +01004313
Ronald Cron8457c122022-03-07 11:32:54 +01004314 /* Info from md_alg will be used instead */
4315 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02004316
Ronald Cron8457c122022-03-07 11:32:54 +01004317 i++;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004318
Ronald Cron8457c122022-03-07 11:32:54 +01004319 /*
4320 * Signature
4321 */
4322 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4323 == MBEDTLS_PK_NONE )
Paul Bakker577e0062013-08-28 11:57:20 +02004324 {
Ronald Cron8457c122022-03-07 11:32:54 +01004325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
4326 " for verify message" ) );
4327 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02004328 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02004329
Ronald Cron8457c122022-03-07 11:32:54 +01004330 /*
4331 * Check the certificate's key type matches the signature alg
4332 */
4333 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
4334 {
4335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4336 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
4337 }
4338
4339 i++;
4340
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004341 if( i + 2 > ssl->in_hslen )
4342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004344 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00004345 }
4346
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004347 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4348 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01004349
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004350 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00004351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Hanno Beckerd934a2a2021-06-24 10:23:45 +01004353 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 }
4355
Simon Butcher99000142016-10-13 17:21:01 +01004356 /* Calculate hash and verify signature */
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02004357 {
4358 size_t dummy_hlen;
4359 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4360 }
Simon Butcher99000142016-10-13 17:21:01 +01004361
Hanno Beckera1ab9be2019-02-06 18:31:04 +00004362 if( ( ret = mbedtls_pk_verify( peer_pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02004363 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00004364 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 return( ret );
4368 }
4369
Simon Butcher99000142016-10-13 17:21:01 +01004370 mbedtls_ssl_update_handshake_status( ssl );
4371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004373
Paul Bakkered27a042013-04-18 22:46:23 +02004374 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004375}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004376#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4379static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004380{
Janos Follath865b3eb2019-12-16 11:46:15 +00004381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004382 size_t tlen;
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004383 uint32_t lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4388 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004389
4390 /*
4391 * struct {
4392 * uint32 ticket_lifetime_hint;
4393 * opaque ticket<0..2^16-1>;
4394 * } NewSessionTicket;
4395 *
4396 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4397 * 8 . 9 ticket_len (n)
4398 * 10 . 9+n ticket content
4399 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02004400
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004401 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +02004402 ssl->session_negotiate,
4403 ssl->out_msg + 10,
Angus Grattond8213d02016-05-25 20:56:48 +10004404 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +02004405 &tlen, &lifetime ) ) != 0 )
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004406 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +02004407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004408 tlen = 0;
4409 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004410
Joe Subbiani6dd73642021-07-19 11:56:54 +01004411 MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 );
4412 MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02004413 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004414
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01004415 /*
4416 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4417 * ChangeCipherSpec share the same state.
4418 */
4419 ssl->handshake->new_session_ticket = 0;
4420
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004421 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004422 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004424 return( ret );
4425 }
4426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004428
4429 return( 0 );
4430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004432
Paul Bakker5121ce52009-01-03 21:22:43 +00004433/*
Paul Bakker1961b702013-01-25 14:49:24 +01004434 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004437{
4438 int ret = 0;
4439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01004441
Paul Bakker1961b702013-01-25 14:49:24 +01004442 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00004443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 case MBEDTLS_SSL_HELLO_REQUEST:
4445 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 break;
4447
Paul Bakker1961b702013-01-25 14:49:24 +01004448 /*
4449 * <== ClientHello
4450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004452 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 break;
Paul Bakker1961b702013-01-25 14:49:24 +01004454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455#if defined(MBEDTLS_SSL_PROTO_DTLS)
4456 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4457 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02004458#endif
4459
Paul Bakker1961b702013-01-25 14:49:24 +01004460 /*
4461 * ==> ServerHello
4462 * Certificate
4463 * ( ServerKeyExchange )
4464 * ( CertificateRequest )
4465 * ServerHelloDone
4466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004468 ret = ssl_write_server_hello( ssl );
4469 break;
4470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4472 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004473 break;
4474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004476 ret = ssl_write_server_key_exchange( ssl );
4477 break;
4478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004480 ret = ssl_write_certificate_request( ssl );
4481 break;
4482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004484 ret = ssl_write_server_hello_done( ssl );
4485 break;
4486
4487 /*
4488 * <== ( Certificate/Alert )
4489 * ClientKeyExchange
4490 * ( CertificateVerify )
4491 * ChangeCipherSpec
4492 * Finished
4493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4495 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004496 break;
4497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004499 ret = ssl_parse_client_key_exchange( ssl );
4500 break;
4501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004503 ret = ssl_parse_certificate_verify( ssl );
4504 break;
4505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4507 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004508 break;
4509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 case MBEDTLS_SSL_CLIENT_FINISHED:
4511 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004512 break;
4513
4514 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02004515 * ==> ( NewSessionTicket )
4516 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004517 * Finished
4518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4520#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02004521 if( ssl->handshake->new_session_ticket != 0 )
4522 ret = ssl_write_new_session_ticket( ssl );
4523 else
Paul Bakkera503a632013-08-14 13:48:06 +02004524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004526 break;
4527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 case MBEDTLS_SSL_SERVER_FINISHED:
4529 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004530 break;
4531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 case MBEDTLS_SSL_FLUSH_BUFFERS:
4533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4534 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004535 break;
4536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4538 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004539 break;
4540
4541 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 }
4545
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 return( ret );
4547}
TRodziewicz8476f2f2021-06-02 14:34:47 +02004548
TRodziewicz3946f792021-06-14 12:11:18 +02004549void mbedtls_ssl_conf_preference_order( mbedtls_ssl_config *conf, int order )
TRodziewicz8476f2f2021-06-02 14:34:47 +02004550{
TRodziewicz3946f792021-06-14 12:11:18 +02004551 conf->respect_cli_pref = order;
TRodziewicz8476f2f2021-06-02 14:34:47 +02004552}
4553
Jerry Yufb4b6472022-01-27 15:03:26 +08004554#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_2 */