blob: 79f34d3457ddbc6a98ffd44116a1979a07745b0d [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 Becker49770ff2019-04-25 16:55:15 +0100238 * struct {
239 * opaque cid<0..2^8-1>;
240 * } ConnectionId;
241 */
242
243 *olen = 0;
244 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
245 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
246 {
Hanno Becker261602c2017-04-12 14:54:42 +0100247 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100248 }
249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
250
251 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
252 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100253 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100254
255 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100256 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
257 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100258 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100259 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
260 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100261
262 *p++ = (uint8_t) ssl->own_cid_len;
263 memcpy( p, ssl->own_cid, ssl->own_cid_len );
264
265 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100266
267 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100268}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100269#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200272MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100273static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
274 unsigned char *buf,
275 const unsigned char *end,
276 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200277{
278 unsigned char *p = buf;
279
Simon Butcher0fc94e92015-09-28 20:52:04 +0100280 *olen = 0;
281
Hanno Becker261602c2017-04-12 14:54:42 +0100282 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
283 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200284
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100285 MBEDTLS_SSL_DEBUG_MSG( 3,
286 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200287
Hanno Becker261602c2017-04-12 14:54:42 +0100288 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100289
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100290 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
291 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200292
293 *p++ = 0x00;
294 *p++ = 1;
295
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200296 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200297
298 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100299
300 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200305MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100306static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
307 unsigned char *buf,
308 const unsigned char *end,
309 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100310{
311 unsigned char *p = buf;
312
Simon Butcher0fc94e92015-09-28 20:52:04 +0100313 *olen = 0;
314
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100315 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100316 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100317
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100318 MBEDTLS_SSL_DEBUG_MSG( 3,
319 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100320
Hanno Becker261602c2017-04-12 14:54:42 +0100321 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100322
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100323 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
324 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100325
326 *p++ = 0x00;
327 *p++ = 0x00;
328
329 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100330
331 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200336MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100337static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
338 unsigned char *buf,
339 const unsigned char *end,
340 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200341{
342 unsigned char *p = buf;
343
Simon Butcher0fc94e92015-09-28 20:52:04 +0100344 *olen = 0;
345
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100346 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100347 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200348
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100349 MBEDTLS_SSL_DEBUG_MSG( 3,
350 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200351
Hanno Becker261602c2017-04-12 14:54:42 +0100352 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100353
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100354 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
355 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200356
357 *p++ = 0x00;
358 *p++ = 0x00;
359
360 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100361
362 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200367MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100368static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
369 unsigned char *buf,
370 const unsigned char *end,
371 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200372{
373 unsigned char *p = buf;
374 size_t tlen = ssl->session_negotiate->ticket_len;
375
Simon Butcher0fc94e92015-09-28 20:52:04 +0100376 *olen = 0;
377
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200378 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100379 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200380
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100381 MBEDTLS_SSL_DEBUG_MSG( 3,
382 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200383
Hanno Becker261602c2017-04-12 14:54:42 +0100384 /* The addition is safe here since the ticket length is 16 bit. */
385 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100386
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100387 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
388 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200389
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100390 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
391 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
393 *olen = 4;
394
Simon Butchered997662015-09-28 02:14:30 +0100395 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100396 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200397
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100398 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000399 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400
401 memcpy( p, ssl->session_negotiate->ticket, tlen );
402
403 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100404
405 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200408
Ron Eldora9788042018-12-05 11:04:31 +0200409#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200410MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascal77696ee2020-09-22 21:49:40 +0200411static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200412 unsigned char *buf,
413 const unsigned char *end,
414 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100415{
416 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200417 size_t protection_profiles_index = 0, ext_len = 0;
418 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100419
420 *olen = 0;
421
Johan Pascalc3ccd982020-10-28 17:18:18 +0100422 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
423 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
424 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100425 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200426 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100427 }
428
Ron Eldora9788042018-12-05 11:04:31 +0200429 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100430 * uint8 SRTPProtectionProfile[2];
431 *
432 * struct {
433 * SRTPProtectionProfiles SRTPProtectionProfiles;
434 * opaque srtp_mki<0..255>;
435 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100436 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100437 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200438 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200439 {
440 mki_len = ssl->dtls_srtp_info.mki_len;
441 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300442 /* Extension length = 2 bytes for profiles length,
443 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
444 * 1 byte for srtp_mki vector length and the mki_len value
445 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200446 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
447
Johan Pascal77696ee2020-09-22 21:49:40 +0200448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
449
450 /* Check there is room in the buffer for the extension + 4 bytes
451 * - the extension tag (2 bytes)
452 * - the extension length (2 bytes)
453 */
454 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
455
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100456 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
457 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200458
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100459 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
460 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100461
Ron Eldor3adb9922017-12-21 10:15:08 +0200462 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200463 /* micro-optimization:
464 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
465 * which is lower than 127, so the upper byte of the length is always 0
466 * For the documentation, the more generic code is left in comments
467 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
468 * >> 8 ) & 0xFF );
469 */
470 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100471 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100472
Ron Eldoref72faf2018-07-12 11:54:20 +0300473 for( protection_profiles_index=0;
474 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
475 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100476 {
Johan Pascal43f94902020-09-22 12:25:52 +0200477 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200478 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200479 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200480 {
481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
482 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100483 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
484 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200485 }
486 else
487 {
488 /*
489 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200490 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200491 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200492 MBEDTLS_SSL_DEBUG_MSG( 3,
493 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200494 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200495 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
496 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200497 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100498 }
499 }
500
Ron Eldor591f1622018-01-22 12:30:04 +0200501 *p++ = mki_len & 0xFF;
502
503 if( mki_len != 0 )
504 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200505 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200506 /*
507 * Increment p to point to the current position.
508 */
509 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300510 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
511 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200512 }
513
Ron Eldoref72faf2018-07-12 11:54:20 +0300514 /*
515 * total extension length: extension type (2 bytes)
516 * + extension length (2 bytes)
517 * + protection profile length (2 bytes)
518 * + 2 * number of protection profiles
519 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200520 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300521 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200522 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200523
524 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100525}
526#endif /* MBEDTLS_SSL_DTLS_SRTP */
527
Ronald Cron4079abc2022-02-20 10:35:26 +0100528int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
529 unsigned char *buf,
530 const unsigned char *end,
531 int uses_ec,
532 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100533{
534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
535 unsigned char *p = buf;
536 size_t ext_len = 0;
537
538 (void) ssl;
539 (void) end;
540 (void) uses_ec;
541 (void) ret;
542 (void) ext_len;
543
544 *out_len = 0;
545
546 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
547 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
548#if defined(MBEDTLS_SSL_RENEGOTIATION)
549 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
550 {
551 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
552 return( ret );
553 }
554 p += ext_len;
555#endif
556
557#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
558 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
559 if( uses_ec )
560 {
561 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
562 &ext_len ) ) != 0 )
563 {
564 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
565 return( ret );
566 }
567 p += ext_len;
568 }
569#endif
570
571#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
572 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
573 {
574 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
575 return( ret );
576 }
577 p += ext_len;
578#endif
579
580#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
581 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
582 {
583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
584 return( ret );
585 }
586 p += ext_len;
587#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
588
589#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
590 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
591 &ext_len ) ) != 0 )
592 {
593 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
594 return( ret );
595 }
596 p += ext_len;
597#endif
598
599#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
600 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
601 {
602 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
603 return( ret );
604 }
605 p += ext_len;
606#endif
607
608#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
609 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
610 {
611 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
612 return( ret );
613 }
614 p += ext_len;
615#endif
616
617#if defined(MBEDTLS_SSL_DTLS_SRTP)
618 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
621 return( ret );
622 }
623 p += ext_len;
624#endif
625
626#if defined(MBEDTLS_SSL_SESSION_TICKETS)
627 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
628 {
629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
630 return( ret );
631 }
632 p += ext_len;
633#endif
634
635 *out_len = p - buf;
636
637 return( 0 );
638}
639
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200640MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200642 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000643 size_t len )
644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_SSL_RENEGOTIATION)
646 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000647 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100648 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000649 if( len != 1 + ssl->verify_data_len * 2 ||
650 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200651 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100652 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200653 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100654 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100657 mbedtls_ssl_send_alert_message(
658 ssl,
659 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
660 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100661 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000662 }
663 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100666 {
667 if( len != 1 || buf[0] != 0x00 )
668 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200669 MBEDTLS_SSL_DEBUG_MSG( 1,
670 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100671 mbedtls_ssl_send_alert_message(
672 ssl,
673 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
674 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100675 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100676 }
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100679 }
Paul Bakker48916f92012-09-16 19:57:18 +0000680
681 return( 0 );
682}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200685MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200687 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200688 size_t len )
689{
690 /*
691 * server should use the extension only if we did,
692 * and if so the server's value should match ours (and len is always 1)
693 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200694 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200695 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200696 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200697 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100698 MBEDTLS_SSL_DEBUG_MSG( 1,
699 ( "non-matching max fragment length extension" ) );
700 mbedtls_ssl_send_alert_message(
701 ssl,
702 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100703 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
704 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200705 }
706
707 return( 0 );
708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000710
Hanno Beckera0e20d02019-05-15 14:03:01 +0100711#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200712MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera8373a12019-04-26 15:37:26 +0100713static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
714 const unsigned char *buf,
715 size_t len )
716{
717 size_t peer_cid_len;
718
719 if( /* CID extension only makes sense in DTLS */
720 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
721 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100722 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100723 {
Hanno Becker22626482019-05-03 12:46:59 +0100724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100725 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100726 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100727 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100728 }
729
730 if( len == 0 )
731 {
732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
733 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100734 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
735 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100736 }
737
738 peer_cid_len = *buf++;
739 len--;
740
741 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
742 {
Hanno Becker22626482019-05-03 12:46:59 +0100743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100744 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100745 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
746 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100747 }
748
749 if( len != peer_cid_len )
750 {
Hanno Becker22626482019-05-03 12:46:59 +0100751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100753 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
754 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100755 }
756
Hanno Becker5a299902019-05-03 12:47:49 +0100757 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100758 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
759 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
760
761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
762 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
763
Hanno Beckera8373a12019-04-26 15:37:26 +0100764 return( 0 );
765}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100766#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200769MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100771 const unsigned char *buf,
772 size_t len )
773{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200774 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100775 len != 0 )
776 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100777 MBEDTLS_SSL_DEBUG_MSG( 1,
778 ( "non-matching encrypt-then-MAC extension" ) );
779 mbedtls_ssl_send_alert_message(
780 ssl,
781 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100782 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100783 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100784 }
785
786 ((void) buf);
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100789
790 return( 0 );
791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200795MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200797 const unsigned char *buf,
798 size_t len )
799{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200800 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200801 len != 0 )
802 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100803 MBEDTLS_SSL_DEBUG_MSG( 1,
804 ( "non-matching extended master secret extension" ) );
805 mbedtls_ssl_send_alert_message(
806 ssl,
807 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100808 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
809 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200810 }
811
812 ((void) buf);
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200815
816 return( 0 );
817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200821MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200823 const unsigned char *buf,
824 size_t len )
825{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200826 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200827 len != 0 )
828 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100829 MBEDTLS_SSL_DEBUG_MSG( 1,
830 ( "non-matching session ticket extension" ) );
831 mbedtls_ssl_send_alert_message(
832 ssl,
833 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100834 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
835 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200836 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200837
838 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200839
840 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200841
842 return( 0 );
843}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200845
Robert Cragie136884c2015-10-02 13:34:31 +0100846#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100847 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200848MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200850 const unsigned char *buf,
851 size_t len )
852{
853 size_t list_size;
854 const unsigned char *p;
855
Philippe Antoine747fd532018-05-30 09:13:21 +0200856 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200859 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
860 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100861 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200862 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200863 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200864
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200865 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200866 while( list_size > 0 )
867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
869 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200870 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200871#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
872 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200873 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200874#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
875 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100876#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200877 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
878 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200881 return( 0 );
882 }
883
884 list_size--;
885 p++;
886 }
887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200889 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
890 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100891 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200892}
Darryl Green11999bb2018-03-13 15:22:58 +0000893#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100894 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200895
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200896#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200897MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200898static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
899 const unsigned char *buf,
900 size_t len )
901{
Janos Follath865b3eb2019-12-16 11:46:15 +0000902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200903
Hanno Beckere694c3e2017-12-27 21:34:08 +0000904 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200905 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
906 {
907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
908 return( 0 );
909 }
910
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200911 /* If we got here, we no longer need our cached extension */
912 mbedtls_free( ssl->handshake->ecjpake_cache );
913 ssl->handshake->ecjpake_cache = NULL;
914 ssl->handshake->ecjpake_cache_len = 0;
915
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200916 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
917 buf, len ) ) != 0 )
918 {
919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100920 mbedtls_ssl_send_alert_message(
921 ssl,
922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
923 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200924 return( ret );
925 }
926
927 return( 0 );
928}
929#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200934 const unsigned char *buf, size_t len )
935{
936 size_t list_len, name_len;
937 const char **p;
938
939 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200940 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200941 {
942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100943 mbedtls_ssl_send_alert_message(
944 ssl,
945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100946 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
947 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200948 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200949
950 /*
951 * opaque ProtocolName<1..2^8-1>;
952 *
953 * struct {
954 * ProtocolName protocol_name_list<2..2^16-1>
955 * } ProtocolNameList;
956 *
957 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
958 */
959
960 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
961 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200962 {
963 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100965 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200966 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200967
968 list_len = ( buf[0] << 8 ) | buf[1];
969 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200970 {
971 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
972 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100973 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200974 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200975
976 name_len = buf[2];
977 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200978 {
979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100981 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200982 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200983
984 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200985 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200986 {
987 if( name_len == strlen( *p ) &&
988 memcmp( buf + 3, *p, name_len ) == 0 )
989 {
990 ssl->alpn_chosen = *p;
991 return( 0 );
992 }
993 }
994
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200996 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
997 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100998 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001001
Johan Pascalb62bb512015-12-03 21:56:45 +01001002#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001003MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascalb62bb512015-12-03 21:56:45 +01001004static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001005 const unsigned char *buf,
1006 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001007{
Johan Pascal43f94902020-09-22 12:25:52 +02001008 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001009 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001010 uint16_t server_protection_profile_value = 0;
1011
1012 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001013 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1014 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1015 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001016 return( 0 );
1017
Ron Eldora9788042018-12-05 11:04:31 +02001018 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001019 * uint8 SRTPProtectionProfile[2];
1020 *
1021 * struct {
1022 * SRTPProtectionProfiles SRTPProtectionProfiles;
1023 * opaque srtp_mki<0..255>;
1024 * } UseSRTPData;
1025
1026 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1027 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001028 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001029 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001030 {
1031 mki_len = ssl->dtls_srtp_info.mki_len;
1032 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001033
Ron Eldoref72faf2018-07-12 11:54:20 +03001034 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001035 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1036 * + protection profile (2 bytes)
1037 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001038 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001039 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001040 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001041 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001042
1043 /*
1044 * get the server protection profile
1045 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001046
1047 /*
1048 * protection profile length must be 0x0002 as we must have only
1049 * one protection profile in server Hello
1050 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001051 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001052 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001053
1054 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001055 server_protection = mbedtls_ssl_check_srtp_profile_value(
1056 server_protection_profile_value );
1057 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001058 {
Johan Pascal43f94902020-09-22 12:25:52 +02001059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1060 mbedtls_ssl_get_srtp_profile_as_string(
1061 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001062 }
1063
Johan Pascal43f94902020-09-22 12:25:52 +02001064 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001065
Johan Pascalb62bb512015-12-03 21:56:45 +01001066 /*
1067 * Check we have the server profile in our list
1068 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001069 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001070 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001071 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1072 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001073 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1075 mbedtls_ssl_get_srtp_profile_as_string(
1076 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001077 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001078 }
1079 }
1080
Ron Eldor591f1622018-01-22 12:30:04 +02001081 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001082 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001083 {
1084 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001085 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001086 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001087 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001088
1089 /* If server does not use mki in its reply, make sure the client won't keep
1090 * one as negotiated */
1091 if( len == 5 )
1092 {
1093 ssl->dtls_srtp_info.mki_len = 0;
1094 }
1095
Ron Eldoref72faf2018-07-12 11:54:20 +03001096 /*
1097 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001098 * If the client detects a nonzero-length MKI in the server's response
1099 * that is different than the one the client offered, then the client
1100 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1101 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001102 if( len > 5 && ( buf[4] != mki_len ||
1103 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001104 {
1105 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1106 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001107 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001108 }
Ron Eldorb4655392018-07-05 18:25:39 +03001109#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001110 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001111 {
Ron Eldora9788042018-12-05 11:04:31 +02001112 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1113 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001114 }
1115#endif
Ron Eldora9788042018-12-05 11:04:31 +02001116 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001117}
1118#endif /* MBEDTLS_SSL_DTLS_SRTP */
1119
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001120/*
1121 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001124MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001128 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001129 unsigned char cookie_len;
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001132
Gilles Peskineb64bf062019-09-27 14:02:44 +02001133 /* Check that there is enough room for:
1134 * - 2 bytes of version
1135 * - 1 byte of cookie_len
1136 */
1137 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1138 {
1139 MBEDTLS_SSL_DEBUG_MSG( 1,
1140 ( "incoming HelloVerifyRequest message is too short" ) );
1141 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1142 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001143 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001144 }
1145
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001146 /*
1147 * struct {
1148 * ProtocolVersion server_version;
1149 * opaque cookie<0..2^8-1>;
1150 * } HelloVerifyRequest;
1151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001153 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001154 p += 2;
1155
TRodziewicz2d8800e2021-05-13 19:14:19 +02001156 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001157 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1158 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1159 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001160 */
Glenn Strauss83158112022-04-13 14:59:34 -04001161 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1166 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001167
Hanno Beckerbc000442021-06-24 09:18:19 +01001168 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001169 }
1170
1171 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001172 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1173 {
1174 MBEDTLS_SSL_DEBUG_MSG( 1,
1175 ( "cookie length does not match incoming message size" ) );
1176 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1177 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001178 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001179 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001180 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001181
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001182 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001183
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001184 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1185 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001186 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001188 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001189 }
1190
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001191 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001192 ssl->handshake->verify_cookie_len = cookie_len;
1193
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001194 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1196 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001201
1202 return( 0 );
1203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001205
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001206MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001208{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001209 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001210 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001211 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001212 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001213 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001215 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001216#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001217 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Hanno Becker327c93b2018-08-15 13:56:18 +01001222 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001224 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 return( ret );
1227 }
1228
Hanno Becker79594fd2019-05-08 09:38:41 +01001229 buf = ssl->in_msg;
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_SSL_RENEGOTIATION)
1234 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001235 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001236 ssl->renego_records_seen++;
1237
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001238 if( ssl->conf->renego_max_records >= 0 &&
1239 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001240 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001241 MBEDTLS_SSL_DEBUG_MSG( 1,
1242 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001244 }
1245
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001246 MBEDTLS_SSL_DEBUG_MSG( 1,
1247 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001248
1249 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001251 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001255 mbedtls_ssl_send_alert_message(
1256 ssl,
1257 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1258 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001263 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001269 return( ssl_parse_hello_verify_request( ssl ) );
1270 }
1271 else
1272 {
1273 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001274 mbedtls_free( ssl->handshake->cookie );
1275 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001276 ssl->handshake->verify_cookie_len = 0;
1277 }
1278 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1282 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001285 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1286 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001287 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 }
1289
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001290 /*
1291 * 0 . 1 server_version
1292 * 2 . 33 random (maybe including 4 bytes of Unix time)
1293 * 34 . 34 session_id length = n
1294 * 35 . 34+n session_id
1295 * 35+n . 36+n cipher_suite
1296 * 37+n . 37+n compression_method
1297 *
1298 * 38+n . 39+n extensions length (optional)
1299 * 40+n . .. extensions
1300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001302
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001303 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1304 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001305 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Glenn Strauss60bfe602022-03-14 19:04:24 -04001307 if( ssl->tls_version < ssl->conf->min_tls_version ||
1308 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001309 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001310 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001311 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1312 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001313 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001314 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1317 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001318
Hanno Beckerbc000442021-06-24 09:18:19 +01001319 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001320 }
1321
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001322 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001323 ( (unsigned long) buf[2] << 24 ) |
1324 ( (unsigned long) buf[3] << 16 ) |
1325 ( (unsigned long) buf[4] << 8 ) |
1326 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001328 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001330 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Paul Bakker48916f92012-09-16 19:57:18 +00001334 if( n > 32 )
1335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001339 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001340 }
1341
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001342 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001344 ext_len = ( ( buf[38 + n] << 8 )
1345 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Paul Bakker48916f92012-09-16 19:57:18 +00001347 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001351 mbedtls_ssl_send_alert_message(
1352 ssl,
1353 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1354 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001355 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001358 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001359 {
1360 ext_len = 0;
1361 }
1362 else
1363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001365 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1366 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001367 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001370 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001371 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001372
1373 /*
1374 * Read and check compression
1375 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001376 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001378 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001379 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001380 MBEDTLS_SSL_DEBUG_MSG( 1,
1381 ( "server hello, bad compression: %d", comp ) );
1382 mbedtls_ssl_send_alert_message(
1383 ssl,
1384 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1385 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001387 }
1388
Paul Bakker380da532012-04-18 16:10:25 +00001389 /*
1390 * Initialize update checksum functions
1391 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001392 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1393 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001394 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001395 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001396 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1398 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001400 }
Paul Bakker380da532012-04-18 16:10:25 +00001401
Hanno Beckere694c3e2017-12-27 21:34:08 +00001402 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001403
Paul Elliottd48d5c62021-01-07 14:47:05 +00001404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407 /*
1408 * Check if the session can be resumed
1409 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001410 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_SSL_RENEGOTIATION)
1412 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001413#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001414 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001415 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001416 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 {
1418 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001419 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001421 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001422#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001423 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001424 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001425 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 }
1427 else
1428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 }
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001433 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Paul Elliott3891caf2020-12-17 18:42:40 +00001435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1437 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Andrzej Kurek03bac442018-04-25 05:06:07 -04001439 /*
1440 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001441 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 i = 0;
1443 while( 1 )
1444 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001445 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001448 mbedtls_ssl_send_alert_message(
1449 ssl,
1450 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1451 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001452 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
1454
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001455 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001456 ssl->session_negotiate->ciphersuite )
1457 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 }
1461
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001462 suite_info = mbedtls_ssl_ciphersuite_from_id(
1463 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001464 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1465 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001466 {
1467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001468 mbedtls_ssl_send_alert_message(
1469 ssl,
1470 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001471 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1472 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001473 }
1474
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001475 MBEDTLS_SSL_DEBUG_MSG( 3,
1476 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001477
Gilles Peskineeccd8882020-03-10 12:19:08 +01001478#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001479 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001480 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001481 {
1482 ssl->handshake->ecrs_enabled = 1;
1483 }
1484#endif
1485
Thomas Daubney20f89a92022-06-20 15:12:19 +01001486 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001489 mbedtls_ssl_send_alert_message(
1490 ssl,
1491 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1492 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001493 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 }
1495
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001496 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001497
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001498 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001499 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001500
Paul Bakker48916f92012-09-16 19:57:18 +00001501 while( ext_len )
1502 {
1503 unsigned int ext_id = ( ( ext[0] << 8 )
1504 | ( ext[1] ) );
1505 unsigned int ext_size = ( ( ext[2] << 8 )
1506 | ( ext[3] ) );
1507
1508 if( ext_size + 4 > ext_len )
1509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001511 mbedtls_ssl_send_alert_message(
1512 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1513 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001514 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001515 }
1516
1517 switch( ext_id )
1518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1521#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001522 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001523#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001524
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001525 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1526 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001527 return( ret );
1528
1529 break;
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1532 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001533 MBEDTLS_SSL_DEBUG_MSG( 3,
1534 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001535
1536 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1537 ext + 4, ext_size ) ) != 0 )
1538 {
1539 return( ret );
1540 }
1541
1542 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001544
Hanno Beckera0e20d02019-05-15 14:03:01 +01001545#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001546 case MBEDTLS_TLS_EXT_CID:
1547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1548
1549 if( ( ret = ssl_parse_cid_ext( ssl,
1550 ext + 4,
1551 ext_size ) ) != 0 )
1552 {
1553 return( ret );
1554 }
1555
1556 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1560 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001562
1563 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1564 ext + 4, ext_size ) ) != 0 )
1565 {
1566 return( ret );
1567 }
1568
1569 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1573 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001574 MBEDTLS_SSL_DEBUG_MSG( 3,
1575 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001576
1577 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1578 ext + 4, ext_size ) ) != 0 )
1579 {
1580 return( ret );
1581 }
1582
1583 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001589
1590 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1591 ext + 4, ext_size ) ) != 0 )
1592 {
1593 return( ret );
1594 }
1595
1596 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001598
Robert Cragie136884c2015-10-02 13:34:31 +01001599#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001600 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001602 MBEDTLS_SSL_DEBUG_MSG( 3,
1603 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001604
1605 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1606 ext + 4, ext_size ) ) != 0 )
1607 {
1608 return( ret );
1609 }
1610
1611 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001612#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1613 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001614
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001615#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1616 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1618
1619 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1620 ext + 4, ext_size ) ) != 0 )
1621 {
1622 return( ret );
1623 }
1624
1625 break;
1626#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_ALPN)
1629 case MBEDTLS_TLS_EXT_ALPN:
1630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001631
Johan Pascal275874b2020-10-27 10:43:53 +01001632 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1633 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001634
1635 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001637
Johan Pascalb62bb512015-12-03 21:56:45 +01001638#if defined(MBEDTLS_SSL_DTLS_SRTP)
1639 case MBEDTLS_TLS_EXT_USE_SRTP:
1640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1641
Johan Pascalc3ccd982020-10-28 17:18:18 +01001642 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1643 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001644
1645 break;
1646#endif /* MBEDTLS_SSL_DTLS_SRTP */
1647
Paul Bakker48916f92012-09-16 19:57:18 +00001648 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001649 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001650 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001651 }
1652
1653 ext_len -= 4 + ext_size;
1654 ext += 4 + ext_size;
1655
1656 if( ext_len > 0 && ext_len < 4 )
1657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001659 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001660 }
1661 }
1662
1663 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001664 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1665 * extensions. It sets the transform data for the resumed session which in
1666 * case of DTLS includes the server CID extracted from the CID extension.
1667 */
1668 if( ssl->handshake->resume )
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001669 {
1670 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
1671 {
1672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
1673 mbedtls_ssl_send_alert_message(
1674 ssl,
1675 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1676 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
1677 return( ret );
1678 }
1679 }
1680
Paul Bakker48916f92012-09-16 19:57:18 +00001681 /*
1682 * Renegotiation security checks
1683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001685 ssl->conf->allow_legacy_renegotiation ==
1686 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001687 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001688 MBEDTLS_SSL_DEBUG_MSG( 1,
1689 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001690 handshake_failure = 1;
1691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_SSL_RENEGOTIATION)
1693 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1694 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001695 renegotiation_info_seen == 0 )
1696 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001697 MBEDTLS_SSL_DEBUG_MSG( 1,
1698 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001699 handshake_failure = 1;
1700 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1702 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001703 ssl->conf->allow_legacy_renegotiation ==
1704 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001707 handshake_failure = 1;
1708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1710 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001711 renegotiation_info_seen == 1 )
1712 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001713 MBEDTLS_SSL_DEBUG_MSG( 1,
1714 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001715 handshake_failure = 1;
1716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001718
1719 if( handshake_failure == 1 )
1720 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001721 mbedtls_ssl_send_alert_message(
1722 ssl,
1723 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1724 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001725 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
1730 return( 0 );
1731}
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1734 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001735MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001736static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1737 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001738 unsigned char *end )
1739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001741 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001742
Paul Bakker29e1f122013-04-16 13:07:56 +02001743 /*
1744 * Ephemeral DH parameters:
1745 *
1746 * struct {
1747 * opaque dh_p<1..2^16-1>;
1748 * opaque dh_g<1..2^16-1>;
1749 * opaque dh_Ys<1..2^16-1>;
1750 * } ServerDHParams;
1751 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001752 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1753 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001756 return( ret );
1757 }
1758
Gilles Peskine487bbf62021-05-27 22:17:07 +02001759 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001760 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001761 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001763 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001764 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001765 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001766 }
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1769 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1770 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001771
1772 return( ret );
1773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1775 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001776
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001777#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1778 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1779 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1780#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001781MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001782static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001783 unsigned char **p,
1784 unsigned char *end )
1785{
1786 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001787 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001788 uint8_t ecpoint_len;
1789 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1790
1791 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001792 * struct {
1793 * ECParameters curve_params;
1794 * ECPoint public;
1795 * } ServerECDHParams;
1796 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001797 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001798 * 2..3 NamedCurve
1799 * 4 ECPoint.len
1800 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001801 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001802 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001803 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001804
1805 /* First byte is curve_type; only named_curve is handled */
1806 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001807 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001808
1809 /* Next two bytes are the namedcurve value */
1810 tls_id = *(*p)++;
1811 tls_id <<= 8;
1812 tls_id |= *(*p)++;
1813
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001814 /* Check it's a curve we offered */
1815 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001816 {
1817 MBEDTLS_SSL_DEBUG_MSG( 2,
1818 ( "bad server key exchange message (ECDHE curve): %u",
1819 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001820 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001821 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001822
Hanno Beckerbb89e272019-01-08 12:54:37 +00001823 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001824 if( ( handshake->ecdh_psa_type =
1825 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001826 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001827 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001828 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001829 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001830
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001831 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001832 ecpoint_len = *(*p)++;
1833 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001834 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001835
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001836 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1837 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001839 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1840 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001841 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001842
Hanno Beckerbb89e272019-01-08 12:54:37 +00001843 return( 0 );
1844}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001845#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001846MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrong1f198d82022-04-13 15:02:30 +02001847static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1848{
1849 const mbedtls_ecp_curve_info *curve_info;
1850 mbedtls_ecp_group_id grp_id;
1851#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1852 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1853#else
1854 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1855#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001856
Neil Armstrong1f198d82022-04-13 15:02:30 +02001857 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1858 if( curve_info == NULL )
1859 {
1860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1861 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1862 }
1863
1864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1865
1866 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1867 return( -1 );
1868
1869 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1870 MBEDTLS_DEBUG_ECDH_QP );
1871
1872 return( 0 );
1873}
1874
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001875MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001877 unsigned char **p,
1878 unsigned char *end )
1879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001881
Paul Bakker29e1f122013-04-16 13:07:56 +02001882 /*
1883 * Ephemeral ECDH parameters:
1884 *
1885 * struct {
1886 * ECParameters curve_params;
1887 * ECPoint public;
1888 * } ServerECDHParams;
1889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001891 (const unsigned char **) p, end ) ) != 0 )
1892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001894#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001895 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1896 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001897#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001898 return( ret );
1899 }
1900
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001901 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001902 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001903 MBEDTLS_SSL_DEBUG_MSG( 1,
1904 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001905 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001906 }
1907
Paul Bakker29e1f122013-04-16 13:07:56 +02001908 return( ret );
1909}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001910#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1912 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1913 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001914
Gilles Peskineeccd8882020-03-10 12:19:08 +01001915#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001916MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001918 unsigned char **p,
1919 unsigned char *end )
1920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001922 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001923 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001924
1925 /*
1926 * PSK parameters:
1927 *
1928 * opaque psk_identity_hint<0..2^16-1>;
1929 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001930 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001931 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001932 MBEDTLS_SSL_DEBUG_MSG( 1,
1933 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001934 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001935 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001936 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001937 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001938
irwir6527bd62019-09-21 18:51:25 +03001939 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001940 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001941 MBEDTLS_SSL_DEBUG_MSG( 1,
1942 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001943 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001944 }
1945
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001946 /*
1947 * Note: we currently ignore the PKS identity hint, as we only allow one
1948 * PSK to be provisionned on the client. This could be changed later if
1949 * someone needs that feature.
1950 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001951 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001952 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001953
1954 return( ret );
1955}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001956#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1959 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001960/*
1961 * Generate a pre-master secret and encrypt it with the server's RSA key
1962 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001963MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001965 size_t offset, size_t *olen,
1966 size_t pms_offset )
1967{
Janos Follath865b3eb2019-12-16 11:46:15 +00001968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001969 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001970 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001971 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001972
Angus Grattond8213d02016-05-25 20:56:48 +10001973 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001974 {
1975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1976 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1977 }
1978
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001979 /*
1980 * Generate (part of) the pre-master as
1981 * struct {
1982 * ProtocolVersion client_version;
1983 * opaque random[46];
1984 * } PreMasterSecret;
1985 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001986 mbedtls_ssl_write_version( p, ssl->conf->transport,
1987 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001988
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001989 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001992 return( ret );
1993 }
1994
1995 ssl->handshake->pmslen = 48;
1996
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001997#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1998 peer_pk = &ssl->handshake->peer_pubkey;
1999#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002000 if( ssl->session_negotiate->peer_cert == NULL )
2001 {
Hanno Becker8273df82019-02-06 17:37:32 +00002002 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002005 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002006 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2007#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002008
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002009 /*
2010 * Now write it out, encrypted
2011 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002012 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2015 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002016 }
2017
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002018 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002019 p, ssl->handshake->pmslen,
2020 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002021 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002022 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002025 return( ret );
2026 }
2027
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028 if( len_bytes == 2 )
2029 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002030 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002031 *olen += 2;
2032 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002033
Hanno Beckerae553dd2019-02-08 14:06:00 +00002034#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2035 /* We don't need the peer's public key anymore. Free it. */
2036 mbedtls_pk_free( peer_pk );
2037#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002038 return( 0 );
2039}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2041 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2044 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002045MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002047{
Janos Follath865b3eb2019-12-16 11:46:15 +00002048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002050 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002051
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002052#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2053 peer_pk = &ssl->handshake->peer_pubkey;
2054#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002055 if( ssl->session_negotiate->peer_cert == NULL )
2056 {
Hanno Becker8273df82019-02-06 17:37:32 +00002057 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002059 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002060 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002061 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2062#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002063
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002064 /* This is a public key, so it can't be opaque, so can_do() is a good
2065 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002066 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2069 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002070 }
2071
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002072 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002073
Przemek Stekielea4000f2022-03-16 09:49:33 +01002074#if defined(MBEDTLS_USE_PSA_CRYPTO)
2075 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002076 size_t olen = 0;
2077
2078 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2079 {
2080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2081 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2082 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002083
2084 ssl->handshake->ecdh_psa_type =
2085 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2086 &ecdh_bits ) );
2087
2088 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2089 {
2090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2091 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2092 }
2093
2094 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2095
Przemek Stekielea4000f2022-03-16 09:49:33 +01002096 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002097 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2098 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2099 ssl->handshake->ecdh_psa_peerkey,
2100 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002101
Przemek Stekiel561a4232022-03-16 13:16:24 +01002102 if ( ret != 0 )
2103 {
2104 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2105 return( ret );
2106 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002107
Przemek Stekiel561a4232022-03-16 13:16:24 +01002108 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002109#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2111 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002114 return( ret );
2115 }
2116
2117 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002120 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002121 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002122#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002123#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2124 /* We don't need the peer's public key anymore. Free it,
2125 * so that more RAM is available for upcoming expensive
2126 * operations like ECDHE. */
2127 mbedtls_pk_free( peer_pk );
2128#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2129
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002130 return( ret );
2131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2133 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002134
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002135MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002137{
Janos Follath865b3eb2019-12-16 11:46:15 +00002138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002139 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002140 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002141 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2146 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 ssl->state++;
2150 return( 0 );
2151 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002152 ((void) p);
2153 ((void) end);
2154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2157 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2158 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2159 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002160 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002161 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002164 mbedtls_ssl_send_alert_message(
2165 ssl,
2166 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2167 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002168 return( ret );
2169 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002172 ssl->state++;
2173 return( 0 );
2174 }
2175 ((void) p);
2176 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2178 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002179
Gilles Peskineeccd8882020-03-10 12:19:08 +01002180#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002181 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002182 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002183 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002184 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002185 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002186#endif
2187
Hanno Becker327c93b2018-08-15 13:56:18 +01002188 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 return( ret );
2192 }
2193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002197 mbedtls_ssl_send_alert_message(
2198 ssl,
2199 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2200 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 }
2203
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002204 /*
2205 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2206 * doesn't use a psk_identity_hint
2207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2211 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002212 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002213 /* Current message is probably either
2214 * CertificateRequest or ServerHelloDone */
2215 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002216 goto exit;
2217 }
2218
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002219 MBEDTLS_SSL_DEBUG_MSG( 1,
2220 ( "server key exchange message must not be skipped" ) );
2221 mbedtls_ssl_send_alert_message(
2222 ssl,
2223 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2224 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 }
2228
Gilles Peskineeccd8882020-03-10 12:19:08 +01002229#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002230 if( ssl->handshake->ecrs_enabled )
2231 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2232
2233start_processing:
2234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002236 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002238
Gilles Peskineeccd8882020-03-10 12:19:08 +01002239#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2241 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2242 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2243 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002244 {
2245 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002248 mbedtls_ssl_send_alert_message(
2249 ssl,
2250 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002251 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002252 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002253 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002254 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002255#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2258 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2259 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2260 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002261 ; /* nothing more to do */
2262 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2264 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2265#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2266 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2267 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2268 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002270 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002273 mbedtls_ssl_send_alert_message(
2274 ssl,
2275 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2276 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002277 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002278 }
2279 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2282 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002283#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2284 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2286 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002289 {
2290 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002293 mbedtls_ssl_send_alert_message(
2294 ssl,
2295 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2296 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002297 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002298 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002299 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2302 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2303 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002304#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2305 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2306 {
2307 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2308 p, end - p );
2309 if( ret != 0 )
2310 {
2311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002312 mbedtls_ssl_send_alert_message(
2313 ssl,
2314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002315 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2316 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002317 }
2318 }
2319 else
2320#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002324 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002325
Gilles Peskineeccd8882020-03-10 12:19:08 +01002326#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002327 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002328 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002329 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002330#if defined(MBEDTLS_USE_PSA_CRYPTO)
2331 unsigned char hash[PSA_HASH_MAX_SIZE];
2332#else
2333 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2334#endif
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 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002474#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002475 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2476#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002477 mbedtls_ssl_send_alert_message(
2478 ssl,
2479 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2480 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002482#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002483 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2484 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2485#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002486 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002487 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002488
2489#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2490 /* We don't need the peer's public key anymore. Free it,
2491 * so that more RAM is available for upcoming expensive
2492 * operations like ECDHE. */
2493 mbedtls_pk_free( peer_pk );
2494#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002496#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002498exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 ssl->state++;
2500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
2503 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504}
2505
Gilles Peskineeccd8882020-03-10 12:19:08 +01002506#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002507MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002509{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002510 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002511 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002514
Hanno Becker1aa267c2017-04-28 17:08:27 +01002515 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002518 ssl->state++;
2519 return( 0 );
2520 }
2521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002524}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002525#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002526MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528{
Janos Follath865b3eb2019-12-16 11:46:15 +00002529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002530 unsigned char *buf;
2531 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002532 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002533 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002534 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002535 size_t sig_alg_len;
2536#if defined(MBEDTLS_DEBUG_C)
2537 unsigned char *sig_alg;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002538 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Hanno Becker1aa267c2017-04-28 17:08:27 +01002543 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002546 ssl->state++;
2547 return( 0 );
2548 }
2549
Hanno Becker327c93b2018-08-15 13:56:18 +01002550 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2553 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002556 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2557 {
2558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002559 mbedtls_ssl_send_alert_message(
2560 ssl,
2561 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2562 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002566 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002567 ssl->handshake->client_auth =
2568 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002571 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Jerry Yufb28b882022-01-28 11:05:58 +08002573 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002574 {
Johan Pascala89ca862020-08-25 10:03:19 +02002575 /* Current message is probably the ServerHelloDone */
2576 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002577 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002578 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002579
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002580 /*
2581 * struct {
2582 * ClientCertificateType certificate_types<1..2^8-1>;
2583 * SignatureAndHashAlgorithm
2584 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2585 * DistinguishedName certificate_authorities<0..2^16-1>;
2586 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002587 *
2588 * Since we only support a single certificate on clients, let's just
2589 * ignore all the information that's supposed to help us pick a
2590 * certificate.
2591 *
2592 * We could check that our certificate matches the request, and bail out
2593 * if it doesn't, but it's simpler to just send the certificate anyway,
2594 * and give the server the opportunity to decide if it should terminate
2595 * the connection when it doesn't like our certificate.
2596 *
2597 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2598 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002599 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002600 *
2601 * However, we still minimally parse the message to check it is at least
2602 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002603 */
Paul Bakker926af752012-11-23 13:38:07 +01002604 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002605
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002606 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002607 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2608 {
2609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2611 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002612 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002615 n = cert_type_len;
2616
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002617 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002618 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002619 * * the length of the signature algorithms field (if minor version of
2620 * SSL is 3),
2621 * * distinguished name length otherwise.
2622 * Both reach at most the index:
2623 * ...hdr_len + 2 + n,
2624 * therefore the buffer length at this point must be greater than that
2625 * regardless of the actual code path.
2626 */
2627 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002630 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2631 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002632 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002633 }
2634
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002635 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002636 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2637 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2638
2639 /*
2640 * The furthest access in buf is in the loop few lines below:
2641 * sig_alg[i + 1],
2642 * where:
2643 * sig_alg = buf + ...hdr_len + 3 + n,
2644 * max(i) = sig_alg_len - 1.
2645 * Therefore the furthest access is:
2646 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2647 * which reduces to:
2648 * buf[...hdr_len + 3 + n + sig_alg_len],
2649 * which is one less than we need the buf to be.
2650 */
2651 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002652 {
Ronald Cron90915f22022-03-07 11:11:36 +01002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2654 mbedtls_ssl_send_alert_message(
2655 ssl,
2656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2657 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2658 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002659 }
Paul Bakker926af752012-11-23 13:38:07 +01002660
Ronald Cron90915f22022-03-07 11:11:36 +01002661#if defined(MBEDTLS_DEBUG_C)
2662 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2663 for( size_t i = 0; i < sig_alg_len; i += 2 )
2664 {
2665 MBEDTLS_SSL_DEBUG_MSG( 3,
2666 ( "Supported Signature Algorithm found: %d,%d",
2667 sig_alg[i], sig_alg[i + 1] ) );
2668 }
2669#endif
2670
2671 n += 2 + sig_alg_len;
2672
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002673 /* certificate_authorities */
2674 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2675 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002676
2677 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002678 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002681 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002683 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002684 }
2685
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002686#if defined(MBEDTLS_DEBUG_C)
2687 dn = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n - dn_len;
2688 for( size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len )
2689 {
2690 unsigned char *p = dn + i + 2;
2691 mbedtls_x509_name name;
2692 mbedtls_x509_name *name_cur, *name_prv;
2693 size_t asn1_len;
2694 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
2695 memset( &name, 0, sizeof( name ) );
2696 dni_len = MBEDTLS_GET_UINT16_BE( dn + i, 0 );
2697 if( dni_len > dn_len - i - 2 ||
2698 mbedtls_asn1_get_tag( &p, p + dni_len, &asn1_len,
2699 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) != 0 ||
2700 mbedtls_x509_get_name( &p, p + asn1_len, &name ) != 0 )
2701 {
2702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2703 mbedtls_ssl_send_alert_message(
2704 ssl,
2705 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2706 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2707 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
2708 }
2709 MBEDTLS_SSL_DEBUG_MSG( 3,
2710 ( "DN hint: %.*s",
2711 mbedtls_x509_dn_gets( s, sizeof(s), &name ), s ) );
2712 name_cur = name.next;
2713 while( name_cur != NULL )
2714 {
2715 name_prv = name_cur;
2716 name_cur = name_cur->next;
2717 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2718 mbedtls_free( name_prv );
2719 }
2720 }
2721#endif
2722
Paul Bakker926af752012-11-23 13:38:07 +01002723exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
2726 return( 0 );
2727}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002728#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002730MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002732{
Janos Follath865b3eb2019-12-16 11:46:15 +00002733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Hanno Becker327c93b2018-08-15 13:56:18 +01002737 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2740 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002742
2743 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2744 {
2745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2746 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2747 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2750 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002753 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2754 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002755 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
2757
2758 ssl->state++;
2759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002763#endif
2764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
2767 return( 0 );
2768}
2769
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002770MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772{
Janos Follath865b3eb2019-12-16 11:46:15 +00002773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002774
2775 size_t header_len;
2776 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002778 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2783 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 /*
2786 * DHM key exchange -- send G^X mod P
2787 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002788 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Joe Subbiani6dd73642021-07-19 11:56:54 +01002790 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002791 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002794 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002795 &ssl->out_msg[header_len], content_len,
2796 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 if( ret != 0 )
2798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 return( ret );
2801 }
2802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2804 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002807 ssl->handshake->premaster,
2808 MBEDTLS_PREMASTER_SIZE,
2809 &ssl->handshake->pmslen,
2810 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 return( ret );
2814 }
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 }
2818 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002820#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2821 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2822 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2823 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002824 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002825 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2826 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2827 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002828 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002829#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2831 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002832 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002833
2834 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2835
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002836 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002837
Hanno Becker0a94a642019-01-11 14:35:30 +00002838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2839
Hanno Becker4a63ed42019-01-08 11:39:35 +00002840 /*
2841 * Generate EC private key for ECDHE exchange.
2842 */
2843
Hanno Becker4a63ed42019-01-08 11:39:35 +00002844 /* The master secret is obtained from the shared ECDH secret by
2845 * applying the TLS 1.2 PRF with a specific salt and label. While
2846 * the PSA Crypto API encourages combining key agreement schemes
2847 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2848 * yet support the provisioning of salt + label to the KDF.
2849 * For the time being, we therefore need to split the computation
2850 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002851 key_attributes = psa_key_attributes_init();
2852 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002853 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002854 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2855 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002856
2857 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002858 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002859 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002860 if( status != PSA_SUCCESS )
2861 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2862
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002863 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002864 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002865 * but we just need to add a length byte before that. */
2866 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2867 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2868 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2869 size_t own_pubkey_len;
2870
Hanno Becker4a63ed42019-01-08 11:39:35 +00002871 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002872 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002873 &own_pubkey_len );
2874 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002875 {
2876 psa_destroy_key( handshake->ecdh_psa_privkey );
2877 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002878 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002879 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002880
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002881 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2882 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002883
Hanno Becker4a63ed42019-01-08 11:39:35 +00002884 /* The ECDH secret is the premaster secret used for key derivation. */
2885
Janos Follathdf3b0892019-08-08 11:12:24 +01002886 /* Compute ECDH shared secret. */
2887 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2888 handshake->ecdh_psa_privkey,
2889 handshake->ecdh_psa_peerkey,
2890 handshake->ecdh_psa_peerkey_len,
2891 ssl->handshake->premaster,
2892 sizeof( ssl->handshake->premaster ),
2893 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002894
Andrzej Kureka0237f82022-02-24 13:24:52 -05002895 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002896 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002897
2898 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2899 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002900#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002901 /*
2902 * ECDH key exchange -- send client public value
2903 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002904 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002905
Gilles Peskineeccd8882020-03-10 12:19:08 +01002906#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002907 if( ssl->handshake->ecrs_enabled )
2908 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002909 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002910 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002911
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002912 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2913 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002914#endif
2915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002917 &content_len,
2918 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002919 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002920 if( ret != 0 )
2921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002923#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002924 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2925 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2926#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002927 return( ret );
2928 }
2929
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002930 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2931 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002932
Gilles Peskineeccd8882020-03-10 12:19:08 +01002933#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002934 if( ssl->handshake->ecrs_enabled )
2935 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002936 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002937 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002938 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002939
2940ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002941 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002942 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002945 &ssl->handshake->pmslen,
2946 ssl->handshake->premaster,
2947 MBEDTLS_MPI_MAX_SIZE,
2948 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002951#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002952 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2953 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2954#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002955 return( ret );
2956 }
2957
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002958 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2959 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02002960#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01002961 }
2962 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2964 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2965 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2966 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002967#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2968 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2969 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2970 {
2971 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2972 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2973 psa_key_attributes_t key_attributes;
2974
2975 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2976
2977 /*
2978 * opaque psk_identity<0..2^16-1>;
2979 */
2980 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002981 /* We don't offer PSK suites if we don't have a PSK,
2982 * and we check that the server's choice is among the
2983 * ciphersuites we offered, so this should never happen. */
2984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002985
Neil Armstrongfc834f22022-03-23 17:54:38 +01002986 /* uint16 to store content length */
2987 const size_t content_len_size = 2;
2988
Neil Armstrong868af822022-03-09 10:26:25 +01002989 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002990
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002991 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002992 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002993 {
2994 MBEDTLS_SSL_DEBUG_MSG( 1,
2995 ( "psk identity too long or SSL buffer too short" ) );
2996 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2997 }
2998
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002999 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003000
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003001 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
3002 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
3003 header_len += content_len_size;
3004
3005 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003006 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003007 p += ssl->conf->psk_identity_len;
3008
Neil Armstrong868af822022-03-09 10:26:25 +01003009 header_len += ssl->conf->psk_identity_len;
3010
3011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3012
3013 /*
3014 * Generate EC private key for ECDHE exchange.
3015 */
3016
3017 /* The master secret is obtained from the shared ECDH secret by
3018 * applying the TLS 1.2 PRF with a specific salt and label. While
3019 * the PSA Crypto API encourages combining key agreement schemes
3020 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3021 * yet support the provisioning of salt + label to the KDF.
3022 * For the time being, we therefore need to split the computation
3023 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3024 key_attributes = psa_key_attributes_init();
3025 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3026 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3027 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3028 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3029
3030 /* Generate ECDH private key. */
3031 status = psa_generate_key( &key_attributes,
3032 &handshake->ecdh_psa_privkey );
3033 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003034 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003035
3036 /* Export the public part of the ECDH private key from PSA.
3037 * The export format is an ECPoint structure as expected by TLS,
3038 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003039 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003040 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3041 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003042 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003043
3044 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3045 own_pubkey, own_pubkey_max_len,
3046 &own_pubkey_len );
3047 if( status != PSA_SUCCESS )
3048 {
3049 psa_destroy_key( handshake->ecdh_psa_privkey );
3050 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003051 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003052 }
3053
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003054 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003055 content_len = own_pubkey_len + 1;
3056
Neil Armstrong25400452022-03-23 17:44:07 +01003057 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3058 * - a uint16 containing the length (in octets) of the ECDH computation
3059 * - the octet string produced by the ECDH computation
3060 * - a uint16 containing the length (in octets) of the PSK
3061 * - the PSK itself
3062 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003063 unsigned char *pms = ssl->handshake->premaster;
3064 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003065 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003066 /* uint16 to store length (in octets) of the ECDH computation */
3067 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003068 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003069
Neil Armstrong25400452022-03-23 17:44:07 +01003070 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003071 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3072 handshake->ecdh_psa_privkey,
3073 handshake->ecdh_psa_peerkey,
3074 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003075 pms + zlen_size,
3076 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003077 &zlen );
3078
3079 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3080 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3081
Neil Armstrongc530aa62022-03-23 17:45:01 +01003082 if( status != PSA_SUCCESS )
3083 return( psa_ssl_status_to_mbedtls( status ) );
3084 else if( destruction_status != PSA_SUCCESS )
3085 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003086
Neil Armstrong25400452022-03-23 17:44:07 +01003087 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003088 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3089 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003090 }
3091 else
3092#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3093 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003094#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003095 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003096 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003097 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003098 * opaque psk_identity<0..2^16-1>;
3099 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003100 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003101 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003102 /* We don't offer PSK suites if we don't have a PSK,
3103 * and we check that the server's choice is among the
3104 * ciphersuites we offered, so this should never happen. */
3105 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003106 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003107
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003108 header_len = 4;
3109 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003110
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003111 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003112 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003113 MBEDTLS_SSL_DEBUG_MSG( 1,
3114 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003115 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3116 }
3117
Joe Subbianifbeb6922021-07-16 14:27:50 +01003118 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3119 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003120
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003121 memcpy( ssl->out_msg + header_len,
3122 ssl->conf->psk_identity,
3123 ssl->conf->psk_identity_len );
3124 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3127 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003128 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003129 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003130 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003131 else
3132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3134 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003135 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003136 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3137 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003138 return( ret );
3139 }
3140 else
3141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3143 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003144 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003145 /*
3146 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3147 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003148 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003149
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003150 if( header_len + 2 + content_len >
3151 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003152 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003153 MBEDTLS_SSL_DEBUG_MSG( 1,
3154 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003155 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3156 }
3157
Joe Subbianifbeb6922021-07-16 14:27:50 +01003158 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3159 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003162 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003163 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003164 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003165 if( ret != 0 )
3166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003168 return( ret );
3169 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003170
3171#if defined(MBEDTLS_USE_PSA_CRYPTO)
3172 unsigned char *pms = ssl->handshake->premaster;
3173 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3174 size_t pms_len;
3175
3176 /* Write length only when we know the actual value */
3177 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3178 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3179 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3180 {
3181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3182 return( ret );
3183 }
3184 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3185 pms += 2 + pms_len;
3186
3187 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3188#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003189 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003192#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3193 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003195 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003196 /*
3197 * ClientECDiffieHellmanPublic public;
3198 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003199 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3200 &content_len,
3201 &ssl->out_msg[header_len],
3202 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003203 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003204 if( ret != 0 )
3205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003207 return( ret );
3208 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003209
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003210 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3211 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003212 }
3213 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003214#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003218 }
3219
Neil Armstrong80f6f322022-05-03 17:56:38 +02003220#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3221 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3222 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003223 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003224 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003225 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003226 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003227 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003228#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003229 }
3230 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003231#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3233 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003235 header_len = 4;
3236 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3237 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003238 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 }
Paul Bakkered27a042013-04-18 22:46:23 +02003240 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003242#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3243 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3244 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003245 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003246
3247 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003248 ssl->out_msg + header_len,
3249 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3250 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003251 ssl->conf->f_rng, ssl->conf->p_rng );
3252 if( ret != 0 )
3253 {
3254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3255 return( ret );
3256 }
3257
3258 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3259 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3260 ssl->conf->f_rng, ssl->conf->p_rng );
3261 if( ret != 0 )
3262 {
3263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3264 return( ret );
3265 }
3266 }
3267 else
3268#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003269 {
3270 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3272 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003274
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003275 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3277 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
3279 ssl->state++;
3280
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003281 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 return( ret );
3285 }
3286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
3289 return( 0 );
3290}
3291
Gilles Peskineeccd8882020-03-10 12:19:08 +01003292#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003293MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003296 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003297 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003305 return( ret );
3306 }
3307
Hanno Becker77adddc2019-02-07 12:32:43 +00003308 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003311 ssl->state++;
3312 return( 0 );
3313 }
3314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003317}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003318#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003319MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003321{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003323 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003324 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003325 size_t n = 0, offset = 0;
3326 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003327 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003329 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003330 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003331#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3332 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3333#else
3334 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3335#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003338
Gilles Peskineeccd8882020-03-10 12:19:08 +01003339#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003340 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003341 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003342 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003343 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003344 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003345#endif
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003350 return( ret );
3351 }
3352
Hanno Becker77adddc2019-02-07 12:32:43 +00003353 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003356 ssl->state++;
3357 return( 0 );
3358 }
3359
Jerry Yufb28b882022-01-28 11:05:58 +08003360 if( ssl->handshake->client_auth == 0 ||
3361 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 {
Johan Pascala89ca862020-08-25 10:03:19 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3364 ssl->state++;
3365 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 }
3367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003372 }
3373
3374 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003375 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003377#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003378 if( ssl->handshake->ecrs_enabled )
3379 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3380
3381sign:
3382#endif
3383
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003384 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003385
Ronald Cron90915f22022-03-07 11:11:36 +01003386 /*
3387 * digitally-signed struct {
3388 * opaque handshake_messages[handshake_messages_length];
3389 * };
3390 *
3391 * Taking shortcut here. We assume that the server always allows the
3392 * PRF Hash function and has sent it in the allowed signature
3393 * algorithms list received in the Certificate Request message.
3394 *
3395 * Until we encounter a server that does not, we will take this
3396 * shortcut.
3397 *
3398 * Reason: Otherwise we should have running hashes for SHA512 and
3399 * SHA224 in order to satisfy 'weird' needs from the server
3400 * side.
3401 */
3402 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003403 {
Ronald Cron90915f22022-03-07 11:11:36 +01003404 md_alg = MBEDTLS_MD_SHA384;
3405 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003406 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003407 else
Paul Bakker577e0062013-08-28 11:57:20 +02003408 {
Ronald Cron90915f22022-03-07 11:11:36 +01003409 md_alg = MBEDTLS_MD_SHA256;
3410 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003411 }
Ronald Cron90915f22022-03-07 11:11:36 +01003412 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3413
3414 /* Info from md_alg will be used instead */
3415 hashlen = 0;
3416 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003417
Gilles Peskineeccd8882020-03-10 12:19:08 +01003418#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003419 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003420 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003421#endif
3422
3423 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3424 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003425 ssl->out_msg + 6 + offset,
3426 out_buf_len - 6 - offset,
3427 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003428 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003431#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003432 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3433 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3434#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003435 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003436 }
Paul Bakker926af752012-11-23 13:38:07 +01003437
Joe Subbiani6dd73642021-07-19 11:56:54 +01003438 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
Paul Bakker1ef83d62012-04-11 12:09:53 +00003440 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3442 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003443
3444 ssl->state++;
3445
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003446 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 return( ret );
3450 }
3451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
Paul Bakkered27a042013-04-18 22:46:23 +02003454 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003456#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003459MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003461{
Janos Follath865b3eb2019-12-16 11:46:15 +00003462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463 uint32_t lifetime;
3464 size_t ticket_len;
3465 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003466 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003469
Hanno Becker327c93b2018-08-15 13:56:18 +01003470 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003473 return( ret );
3474 }
3475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003479 mbedtls_ssl_send_alert_message(
3480 ssl,
3481 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3482 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484 }
3485
3486 /*
3487 * struct {
3488 * uint32 ticket_lifetime_hint;
3489 * opaque ticket<0..2^16-1>;
3490 * } NewSessionTicket;
3491 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003492 * 0 . 3 ticket_lifetime_hint
3493 * 4 . 5 ticket_len (n)
3494 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3497 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003500 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3501 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003502 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003503 }
3504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003506
Philippe Antoineb5b25432018-05-11 11:06:29 +02003507 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3508 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003509
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003510 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003515 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3516 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003517 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003518 }
3519
Paul Elliottd48d5c62021-01-07 14:47:05 +00003520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003521
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003522 /* We're not waiting for a NewSessionTicket message any more */
3523 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003525
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003526 /*
3527 * Zero-length ticket means the server changed his mind and doesn't want
3528 * to send a ticket after all, so just forget it
3529 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003530 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003531 return( 0 );
3532
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003533 if( ssl->session != NULL && ssl->session->ticket != NULL )
3534 {
3535 mbedtls_platform_zeroize( ssl->session->ticket,
3536 ssl->session->ticket_len );
3537 mbedtls_free( ssl->session->ticket );
3538 ssl->session->ticket = NULL;
3539 ssl->session->ticket_len = 0;
3540 }
3541
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003542 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3543 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003545 ssl->session_negotiate->ticket = NULL;
3546 ssl->session_negotiate->ticket_len = 0;
3547
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003548 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003549 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003551 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3552 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003553 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003554 }
3555
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003556 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003557
3558 ssl->session_negotiate->ticket = ticket;
3559 ssl->session_negotiate->ticket_len = ticket_len;
3560 ssl->session_negotiate->ticket_lifetime = lifetime;
3561
3562 /*
3563 * RFC 5077 section 3.4:
3564 * "If the client receives a session ticket from the server, then it
3565 * discards any Session ID that was sent in the ServerHello."
3566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003568 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003571
3572 return( 0 );
3573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003575
Paul Bakker5121ce52009-01-03 21:22:43 +00003576/*
Paul Bakker1961b702013-01-25 14:49:24 +01003577 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003580{
3581 int ret = 0;
3582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003584 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3586 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003587 ssl->handshake->new_session_ticket != 0 )
3588 {
Jerry Yua357cf42022-07-12 05:36:45 +00003589 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003590 }
3591#endif
3592
Paul Bakker1961b702013-01-25 14:49:24 +01003593 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 case MBEDTLS_SSL_HELLO_REQUEST:
3596 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 break;
3598
Paul Bakker1961b702013-01-25 14:49:24 +01003599 /*
3600 * ==> ClientHello
3601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003603 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003604 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003605
Paul Bakker1961b702013-01-25 14:49:24 +01003606 /*
3607 * <== ServerHello
3608 * Certificate
3609 * ( ServerKeyExchange )
3610 * ( CertificateRequest )
3611 * ServerHelloDone
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003614 ret = ssl_parse_server_hello( ssl );
3615 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3618 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003619 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003622 ret = ssl_parse_server_key_exchange( ssl );
3623 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003626 ret = ssl_parse_certificate_request( ssl );
3627 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003630 ret = ssl_parse_server_hello_done( ssl );
3631 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
Paul Bakker1961b702013-01-25 14:49:24 +01003633 /*
3634 * ==> ( Certificate/Alert )
3635 * ClientKeyExchange
3636 * ( CertificateVerify )
3637 * ChangeCipherSpec
3638 * Finished
3639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3641 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003642 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003645 ret = ssl_write_client_key_exchange( ssl );
3646 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003649 ret = ssl_write_certificate_verify( ssl );
3650 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3653 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003654 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 case MBEDTLS_SSL_CLIENT_FINISHED:
3657 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003658 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
Paul Bakker1961b702013-01-25 14:49:24 +01003660 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003661 * <== ( NewSessionTicket )
3662 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003663 * Finished
3664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00003666 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003667 ret = ssl_parse_new_session_ticket( ssl );
3668 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003669#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3672 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003673 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 case MBEDTLS_SSL_SERVER_FINISHED:
3676 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003677 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 case MBEDTLS_SSL_FLUSH_BUFFERS:
3680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3681 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003682 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3685 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003686 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003687
Paul Bakker1961b702013-01-25 14:49:24 +01003688 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
3693 return( ret );
3694}
Jerry Yuc5aef882021-12-23 20:15:02 +08003695
Jerry Yufb4b6472022-01-27 15:03:26 +08003696#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */