blob: ba39d898136f74b6581f178890e1ad9bdcefe809 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-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_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020026#else
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020028#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010029#define mbedtls_free free
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020030#endif
31
SimonBd5800b72016-04-26 07:43:27 +010032#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010033#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000034#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/debug.h"
36#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010038
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020041#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000042#endif /* MBEDTLS_USE_PSA_CRYPTO */
43
SimonBd5800b72016-04-26 07:43:27 +010044#include <string.h>
45
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020046#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010049#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050053#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020054#endif
55
Andrzej Kurek0ce59212022-08-17 07:54:34 -040056#include "hash_info.h"
57
Gilles Peskineeccd8882020-03-10 12:19:08 +010058#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Ronald Crond491c2d2022-02-19 18:30:46 +010059int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010060{
61 if( conf->psk_identity == NULL ||
62 conf->psk_identity_len == 0 )
63 {
64 return( 0 );
65 }
66
Hanno Becker2e4f6162018-10-23 11:54:44 +010067#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020068 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010069 return( 1 );
Neil Armstrong8ecd6682022-05-05 11:40:35 +020070#endif /* MBEDTLS_USE_PSA_CRYPTO */
71
Neil Armstronge952a302022-05-03 10:22:14 +020072 if( conf->psk != NULL && conf->psk_len != 0 )
73 return( 1 );
Hanno Becker2e4f6162018-10-23 11:54:44 +010074
75 return( 0 );
76}
Gilles Peskineeccd8882020-03-10 12:19:08 +010077#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020080MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +010081static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
82 unsigned char *buf,
83 const unsigned char *end,
84 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010085{
86 unsigned char *p = buf;
87
88 *olen = 0;
89
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010090 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010091 * initial ClientHello, in which case also adding the renegotiation
92 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +010094 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +010095
Hanno Beckerb2fff6d2017-05-08 11:06:19 +010096 MBEDTLS_SSL_DEBUG_MSG( 3,
97 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +010098
Hanno Becker261602c2017-04-12 14:54:42 +010099 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100100
Paul Bakkerd3edc862013-03-20 16:07:17 +0100101 /*
102 * Secure renegotiation
103 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100104 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
105 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100106
107 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100108 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
109 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100110
111 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
112
113 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100114
115 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200119#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100120 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100121
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200122MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100123static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
124 unsigned char *buf,
125 const unsigned char *end,
126 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100127{
128 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100129 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100130
131 *olen = 0;
132
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100133 MBEDTLS_SSL_DEBUG_MSG( 3,
134 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100135 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100136
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100137 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
138 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100139
140 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100141 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200142
143 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100145
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200146 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100147
148 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100149}
Darryl Green11999bb2018-03-13 15:22:58 +0000150#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100151 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200153#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200154MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100155static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
156 unsigned char *buf,
157 const unsigned char *end,
158 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200159{
Janos Follath865b3eb2019-12-16 11:46:15 +0000160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200161 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200162 size_t kkpp_len;
163
164 *olen = 0;
165
166 /* Skip costly extension if we can't use EC J-PAKE anyway */
167 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100168 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200169
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100170 MBEDTLS_SSL_DEBUG_MSG( 3,
171 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200172
Hanno Becker261602c2017-04-12 14:54:42 +0100173 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200174
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100175 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
176 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200177
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200178 /*
179 * We may need to send ClientHello multiple times for Hello verification.
180 * We don't want to compute fresh values every time (both for performance
181 * and consistency reasons), so cache the extension content.
182 */
183 if( ssl->handshake->ecjpake_cache == NULL ||
184 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200185 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
187
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200188 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100189 p + 2, end - p - 2, &kkpp_len,
190 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200191 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200192 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100193 MBEDTLS_SSL_DEBUG_RET( 1 ,
194 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100195 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200196 }
197
198 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
199 if( ssl->handshake->ecjpake_cache == NULL )
200 {
201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100202 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200203 }
204
205 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
206 ssl->handshake->ecjpake_cache_len = kkpp_len;
207 }
208 else
209 {
210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
211
212 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100213 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200214
215 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200216 }
217
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100218 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
219 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200220
221 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100222
223 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200224}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200225#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000226
Hanno Beckera0e20d02019-05-15 14:03:01 +0100227#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200228MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100229static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
230 unsigned char *buf,
231 const unsigned char *end,
232 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100233{
234 unsigned char *p = buf;
235 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100236
237 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100238 * Quoting draft-ietf-tls-dtls-connection-id-05
239 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100240 *
241 * struct {
242 * opaque cid<0..2^8-1>;
243 * } ConnectionId;
244 */
245
246 *olen = 0;
247 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
248 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
249 {
Hanno Becker261602c2017-04-12 14:54:42 +0100250 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100251 }
252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
253
254 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
255 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100256 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100257
258 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100259 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
260 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100261 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100262 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
263 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100264
265 *p++ = (uint8_t) ssl->own_cid_len;
266 memcpy( p, ssl->own_cid, ssl->own_cid_len );
267
268 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100269
270 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200275MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100276static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
277 unsigned char *buf,
278 const unsigned char *end,
279 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200280{
281 unsigned char *p = buf;
282
Simon Butcher0fc94e92015-09-28 20:52:04 +0100283 *olen = 0;
284
Hanno Becker261602c2017-04-12 14:54:42 +0100285 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
286 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200287
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100288 MBEDTLS_SSL_DEBUG_MSG( 3,
289 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200290
Hanno Becker261602c2017-04-12 14:54:42 +0100291 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100292
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100293 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
294 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200295
296 *p++ = 0x00;
297 *p++ = 1;
298
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200299 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200300
301 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100302
303 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200308MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100309static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
310 unsigned char *buf,
311 const unsigned char *end,
312 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100313{
314 unsigned char *p = buf;
315
Simon Butcher0fc94e92015-09-28 20:52:04 +0100316 *olen = 0;
317
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100318 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100319 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100320
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100321 MBEDTLS_SSL_DEBUG_MSG( 3,
322 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100323
Hanno Becker261602c2017-04-12 14:54:42 +0100324 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100325
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100326 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
327 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100328
329 *p++ = 0x00;
330 *p++ = 0x00;
331
332 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100333
334 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200339MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100340static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
341 unsigned char *buf,
342 const unsigned char *end,
343 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200344{
345 unsigned char *p = buf;
346
Simon Butcher0fc94e92015-09-28 20:52:04 +0100347 *olen = 0;
348
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100349 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100350 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200351
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100352 MBEDTLS_SSL_DEBUG_MSG( 3,
353 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200354
Hanno Becker261602c2017-04-12 14:54:42 +0100355 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100356
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100357 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
358 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200359
360 *p++ = 0x00;
361 *p++ = 0x00;
362
363 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100364
365 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200370MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100371static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
372 unsigned char *buf,
373 const unsigned char *end,
374 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200375{
376 unsigned char *p = buf;
377 size_t tlen = ssl->session_negotiate->ticket_len;
378
Simon Butcher0fc94e92015-09-28 20:52:04 +0100379 *olen = 0;
380
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200381 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100382 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200383
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100384 MBEDTLS_SSL_DEBUG_MSG( 3,
385 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200386
Hanno Becker261602c2017-04-12 14:54:42 +0100387 /* The addition is safe here since the ticket length is 16 bit. */
388 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100389
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100390 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
391 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100393 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
394 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200395
396 *olen = 4;
397
Simon Butchered997662015-09-28 02:14:30 +0100398 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100399 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100401 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000402 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200403
404 memcpy( p, ssl->session_negotiate->ticket, tlen );
405
406 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100407
408 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200411
Ron Eldora9788042018-12-05 11:04:31 +0200412#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200413MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascal77696ee2020-09-22 21:49:40 +0200414static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200415 unsigned char *buf,
416 const unsigned char *end,
417 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100418{
419 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200420 size_t protection_profiles_index = 0, ext_len = 0;
421 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100422
423 *olen = 0;
424
Johan Pascalc3ccd982020-10-28 17:18:18 +0100425 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
426 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
427 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200429 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100430 }
431
Ron Eldora9788042018-12-05 11:04:31 +0200432 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100433 * uint8 SRTPProtectionProfile[2];
434 *
435 * struct {
436 * SRTPProtectionProfiles SRTPProtectionProfiles;
437 * opaque srtp_mki<0..255>;
438 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100439 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100440 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200441 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200442 {
443 mki_len = ssl->dtls_srtp_info.mki_len;
444 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300445 /* Extension length = 2 bytes for profiles length,
446 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
447 * 1 byte for srtp_mki vector length and the mki_len value
448 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200449 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
450
Johan Pascal77696ee2020-09-22 21:49:40 +0200451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
452
453 /* Check there is room in the buffer for the extension + 4 bytes
454 * - the extension tag (2 bytes)
455 * - the extension length (2 bytes)
456 */
457 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
458
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100459 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
460 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200461
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100462 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
463 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100464
Ron Eldor3adb9922017-12-21 10:15:08 +0200465 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200466 /* micro-optimization:
467 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
468 * which is lower than 127, so the upper byte of the length is always 0
469 * For the documentation, the more generic code is left in comments
470 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
471 * >> 8 ) & 0xFF );
472 */
473 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100474 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100475
Ron Eldoref72faf2018-07-12 11:54:20 +0300476 for( protection_profiles_index=0;
477 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
478 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100479 {
Johan Pascal43f94902020-09-22 12:25:52 +0200480 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200481 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200482 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200483 {
484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
485 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100486 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
487 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200488 }
489 else
490 {
491 /*
492 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200493 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200494 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200495 MBEDTLS_SSL_DEBUG_MSG( 3,
496 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200497 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200498 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
499 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200500 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100501 }
502 }
503
Ron Eldor591f1622018-01-22 12:30:04 +0200504 *p++ = mki_len & 0xFF;
505
506 if( mki_len != 0 )
507 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200508 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200509 /*
510 * Increment p to point to the current position.
511 */
512 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300513 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
514 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200515 }
516
Ron Eldoref72faf2018-07-12 11:54:20 +0300517 /*
518 * total extension length: extension type (2 bytes)
519 * + extension length (2 bytes)
520 * + protection profile length (2 bytes)
521 * + 2 * number of protection profiles
522 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200523 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300524 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200525 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200526
527 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100528}
529#endif /* MBEDTLS_SSL_DTLS_SRTP */
530
Ronald Cron4079abc2022-02-20 10:35:26 +0100531int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
532 unsigned char *buf,
533 const unsigned char *end,
534 int uses_ec,
535 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100536{
537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
538 unsigned char *p = buf;
539 size_t ext_len = 0;
540
541 (void) ssl;
542 (void) end;
543 (void) uses_ec;
544 (void) ret;
545 (void) ext_len;
546
547 *out_len = 0;
548
549 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
550 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
551#if defined(MBEDTLS_SSL_RENEGOTIATION)
552 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
553 {
554 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
555 return( ret );
556 }
557 p += ext_len;
558#endif
559
560#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
561 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
562 if( uses_ec )
563 {
564 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
565 &ext_len ) ) != 0 )
566 {
567 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
568 return( ret );
569 }
570 p += ext_len;
571 }
572#endif
573
574#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
575 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
576 {
577 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
578 return( ret );
579 }
580 p += ext_len;
581#endif
582
583#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
584 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
585 {
586 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
587 return( ret );
588 }
589 p += ext_len;
590#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
591
592#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
593 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
594 &ext_len ) ) != 0 )
595 {
596 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
597 return( ret );
598 }
599 p += ext_len;
600#endif
601
602#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
603 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
604 {
605 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
606 return( ret );
607 }
608 p += ext_len;
609#endif
610
611#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
612 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
613 {
614 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
615 return( ret );
616 }
617 p += ext_len;
618#endif
619
620#if defined(MBEDTLS_SSL_DTLS_SRTP)
621 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
622 {
623 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
624 return( ret );
625 }
626 p += ext_len;
627#endif
628
629#if defined(MBEDTLS_SSL_SESSION_TICKETS)
630 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
631 {
632 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
633 return( ret );
634 }
635 p += ext_len;
636#endif
637
638 *out_len = p - buf;
639
640 return( 0 );
641}
642
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200643MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200645 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000646 size_t len )
647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#if defined(MBEDTLS_SSL_RENEGOTIATION)
649 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000650 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100651 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000652 if( len != 1 + ssl->verify_data_len * 2 ||
653 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200654 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100655 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200656 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100657 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100660 mbedtls_ssl_send_alert_message(
661 ssl,
662 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
663 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100664 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000665 }
666 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100667 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100669 {
670 if( len != 1 || buf[0] != 0x00 )
671 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200672 MBEDTLS_SSL_DEBUG_MSG( 1,
673 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100674 mbedtls_ssl_send_alert_message(
675 ssl,
676 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
677 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100678 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100679 }
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100682 }
Paul Bakker48916f92012-09-16 19:57:18 +0000683
684 return( 0 );
685}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200688MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200690 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200691 size_t len )
692{
693 /*
694 * server should use the extension only if we did,
695 * and if so the server's value should match ours (and len is always 1)
696 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200697 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200698 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200699 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200700 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100701 MBEDTLS_SSL_DEBUG_MSG( 1,
702 ( "non-matching max fragment length extension" ) );
703 mbedtls_ssl_send_alert_message(
704 ssl,
705 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100706 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
707 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200708 }
709
710 return( 0 );
711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000713
Hanno Beckera0e20d02019-05-15 14:03:01 +0100714#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200715MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera8373a12019-04-26 15:37:26 +0100716static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
717 const unsigned char *buf,
718 size_t len )
719{
720 size_t peer_cid_len;
721
722 if( /* CID extension only makes sense in DTLS */
723 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
724 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100725 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100726 {
Hanno Becker22626482019-05-03 12:46:59 +0100727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100729 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100730 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100731 }
732
733 if( len == 0 )
734 {
735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
736 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100737 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
738 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100739 }
740
741 peer_cid_len = *buf++;
742 len--;
743
744 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
745 {
Hanno Becker22626482019-05-03 12:46:59 +0100746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100748 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
749 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100750 }
751
752 if( len != peer_cid_len )
753 {
Hanno Becker22626482019-05-03 12:46:59 +0100754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100755 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100756 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
757 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100758 }
759
Hanno Becker5a299902019-05-03 12:47:49 +0100760 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100761 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
762 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
763
764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
765 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
766
Hanno Beckera8373a12019-04-26 15:37:26 +0100767 return( 0 );
768}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100769#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200772MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100774 const unsigned char *buf,
775 size_t len )
776{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200777 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100778 len != 0 )
779 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100780 MBEDTLS_SSL_DEBUG_MSG( 1,
781 ( "non-matching encrypt-then-MAC extension" ) );
782 mbedtls_ssl_send_alert_message(
783 ssl,
784 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100785 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100786 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100787 }
788
789 ((void) buf);
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100792
793 return( 0 );
794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200798MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200800 const unsigned char *buf,
801 size_t len )
802{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200803 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200804 len != 0 )
805 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100806 MBEDTLS_SSL_DEBUG_MSG( 1,
807 ( "non-matching extended master secret extension" ) );
808 mbedtls_ssl_send_alert_message(
809 ssl,
810 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100811 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
812 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200813 }
814
815 ((void) buf);
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200818
819 return( 0 );
820}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200824MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200826 const unsigned char *buf,
827 size_t len )
828{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200829 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200830 len != 0 )
831 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100832 MBEDTLS_SSL_DEBUG_MSG( 1,
833 ( "non-matching session ticket extension" ) );
834 mbedtls_ssl_send_alert_message(
835 ssl,
836 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100837 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
838 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200839 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200840
841 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200842
843 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200844
845 return( 0 );
846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200848
Robert Cragie136884c2015-10-02 13:34:31 +0100849#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100850 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200851MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200853 const unsigned char *buf,
854 size_t len )
855{
856 size_t list_size;
857 const unsigned char *p;
858
Philippe Antoine747fd532018-05-30 09:13:21 +0200859 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200862 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
863 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100864 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200865 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200866 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200867
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200868 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200869 while( list_size > 0 )
870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
872 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200873 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200874#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
875 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200876 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200877#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
878 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100879#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200880 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
881 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200884 return( 0 );
885 }
886
887 list_size--;
888 p++;
889 }
890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200892 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
893 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100894 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200895}
Darryl Green11999bb2018-03-13 15:22:58 +0000896#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100897 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200898
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200899#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200900MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200901static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
902 const unsigned char *buf,
903 size_t len )
904{
Janos Follath865b3eb2019-12-16 11:46:15 +0000905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906
Hanno Beckere694c3e2017-12-27 21:34:08 +0000907 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200908 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
909 {
910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
911 return( 0 );
912 }
913
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200914 /* If we got here, we no longer need our cached extension */
915 mbedtls_free( ssl->handshake->ecjpake_cache );
916 ssl->handshake->ecjpake_cache = NULL;
917 ssl->handshake->ecjpake_cache_len = 0;
918
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200919 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
920 buf, len ) ) != 0 )
921 {
922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100923 mbedtls_ssl_send_alert_message(
924 ssl,
925 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
926 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200927 return( ret );
928 }
929
930 return( 0 );
931}
932#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200935MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200937 const unsigned char *buf, size_t len )
938{
939 size_t list_len, name_len;
940 const char **p;
941
942 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200943 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200944 {
945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100946 mbedtls_ssl_send_alert_message(
947 ssl,
948 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100949 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
950 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200951 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200952
953 /*
954 * opaque ProtocolName<1..2^8-1>;
955 *
956 * struct {
957 * ProtocolName protocol_name_list<2..2^16-1>
958 * } ProtocolNameList;
959 *
960 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
961 */
962
963 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
964 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200965 {
966 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
967 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100968 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200969 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200970
971 list_len = ( buf[0] << 8 ) | buf[1];
972 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200973 {
974 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
975 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100976 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200977 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200978
979 name_len = buf[2];
980 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200981 {
982 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
983 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100984 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200985 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200986
987 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200988 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200989 {
990 if( name_len == strlen( *p ) &&
991 memcmp( buf + 3, *p, name_len ) == 0 )
992 {
993 ssl->alpn_chosen = *p;
994 return( 0 );
995 }
996 }
997
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200999 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1000 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001001 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001004
Johan Pascalb62bb512015-12-03 21:56:45 +01001005#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001006MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascalb62bb512015-12-03 21:56:45 +01001007static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001008 const unsigned char *buf,
1009 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001010{
Johan Pascal43f94902020-09-22 12:25:52 +02001011 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001012 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001013 uint16_t server_protection_profile_value = 0;
1014
1015 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001016 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1017 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1018 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001019 return( 0 );
1020
Ron Eldora9788042018-12-05 11:04:31 +02001021 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001022 * uint8 SRTPProtectionProfile[2];
1023 *
1024 * struct {
1025 * SRTPProtectionProfiles SRTPProtectionProfiles;
1026 * opaque srtp_mki<0..255>;
1027 * } UseSRTPData;
1028
1029 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1030 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001031 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001032 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001033 {
1034 mki_len = ssl->dtls_srtp_info.mki_len;
1035 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001036
Ron Eldoref72faf2018-07-12 11:54:20 +03001037 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001038 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1039 * + protection profile (2 bytes)
1040 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001041 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001042 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001043 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001044 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001045
1046 /*
1047 * get the server protection profile
1048 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001049
1050 /*
1051 * protection profile length must be 0x0002 as we must have only
1052 * one protection profile in server Hello
1053 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001054 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001055 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001056
1057 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001058 server_protection = mbedtls_ssl_check_srtp_profile_value(
1059 server_protection_profile_value );
1060 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001061 {
Johan Pascal43f94902020-09-22 12:25:52 +02001062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1063 mbedtls_ssl_get_srtp_profile_as_string(
1064 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001065 }
1066
Johan Pascal43f94902020-09-22 12:25:52 +02001067 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001068
Johan Pascalb62bb512015-12-03 21:56:45 +01001069 /*
1070 * Check we have the server profile in our list
1071 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001072 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001073 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001074 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1075 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001076 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1078 mbedtls_ssl_get_srtp_profile_as_string(
1079 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001080 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001081 }
1082 }
1083
Ron Eldor591f1622018-01-22 12:30:04 +02001084 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001085 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001086 {
1087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001088 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001089 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001090 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001091
1092 /* If server does not use mki in its reply, make sure the client won't keep
1093 * one as negotiated */
1094 if( len == 5 )
1095 {
1096 ssl->dtls_srtp_info.mki_len = 0;
1097 }
1098
Ron Eldoref72faf2018-07-12 11:54:20 +03001099 /*
1100 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001101 * If the client detects a nonzero-length MKI in the server's response
1102 * that is different than the one the client offered, then the client
1103 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1104 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001105 if( len > 5 && ( buf[4] != mki_len ||
1106 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001107 {
1108 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1109 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001110 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001111 }
Ron Eldorb4655392018-07-05 18:25:39 +03001112#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001113 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001114 {
Ron Eldora9788042018-12-05 11:04:31 +02001115 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1116 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001117 }
1118#endif
Ron Eldora9788042018-12-05 11:04:31 +02001119 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001120}
1121#endif /* MBEDTLS_SSL_DTLS_SRTP */
1122
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001123/*
1124 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001127MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001131 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001132 unsigned char cookie_len;
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001135
Gilles Peskineb64bf062019-09-27 14:02:44 +02001136 /* Check that there is enough room for:
1137 * - 2 bytes of version
1138 * - 1 byte of cookie_len
1139 */
1140 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1141 {
1142 MBEDTLS_SSL_DEBUG_MSG( 1,
1143 ( "incoming HelloVerifyRequest message is too short" ) );
1144 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1145 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001146 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001147 }
1148
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001149 /*
1150 * struct {
1151 * ProtocolVersion server_version;
1152 * opaque cookie<0..2^8-1>;
1153 * } HelloVerifyRequest;
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001156 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001157 p += 2;
1158
TRodziewicz2d8800e2021-05-13 19:14:19 +02001159 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001160 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1161 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1162 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001163 */
Glenn Strauss83158112022-04-13 14:59:34 -04001164 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1169 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001170
Hanno Beckerbc000442021-06-24 09:18:19 +01001171 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001172 }
1173
1174 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001175 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1176 {
1177 MBEDTLS_SSL_DEBUG_MSG( 1,
1178 ( "cookie length does not match incoming message size" ) );
1179 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1180 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001181 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001182 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001183 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001184
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001185 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001186
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001187 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1188 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001189 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001191 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001192 }
1193
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001194 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001195 ssl->handshake->verify_cookie_len = cookie_len;
1196
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001197 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1199 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001204
1205 return( 0 );
1206}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001208
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001209MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001212 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001213 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001214 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001215 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001216 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001218 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001219#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001220 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001224
Hanno Becker327c93b2018-08-15 13:56:18 +01001225 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001227 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 return( ret );
1230 }
1231
Hanno Becker79594fd2019-05-08 09:38:41 +01001232 buf = ssl->in_msg;
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_SSL_RENEGOTIATION)
1237 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001238 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001239 ssl->renego_records_seen++;
1240
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001241 if( ssl->conf->renego_max_records >= 0 &&
1242 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001243 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001244 MBEDTLS_SSL_DEBUG_MSG( 1,
1245 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001247 }
1248
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001249 MBEDTLS_SSL_DEBUG_MSG( 1,
1250 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001251
1252 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001254 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001258 mbedtls_ssl_send_alert_message(
1259 ssl,
1260 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1261 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 }
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001266 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001272 return( ssl_parse_hello_verify_request( ssl ) );
1273 }
1274 else
1275 {
1276 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001277 mbedtls_free( ssl->handshake->cookie );
1278 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001279 ssl->handshake->verify_cookie_len = 0;
1280 }
1281 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1285 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001288 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1289 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001290 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
1292
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001293 /*
1294 * 0 . 1 server_version
1295 * 2 . 33 random (maybe including 4 bytes of Unix time)
1296 * 34 . 34 session_id length = n
1297 * 35 . 34+n session_id
1298 * 35+n . 36+n cipher_suite
1299 * 37+n . 37+n compression_method
1300 *
1301 * 38+n . 39+n extensions length (optional)
1302 * 40+n . .. extensions
1303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001305
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001306 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1307 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001308 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Glenn Strauss60bfe602022-03-14 19:04:24 -04001310 if( ssl->tls_version < ssl->conf->min_tls_version ||
1311 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001312 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001313 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001314 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1315 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001316 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001317 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1320 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001321
Hanno Beckerbc000442021-06-24 09:18:19 +01001322 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001323 }
1324
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001326 ( (unsigned long) buf[2] << 24 ) |
1327 ( (unsigned long) buf[3] << 16 ) |
1328 ( (unsigned long) buf[4] << 8 ) |
1329 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001331 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001333 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Paul Bakker48916f92012-09-16 19:57:18 +00001337 if( n > 32 )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001340 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1341 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001342 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001343 }
1344
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001345 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001347 ext_len = ( ( buf[38 + n] << 8 )
1348 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Paul Bakker48916f92012-09-16 19:57:18 +00001350 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001354 mbedtls_ssl_send_alert_message(
1355 ssl,
1356 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1357 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001358 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001359 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001361 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001362 {
1363 ext_len = 0;
1364 }
1365 else
1366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001368 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1369 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001370 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001373 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001374 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001375
1376 /*
1377 * Read and check compression
1378 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001379 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001381 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001382 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001383 MBEDTLS_SSL_DEBUG_MSG( 1,
1384 ( "server hello, bad compression: %d", comp ) );
1385 mbedtls_ssl_send_alert_message(
1386 ssl,
1387 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1388 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001390 }
1391
Paul Bakker380da532012-04-18 16:10:25 +00001392 /*
1393 * Initialize update checksum functions
1394 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001395 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1396 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001397 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001398 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001399 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001400 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1401 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001403 }
Paul Bakker380da532012-04-18 16:10:25 +00001404
Hanno Beckere694c3e2017-12-27 21:34:08 +00001405 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001406
Paul Elliottd48d5c62021-01-07 14:47:05 +00001407 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
1410 /*
1411 * Check if the session can be resumed
1412 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001413 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_RENEGOTIATION)
1415 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001416#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001417 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001418 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001419 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 {
1421 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001422 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001424 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001425#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001426 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001427 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001428 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 }
1430 else
1431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 }
1434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001436 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Paul Elliott3891caf2020-12-17 18:42:40 +00001438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1440 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Andrzej Kurek03bac442018-04-25 05:06:07 -04001442 /*
1443 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001444 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 i = 0;
1446 while( 1 )
1447 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001448 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001451 mbedtls_ssl_send_alert_message(
1452 ssl,
1453 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1454 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001455 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 }
1457
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001458 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001459 ssl->session_negotiate->ciphersuite )
1460 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001462 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 }
1464
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001465 suite_info = mbedtls_ssl_ciphersuite_from_id(
1466 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001467 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1468 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001469 {
1470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001471 mbedtls_ssl_send_alert_message(
1472 ssl,
1473 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001474 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1475 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001476 }
1477
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001478 MBEDTLS_SSL_DEBUG_MSG( 3,
1479 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001480
Gilles Peskineeccd8882020-03-10 12:19:08 +01001481#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001482 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001483 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001484 {
1485 ssl->handshake->ecrs_enabled = 1;
1486 }
1487#endif
1488
Thomas Daubney20f89a92022-06-20 15:12:19 +01001489 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001492 mbedtls_ssl_send_alert_message(
1493 ssl,
1494 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1495 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001496 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 }
1498
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001499 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001500
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001501 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001502 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001503
Paul Bakker48916f92012-09-16 19:57:18 +00001504 while( ext_len )
1505 {
1506 unsigned int ext_id = ( ( ext[0] << 8 )
1507 | ( ext[1] ) );
1508 unsigned int ext_size = ( ( ext[2] << 8 )
1509 | ( ext[3] ) );
1510
1511 if( ext_size + 4 > ext_len )
1512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001514 mbedtls_ssl_send_alert_message(
1515 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1516 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001517 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001518 }
1519
1520 switch( ext_id )
1521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1524#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001525 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001526#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001527
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001528 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1529 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001530 return( ret );
1531
1532 break;
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1535 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001536 MBEDTLS_SSL_DEBUG_MSG( 3,
1537 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001538
1539 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1540 ext + 4, ext_size ) ) != 0 )
1541 {
1542 return( ret );
1543 }
1544
1545 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001547
Hanno Beckera0e20d02019-05-15 14:03:01 +01001548#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001549 case MBEDTLS_TLS_EXT_CID:
1550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1551
1552 if( ( ret = ssl_parse_cid_ext( ssl,
1553 ext + 4,
1554 ext_size ) ) != 0 )
1555 {
1556 return( ret );
1557 }
1558
1559 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001560#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1563 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001565
1566 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1567 ext + 4, ext_size ) ) != 0 )
1568 {
1569 return( ret );
1570 }
1571
1572 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1576 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001577 MBEDTLS_SSL_DEBUG_MSG( 3,
1578 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001579
1580 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1581 ext + 4, ext_size ) ) != 0 )
1582 {
1583 return( ret );
1584 }
1585
1586 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1590 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1591 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001592
1593 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1594 ext + 4, ext_size ) ) != 0 )
1595 {
1596 return( ret );
1597 }
1598
1599 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001601
Robert Cragie136884c2015-10-02 13:34:31 +01001602#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001603 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001605 MBEDTLS_SSL_DEBUG_MSG( 3,
1606 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001607
1608 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1609 ext + 4, ext_size ) ) != 0 )
1610 {
1611 return( ret );
1612 }
1613
1614 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001615#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1616 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001617
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001618#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1619 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1621
1622 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1623 ext + 4, ext_size ) ) != 0 )
1624 {
1625 return( ret );
1626 }
1627
1628 break;
1629#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_ALPN)
1632 case MBEDTLS_TLS_EXT_ALPN:
1633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001634
Johan Pascal275874b2020-10-27 10:43:53 +01001635 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1636 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001637
1638 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001640
Johan Pascalb62bb512015-12-03 21:56:45 +01001641#if defined(MBEDTLS_SSL_DTLS_SRTP)
1642 case MBEDTLS_TLS_EXT_USE_SRTP:
1643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1644
Johan Pascalc3ccd982020-10-28 17:18:18 +01001645 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1646 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001647
1648 break;
1649#endif /* MBEDTLS_SSL_DTLS_SRTP */
1650
Paul Bakker48916f92012-09-16 19:57:18 +00001651 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001652 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001653 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001654 }
1655
1656 ext_len -= 4 + ext_size;
1657 ext += 4 + ext_size;
1658
1659 if( ext_len > 0 && ext_len < 4 )
1660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001662 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001663 }
1664 }
1665
1666 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001667 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1668 * extensions. It sets the transform data for the resumed session which in
1669 * case of DTLS includes the server CID extracted from the CID extension.
1670 */
1671 if( ssl->handshake->resume )
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001672 {
1673 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
1674 {
1675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
1676 mbedtls_ssl_send_alert_message(
1677 ssl,
1678 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1679 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
1680 return( ret );
1681 }
1682 }
1683
Paul Bakker48916f92012-09-16 19:57:18 +00001684 /*
1685 * Renegotiation security checks
1686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001688 ssl->conf->allow_legacy_renegotiation ==
1689 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001690 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001691 MBEDTLS_SSL_DEBUG_MSG( 1,
1692 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001693 handshake_failure = 1;
1694 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#if defined(MBEDTLS_SSL_RENEGOTIATION)
1696 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1697 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001698 renegotiation_info_seen == 0 )
1699 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001700 MBEDTLS_SSL_DEBUG_MSG( 1,
1701 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001702 handshake_failure = 1;
1703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1705 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001706 ssl->conf->allow_legacy_renegotiation ==
1707 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001710 handshake_failure = 1;
1711 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1713 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001714 renegotiation_info_seen == 1 )
1715 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001716 MBEDTLS_SSL_DEBUG_MSG( 1,
1717 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001718 handshake_failure = 1;
1719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001721
1722 if( handshake_failure == 1 )
1723 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001724 mbedtls_ssl_send_alert_message(
1725 ssl,
1726 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1727 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001728 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
1733 return( 0 );
1734}
1735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1737 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001738MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001739static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1740 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001741 unsigned char *end )
1742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001744 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001745
Paul Bakker29e1f122013-04-16 13:07:56 +02001746 /*
1747 * Ephemeral DH parameters:
1748 *
1749 * struct {
1750 * opaque dh_p<1..2^16-1>;
1751 * opaque dh_g<1..2^16-1>;
1752 * opaque dh_Ys<1..2^16-1>;
1753 * } ServerDHParams;
1754 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001755 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1756 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001759 return( ret );
1760 }
1761
Gilles Peskine487bbf62021-05-27 22:17:07 +02001762 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001763 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001764 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001766 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001767 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001768 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001769 }
1770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1772 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1773 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001774
1775 return( ret );
1776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1778 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001779
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001780#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1781 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1782 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1783#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001784MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001785static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001786 unsigned char **p,
1787 unsigned char *end )
1788{
1789 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001790 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001791 uint8_t ecpoint_len;
1792 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1793
1794 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001795 * struct {
1796 * ECParameters curve_params;
1797 * ECPoint public;
1798 * } ServerECDHParams;
1799 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001800 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001801 * 2..3 NamedCurve
1802 * 4 ECPoint.len
1803 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001804 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001805 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001806 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001807
1808 /* First byte is curve_type; only named_curve is handled */
1809 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001810 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001811
1812 /* Next two bytes are the namedcurve value */
1813 tls_id = *(*p)++;
1814 tls_id <<= 8;
1815 tls_id |= *(*p)++;
1816
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001817 /* Check it's a curve we offered */
1818 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001819 {
1820 MBEDTLS_SSL_DEBUG_MSG( 2,
1821 ( "bad server key exchange message (ECDHE curve): %u",
1822 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001823 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001824 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001825
Hanno Beckerbb89e272019-01-08 12:54:37 +00001826 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001827 if( ( handshake->ecdh_psa_type =
1828 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001829 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001830 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001831 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001832 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001833
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001834 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001835 ecpoint_len = *(*p)++;
1836 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001837 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001839 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1840 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001841
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001842 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1843 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001844 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001845
Hanno Beckerbb89e272019-01-08 12:54:37 +00001846 return( 0 );
1847}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001848#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001849MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrong1f198d82022-04-13 15:02:30 +02001850static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1851{
1852 const mbedtls_ecp_curve_info *curve_info;
1853 mbedtls_ecp_group_id grp_id;
1854#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1855 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1856#else
1857 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1858#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001859
Neil Armstrong1f198d82022-04-13 15:02:30 +02001860 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1861 if( curve_info == NULL )
1862 {
1863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1864 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1865 }
1866
1867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1868
1869 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1870 return( -1 );
1871
1872 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1873 MBEDTLS_DEBUG_ECDH_QP );
1874
1875 return( 0 );
1876}
1877
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001878MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001880 unsigned char **p,
1881 unsigned char *end )
1882{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001884
Paul Bakker29e1f122013-04-16 13:07:56 +02001885 /*
1886 * Ephemeral ECDH parameters:
1887 *
1888 * struct {
1889 * ECParameters curve_params;
1890 * ECPoint public;
1891 * } ServerECDHParams;
1892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001894 (const unsigned char **) p, end ) ) != 0 )
1895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001897#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001898 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1899 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001900#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001901 return( ret );
1902 }
1903
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001904 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001905 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001906 MBEDTLS_SSL_DEBUG_MSG( 1,
1907 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001908 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001909 }
1910
Paul Bakker29e1f122013-04-16 13:07:56 +02001911 return( ret );
1912}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001913#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1915 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1916 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001917
Gilles Peskineeccd8882020-03-10 12:19:08 +01001918#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001919MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001921 unsigned char **p,
1922 unsigned char *end )
1923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001925 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001926 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001927
1928 /*
1929 * PSK parameters:
1930 *
1931 * opaque psk_identity_hint<0..2^16-1>;
1932 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001933 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001934 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001935 MBEDTLS_SSL_DEBUG_MSG( 1,
1936 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001937 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001938 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001939 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001940 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941
irwir6527bd62019-09-21 18:51:25 +03001942 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001943 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001944 MBEDTLS_SSL_DEBUG_MSG( 1,
1945 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001946 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001947 }
1948
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001949 /*
1950 * Note: we currently ignore the PKS identity hint, as we only allow one
1951 * PSK to be provisionned on the client. This could be changed later if
1952 * someone needs that feature.
1953 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001954 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001955 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001956
1957 return( ret );
1958}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001959#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1962 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001963/*
1964 * Generate a pre-master secret and encrypt it with the server's RSA key
1965 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001966MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001968 size_t offset, size_t *olen,
1969 size_t pms_offset )
1970{
Janos Follath865b3eb2019-12-16 11:46:15 +00001971 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001972 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001973 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001974 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001975
Angus Grattond8213d02016-05-25 20:56:48 +10001976 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001977 {
1978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1979 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1980 }
1981
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001982 /*
1983 * Generate (part of) the pre-master as
1984 * struct {
1985 * ProtocolVersion client_version;
1986 * opaque random[46];
1987 * } PreMasterSecret;
1988 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001989 mbedtls_ssl_write_version( p, ssl->conf->transport,
1990 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001991
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001992 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001995 return( ret );
1996 }
1997
1998 ssl->handshake->pmslen = 48;
1999
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002000#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2001 peer_pk = &ssl->handshake->peer_pubkey;
2002#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002003 if( ssl->session_negotiate->peer_cert == NULL )
2004 {
Hanno Becker8273df82019-02-06 17:37:32 +00002005 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002008 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002009 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2010#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002011
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002012 /*
2013 * Now write it out, encrypted
2014 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002015 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2018 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002019 }
2020
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002021 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002022 p, ssl->handshake->pmslen,
2023 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002024 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002025 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028 return( ret );
2029 }
2030
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002031 if( len_bytes == 2 )
2032 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002033 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002034 *olen += 2;
2035 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002036
Hanno Beckerae553dd2019-02-08 14:06:00 +00002037#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2038 /* We don't need the peer's public key anymore. Free it. */
2039 mbedtls_pk_free( peer_pk );
2040#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002041 return( 0 );
2042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2044 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2047 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002048MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002050{
Janos Follath865b3eb2019-12-16 11:46:15 +00002051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002053 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002054
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002055#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2056 peer_pk = &ssl->handshake->peer_pubkey;
2057#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002058 if( ssl->session_negotiate->peer_cert == NULL )
2059 {
Hanno Becker8273df82019-02-06 17:37:32 +00002060 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002063 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002064 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2065#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002066
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002067 /* This is a public key, so it can't be opaque, so can_do() is a good
2068 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002069 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2072 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002073 }
2074
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002075 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002076
Przemek Stekielea4000f2022-03-16 09:49:33 +01002077#if defined(MBEDTLS_USE_PSA_CRYPTO)
2078 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002079 size_t olen = 0;
2080
2081 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2082 {
2083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2084 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2085 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002086
2087 ssl->handshake->ecdh_psa_type =
2088 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2089 &ecdh_bits ) );
2090
2091 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2092 {
2093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2094 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2095 }
2096
2097 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2098
Przemek Stekielea4000f2022-03-16 09:49:33 +01002099 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002100 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2101 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2102 ssl->handshake->ecdh_psa_peerkey,
2103 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002104
Przemek Stekiel561a4232022-03-16 13:16:24 +01002105 if ( ret != 0 )
2106 {
2107 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2108 return( ret );
2109 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002110
Przemek Stekiel561a4232022-03-16 13:16:24 +01002111 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002112#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2114 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002117 return( ret );
2118 }
2119
2120 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002123 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002124 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002125#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002126#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2127 /* We don't need the peer's public key anymore. Free it,
2128 * so that more RAM is available for upcoming expensive
2129 * operations like ECDHE. */
2130 mbedtls_pk_free( peer_pk );
2131#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2132
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002133 return( ret );
2134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2136 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002137
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002138MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002140{
Janos Follath865b3eb2019-12-16 11:46:15 +00002141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002142 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002143 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002144 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2149 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 ssl->state++;
2153 return( 0 );
2154 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002155 ((void) p);
2156 ((void) end);
2157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2160 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2161 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2162 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002163 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002164 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002167 mbedtls_ssl_send_alert_message(
2168 ssl,
2169 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2170 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002171 return( ret );
2172 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002175 ssl->state++;
2176 return( 0 );
2177 }
2178 ((void) p);
2179 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2181 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002182
Gilles Peskineeccd8882020-03-10 12:19:08 +01002183#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002184 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002185 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002186 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002187 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002188 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002189#endif
2190
Hanno Becker327c93b2018-08-15 13:56:18 +01002191 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 return( ret );
2195 }
2196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002200 mbedtls_ssl_send_alert_message(
2201 ssl,
2202 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2203 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 }
2206
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002207 /*
2208 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2209 * doesn't use a psk_identity_hint
2210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2214 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002215 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002216 /* Current message is probably either
2217 * CertificateRequest or ServerHelloDone */
2218 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002219 goto exit;
2220 }
2221
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002222 MBEDTLS_SSL_DEBUG_MSG( 1,
2223 ( "server key exchange message must not be skipped" ) );
2224 mbedtls_ssl_send_alert_message(
2225 ssl,
2226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2227 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
Gilles Peskineeccd8882020-03-10 12:19:08 +01002232#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002233 if( ssl->handshake->ecrs_enabled )
2234 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2235
2236start_processing:
2237#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002239 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002241
Gilles Peskineeccd8882020-03-10 12:19:08 +01002242#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2244 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2245 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2246 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002247 {
2248 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002251 mbedtls_ssl_send_alert_message(
2252 ssl,
2253 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002255 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002256 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002257 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002258#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2261 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2262 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2263 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002264 ; /* nothing more to do */
2265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2267 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2268#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2269 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2270 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2271 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002273 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002276 mbedtls_ssl_send_alert_message(
2277 ssl,
2278 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2279 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002280 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002281 }
2282 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002283 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2285 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002286#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2287 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2289 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2290 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2291 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002292 {
2293 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002296 mbedtls_ssl_send_alert_message(
2297 ssl,
2298 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2299 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002300 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002301 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002302 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002303 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2305 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2306 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002307#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2308 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2309 {
2310 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2311 p, end - p );
2312 if( ret != 0 )
2313 {
2314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002315 mbedtls_ssl_send_alert_message(
2316 ssl,
2317 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002318 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2319 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002320 }
2321 }
2322 else
2323#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002328
Gilles Peskineeccd8882020-03-10 12:19:08 +01002329#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002330 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002331 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002332 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002333 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2336 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2337 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002338 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002339 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002340 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002341
Hanno Beckera6899bb2019-02-06 18:26:03 +00002342 mbedtls_pk_context * peer_pk;
2343
Jerry Yu693a47a2022-06-23 14:02:28 +08002344#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2345 peer_pk = &ssl->handshake->peer_pubkey;
2346#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2347 if( ssl->session_negotiate->peer_cert == NULL )
2348 {
2349 /* Should never happen */
2350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2352 }
2353 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2354#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2355
Paul Bakker29e1f122013-04-16 13:07:56 +02002356 /*
2357 * Handle the digitally-signed structure
2358 */
Jerry Yu693a47a2022-06-23 14:02:28 +08002359 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2360 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yu95b743c2022-07-23 11:37:50 +08002361 if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuc3bf7482022-07-29 10:27:17 +08002362 sig_alg, &pk_alg, &md_alg ) != 0 &&
Jerry Yu693a47a2022-06-23 14:02:28 +08002363 ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) &&
2364 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002365 {
Ronald Cron90915f22022-03-07 11:11:36 +01002366 MBEDTLS_SSL_DEBUG_MSG( 1,
2367 ( "bad server key exchange message" ) );
2368 mbedtls_ssl_send_alert_message(
2369 ssl,
2370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2371 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2372 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002373 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002374 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002375
Jerry Yu693a47a2022-06-23 14:02:28 +08002376 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002377 {
Ronald Cron90915f22022-03-07 11:11:36 +01002378 MBEDTLS_SSL_DEBUG_MSG( 1,
2379 ( "bad server key exchange message" ) );
2380 mbedtls_ssl_send_alert_message(
2381 ssl,
2382 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2383 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2384 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002385 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002386
2387 /*
2388 * Read signature
2389 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002390
2391 if( p > end - 2 )
2392 {
2393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002394 mbedtls_ssl_send_alert_message(
2395 ssl,
2396 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2397 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002398 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002399 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002400 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002401 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002402
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002403 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002406 mbedtls_ssl_send_alert_message(
2407 ssl,
2408 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2409 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002410 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002414
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002415 /*
2416 * Compute the hash that has been signed
2417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002419 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002420 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2421 params, params_len,
2422 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002423 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002424 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002425 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002426 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002430 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002431
Gilles Peskineca1d7422018-04-24 11:53:22 +02002432 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002433
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002434 /*
2435 * Verify signature
2436 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002437 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002440 mbedtls_ssl_send_alert_message(
2441 ssl,
2442 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2443 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002445 }
2446
Gilles Peskineeccd8882020-03-10 12:19:08 +01002447#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002448 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002449 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002450#endif
2451
Jerry Yu693a47a2022-06-23 14:02:28 +08002452#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2453 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
2454 {
Jerry Yu693a47a2022-06-23 14:02:28 +08002455 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2456 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002457 rsassa_pss_options.expected_salt_len =
2458 mbedtls_hash_info_get_size( md_alg );
2459 if( rsassa_pss_options.expected_salt_len == 0 )
Jerry Yu693a47a2022-06-23 14:02:28 +08002460 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002461
Jerry Yu693a47a2022-06-23 14:02:28 +08002462 ret = mbedtls_pk_verify_ext( pk_alg, &rsassa_pss_options,
2463 peer_pk,
2464 md_alg, hash, hashlen,
2465 p, sig_len );
2466 }
2467 else
2468#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2469 ret = mbedtls_pk_verify_restartable( peer_pk,
2470 md_alg, hash, hashlen, p, sig_len, rs_ctx );
2471
2472 if( ret != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002473 {
David Horstmannb21bbef2022-10-06 17:49:31 +01002474 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002475#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
David Horstmannb21bbef2022-10-06 17:49:31 +01002476 send_alert_msg = ( ret != MBEDTLS_ERR_ECP_IN_PROGRESS );
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002477#endif
David Horstmannb21bbef2022-10-06 17:49:31 +01002478 if( send_alert_msg )
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002479 mbedtls_ssl_send_alert_message(
2480 ssl,
2481 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2482 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002484#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002485 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2486 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2487#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002488 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002489 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002490
2491#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2492 /* We don't need the peer's public key anymore. Free it,
2493 * so that more RAM is available for upcoming expensive
2494 * operations like ECDHE. */
2495 mbedtls_pk_free( peer_pk );
2496#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002498#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002500exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 ssl->state++;
2502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
2505 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506}
2507
Gilles Peskineeccd8882020-03-10 12:19:08 +01002508#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002509MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002511{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002512 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002513 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002516
Hanno Becker1aa267c2017-04-28 17:08:27 +01002517 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002520 ssl->state++;
2521 return( 0 );
2522 }
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002526}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002527#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002528MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002530{
Janos Follath865b3eb2019-12-16 11:46:15 +00002531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002532 unsigned char *buf;
2533 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002534 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002535 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002536 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002537 size_t sig_alg_len;
2538#if defined(MBEDTLS_DEBUG_C)
2539 unsigned char *sig_alg;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002540 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002541#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
Hanno Becker1aa267c2017-04-28 17:08:27 +01002545 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002548 ssl->state++;
2549 return( 0 );
2550 }
2551
Hanno Becker327c93b2018-08-15 13:56:18 +01002552 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2555 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 }
2557
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002558 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2559 {
2560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002561 mbedtls_ssl_send_alert_message(
2562 ssl,
2563 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2564 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002565 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002568 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002569 ssl->handshake->client_auth =
2570 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002573 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Jerry Yufb28b882022-01-28 11:05:58 +08002575 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002576 {
Johan Pascala89ca862020-08-25 10:03:19 +02002577 /* Current message is probably the ServerHelloDone */
2578 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002579 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002580 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002581
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002582 /*
2583 * struct {
2584 * ClientCertificateType certificate_types<1..2^8-1>;
2585 * SignatureAndHashAlgorithm
2586 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2587 * DistinguishedName certificate_authorities<0..2^16-1>;
2588 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002589 *
2590 * Since we only support a single certificate on clients, let's just
2591 * ignore all the information that's supposed to help us pick a
2592 * certificate.
2593 *
2594 * We could check that our certificate matches the request, and bail out
2595 * if it doesn't, but it's simpler to just send the certificate anyway,
2596 * and give the server the opportunity to decide if it should terminate
2597 * the connection when it doesn't like our certificate.
2598 *
2599 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2600 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002601 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002602 *
2603 * However, we still minimally parse the message to check it is at least
2604 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002605 */
Paul Bakker926af752012-11-23 13:38:07 +01002606 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002607
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002608 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002609 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2610 {
2611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2613 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002614 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002615 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002617 n = cert_type_len;
2618
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002619 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002620 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002621 * * the length of the signature algorithms field (if minor version of
2622 * SSL is 3),
2623 * * distinguished name length otherwise.
2624 * Both reach at most the index:
2625 * ...hdr_len + 2 + n,
2626 * therefore the buffer length at this point must be greater than that
2627 * regardless of the actual code path.
2628 */
2629 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002632 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2633 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002634 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002635 }
2636
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002637 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002638 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2639 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2640
2641 /*
2642 * The furthest access in buf is in the loop few lines below:
2643 * sig_alg[i + 1],
2644 * where:
2645 * sig_alg = buf + ...hdr_len + 3 + n,
2646 * max(i) = sig_alg_len - 1.
2647 * Therefore the furthest access is:
2648 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2649 * which reduces to:
2650 * buf[...hdr_len + 3 + n + sig_alg_len],
2651 * which is one less than we need the buf to be.
2652 */
2653 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002654 {
Ronald Cron90915f22022-03-07 11:11:36 +01002655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2656 mbedtls_ssl_send_alert_message(
2657 ssl,
2658 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2659 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2660 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002661 }
Paul Bakker926af752012-11-23 13:38:07 +01002662
Ronald Cron90915f22022-03-07 11:11:36 +01002663#if defined(MBEDTLS_DEBUG_C)
2664 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2665 for( size_t i = 0; i < sig_alg_len; i += 2 )
2666 {
2667 MBEDTLS_SSL_DEBUG_MSG( 3,
2668 ( "Supported Signature Algorithm found: %d,%d",
2669 sig_alg[i], sig_alg[i + 1] ) );
2670 }
2671#endif
2672
2673 n += 2 + sig_alg_len;
2674
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002675 /* certificate_authorities */
2676 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2677 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002678
2679 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002680 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002683 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2684 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002685 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002686 }
2687
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002688#if defined(MBEDTLS_DEBUG_C)
2689 dn = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n - dn_len;
2690 for( size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len )
2691 {
2692 unsigned char *p = dn + i + 2;
2693 mbedtls_x509_name name;
2694 mbedtls_x509_name *name_cur, *name_prv;
2695 size_t asn1_len;
2696 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
2697 memset( &name, 0, sizeof( name ) );
2698 dni_len = MBEDTLS_GET_UINT16_BE( dn + i, 0 );
2699 if( dni_len > dn_len - i - 2 ||
2700 mbedtls_asn1_get_tag( &p, p + dni_len, &asn1_len,
2701 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) != 0 ||
2702 mbedtls_x509_get_name( &p, p + asn1_len, &name ) != 0 )
2703 {
2704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2705 mbedtls_ssl_send_alert_message(
2706 ssl,
2707 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2708 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2709 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
2710 }
2711 MBEDTLS_SSL_DEBUG_MSG( 3,
2712 ( "DN hint: %.*s",
2713 mbedtls_x509_dn_gets( s, sizeof(s), &name ), s ) );
2714 name_cur = name.next;
2715 while( name_cur != NULL )
2716 {
2717 name_prv = name_cur;
2718 name_cur = name_cur->next;
2719 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2720 mbedtls_free( name_prv );
2721 }
2722 }
2723#endif
2724
Paul Bakker926af752012-11-23 13:38:07 +01002725exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
2728 return( 0 );
2729}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002730#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002732MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002734{
Janos Follath865b3eb2019-12-16 11:46:15 +00002735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Hanno Becker327c93b2018-08-15 13:56:18 +01002739 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2742 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002744
2745 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2746 {
2747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2748 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2752 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002755 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2756 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002757 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 }
2759
2760 ssl->state++;
2761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002765#endif
2766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
2769 return( 0 );
2770}
2771
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002772MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002774{
Janos Follath865b3eb2019-12-16 11:46:15 +00002775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002776
2777 size_t header_len;
2778 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002779 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002780 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2785 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 /*
2788 * DHM key exchange -- send G^X mod P
2789 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002790 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Joe Subbiani6dd73642021-07-19 11:56:54 +01002792 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002793 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002796 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002797 &ssl->out_msg[header_len], content_len,
2798 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 if( ret != 0 )
2800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 return( ret );
2803 }
2804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2806 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002809 ssl->handshake->premaster,
2810 MBEDTLS_PREMASTER_SIZE,
2811 &ssl->handshake->pmslen,
2812 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 return( ret );
2816 }
2817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 }
2820 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002822#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2823 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2824 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2825 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002826 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002827 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2828 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2829 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002830 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002831#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2833 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002834 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002835
2836 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2837
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002838 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002839
Hanno Becker0a94a642019-01-11 14:35:30 +00002840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2841
Hanno Becker4a63ed42019-01-08 11:39:35 +00002842 /*
2843 * Generate EC private key for ECDHE exchange.
2844 */
2845
Hanno Becker4a63ed42019-01-08 11:39:35 +00002846 /* The master secret is obtained from the shared ECDH secret by
2847 * applying the TLS 1.2 PRF with a specific salt and label. While
2848 * the PSA Crypto API encourages combining key agreement schemes
2849 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2850 * yet support the provisioning of salt + label to the KDF.
2851 * For the time being, we therefore need to split the computation
2852 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002853 key_attributes = psa_key_attributes_init();
2854 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002855 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002856 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2857 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002858
2859 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002860 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002861 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002862 if( status != PSA_SUCCESS )
2863 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2864
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002865 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002866 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002867 * but we just need to add a length byte before that. */
2868 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2869 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2870 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2871 size_t own_pubkey_len;
2872
Hanno Becker4a63ed42019-01-08 11:39:35 +00002873 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002874 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002875 &own_pubkey_len );
2876 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002877 {
2878 psa_destroy_key( handshake->ecdh_psa_privkey );
2879 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002880 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002881 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002882
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002883 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2884 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002885
Hanno Becker4a63ed42019-01-08 11:39:35 +00002886 /* The ECDH secret is the premaster secret used for key derivation. */
2887
Janos Follathdf3b0892019-08-08 11:12:24 +01002888 /* Compute ECDH shared secret. */
2889 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2890 handshake->ecdh_psa_privkey,
2891 handshake->ecdh_psa_peerkey,
2892 handshake->ecdh_psa_peerkey_len,
2893 ssl->handshake->premaster,
2894 sizeof( ssl->handshake->premaster ),
2895 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002896
Andrzej Kureka0237f82022-02-24 13:24:52 -05002897 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002898 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002899
2900 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2901 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002902#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002903 /*
2904 * ECDH key exchange -- send client public value
2905 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002906 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002907
Gilles Peskineeccd8882020-03-10 12:19:08 +01002908#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002909 if( ssl->handshake->ecrs_enabled )
2910 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002911 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002912 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002913
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002914 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2915 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002916#endif
2917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002919 &content_len,
2920 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002921 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002922 if( ret != 0 )
2923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002925#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002926 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2927 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2928#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002929 return( ret );
2930 }
2931
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002932 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2933 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002934
Gilles Peskineeccd8882020-03-10 12:19:08 +01002935#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002936 if( ssl->handshake->ecrs_enabled )
2937 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002938 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002939 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002940 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002941
2942ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002943 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002944 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002947 &ssl->handshake->pmslen,
2948 ssl->handshake->premaster,
2949 MBEDTLS_MPI_MAX_SIZE,
2950 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002953#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002954 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2955 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2956#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002957 return( ret );
2958 }
2959
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002960 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2961 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02002962#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01002963 }
2964 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2966 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2967 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2968 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002969#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2970 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2971 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2972 {
2973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2974 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2975 psa_key_attributes_t key_attributes;
2976
2977 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2978
2979 /*
2980 * opaque psk_identity<0..2^16-1>;
2981 */
2982 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002983 /* We don't offer PSK suites if we don't have a PSK,
2984 * and we check that the server's choice is among the
2985 * ciphersuites we offered, so this should never happen. */
2986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002987
Neil Armstrongfc834f22022-03-23 17:54:38 +01002988 /* uint16 to store content length */
2989 const size_t content_len_size = 2;
2990
Neil Armstrong868af822022-03-09 10:26:25 +01002991 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002992
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002993 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002994 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002995 {
2996 MBEDTLS_SSL_DEBUG_MSG( 1,
2997 ( "psk identity too long or SSL buffer too short" ) );
2998 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2999 }
3000
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003001 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003002
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003003 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
3004 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
3005 header_len += content_len_size;
3006
3007 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003008 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003009 p += ssl->conf->psk_identity_len;
3010
Neil Armstrong868af822022-03-09 10:26:25 +01003011 header_len += ssl->conf->psk_identity_len;
3012
3013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3014
3015 /*
3016 * Generate EC private key for ECDHE exchange.
3017 */
3018
3019 /* The master secret is obtained from the shared ECDH secret by
3020 * applying the TLS 1.2 PRF with a specific salt and label. While
3021 * the PSA Crypto API encourages combining key agreement schemes
3022 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3023 * yet support the provisioning of salt + label to the KDF.
3024 * For the time being, we therefore need to split the computation
3025 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3026 key_attributes = psa_key_attributes_init();
3027 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3028 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3029 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3030 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3031
3032 /* Generate ECDH private key. */
3033 status = psa_generate_key( &key_attributes,
3034 &handshake->ecdh_psa_privkey );
3035 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003036 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003037
3038 /* Export the public part of the ECDH private key from PSA.
3039 * The export format is an ECPoint structure as expected by TLS,
3040 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003041 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003042 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3043 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003044 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003045
3046 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3047 own_pubkey, own_pubkey_max_len,
3048 &own_pubkey_len );
3049 if( status != PSA_SUCCESS )
3050 {
3051 psa_destroy_key( handshake->ecdh_psa_privkey );
3052 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003053 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003054 }
3055
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003056 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003057 content_len = own_pubkey_len + 1;
3058
Neil Armstrong25400452022-03-23 17:44:07 +01003059 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3060 * - a uint16 containing the length (in octets) of the ECDH computation
3061 * - the octet string produced by the ECDH computation
3062 * - a uint16 containing the length (in octets) of the PSK
3063 * - the PSK itself
3064 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003065 unsigned char *pms = ssl->handshake->premaster;
3066 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003067 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003068 /* uint16 to store length (in octets) of the ECDH computation */
3069 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003070 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003071
Neil Armstrong25400452022-03-23 17:44:07 +01003072 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003073 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3074 handshake->ecdh_psa_privkey,
3075 handshake->ecdh_psa_peerkey,
3076 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003077 pms + zlen_size,
3078 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003079 &zlen );
3080
3081 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3082 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3083
Neil Armstrongc530aa62022-03-23 17:45:01 +01003084 if( status != PSA_SUCCESS )
3085 return( psa_ssl_status_to_mbedtls( status ) );
3086 else if( destruction_status != PSA_SUCCESS )
3087 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003088
Neil Armstrong25400452022-03-23 17:44:07 +01003089 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003090 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3091 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003092 }
3093 else
3094#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3095 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003096#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003097 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003098 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003099 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003100 * opaque psk_identity<0..2^16-1>;
3101 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003102 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003103 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003104 /* We don't offer PSK suites if we don't have a PSK,
3105 * and we check that the server's choice is among the
3106 * ciphersuites we offered, so this should never happen. */
3107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003108 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003109
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003110 header_len = 4;
3111 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003112
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003113 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003114 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003115 MBEDTLS_SSL_DEBUG_MSG( 1,
3116 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003117 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3118 }
3119
Joe Subbianifbeb6922021-07-16 14:27:50 +01003120 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3121 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003122
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003123 memcpy( ssl->out_msg + header_len,
3124 ssl->conf->psk_identity,
3125 ssl->conf->psk_identity_len );
3126 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3129 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003130 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003131 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003132 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003133 else
3134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3136 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003137 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003138 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3139 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003140 return( ret );
3141 }
3142 else
3143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3145 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003146 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003147 /*
3148 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3149 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003150 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003151
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003152 if( header_len + 2 + content_len >
3153 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003154 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003155 MBEDTLS_SSL_DEBUG_MSG( 1,
3156 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003157 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3158 }
3159
Joe Subbianifbeb6922021-07-16 14:27:50 +01003160 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3161 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003164 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003165 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003166 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003167 if( ret != 0 )
3168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003170 return( ret );
3171 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003172
3173#if defined(MBEDTLS_USE_PSA_CRYPTO)
3174 unsigned char *pms = ssl->handshake->premaster;
3175 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3176 size_t pms_len;
3177
3178 /* Write length only when we know the actual value */
3179 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3180 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3181 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3182 {
3183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3184 return( ret );
3185 }
3186 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3187 pms += 2 + pms_len;
3188
3189 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3190#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003191 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003194#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3195 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003197 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003198 /*
3199 * ClientECDiffieHellmanPublic public;
3200 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003201 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3202 &content_len,
3203 &ssl->out_msg[header_len],
3204 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003205 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003206 if( ret != 0 )
3207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003209 return( ret );
3210 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003211
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003212 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3213 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003214 }
3215 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003216#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003220 }
3221
Neil Armstrong80f6f322022-05-03 17:56:38 +02003222#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3223 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3224 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003225 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003226 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003227 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003228 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003229 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003230#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003231 }
3232 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003233#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3235 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003237 header_len = 4;
3238 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3239 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003240 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 }
Paul Bakkered27a042013-04-18 22:46:23 +02003242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003244#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3245 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3246 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003247 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003248
3249 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003250 ssl->out_msg + header_len,
3251 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3252 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003253 ssl->conf->f_rng, ssl->conf->p_rng );
3254 if( ret != 0 )
3255 {
3256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3257 return( ret );
3258 }
3259
3260 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3261 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3262 ssl->conf->f_rng, ssl->conf->p_rng );
3263 if( ret != 0 )
3264 {
3265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3266 return( ret );
3267 }
3268 }
3269 else
3270#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003271 {
3272 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003277 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3279 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
3281 ssl->state++;
3282
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003283 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 return( ret );
3287 }
3288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
3291 return( 0 );
3292}
3293
Gilles Peskineeccd8882020-03-10 12:19:08 +01003294#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003295MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003298 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003299 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003307 return( ret );
3308 }
3309
Hanno Becker77adddc2019-02-07 12:32:43 +00003310 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003313 ssl->state++;
3314 return( 0 );
3315 }
3316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003319}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003320#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003321MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003323{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003325 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003326 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003327 size_t n = 0, offset = 0;
3328 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003329 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003331 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003332 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003333#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3334 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3335#else
3336 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3337#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003340
Gilles Peskineeccd8882020-03-10 12:19:08 +01003341#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003342 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003343 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003344 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003345 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003346 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003347#endif
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003352 return( ret );
3353 }
3354
Hanno Becker77adddc2019-02-07 12:32:43 +00003355 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003358 ssl->state++;
3359 return( 0 );
3360 }
3361
Jerry Yufb28b882022-01-28 11:05:58 +08003362 if( ssl->handshake->client_auth == 0 ||
3363 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 {
Johan Pascala89ca862020-08-25 10:03:19 +02003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3366 ssl->state++;
3367 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003368 }
3369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 }
3375
3376 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003377 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003379#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003380 if( ssl->handshake->ecrs_enabled )
3381 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3382
3383sign:
3384#endif
3385
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003386 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003387
Ronald Cron90915f22022-03-07 11:11:36 +01003388 /*
3389 * digitally-signed struct {
3390 * opaque handshake_messages[handshake_messages_length];
3391 * };
3392 *
3393 * Taking shortcut here. We assume that the server always allows the
3394 * PRF Hash function and has sent it in the allowed signature
3395 * algorithms list received in the Certificate Request message.
3396 *
3397 * Until we encounter a server that does not, we will take this
3398 * shortcut.
3399 *
3400 * Reason: Otherwise we should have running hashes for SHA512 and
3401 * SHA224 in order to satisfy 'weird' needs from the server
3402 * side.
3403 */
3404 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003405 {
Ronald Cron90915f22022-03-07 11:11:36 +01003406 md_alg = MBEDTLS_MD_SHA384;
3407 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003408 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003409 else
Paul Bakker577e0062013-08-28 11:57:20 +02003410 {
Ronald Cron90915f22022-03-07 11:11:36 +01003411 md_alg = MBEDTLS_MD_SHA256;
3412 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003413 }
Ronald Cron90915f22022-03-07 11:11:36 +01003414 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3415
3416 /* Info from md_alg will be used instead */
3417 hashlen = 0;
3418 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003419
Gilles Peskineeccd8882020-03-10 12:19:08 +01003420#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003421 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003422 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003423#endif
3424
3425 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3426 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003427 ssl->out_msg + 6 + offset,
3428 out_buf_len - 6 - offset,
3429 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003430 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003433#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003434 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3435 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3436#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003437 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003438 }
Paul Bakker926af752012-11-23 13:38:07 +01003439
Joe Subbiani6dd73642021-07-19 11:56:54 +01003440 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Paul Bakker1ef83d62012-04-11 12:09:53 +00003442 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3444 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003445
3446 ssl->state++;
3447
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003448 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 return( ret );
3452 }
3453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455
Paul Bakkered27a042013-04-18 22:46:23 +02003456 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003458#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003461MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463{
Janos Follath865b3eb2019-12-16 11:46:15 +00003464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003465 uint32_t lifetime;
3466 size_t ticket_len;
3467 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003468 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003471
Hanno Becker327c93b2018-08-15 13:56:18 +01003472 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003475 return( ret );
3476 }
3477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003481 mbedtls_ssl_send_alert_message(
3482 ssl,
3483 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3484 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003486 }
3487
3488 /*
3489 * struct {
3490 * uint32 ticket_lifetime_hint;
3491 * opaque ticket<0..2^16-1>;
3492 * } NewSessionTicket;
3493 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003494 * 0 . 3 ticket_lifetime_hint
3495 * 4 . 5 ticket_len (n)
3496 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3499 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003502 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3503 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003504 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003505 }
3506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003508
Philippe Antoineb5b25432018-05-11 11:06:29 +02003509 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3510 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003511
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003512 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003517 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3518 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003519 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003520 }
3521
Paul Elliottd48d5c62021-01-07 14:47:05 +00003522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003523
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003524 /* We're not waiting for a NewSessionTicket message any more */
3525 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003527
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003528 /*
3529 * Zero-length ticket means the server changed his mind and doesn't want
3530 * to send a ticket after all, so just forget it
3531 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003532 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003533 return( 0 );
3534
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003535 if( ssl->session != NULL && ssl->session->ticket != NULL )
3536 {
3537 mbedtls_platform_zeroize( ssl->session->ticket,
3538 ssl->session->ticket_len );
3539 mbedtls_free( ssl->session->ticket );
3540 ssl->session->ticket = NULL;
3541 ssl->session->ticket_len = 0;
3542 }
3543
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003544 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3545 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003547 ssl->session_negotiate->ticket = NULL;
3548 ssl->session_negotiate->ticket_len = 0;
3549
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003550 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003551 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003553 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3554 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003555 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003556 }
3557
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003558 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003559
3560 ssl->session_negotiate->ticket = ticket;
3561 ssl->session_negotiate->ticket_len = ticket_len;
3562 ssl->session_negotiate->ticket_lifetime = lifetime;
3563
3564 /*
3565 * RFC 5077 section 3.4:
3566 * "If the client receives a session ticket from the server, then it
3567 * discards any Session ID that was sent in the ServerHello."
3568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003570 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003573
3574 return( 0 );
3575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003577
Paul Bakker5121ce52009-01-03 21:22:43 +00003578/*
Paul Bakker1961b702013-01-25 14:49:24 +01003579 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003582{
3583 int ret = 0;
3584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003586 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3588 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003589 ssl->handshake->new_session_ticket != 0 )
3590 {
Jerry Yua357cf42022-07-12 05:36:45 +00003591 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003592 }
3593#endif
3594
Paul Bakker1961b702013-01-25 14:49:24 +01003595 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 case MBEDTLS_SSL_HELLO_REQUEST:
3598 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 break;
3600
Paul Bakker1961b702013-01-25 14:49:24 +01003601 /*
3602 * ==> ClientHello
3603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003605 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003606 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
Paul Bakker1961b702013-01-25 14:49:24 +01003608 /*
3609 * <== ServerHello
3610 * Certificate
3611 * ( ServerKeyExchange )
3612 * ( CertificateRequest )
3613 * ServerHelloDone
3614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003616 ret = ssl_parse_server_hello( ssl );
3617 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3620 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003621 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003624 ret = ssl_parse_server_key_exchange( ssl );
3625 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003628 ret = ssl_parse_certificate_request( ssl );
3629 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003632 ret = ssl_parse_server_hello_done( ssl );
3633 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003634
Paul Bakker1961b702013-01-25 14:49:24 +01003635 /*
3636 * ==> ( Certificate/Alert )
3637 * ClientKeyExchange
3638 * ( CertificateVerify )
3639 * ChangeCipherSpec
3640 * Finished
3641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3643 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003644 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003647 ret = ssl_write_client_key_exchange( ssl );
3648 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003651 ret = ssl_write_certificate_verify( ssl );
3652 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3655 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003656 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 case MBEDTLS_SSL_CLIENT_FINISHED:
3659 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003660 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
Paul Bakker1961b702013-01-25 14:49:24 +01003662 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003663 * <== ( NewSessionTicket )
3664 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003665 * Finished
3666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00003668 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003669 ret = ssl_parse_new_session_ticket( ssl );
3670 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003671#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3674 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003675 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 case MBEDTLS_SSL_SERVER_FINISHED:
3678 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003679 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 case MBEDTLS_SSL_FLUSH_BUFFERS:
3682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3683 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003684 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3687 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003688 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003689
Paul Bakker1961b702013-01-25 14:49:24 +01003690 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003694
3695 return( ret );
3696}
Jerry Yuc5aef882021-12-23 20:15:02 +08003697
Jerry Yufb4b6472022-01-27 15:03:26 +08003698#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */