blob: 6dd8ef50fe9bbd828fcc3435aa0f80085397a078 [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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020025
SimonBd5800b72016-04-26 07:43:27 +010026#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010027#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000028#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000029#include "mbedtls/debug.h"
30#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020031#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010032
Hanno Beckerbb89e272019-01-08 12:54:37 +000033#if defined(MBEDTLS_USE_PSA_CRYPTO)
34#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020035#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000036#endif /* MBEDTLS_USE_PSA_CRYPTO */
37
SimonBd5800b72016-04-26 07:43:27 +010038#include <string.h>
39
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020040#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010043#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020048#endif
49
Andrzej Kurek0ce59212022-08-17 07:54:34 -040050#include "hash_info.h"
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020053MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +010054static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
55 unsigned char *buf,
56 const unsigned char *end,
57 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010058{
59 unsigned char *p = buf;
60
61 *olen = 0;
62
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010063 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010064 * initial ClientHello, in which case also adding the renegotiation
65 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +010067 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +010068
Hanno Beckerb2fff6d2017-05-08 11:06:19 +010069 MBEDTLS_SSL_DEBUG_MSG( 3,
70 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +010071
Hanno Becker261602c2017-04-12 14:54:42 +010072 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +010073
Paul Bakkerd3edc862013-03-20 16:07:17 +010074 /*
75 * Secure renegotiation
76 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010077 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
78 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +010079
80 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +010081 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
82 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +010083
84 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
85
86 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +010087
88 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +010089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +010091
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +020092#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +010093 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +010094
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020095MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +010096static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
97 unsigned char *buf,
98 const unsigned char *end,
99 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100100{
101 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100102 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100103
104 *olen = 0;
105
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100106 MBEDTLS_SSL_DEBUG_MSG( 3,
107 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100108 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100109
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100110 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
111 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100112
113 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200115
116 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200119 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100120
121 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122}
Darryl Green11999bb2018-03-13 15:22:58 +0000123#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100124 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100125
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200126#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200127MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100128static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
129 unsigned char *buf,
130 const unsigned char *end,
131 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200132{
Janos Follath865b3eb2019-12-16 11:46:15 +0000133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200134 unsigned char *p = buf;
Valerio Setti02c25b52022-11-15 14:08:42 +0100135 size_t kkpp_len = 0;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200136
137 *olen = 0;
138
139 /* Skip costly extension if we can't use EC J-PAKE anyway */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200140#if defined(MBEDTLS_USE_PSA_CRYPTO)
141 if( ssl->handshake->psa_pake_ctx_is_ok != 1 )
142 return( 0 );
143#else
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200144 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100145 return( 0 );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200146#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200147
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100148 MBEDTLS_SSL_DEBUG_MSG( 3,
149 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200150
Hanno Becker261602c2017-04-12 14:54:42 +0100151 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200152
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100153 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
154 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200155
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200156 /*
157 * We may need to send ClientHello multiple times for Hello verification.
158 * We don't want to compute fresh values every time (both for performance
159 * and consistency reasons), so cache the extension content.
160 */
161 if( ssl->handshake->ecjpake_cache == NULL ||
162 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200163 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
165
Neil Armstrongca7d5062022-05-31 14:43:23 +0200166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settia08b1a42022-11-17 15:10:02 +0100167 ret = mbedtls_psa_ecjpake_write_round_one(&ssl->handshake->psa_pake_ctx,
Valerio Setti02c25b52022-11-15 14:08:42 +0100168 p + 2, end - p - 2, &kkpp_len );
169 if ( ret != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +0200170 {
Valerio Setti02c25b52022-11-15 14:08:42 +0100171 psa_destroy_key( ssl->handshake->psa_pake_password );
172 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
173 MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret );
174 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200175 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200176#else
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200177 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100178 p + 2, end - p - 2, &kkpp_len,
179 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200180 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200181 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100182 MBEDTLS_SSL_DEBUG_RET( 1 ,
183 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100184 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200185 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200187
188 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
189 if( ssl->handshake->ecjpake_cache == NULL )
190 {
191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100192 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200193 }
194
195 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
196 ssl->handshake->ecjpake_cache_len = kkpp_len;
197 }
198 else
199 {
200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
201
202 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100203 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200204
205 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200206 }
207
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100208 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
209 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200210
211 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100212
213 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200214}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200215#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000216
Hanno Beckera0e20d02019-05-15 14:03:01 +0100217#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200218MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100219static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
220 unsigned char *buf,
221 const unsigned char *end,
222 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100223{
224 unsigned char *p = buf;
225 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100226
227 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100228 * Quoting draft-ietf-tls-dtls-connection-id-05
229 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100230 *
231 * struct {
232 * opaque cid<0..2^8-1>;
233 * } ConnectionId;
234 */
235
236 *olen = 0;
237 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
238 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
239 {
Hanno Becker261602c2017-04-12 14:54:42 +0100240 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100241 }
242 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
243
244 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
245 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100246 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100247
248 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100249 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
250 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100251 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100252 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
253 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100254
255 *p++ = (uint8_t) ssl->own_cid_len;
256 memcpy( p, ssl->own_cid, ssl->own_cid_len );
257
258 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100259
260 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100261}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100262#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200265MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100266static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
267 unsigned char *buf,
268 const unsigned char *end,
269 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200270{
271 unsigned char *p = buf;
272
Simon Butcher0fc94e92015-09-28 20:52:04 +0100273 *olen = 0;
274
Hanno Becker261602c2017-04-12 14:54:42 +0100275 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
276 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200277
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100278 MBEDTLS_SSL_DEBUG_MSG( 3,
279 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200280
Hanno Becker261602c2017-04-12 14:54:42 +0100281 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100282
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100283 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
284 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200285
286 *p++ = 0x00;
287 *p++ = 1;
288
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200289 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200290
291 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100292
293 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200298MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100299static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
300 unsigned char *buf,
301 const unsigned char *end,
302 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100303{
304 unsigned char *p = buf;
305
Simon Butcher0fc94e92015-09-28 20:52:04 +0100306 *olen = 0;
307
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100308 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100309 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100310
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100311 MBEDTLS_SSL_DEBUG_MSG( 3,
312 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100313
Hanno Becker261602c2017-04-12 14:54:42 +0100314 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100315
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100316 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
317 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100318
319 *p++ = 0x00;
320 *p++ = 0x00;
321
322 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100323
324 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200329MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100330static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
331 unsigned char *buf,
332 const unsigned char *end,
333 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200334{
335 unsigned char *p = buf;
336
Simon Butcher0fc94e92015-09-28 20:52:04 +0100337 *olen = 0;
338
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100339 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100340 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200341
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100342 MBEDTLS_SSL_DEBUG_MSG( 3,
343 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200344
Hanno Becker261602c2017-04-12 14:54:42 +0100345 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100346
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100347 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
348 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200349
350 *p++ = 0x00;
351 *p++ = 0x00;
352
353 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100354
355 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200360MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100361static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
362 unsigned char *buf,
363 const unsigned char *end,
364 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200365{
366 unsigned char *p = buf;
367 size_t tlen = ssl->session_negotiate->ticket_len;
368
Simon Butcher0fc94e92015-09-28 20:52:04 +0100369 *olen = 0;
370
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200371 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100372 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200373
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100374 MBEDTLS_SSL_DEBUG_MSG( 3,
375 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200376
Hanno Becker261602c2017-04-12 14:54:42 +0100377 /* The addition is safe here since the ticket length is 16 bit. */
378 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100379
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100380 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
381 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200382
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100383 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
384 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200385
386 *olen = 4;
387
Simon Butchered997662015-09-28 02:14:30 +0100388 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100389 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200390
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100391 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000392 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
394 memcpy( p, ssl->session_negotiate->ticket, tlen );
395
396 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100397
398 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401
Ron Eldora9788042018-12-05 11:04:31 +0200402#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200403MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascal77696ee2020-09-22 21:49:40 +0200404static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200405 unsigned char *buf,
406 const unsigned char *end,
407 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100408{
409 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200410 size_t protection_profiles_index = 0, ext_len = 0;
411 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100412
413 *olen = 0;
414
Johan Pascalc3ccd982020-10-28 17:18:18 +0100415 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
416 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
417 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100418 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200419 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100420 }
421
Ron Eldora9788042018-12-05 11:04:31 +0200422 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100423 * uint8 SRTPProtectionProfile[2];
424 *
425 * struct {
426 * SRTPProtectionProfiles SRTPProtectionProfiles;
427 * opaque srtp_mki<0..255>;
428 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100429 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100430 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200431 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200432 {
433 mki_len = ssl->dtls_srtp_info.mki_len;
434 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300435 /* Extension length = 2 bytes for profiles length,
436 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
437 * 1 byte for srtp_mki vector length and the mki_len value
438 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200439 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
440
Johan Pascal77696ee2020-09-22 21:49:40 +0200441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
442
443 /* Check there is room in the buffer for the extension + 4 bytes
444 * - the extension tag (2 bytes)
445 * - the extension length (2 bytes)
446 */
447 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
448
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100449 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
450 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200451
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100452 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
453 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100454
Ron Eldor3adb9922017-12-21 10:15:08 +0200455 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200456 /* micro-optimization:
457 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
458 * which is lower than 127, so the upper byte of the length is always 0
459 * For the documentation, the more generic code is left in comments
460 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
461 * >> 8 ) & 0xFF );
462 */
463 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100464 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100465
Ron Eldoref72faf2018-07-12 11:54:20 +0300466 for( protection_profiles_index=0;
467 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
468 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100469 {
Johan Pascal43f94902020-09-22 12:25:52 +0200470 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200471 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200472 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200473 {
474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
475 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100476 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
477 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200478 }
479 else
480 {
481 /*
482 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200483 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200484 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200485 MBEDTLS_SSL_DEBUG_MSG( 3,
486 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200487 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200488 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
489 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200490 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100491 }
492 }
493
Ron Eldor591f1622018-01-22 12:30:04 +0200494 *p++ = mki_len & 0xFF;
495
496 if( mki_len != 0 )
497 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200498 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200499 /*
500 * Increment p to point to the current position.
501 */
502 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300503 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
504 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200505 }
506
Ron Eldoref72faf2018-07-12 11:54:20 +0300507 /*
508 * total extension length: extension type (2 bytes)
509 * + extension length (2 bytes)
510 * + protection profile length (2 bytes)
511 * + 2 * number of protection profiles
512 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200513 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300514 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200515 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200516
517 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100518}
519#endif /* MBEDTLS_SSL_DTLS_SRTP */
520
Ronald Cron4079abc2022-02-20 10:35:26 +0100521int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
522 unsigned char *buf,
523 const unsigned char *end,
524 int uses_ec,
525 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100526{
527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
528 unsigned char *p = buf;
529 size_t ext_len = 0;
530
531 (void) ssl;
532 (void) end;
533 (void) uses_ec;
534 (void) ret;
535 (void) ext_len;
536
537 *out_len = 0;
538
539 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
540 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
541#if defined(MBEDTLS_SSL_RENEGOTIATION)
542 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
543 {
544 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
545 return( ret );
546 }
547 p += ext_len;
548#endif
549
550#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
551 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
552 if( uses_ec )
553 {
554 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
555 &ext_len ) ) != 0 )
556 {
557 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
558 return( ret );
559 }
560 p += ext_len;
561 }
562#endif
563
564#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
565 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
566 {
567 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
568 return( ret );
569 }
570 p += ext_len;
571#endif
572
573#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
574 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
575 {
576 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
577 return( ret );
578 }
579 p += ext_len;
580#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
581
582#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
583 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
584 &ext_len ) ) != 0 )
585 {
586 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
587 return( ret );
588 }
589 p += ext_len;
590#endif
591
592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
593 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
594 {
595 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
596 return( ret );
597 }
598 p += ext_len;
599#endif
600
601#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
602 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
603 {
604 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
605 return( ret );
606 }
607 p += ext_len;
608#endif
609
610#if defined(MBEDTLS_SSL_DTLS_SRTP)
611 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
612 {
613 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
614 return( ret );
615 }
616 p += ext_len;
617#endif
618
619#if defined(MBEDTLS_SSL_SESSION_TICKETS)
620 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
621 {
622 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
623 return( ret );
624 }
625 p += ext_len;
626#endif
627
628 *out_len = p - buf;
629
630 return( 0 );
631}
632
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200633MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200635 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000636 size_t len )
637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_SSL_RENEGOTIATION)
639 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000640 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100641 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000642 if( len != 1 + ssl->verify_data_len * 2 ||
643 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200644 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100645 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200646 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100647 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100650 mbedtls_ssl_send_alert_message(
651 ssl,
652 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
653 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100654 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000655 }
656 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100657 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100659 {
660 if( len != 1 || buf[0] != 0x00 )
661 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200662 MBEDTLS_SSL_DEBUG_MSG( 1,
663 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100664 mbedtls_ssl_send_alert_message(
665 ssl,
666 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
667 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100668 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100672 }
Paul Bakker48916f92012-09-16 19:57:18 +0000673
674 return( 0 );
675}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200678MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200680 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200681 size_t len )
682{
683 /*
684 * server should use the extension only if we did,
685 * and if so the server's value should match ours (and len is always 1)
686 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200687 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200688 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200689 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200690 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100691 MBEDTLS_SSL_DEBUG_MSG( 1,
692 ( "non-matching max fragment length extension" ) );
693 mbedtls_ssl_send_alert_message(
694 ssl,
695 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100696 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
697 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200698 }
699
700 return( 0 );
701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000703
Hanno Beckera0e20d02019-05-15 14:03:01 +0100704#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200705MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera8373a12019-04-26 15:37:26 +0100706static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
707 const unsigned char *buf,
708 size_t len )
709{
710 size_t peer_cid_len;
711
712 if( /* CID extension only makes sense in DTLS */
713 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
714 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100715 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100716 {
Hanno Becker22626482019-05-03 12:46:59 +0100717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100718 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100719 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100720 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100721 }
722
723 if( len == 0 )
724 {
725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100727 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
728 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100729 }
730
731 peer_cid_len = *buf++;
732 len--;
733
734 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
735 {
Hanno Becker22626482019-05-03 12:46:59 +0100736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100738 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
739 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100740 }
741
742 if( len != peer_cid_len )
743 {
Hanno Becker22626482019-05-03 12:46:59 +0100744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100746 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
747 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100748 }
749
Hanno Becker5a299902019-05-03 12:47:49 +0100750 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100751 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
752 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
753
754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
755 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
756
Hanno Beckera8373a12019-04-26 15:37:26 +0100757 return( 0 );
758}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100759#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200762MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100764 const unsigned char *buf,
765 size_t len )
766{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200767 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100768 len != 0 )
769 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100770 MBEDTLS_SSL_DEBUG_MSG( 1,
771 ( "non-matching encrypt-then-MAC extension" ) );
772 mbedtls_ssl_send_alert_message(
773 ssl,
774 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100775 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100776 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100777 }
778
779 ((void) buf);
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100782
783 return( 0 );
784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200788MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200790 const unsigned char *buf,
791 size_t len )
792{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200793 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200794 len != 0 )
795 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100796 MBEDTLS_SSL_DEBUG_MSG( 1,
797 ( "non-matching extended master secret extension" ) );
798 mbedtls_ssl_send_alert_message(
799 ssl,
800 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100801 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
802 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200803 }
804
805 ((void) buf);
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200808
809 return( 0 );
810}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200814MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200816 const unsigned char *buf,
817 size_t len )
818{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200819 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200820 len != 0 )
821 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100822 MBEDTLS_SSL_DEBUG_MSG( 1,
823 ( "non-matching session ticket extension" ) );
824 mbedtls_ssl_send_alert_message(
825 ssl,
826 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100827 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
828 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200829 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200830
831 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200832
833 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200834
835 return( 0 );
836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200838
Robert Cragie136884c2015-10-02 13:34:31 +0100839#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100840 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200841MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200843 const unsigned char *buf,
844 size_t len )
845{
846 size_t list_size;
847 const unsigned char *p;
848
Philippe Antoine747fd532018-05-30 09:13:21 +0200849 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200852 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
853 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100854 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200855 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200856 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200857
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200858 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200859 while( list_size > 0 )
860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
862 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200863 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200864#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
865 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200866 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200867#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
868 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200869#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
870 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200871 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
872 p[0] );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200873#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200875 return( 0 );
876 }
877
878 list_size--;
879 p++;
880 }
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200883 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
884 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100885 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200886}
Darryl Green11999bb2018-03-13 15:22:58 +0000887#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100888 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200889
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200890#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200891MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200892static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
893 const unsigned char *buf,
894 size_t len )
895{
Janos Follath865b3eb2019-12-16 11:46:15 +0000896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200897
Hanno Beckere694c3e2017-12-27 21:34:08 +0000898 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200899 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
900 {
901 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
902 return( 0 );
903 }
904
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200905 /* If we got here, we no longer need our cached extension */
906 mbedtls_free( ssl->handshake->ecjpake_cache );
907 ssl->handshake->ecjpake_cache = NULL;
908 ssl->handshake->ecjpake_cache_len = 0;
909
Neil Armstrongca7d5062022-05-31 14:43:23 +0200910#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settia08b1a42022-11-17 15:10:02 +0100911 if( ( ret = mbedtls_psa_ecjpake_read_round_one(
Valerio Setti02c25b52022-11-15 14:08:42 +0100912 &ssl->handshake->psa_pake_ctx, buf, len ) ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +0200913 {
Valerio Setti02c25b52022-11-15 14:08:42 +0100914 psa_destroy_key( ssl->handshake->psa_pake_password );
915 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200916
Valerio Setti02c25b52022-11-15 14:08:42 +0100917 MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round one", ret );
918 mbedtls_ssl_send_alert_message(
919 ssl,
920 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
921 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
922 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200923 }
924
925 return( 0 );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200926#else
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200927 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
928 buf, len ) ) != 0 )
929 {
930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100931 mbedtls_ssl_send_alert_message(
932 ssl,
933 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
934 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200935 return( ret );
936 }
937
938 return( 0 );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200939#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200940}
941#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200944MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200946 const unsigned char *buf, size_t len )
947{
948 size_t list_len, name_len;
949 const char **p;
950
951 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200952 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200953 {
954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100955 mbedtls_ssl_send_alert_message(
956 ssl,
957 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100958 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
959 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200960 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200961
962 /*
963 * opaque ProtocolName<1..2^8-1>;
964 *
965 * struct {
966 * ProtocolName protocol_name_list<2..2^16-1>
967 * } ProtocolNameList;
968 *
969 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
970 */
971
972 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
973 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200974 {
975 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
976 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100977 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200978 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200979
980 list_len = ( buf[0] << 8 ) | buf[1];
981 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200982 {
983 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
984 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100985 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200986 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200987
988 name_len = buf[2];
989 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200990 {
991 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
992 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100993 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200994 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200995
996 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200997 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200998 {
999 if( name_len == strlen( *p ) &&
1000 memcmp( buf + 3, *p, name_len ) == 0 )
1001 {
1002 ssl->alpn_chosen = *p;
1003 return( 0 );
1004 }
1005 }
1006
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001008 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1009 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001010 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001013
Johan Pascalb62bb512015-12-03 21:56:45 +01001014#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001015MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascalb62bb512015-12-03 21:56:45 +01001016static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001017 const unsigned char *buf,
1018 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001019{
Johan Pascal43f94902020-09-22 12:25:52 +02001020 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001021 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001022 uint16_t server_protection_profile_value = 0;
1023
1024 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001025 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1026 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1027 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001028 return( 0 );
1029
Ron Eldora9788042018-12-05 11:04:31 +02001030 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001031 * uint8 SRTPProtectionProfile[2];
1032 *
1033 * struct {
1034 * SRTPProtectionProfiles SRTPProtectionProfiles;
1035 * opaque srtp_mki<0..255>;
1036 * } UseSRTPData;
1037
1038 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1039 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001040 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001041 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001042 {
1043 mki_len = ssl->dtls_srtp_info.mki_len;
1044 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001045
Ron Eldoref72faf2018-07-12 11:54:20 +03001046 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001047 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1048 * + protection profile (2 bytes)
1049 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001050 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001051 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001052 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001053 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001054
1055 /*
1056 * get the server protection profile
1057 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001058
1059 /*
1060 * protection profile length must be 0x0002 as we must have only
1061 * one protection profile in server Hello
1062 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001063 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001064 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001065
1066 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001067 server_protection = mbedtls_ssl_check_srtp_profile_value(
1068 server_protection_profile_value );
1069 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001070 {
Johan Pascal43f94902020-09-22 12:25:52 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1072 mbedtls_ssl_get_srtp_profile_as_string(
1073 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001074 }
1075
Johan Pascal43f94902020-09-22 12:25:52 +02001076 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001077
Johan Pascalb62bb512015-12-03 21:56:45 +01001078 /*
1079 * Check we have the server profile in our list
1080 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001081 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001082 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001083 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1084 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001085 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1087 mbedtls_ssl_get_srtp_profile_as_string(
1088 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001089 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001090 }
1091 }
1092
Ron Eldor591f1622018-01-22 12:30:04 +02001093 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001094 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001095 {
1096 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001097 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001098 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001099 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001100
1101 /* If server does not use mki in its reply, make sure the client won't keep
1102 * one as negotiated */
1103 if( len == 5 )
1104 {
1105 ssl->dtls_srtp_info.mki_len = 0;
1106 }
1107
Ron Eldoref72faf2018-07-12 11:54:20 +03001108 /*
1109 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001110 * If the client detects a nonzero-length MKI in the server's response
1111 * that is different than the one the client offered, then the client
1112 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1113 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001114 if( len > 5 && ( buf[4] != mki_len ||
1115 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001116 {
1117 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1118 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001119 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001120 }
Ron Eldorb4655392018-07-05 18:25:39 +03001121#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001122 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001123 {
Ron Eldora9788042018-12-05 11:04:31 +02001124 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1125 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001126 }
1127#endif
Ron Eldora9788042018-12-05 11:04:31 +02001128 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001129}
1130#endif /* MBEDTLS_SSL_DTLS_SRTP */
1131
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001132/*
1133 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001136MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001140 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001141 unsigned char cookie_len;
1142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001144
Gilles Peskineb64bf062019-09-27 14:02:44 +02001145 /* Check that there is enough room for:
1146 * - 2 bytes of version
1147 * - 1 byte of cookie_len
1148 */
1149 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1150 {
1151 MBEDTLS_SSL_DEBUG_MSG( 1,
1152 ( "incoming HelloVerifyRequest message is too short" ) );
1153 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1154 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001155 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001156 }
1157
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001158 /*
1159 * struct {
1160 * ProtocolVersion server_version;
1161 * opaque cookie<0..2^8-1>;
1162 * } HelloVerifyRequest;
1163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001165 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001166 p += 2;
1167
TRodziewicz2d8800e2021-05-13 19:14:19 +02001168 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001169 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1170 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1171 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001172 */
Glenn Strauss83158112022-04-13 14:59:34 -04001173 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1178 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001179
Hanno Beckerbc000442021-06-24 09:18:19 +01001180 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001181 }
1182
1183 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001184 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1185 {
1186 MBEDTLS_SSL_DEBUG_MSG( 1,
1187 ( "cookie length does not match incoming message size" ) );
1188 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1189 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001190 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001191 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001192 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001193
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001194 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001195
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001196 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1197 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001198 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001200 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001201 }
1202
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001203 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001204 ssl->handshake->verify_cookie_len = cookie_len;
1205
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001206 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1208 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001213
1214 return( 0 );
1215}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001217
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001218MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001221 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001222 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001223 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001224 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001225 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001227 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001228#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001229 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Hanno Becker327c93b2018-08-15 13:56:18 +01001234 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001236 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 return( ret );
1239 }
1240
Hanno Becker79594fd2019-05-08 09:38:41 +01001241 buf = ssl->in_msg;
1242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_SSL_RENEGOTIATION)
1246 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001247 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001248 ssl->renego_records_seen++;
1249
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001250 if( ssl->conf->renego_max_records >= 0 &&
1251 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001252 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001253 MBEDTLS_SSL_DEBUG_MSG( 1,
1254 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001256 }
1257
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001258 MBEDTLS_SSL_DEBUG_MSG( 1,
1259 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001260
1261 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001263 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001267 mbedtls_ssl_send_alert_message(
1268 ssl,
1269 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1270 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001281 return( ssl_parse_hello_verify_request( ssl ) );
1282 }
1283 else
1284 {
1285 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001286 mbedtls_free( ssl->handshake->cookie );
1287 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001288 ssl->handshake->verify_cookie_len = 0;
1289 }
1290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1294 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001297 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1298 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001299 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
1301
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001302 /*
1303 * 0 . 1 server_version
1304 * 2 . 33 random (maybe including 4 bytes of Unix time)
1305 * 34 . 34 session_id length = n
1306 * 35 . 34+n session_id
1307 * 35+n . 36+n cipher_suite
1308 * 37+n . 37+n compression_method
1309 *
1310 * 38+n . 39+n extensions length (optional)
1311 * 40+n . .. extensions
1312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001314
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001315 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1316 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001317 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Glenn Strauss60bfe602022-03-14 19:04:24 -04001319 if( ssl->tls_version < ssl->conf->min_tls_version ||
1320 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001321 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001322 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001323 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1324 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001325 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001326 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1329 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001330
Hanno Beckerbc000442021-06-24 09:18:19 +01001331 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001332 }
1333
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001335 ( (unsigned long) buf[2] << 24 ) |
1336 ( (unsigned long) buf[3] << 16 ) |
1337 ( (unsigned long) buf[4] << 8 ) |
1338 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001340 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001342 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Paul Bakker48916f92012-09-16 19:57:18 +00001346 if( n > 32 )
1347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001349 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1350 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001351 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001352 }
1353
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001354 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001356 ext_len = ( ( buf[38 + n] << 8 )
1357 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
Paul Bakker48916f92012-09-16 19:57:18 +00001359 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001363 mbedtls_ssl_send_alert_message(
1364 ssl,
1365 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 );
Paul Bakker48916f92012-09-16 19:57:18 +00001368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001370 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001371 {
1372 ext_len = 0;
1373 }
1374 else
1375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001377 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1378 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001379 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001382 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001383 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001384
1385 /*
1386 * Read and check compression
1387 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001388 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001390 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001391 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001392 MBEDTLS_SSL_DEBUG_MSG( 1,
1393 ( "server hello, bad compression: %d", comp ) );
1394 mbedtls_ssl_send_alert_message(
1395 ssl,
1396 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1397 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001399 }
1400
Paul Bakker380da532012-04-18 16:10:25 +00001401 /*
1402 * Initialize update checksum functions
1403 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001404 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1405 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001406 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001407 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001408 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001409 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1410 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001412 }
Paul Bakker380da532012-04-18 16:10:25 +00001413
Hanno Beckere694c3e2017-12-27 21:34:08 +00001414 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001415
Paul Elliottd48d5c62021-01-07 14:47:05 +00001416 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
1419 /*
1420 * Check if the session can be resumed
1421 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001422 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_SSL_RENEGOTIATION)
1424 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001425#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001426 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001427 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001428 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 {
1430 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001431 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001433 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001434#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001435 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001436 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001437 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 }
1439 else
1440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001445 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
Paul Elliott3891caf2020-12-17 18:42:40 +00001447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1449 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Andrzej Kurek03bac442018-04-25 05:06:07 -04001451 /*
1452 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001453 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 i = 0;
1455 while( 1 )
1456 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001457 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001460 mbedtls_ssl_send_alert_message(
1461 ssl,
1462 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1463 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001464 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001465 }
1466
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001467 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001468 ssl->session_negotiate->ciphersuite )
1469 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 }
1473
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001474 suite_info = mbedtls_ssl_ciphersuite_from_id(
1475 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001476 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1477 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001478 {
1479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001480 mbedtls_ssl_send_alert_message(
1481 ssl,
1482 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001483 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1484 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001485 }
1486
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001487 MBEDTLS_SSL_DEBUG_MSG( 3,
1488 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001489
Gilles Peskineeccd8882020-03-10 12:19:08 +01001490#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001491 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001492 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001493 {
1494 ssl->handshake->ecrs_enabled = 1;
1495 }
1496#endif
1497
Thomas Daubney20f89a92022-06-20 15:12:19 +01001498 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001501 mbedtls_ssl_send_alert_message(
1502 ssl,
1503 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1504 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001505 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 }
1507
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001508 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001509
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001510 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001511 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001512
Paul Bakker48916f92012-09-16 19:57:18 +00001513 while( ext_len )
1514 {
1515 unsigned int ext_id = ( ( ext[0] << 8 )
1516 | ( ext[1] ) );
1517 unsigned int ext_size = ( ( ext[2] << 8 )
1518 | ( ext[3] ) );
1519
1520 if( ext_size + 4 > ext_len )
1521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001523 mbedtls_ssl_send_alert_message(
1524 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001526 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001527 }
1528
1529 switch( ext_id )
1530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1533#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001534 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001535#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001536
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001537 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1538 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001539 return( ret );
1540
1541 break;
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1544 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001545 MBEDTLS_SSL_DEBUG_MSG( 3,
1546 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001547
1548 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1549 ext + 4, ext_size ) ) != 0 )
1550 {
1551 return( ret );
1552 }
1553
1554 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001556
Hanno Beckera0e20d02019-05-15 14:03:01 +01001557#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001558 case MBEDTLS_TLS_EXT_CID:
1559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1560
1561 if( ( ret = ssl_parse_cid_ext( ssl,
1562 ext + 4,
1563 ext_size ) ) != 0 )
1564 {
1565 return( ret );
1566 }
1567
1568 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001569#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1572 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001574
1575 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1576 ext + 4, ext_size ) ) != 0 )
1577 {
1578 return( ret );
1579 }
1580
1581 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1585 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001586 MBEDTLS_SSL_DEBUG_MSG( 3,
1587 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001588
1589 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1590 ext + 4, ext_size ) ) != 0 )
1591 {
1592 return( ret );
1593 }
1594
1595 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1599 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001601
1602 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1603 ext + 4, ext_size ) ) != 0 )
1604 {
1605 return( ret );
1606 }
1607
1608 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001610
Robert Cragie136884c2015-10-02 13:34:31 +01001611#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001612 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001614 MBEDTLS_SSL_DEBUG_MSG( 3,
1615 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001616
1617 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1618 ext + 4, ext_size ) ) != 0 )
1619 {
1620 return( ret );
1621 }
1622
1623 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001624#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1625 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001626
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001627#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1628 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1630
1631 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1632 ext + 4, ext_size ) ) != 0 )
1633 {
1634 return( ret );
1635 }
1636
1637 break;
1638#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_SSL_ALPN)
1641 case MBEDTLS_TLS_EXT_ALPN:
1642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001643
Johan Pascal275874b2020-10-27 10:43:53 +01001644 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1645 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001646
1647 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001649
Johan Pascalb62bb512015-12-03 21:56:45 +01001650#if defined(MBEDTLS_SSL_DTLS_SRTP)
1651 case MBEDTLS_TLS_EXT_USE_SRTP:
1652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1653
Johan Pascalc3ccd982020-10-28 17:18:18 +01001654 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1655 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001656
1657 break;
1658#endif /* MBEDTLS_SSL_DTLS_SRTP */
1659
Paul Bakker48916f92012-09-16 19:57:18 +00001660 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001661 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001662 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001663 }
1664
1665 ext_len -= 4 + ext_size;
1666 ext += 4 + ext_size;
1667
1668 if( ext_len > 0 && ext_len < 4 )
1669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001671 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001672 }
1673 }
1674
1675 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001676 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1677 * extensions. It sets the transform data for the resumed session which in
1678 * case of DTLS includes the server CID extracted from the CID extension.
1679 */
1680 if( ssl->handshake->resume )
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001681 {
1682 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
1683 {
1684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
1685 mbedtls_ssl_send_alert_message(
1686 ssl,
1687 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1688 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
1689 return( ret );
1690 }
1691 }
1692
Paul Bakker48916f92012-09-16 19:57:18 +00001693 /*
1694 * Renegotiation security checks
1695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001697 ssl->conf->allow_legacy_renegotiation ==
1698 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001699 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001700 MBEDTLS_SSL_DEBUG_MSG( 1,
1701 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001702 handshake_failure = 1;
1703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_SSL_RENEGOTIATION)
1705 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1706 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001707 renegotiation_info_seen == 0 )
1708 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001709 MBEDTLS_SSL_DEBUG_MSG( 1,
1710 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001711 handshake_failure = 1;
1712 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1714 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001715 ssl->conf->allow_legacy_renegotiation ==
1716 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001719 handshake_failure = 1;
1720 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1722 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001723 renegotiation_info_seen == 1 )
1724 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001725 MBEDTLS_SSL_DEBUG_MSG( 1,
1726 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001727 handshake_failure = 1;
1728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001730
1731 if( handshake_failure == 1 )
1732 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001733 mbedtls_ssl_send_alert_message(
1734 ssl,
1735 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1736 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001737 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
1742 return( 0 );
1743}
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1746 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001747MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001748static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1749 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001750 unsigned char *end )
1751{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001753 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001754
Paul Bakker29e1f122013-04-16 13:07:56 +02001755 /*
1756 * Ephemeral DH parameters:
1757 *
1758 * struct {
1759 * opaque dh_p<1..2^16-1>;
1760 * opaque dh_g<1..2^16-1>;
1761 * opaque dh_Ys<1..2^16-1>;
1762 * } ServerDHParams;
1763 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001764 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1765 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001768 return( ret );
1769 }
1770
Gilles Peskine487bbf62021-05-27 22:17:07 +02001771 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001772 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001773 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001775 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001776 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001777 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001778 }
1779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1781 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1782 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001783
1784 return( ret );
1785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1787 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001788
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001789#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001790#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1791 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1792 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001793MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001794static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001795 unsigned char **p,
1796 unsigned char *end )
1797{
1798 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001799 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001800 uint8_t ecpoint_len;
1801 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1802
1803 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001804 * struct {
1805 * ECParameters curve_params;
1806 * ECPoint public;
1807 * } ServerECDHParams;
1808 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001809 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001810 * 2..3 NamedCurve
1811 * 4 ECPoint.len
1812 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001813 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001814 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001815 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001816
1817 /* First byte is curve_type; only named_curve is handled */
1818 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001819 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001820
1821 /* Next two bytes are the namedcurve value */
1822 tls_id = *(*p)++;
1823 tls_id <<= 8;
1824 tls_id |= *(*p)++;
1825
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001826 /* Check it's a curve we offered */
1827 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001828 {
1829 MBEDTLS_SSL_DEBUG_MSG( 2,
1830 ( "bad server key exchange message (ECDHE curve): %u",
1831 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001832 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001833 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001834
Hanno Beckerbb89e272019-01-08 12:54:37 +00001835 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001836 if( ( handshake->ecdh_psa_type =
1837 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001839 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001840 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001841 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001842
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001843 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001844 ecpoint_len = *(*p)++;
1845 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001846 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001847
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001848 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1849 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001850
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001851 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1852 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001853 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001854
Hanno Beckerbb89e272019-01-08 12:54:37 +00001855 return( 0 );
1856}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001857#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1858 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1859 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001860#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001861#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1862 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1863 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1864 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1865 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001866MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrong1f198d82022-04-13 15:02:30 +02001867static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1868{
1869 const mbedtls_ecp_curve_info *curve_info;
1870 mbedtls_ecp_group_id grp_id;
1871#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1872 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1873#else
1874 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1875#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001876
Neil Armstrong1f198d82022-04-13 15:02:30 +02001877 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1878 if( curve_info == NULL )
1879 {
1880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1882 }
1883
1884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1885
1886 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1887 return( -1 );
1888
1889 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1890 MBEDTLS_DEBUG_ECDH_QP );
1891
1892 return( 0 );
1893}
1894
Andrzej Kurek468c5062022-10-24 10:30:14 -04001895#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1896 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1897 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1898 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1899 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1900
1901#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1902 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1903 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001904MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001906 unsigned char **p,
1907 unsigned char *end )
1908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001910
Paul Bakker29e1f122013-04-16 13:07:56 +02001911 /*
1912 * Ephemeral ECDH parameters:
1913 *
1914 * struct {
1915 * ECParameters curve_params;
1916 * ECPoint public;
1917 * } ServerECDHParams;
1918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001920 (const unsigned char **) p, end ) ) != 0 )
1921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001923#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001924 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1925 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001926#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001927 return( ret );
1928 }
1929
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001930 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001931 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001932 MBEDTLS_SSL_DEBUG_MSG( 1,
1933 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001934 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001935 }
1936
Paul Bakker29e1f122013-04-16 13:07:56 +02001937 return( ret );
1938}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001939#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1940 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
1941 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1942#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001943#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001944MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001946 unsigned char **p,
1947 unsigned char *end )
1948{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001950 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001951 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001952
1953 /*
1954 * PSK parameters:
1955 *
1956 * opaque psk_identity_hint<0..2^16-1>;
1957 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001958 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001959 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001960 MBEDTLS_SSL_DEBUG_MSG( 1,
1961 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001962 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001963 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001964 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001965 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001966
irwir6527bd62019-09-21 18:51:25 +03001967 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001968 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001969 MBEDTLS_SSL_DEBUG_MSG( 1,
1970 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001971 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001972 }
1973
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001974 /*
1975 * Note: we currently ignore the PKS identity hint, as we only allow one
1976 * PSK to be provisionned on the client. This could be changed later if
1977 * someone needs that feature.
1978 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001979 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001980 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001981
1982 return( ret );
1983}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001984#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1987 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001988/*
1989 * Generate a pre-master secret and encrypt it with the server's RSA key
1990 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001991MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001993 size_t offset, size_t *olen,
1994 size_t pms_offset )
1995{
Janos Follath865b3eb2019-12-16 11:46:15 +00001996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001997 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001998 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001999 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002000
Angus Grattond8213d02016-05-25 20:56:48 +10002001 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002002 {
2003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2004 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2005 }
2006
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002007 /*
2008 * Generate (part of) the pre-master as
2009 * struct {
2010 * ProtocolVersion client_version;
2011 * opaque random[46];
2012 * } PreMasterSecret;
2013 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04002014 mbedtls_ssl_write_version( p, ssl->conf->transport,
2015 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002016
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002017 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002020 return( ret );
2021 }
2022
2023 ssl->handshake->pmslen = 48;
2024
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002025#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2026 peer_pk = &ssl->handshake->peer_pubkey;
2027#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002028 if( ssl->session_negotiate->peer_cert == NULL )
2029 {
Hanno Becker8273df82019-02-06 17:37:32 +00002030 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002032 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002033 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002034 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2035#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002036
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002037 /*
2038 * Now write it out, encrypted
2039 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002040 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2043 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002044 }
2045
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002046 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002047 p, ssl->handshake->pmslen,
2048 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002049 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002050 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002053 return( ret );
2054 }
2055
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002056 if( len_bytes == 2 )
2057 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002058 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002059 *olen += 2;
2060 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002061
Hanno Beckerae553dd2019-02-08 14:06:00 +00002062#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2063 /* We don't need the peer's public key anymore. Free it. */
2064 mbedtls_pk_free( peer_pk );
2065#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002066 return( 0 );
2067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2069 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2072 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002073MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002075{
Janos Follath865b3eb2019-12-16 11:46:15 +00002076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002078 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002079
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002080#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2081 peer_pk = &ssl->handshake->peer_pubkey;
2082#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002083 if( ssl->session_negotiate->peer_cert == NULL )
2084 {
Hanno Becker8273df82019-02-06 17:37:32 +00002085 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002088 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002089 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2090#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002091
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002092 /* This is a public key, so it can't be opaque, so can_do() is a good
2093 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002094 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2097 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002098 }
2099
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002100 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002101
Przemek Stekielea4000f2022-03-16 09:49:33 +01002102#if defined(MBEDTLS_USE_PSA_CRYPTO)
2103 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002104 size_t olen = 0;
2105
2106 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2107 {
2108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2109 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2110 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002111
2112 ssl->handshake->ecdh_psa_type =
2113 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2114 &ecdh_bits ) );
2115
2116 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2117 {
2118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2119 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2120 }
2121
2122 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2123
Przemek Stekielea4000f2022-03-16 09:49:33 +01002124 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002125 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2126 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2127 ssl->handshake->ecdh_psa_peerkey,
2128 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002129
Przemek Stekiel561a4232022-03-16 13:16:24 +01002130 if ( ret != 0 )
2131 {
2132 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2133 return( ret );
2134 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002135
Przemek Stekiel561a4232022-03-16 13:16:24 +01002136 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002137#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2139 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002142 return( ret );
2143 }
2144
2145 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002148 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002149 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002150#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002151#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2152 /* We don't need the peer's public key anymore. Free it,
2153 * so that more RAM is available for upcoming expensive
2154 * operations like ECDHE. */
2155 mbedtls_pk_free( peer_pk );
2156#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2157
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002158 return( ret );
2159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2161 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002162
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002163MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002165{
Janos Follath865b3eb2019-12-16 11:46:15 +00002166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002167 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002168 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002169 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2174 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 ssl->state++;
2178 return( 0 );
2179 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002180 ((void) p);
2181 ((void) end);
2182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2185 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2186 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2187 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002188 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002189 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002192 mbedtls_ssl_send_alert_message(
2193 ssl,
2194 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2195 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002196 return( ret );
2197 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002200 ssl->state++;
2201 return( 0 );
2202 }
2203 ((void) p);
2204 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2206 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002207
Gilles Peskineeccd8882020-03-10 12:19:08 +01002208#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002209 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002210 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002211 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002212 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002213 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002214#endif
2215
Hanno Becker327c93b2018-08-15 13:56:18 +01002216 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 return( ret );
2220 }
2221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002225 mbedtls_ssl_send_alert_message(
2226 ssl,
2227 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2228 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002232 /*
2233 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2234 * doesn't use a psk_identity_hint
2235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2239 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002240 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002241 /* Current message is probably either
2242 * CertificateRequest or ServerHelloDone */
2243 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002244 goto exit;
2245 }
2246
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002247 MBEDTLS_SSL_DEBUG_MSG( 1,
2248 ( "server key exchange message must not be skipped" ) );
2249 mbedtls_ssl_send_alert_message(
2250 ssl,
2251 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2252 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256
Gilles Peskineeccd8882020-03-10 12:19:08 +01002257#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002258 if( ssl->handshake->ecrs_enabled )
2259 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2260
2261start_processing:
2262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002264 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002266
Gilles Peskineeccd8882020-03-10 12:19:08 +01002267#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2269 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2270 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2271 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002272 {
2273 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002276 mbedtls_ssl_send_alert_message(
2277 ssl,
2278 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002279 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002280 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002281 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002282 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002283#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2286 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2287 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002289 ; /* nothing more to do */
2290 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2292 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2293#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2294 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2295 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2296 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002298 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002301 mbedtls_ssl_send_alert_message(
2302 ssl,
2303 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2304 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002305 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002306 }
2307 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002308 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2310 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002311#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2312 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2314 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2315 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2316 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002317 {
2318 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002321 mbedtls_ssl_send_alert_message(
2322 ssl,
2323 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2324 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002325 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002326 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002327 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002328 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2330 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2331 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002332#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2333 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2334 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002335#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002336 /*
2337 * The first 3 bytes are:
2338 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2339 * [1, 2] elliptic curve's TLS ID
2340 *
2341 * However since we only support secp256r1 for now, we check only
2342 * that TLS ID here
2343 */
2344 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE( p, 1 );
2345 const mbedtls_ecp_curve_info *curve_info;
2346
2347 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id(
2348 MBEDTLS_ECP_DP_SECP256R1 ) ) == NULL )
2349 {
2350 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2351 }
2352
2353 if( ( *p != MBEDTLS_ECP_TLS_NAMED_CURVE ) ||
2354 ( read_tls_id != curve_info->tls_id ) )
2355 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
2356
2357 p += 3;
2358
Valerio Settia08b1a42022-11-17 15:10:02 +01002359 if( ( ret = mbedtls_psa_ecjpake_read_round_two(
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002360 &ssl->handshake->psa_pake_ctx, p, end - p ) ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02002361 {
2362 psa_destroy_key( ssl->handshake->psa_pake_password );
2363 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
2364
2365 MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round two", ret );
2366 mbedtls_ssl_send_alert_message(
2367 ssl,
2368 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2369 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2370 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2371 }
2372#else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002373 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2374 p, end - p );
2375 if( ret != 0 )
2376 {
2377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002378 mbedtls_ssl_send_alert_message(
2379 ssl,
2380 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002381 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2382 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002383 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002384#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002385 }
2386 else
2387#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2390 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002391 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002392
Gilles Peskineeccd8882020-03-10 12:19:08 +01002393#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002394 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002395 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002396 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002397 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2400 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2401 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002402 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002403 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002404 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002405
Hanno Beckera6899bb2019-02-06 18:26:03 +00002406 mbedtls_pk_context * peer_pk;
2407
Jerry Yu693a47a2022-06-23 14:02:28 +08002408#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2409 peer_pk = &ssl->handshake->peer_pubkey;
2410#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2411 if( ssl->session_negotiate->peer_cert == NULL )
2412 {
2413 /* Should never happen */
2414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2416 }
2417 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2418#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2419
Paul Bakker29e1f122013-04-16 13:07:56 +02002420 /*
2421 * Handle the digitally-signed structure
2422 */
Jerry Yu693a47a2022-06-23 14:02:28 +08002423 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2424 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yu95b743c2022-07-23 11:37:50 +08002425 if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuc3bf7482022-07-29 10:27:17 +08002426 sig_alg, &pk_alg, &md_alg ) != 0 &&
Jerry Yu693a47a2022-06-23 14:02:28 +08002427 ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) &&
2428 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002429 {
Ronald Cron90915f22022-03-07 11:11:36 +01002430 MBEDTLS_SSL_DEBUG_MSG( 1,
2431 ( "bad server key exchange message" ) );
2432 mbedtls_ssl_send_alert_message(
2433 ssl,
2434 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2435 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2436 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002437 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002438 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002439
Jerry Yu693a47a2022-06-23 14:02:28 +08002440 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002441 {
Ronald Cron90915f22022-03-07 11:11:36 +01002442 MBEDTLS_SSL_DEBUG_MSG( 1,
2443 ( "bad server key exchange message" ) );
2444 mbedtls_ssl_send_alert_message(
2445 ssl,
2446 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2447 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2448 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002449 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002450
2451 /*
2452 * Read signature
2453 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002454
2455 if( p > end - 2 )
2456 {
2457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002458 mbedtls_ssl_send_alert_message(
2459 ssl,
2460 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2461 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002462 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002463 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002464 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002465 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002466
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002467 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002470 mbedtls_ssl_send_alert_message(
2471 ssl,
2472 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2473 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002474 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002478
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002479 /*
2480 * Compute the hash that has been signed
2481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002483 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002484 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2485 params, params_len,
2486 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002487 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002488 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002489 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002490 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002494 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002495
Gilles Peskineca1d7422018-04-24 11:53:22 +02002496 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002497
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002498 /*
2499 * Verify signature
2500 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002501 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002504 mbedtls_ssl_send_alert_message(
2505 ssl,
2506 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2507 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002509 }
2510
Gilles Peskineeccd8882020-03-10 12:19:08 +01002511#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002512 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002513 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002514#endif
2515
Jerry Yu693a47a2022-06-23 14:02:28 +08002516#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2517 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
2518 {
Jerry Yu693a47a2022-06-23 14:02:28 +08002519 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2520 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002521 rsassa_pss_options.expected_salt_len =
2522 mbedtls_hash_info_get_size( md_alg );
2523 if( rsassa_pss_options.expected_salt_len == 0 )
Jerry Yu693a47a2022-06-23 14:02:28 +08002524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002525
Jerry Yu693a47a2022-06-23 14:02:28 +08002526 ret = mbedtls_pk_verify_ext( pk_alg, &rsassa_pss_options,
2527 peer_pk,
2528 md_alg, hash, hashlen,
2529 p, sig_len );
2530 }
2531 else
2532#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2533 ret = mbedtls_pk_verify_restartable( peer_pk,
2534 md_alg, hash, hashlen, p, sig_len, rs_ctx );
2535
2536 if( ret != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002537 {
David Horstmannb21bbef2022-10-06 17:49:31 +01002538 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002539#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
David Horstmannb21bbef2022-10-06 17:49:31 +01002540 send_alert_msg = ( ret != MBEDTLS_ERR_ECP_IN_PROGRESS );
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002541#endif
David Horstmannb21bbef2022-10-06 17:49:31 +01002542 if( send_alert_msg )
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002543 mbedtls_ssl_send_alert_message(
2544 ssl,
2545 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2546 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002548#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002549 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2550 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2551#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002552 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002553 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002554
2555#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2556 /* We don't need the peer's public key anymore. Free it,
2557 * so that more RAM is available for upcoming expensive
2558 * operations like ECDHE. */
2559 mbedtls_pk_free( peer_pk );
2560#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002562#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002564exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 ssl->state++;
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570}
2571
Gilles Peskineeccd8882020-03-10 12:19:08 +01002572#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002573MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002575{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002576 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002577 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002580
Hanno Becker1aa267c2017-04-28 17:08:27 +01002581 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002584 ssl->state++;
2585 return( 0 );
2586 }
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002590}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002591#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002592MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594{
Janos Follath865b3eb2019-12-16 11:46:15 +00002595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002596 unsigned char *buf;
2597 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002598 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002599 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002600 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002601 size_t sig_alg_len;
2602#if defined(MBEDTLS_DEBUG_C)
2603 unsigned char *sig_alg;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002604 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002605#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Hanno Becker1aa267c2017-04-28 17:08:27 +01002609 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002612 ssl->state++;
2613 return( 0 );
2614 }
2615
Hanno Becker327c93b2018-08-15 13:56:18 +01002616 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2619 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002622 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2623 {
2624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002625 mbedtls_ssl_send_alert_message(
2626 ssl,
2627 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2628 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002629 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002632 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002633 ssl->handshake->client_auth =
2634 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002637 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Jerry Yufb28b882022-01-28 11:05:58 +08002639 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002640 {
Johan Pascala89ca862020-08-25 10:03:19 +02002641 /* Current message is probably the ServerHelloDone */
2642 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002643 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002644 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002645
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002646 /*
2647 * struct {
2648 * ClientCertificateType certificate_types<1..2^8-1>;
2649 * SignatureAndHashAlgorithm
2650 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2651 * DistinguishedName certificate_authorities<0..2^16-1>;
2652 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002653 *
2654 * Since we only support a single certificate on clients, let's just
2655 * ignore all the information that's supposed to help us pick a
2656 * certificate.
2657 *
2658 * We could check that our certificate matches the request, and bail out
2659 * if it doesn't, but it's simpler to just send the certificate anyway,
2660 * and give the server the opportunity to decide if it should terminate
2661 * the connection when it doesn't like our certificate.
2662 *
2663 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2664 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002665 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002666 *
2667 * However, we still minimally parse the message to check it is at least
2668 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002669 */
Paul Bakker926af752012-11-23 13:38:07 +01002670 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002671
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002672 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002673 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2674 {
2675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2676 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2677 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002678 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002679 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002681 n = cert_type_len;
2682
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002683 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002684 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002685 * * the length of the signature algorithms field (if minor version of
2686 * SSL is 3),
2687 * * distinguished name length otherwise.
2688 * Both reach at most the index:
2689 * ...hdr_len + 2 + n,
2690 * therefore the buffer length at this point must be greater than that
2691 * regardless of the actual code path.
2692 */
2693 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002696 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2697 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002698 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002699 }
2700
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002701 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002702 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2703 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2704
2705 /*
2706 * The furthest access in buf is in the loop few lines below:
2707 * sig_alg[i + 1],
2708 * where:
2709 * sig_alg = buf + ...hdr_len + 3 + n,
2710 * max(i) = sig_alg_len - 1.
2711 * Therefore the furthest access is:
2712 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2713 * which reduces to:
2714 * buf[...hdr_len + 3 + n + sig_alg_len],
2715 * which is one less than we need the buf to be.
2716 */
2717 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002718 {
Ronald Cron90915f22022-03-07 11:11:36 +01002719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2720 mbedtls_ssl_send_alert_message(
2721 ssl,
2722 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2724 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002725 }
Paul Bakker926af752012-11-23 13:38:07 +01002726
Ronald Cron90915f22022-03-07 11:11:36 +01002727#if defined(MBEDTLS_DEBUG_C)
2728 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2729 for( size_t i = 0; i < sig_alg_len; i += 2 )
2730 {
2731 MBEDTLS_SSL_DEBUG_MSG( 3,
2732 ( "Supported Signature Algorithm found: %d,%d",
2733 sig_alg[i], sig_alg[i + 1] ) );
2734 }
2735#endif
2736
2737 n += 2 + sig_alg_len;
2738
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002739 /* certificate_authorities */
2740 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2741 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002742
2743 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002744 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2748 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002749 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002750 }
2751
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002752#if defined(MBEDTLS_DEBUG_C)
2753 dn = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n - dn_len;
2754 for( size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len )
2755 {
2756 unsigned char *p = dn + i + 2;
2757 mbedtls_x509_name name;
2758 mbedtls_x509_name *name_cur, *name_prv;
2759 size_t asn1_len;
2760 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
2761 memset( &name, 0, sizeof( name ) );
2762 dni_len = MBEDTLS_GET_UINT16_BE( dn + i, 0 );
2763 if( dni_len > dn_len - i - 2 ||
2764 mbedtls_asn1_get_tag( &p, p + dni_len, &asn1_len,
2765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) != 0 ||
2766 mbedtls_x509_get_name( &p, p + asn1_len, &name ) != 0 )
2767 {
2768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2769 mbedtls_ssl_send_alert_message(
2770 ssl,
2771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2772 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2773 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
2774 }
2775 MBEDTLS_SSL_DEBUG_MSG( 3,
2776 ( "DN hint: %.*s",
2777 mbedtls_x509_dn_gets( s, sizeof(s), &name ), s ) );
2778 name_cur = name.next;
2779 while( name_cur != NULL )
2780 {
2781 name_prv = name_cur;
2782 name_cur = name_cur->next;
2783 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2784 mbedtls_free( name_prv );
2785 }
2786 }
2787#endif
2788
Paul Bakker926af752012-11-23 13:38:07 +01002789exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
2792 return( 0 );
2793}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002794#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002796MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002798{
Janos Follath865b3eb2019-12-16 11:46:15 +00002799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
Hanno Becker327c93b2018-08-15 13:56:18 +01002803 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2806 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002808
2809 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2810 {
2811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2812 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2816 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002819 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2820 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002821 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 }
2823
2824 ssl->state++;
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002829#endif
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
2833 return( 0 );
2834}
2835
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002836MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002838{
Janos Follath865b3eb2019-12-16 11:46:15 +00002839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002840
2841 size_t header_len;
2842 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002843 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002844 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2849 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 /*
2852 * DHM key exchange -- send G^X mod P
2853 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002854 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Joe Subbiani6dd73642021-07-19 11:56:54 +01002856 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002857 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002860 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002861 &ssl->out_msg[header_len], content_len,
2862 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002863 if( ret != 0 )
2864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 return( ret );
2867 }
2868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2870 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002873 ssl->handshake->premaster,
2874 MBEDTLS_PREMASTER_SIZE,
2875 &ssl->handshake->pmslen,
2876 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 return( ret );
2880 }
2881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 }
2884 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002886#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2887 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2888 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2889 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002890 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002891 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2892 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2893 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002894 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002895#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2897 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002898 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002899
2900 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2901
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002902 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002903
Hanno Becker0a94a642019-01-11 14:35:30 +00002904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2905
Hanno Becker4a63ed42019-01-08 11:39:35 +00002906 /*
2907 * Generate EC private key for ECDHE exchange.
2908 */
2909
Hanno Becker4a63ed42019-01-08 11:39:35 +00002910 /* The master secret is obtained from the shared ECDH secret by
2911 * applying the TLS 1.2 PRF with a specific salt and label. While
2912 * the PSA Crypto API encourages combining key agreement schemes
2913 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2914 * yet support the provisioning of salt + label to the KDF.
2915 * For the time being, we therefore need to split the computation
2916 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002917 key_attributes = psa_key_attributes_init();
2918 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002919 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002920 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2921 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002922
2923 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002924 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002925 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002926 if( status != PSA_SUCCESS )
2927 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2928
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002929 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002930 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002931 * but we just need to add a length byte before that. */
2932 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2933 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2934 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2935 size_t own_pubkey_len;
2936
Hanno Becker4a63ed42019-01-08 11:39:35 +00002937 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002938 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002939 &own_pubkey_len );
2940 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002941 {
2942 psa_destroy_key( handshake->ecdh_psa_privkey );
2943 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002944 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002945 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002946
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002947 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2948 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002949
Hanno Becker4a63ed42019-01-08 11:39:35 +00002950 /* The ECDH secret is the premaster secret used for key derivation. */
2951
Janos Follathdf3b0892019-08-08 11:12:24 +01002952 /* Compute ECDH shared secret. */
2953 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2954 handshake->ecdh_psa_privkey,
2955 handshake->ecdh_psa_peerkey,
2956 handshake->ecdh_psa_peerkey_len,
2957 ssl->handshake->premaster,
2958 sizeof( ssl->handshake->premaster ),
2959 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002960
Andrzej Kureka0237f82022-02-24 13:24:52 -05002961 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002962 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002963
2964 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2965 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002966#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002967 /*
2968 * ECDH key exchange -- send client public value
2969 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002970 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002971
Gilles Peskineeccd8882020-03-10 12:19:08 +01002972#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002973 if( ssl->handshake->ecrs_enabled )
2974 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002975 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002976 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002977
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002978 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2979 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002980#endif
2981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002983 &content_len,
2984 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002985 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002986 if( ret != 0 )
2987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002989#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002990 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2991 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2992#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002993 return( ret );
2994 }
2995
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002996 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2997 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002998
Gilles Peskineeccd8882020-03-10 12:19:08 +01002999#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003000 if( ssl->handshake->ecrs_enabled )
3001 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003002 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003003 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003004 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003005
3006ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003007 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003008 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003011 &ssl->handshake->pmslen,
3012 ssl->handshake->premaster,
3013 MBEDTLS_MPI_MAX_SIZE,
3014 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003017#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003018 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3019 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3020#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003021 return( ret );
3022 }
3023
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003024 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3025 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02003026#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01003027 }
3028 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3030 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3031 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3032 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01003033#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3034 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3035 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3036 {
3037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3038 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
3039 psa_key_attributes_t key_attributes;
3040
3041 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3042
3043 /*
3044 * opaque psk_identity<0..2^16-1>;
3045 */
3046 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01003047 /* We don't offer PSK suites if we don't have a PSK,
3048 * and we check that the server's choice is among the
3049 * ciphersuites we offered, so this should never happen. */
3050 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003051
Neil Armstrongfc834f22022-03-23 17:54:38 +01003052 /* uint16 to store content length */
3053 const size_t content_len_size = 2;
3054
Neil Armstrong868af822022-03-09 10:26:25 +01003055 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01003056
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003057 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01003058 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01003059 {
3060 MBEDTLS_SSL_DEBUG_MSG( 1,
3061 ( "psk identity too long or SSL buffer too short" ) );
3062 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3063 }
3064
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003065 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003066
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003067 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
3068 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
3069 header_len += content_len_size;
3070
3071 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003072 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003073 p += ssl->conf->psk_identity_len;
3074
Neil Armstrong868af822022-03-09 10:26:25 +01003075 header_len += ssl->conf->psk_identity_len;
3076
3077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3078
3079 /*
3080 * Generate EC private key for ECDHE exchange.
3081 */
3082
3083 /* The master secret is obtained from the shared ECDH secret by
3084 * applying the TLS 1.2 PRF with a specific salt and label. While
3085 * the PSA Crypto API encourages combining key agreement schemes
3086 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3087 * yet support the provisioning of salt + label to the KDF.
3088 * For the time being, we therefore need to split the computation
3089 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3090 key_attributes = psa_key_attributes_init();
3091 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3092 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3093 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3094 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3095
3096 /* Generate ECDH private key. */
3097 status = psa_generate_key( &key_attributes,
3098 &handshake->ecdh_psa_privkey );
3099 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003100 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003101
3102 /* Export the public part of the ECDH private key from PSA.
3103 * The export format is an ECPoint structure as expected by TLS,
3104 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003105 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003106 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3107 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003108 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003109
3110 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3111 own_pubkey, own_pubkey_max_len,
3112 &own_pubkey_len );
3113 if( status != PSA_SUCCESS )
3114 {
3115 psa_destroy_key( handshake->ecdh_psa_privkey );
3116 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003117 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003118 }
3119
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003120 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003121 content_len = own_pubkey_len + 1;
3122
Neil Armstrong25400452022-03-23 17:44:07 +01003123 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3124 * - a uint16 containing the length (in octets) of the ECDH computation
3125 * - the octet string produced by the ECDH computation
3126 * - a uint16 containing the length (in octets) of the PSK
3127 * - the PSK itself
3128 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003129 unsigned char *pms = ssl->handshake->premaster;
3130 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003131 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003132 /* uint16 to store length (in octets) of the ECDH computation */
3133 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003134 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003135
Neil Armstrong25400452022-03-23 17:44:07 +01003136 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003137 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3138 handshake->ecdh_psa_privkey,
3139 handshake->ecdh_psa_peerkey,
3140 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003141 pms + zlen_size,
3142 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003143 &zlen );
3144
3145 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3146 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3147
Neil Armstrongc530aa62022-03-23 17:45:01 +01003148 if( status != PSA_SUCCESS )
3149 return( psa_ssl_status_to_mbedtls( status ) );
3150 else if( destruction_status != PSA_SUCCESS )
3151 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003152
Neil Armstrong25400452022-03-23 17:44:07 +01003153 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003154 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3155 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003156 }
3157 else
3158#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3159 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003160#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003161 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003162 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003163 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003164 * opaque psk_identity<0..2^16-1>;
3165 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003166 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003167 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003168 /* We don't offer PSK suites if we don't have a PSK,
3169 * and we check that the server's choice is among the
3170 * ciphersuites we offered, so this should never happen. */
3171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003172 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003173
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003174 header_len = 4;
3175 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003176
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003177 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003178 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003179 MBEDTLS_SSL_DEBUG_MSG( 1,
3180 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003181 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3182 }
3183
Joe Subbianifbeb6922021-07-16 14:27:50 +01003184 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3185 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003186
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003187 memcpy( ssl->out_msg + header_len,
3188 ssl->conf->psk_identity,
3189 ssl->conf->psk_identity_len );
3190 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3193 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003194 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003195 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003196 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003197 else
3198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3200 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003201 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003202 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3203 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003204 return( ret );
3205 }
3206 else
3207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3209 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003210 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003211 /*
3212 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3213 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003214 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003215
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003216 if( header_len + 2 + content_len >
3217 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003218 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003219 MBEDTLS_SSL_DEBUG_MSG( 1,
3220 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003221 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3222 }
3223
Joe Subbianifbeb6922021-07-16 14:27:50 +01003224 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3225 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003228 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003229 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003230 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003231 if( ret != 0 )
3232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003234 return( ret );
3235 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003236
3237#if defined(MBEDTLS_USE_PSA_CRYPTO)
3238 unsigned char *pms = ssl->handshake->premaster;
3239 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3240 size_t pms_len;
3241
3242 /* Write length only when we know the actual value */
3243 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3244 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3245 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3246 {
3247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3248 return( ret );
3249 }
3250 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3251 pms += 2 + pms_len;
3252
3253 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3254#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003255 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003258#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3259 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003261 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003262 /*
3263 * ClientECDiffieHellmanPublic public;
3264 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003265 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3266 &content_len,
3267 &ssl->out_msg[header_len],
3268 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003269 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003270 if( ret != 0 )
3271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003273 return( ret );
3274 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003275
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003276 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3277 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003278 }
3279 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003280#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003284 }
3285
Neil Armstrong80f6f322022-05-03 17:56:38 +02003286#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3287 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3288 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003289 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003290 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003291 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003292 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003293 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003294#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003295 }
3296 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003297#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3299 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003301 header_len = 4;
3302 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3303 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003304 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 }
Paul Bakkered27a042013-04-18 22:46:23 +02003306 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003308#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3309 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3310 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003311 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003312
Neil Armstrongca7d5062022-05-31 14:43:23 +02003313#if defined(MBEDTLS_USE_PSA_CRYPTO)
3314 unsigned char *out_p = ssl->out_msg + header_len;
3315 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3316 header_len;
Valerio Settia08b1a42022-11-17 15:10:02 +01003317 ret = mbedtls_psa_ecjpake_write_round_two( &ssl->handshake->psa_pake_ctx,
Valerio Setti02c25b52022-11-15 14:08:42 +01003318 out_p, end_p - out_p, &content_len );
3319 if ( ret != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02003320 {
Valerio Setti02c25b52022-11-15 14:08:42 +01003321 psa_destroy_key( ssl->handshake->psa_pake_password );
3322 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
3323 MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret );
3324 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +02003325 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003326#else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003327 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003328 ssl->out_msg + header_len,
3329 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3330 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003331 ssl->conf->f_rng, ssl->conf->p_rng );
3332 if( ret != 0 )
3333 {
3334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3335 return( ret );
3336 }
3337
3338 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3339 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3340 ssl->conf->f_rng, ssl->conf->p_rng );
3341 if( ret != 0 )
3342 {
3343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3344 return( ret );
3345 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003346#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003347 }
3348 else
3349#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003350 {
3351 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003356 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3358 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003359
3360 ssl->state++;
3361
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003362 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 return( ret );
3366 }
3367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
3370 return( 0 );
3371}
3372
Gilles Peskineeccd8882020-03-10 12:19:08 +01003373#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003374MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003376{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003377 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003378 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003386 return( ret );
3387 }
3388
Hanno Becker77adddc2019-02-07 12:32:43 +00003389 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003392 ssl->state++;
3393 return( 0 );
3394 }
3395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003399#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003400MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003404 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003405 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003406 size_t n = 0, offset = 0;
3407 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003408 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003410 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003411 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003412#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3413 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3414#else
3415 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3416#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003419
Gilles Peskineeccd8882020-03-10 12:19:08 +01003420#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003421 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003422 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003423 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003424 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003425 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003426#endif
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003431 return( ret );
3432 }
3433
Hanno Becker77adddc2019-02-07 12:32:43 +00003434 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003437 ssl->state++;
3438 return( 0 );
3439 }
3440
Jerry Yufb28b882022-01-28 11:05:58 +08003441 if( ssl->handshake->client_auth == 0 ||
3442 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 {
Johan Pascala89ca862020-08-25 10:03:19 +02003444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3445 ssl->state++;
3446 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 }
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 }
3454
3455 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003456 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003458#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003459 if( ssl->handshake->ecrs_enabled )
3460 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3461
3462sign:
3463#endif
3464
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003465 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003466
Ronald Cron90915f22022-03-07 11:11:36 +01003467 /*
3468 * digitally-signed struct {
3469 * opaque handshake_messages[handshake_messages_length];
3470 * };
3471 *
3472 * Taking shortcut here. We assume that the server always allows the
3473 * PRF Hash function and has sent it in the allowed signature
3474 * algorithms list received in the Certificate Request message.
3475 *
3476 * Until we encounter a server that does not, we will take this
3477 * shortcut.
3478 *
3479 * Reason: Otherwise we should have running hashes for SHA512 and
3480 * SHA224 in order to satisfy 'weird' needs from the server
3481 * side.
3482 */
3483 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003484 {
Ronald Cron90915f22022-03-07 11:11:36 +01003485 md_alg = MBEDTLS_MD_SHA384;
3486 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003487 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003488 else
Paul Bakker577e0062013-08-28 11:57:20 +02003489 {
Ronald Cron90915f22022-03-07 11:11:36 +01003490 md_alg = MBEDTLS_MD_SHA256;
3491 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003492 }
Ronald Cron90915f22022-03-07 11:11:36 +01003493 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3494
3495 /* Info from md_alg will be used instead */
3496 hashlen = 0;
3497 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003498
Gilles Peskineeccd8882020-03-10 12:19:08 +01003499#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003500 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003501 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003502#endif
3503
3504 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3505 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003506 ssl->out_msg + 6 + offset,
3507 out_buf_len - 6 - offset,
3508 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003509 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003512#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003513 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3514 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3515#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003516 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003517 }
Paul Bakker926af752012-11-23 13:38:07 +01003518
Joe Subbiani6dd73642021-07-19 11:56:54 +01003519 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Paul Bakker1ef83d62012-04-11 12:09:53 +00003521 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3523 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
3525 ssl->state++;
3526
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003527 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003529 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 return( ret );
3531 }
3532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Paul Bakkered27a042013-04-18 22:46:23 +02003535 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003536}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003537#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003540MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003542{
Janos Follath865b3eb2019-12-16 11:46:15 +00003543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003544 uint32_t lifetime;
3545 size_t ticket_len;
3546 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003547 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003550
Hanno Becker327c93b2018-08-15 13:56:18 +01003551 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003554 return( ret );
3555 }
3556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003560 mbedtls_ssl_send_alert_message(
3561 ssl,
3562 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3563 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003565 }
3566
3567 /*
3568 * struct {
3569 * uint32 ticket_lifetime_hint;
3570 * opaque ticket<0..2^16-1>;
3571 * } NewSessionTicket;
3572 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003573 * 0 . 3 ticket_lifetime_hint
3574 * 4 . 5 ticket_len (n)
3575 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3578 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003581 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3582 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003583 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003584 }
3585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003587
Philippe Antoineb5b25432018-05-11 11:06:29 +02003588 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3589 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003590
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003591 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003596 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3597 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003598 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003599 }
3600
Paul Elliottd48d5c62021-01-07 14:47:05 +00003601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003602
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003603 /* We're not waiting for a NewSessionTicket message any more */
3604 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003606
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003607 /*
3608 * Zero-length ticket means the server changed his mind and doesn't want
3609 * to send a ticket after all, so just forget it
3610 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003611 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003612 return( 0 );
3613
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003614 if( ssl->session != NULL && ssl->session->ticket != NULL )
3615 {
3616 mbedtls_platform_zeroize( ssl->session->ticket,
3617 ssl->session->ticket_len );
3618 mbedtls_free( ssl->session->ticket );
3619 ssl->session->ticket = NULL;
3620 ssl->session->ticket_len = 0;
3621 }
3622
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003623 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3624 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003626 ssl->session_negotiate->ticket = NULL;
3627 ssl->session_negotiate->ticket_len = 0;
3628
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003629 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003630 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003632 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3633 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003634 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003635 }
3636
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003637 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003638
3639 ssl->session_negotiate->ticket = ticket;
3640 ssl->session_negotiate->ticket_len = ticket_len;
3641 ssl->session_negotiate->ticket_lifetime = lifetime;
3642
3643 /*
3644 * RFC 5077 section 3.4:
3645 * "If the client receives a session ticket from the server, then it
3646 * discards any Session ID that was sent in the ServerHello."
3647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003649 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003652
3653 return( 0 );
3654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003656
Paul Bakker5121ce52009-01-03 21:22:43 +00003657/*
Paul Bakker1961b702013-01-25 14:49:24 +01003658 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661{
3662 int ret = 0;
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003665 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3667 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003668 ssl->handshake->new_session_ticket != 0 )
3669 {
Jerry Yua357cf42022-07-12 05:36:45 +00003670 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003671 }
3672#endif
3673
Paul Bakker1961b702013-01-25 14:49:24 +01003674 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 case MBEDTLS_SSL_HELLO_REQUEST:
3677 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 break;
3679
Paul Bakker1961b702013-01-25 14:49:24 +01003680 /*
3681 * ==> ClientHello
3682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003684 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003685 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Paul Bakker1961b702013-01-25 14:49:24 +01003687 /*
3688 * <== ServerHello
3689 * Certificate
3690 * ( ServerKeyExchange )
3691 * ( CertificateRequest )
3692 * ServerHelloDone
3693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003695 ret = ssl_parse_server_hello( ssl );
3696 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3699 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003700 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003703 ret = ssl_parse_server_key_exchange( ssl );
3704 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003707 ret = ssl_parse_certificate_request( ssl );
3708 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003711 ret = ssl_parse_server_hello_done( ssl );
3712 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Paul Bakker1961b702013-01-25 14:49:24 +01003714 /*
3715 * ==> ( Certificate/Alert )
3716 * ClientKeyExchange
3717 * ( CertificateVerify )
3718 * ChangeCipherSpec
3719 * Finished
3720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3722 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003723 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003726 ret = ssl_write_client_key_exchange( ssl );
3727 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003730 ret = ssl_write_certificate_verify( ssl );
3731 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3734 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003735 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 case MBEDTLS_SSL_CLIENT_FINISHED:
3738 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003739 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003740
Paul Bakker1961b702013-01-25 14:49:24 +01003741 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003742 * <== ( NewSessionTicket )
3743 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003744 * Finished
3745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00003747 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003748 ret = ssl_parse_new_session_ticket( ssl );
3749 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003750#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3753 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003754 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 case MBEDTLS_SSL_SERVER_FINISHED:
3757 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003758 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 case MBEDTLS_SSL_FLUSH_BUFFERS:
3761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3762 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003763 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3766 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003767 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003768
Paul Bakker1961b702013-01-25 14:49:24 +01003769 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003772 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
3774 return( ret );
3775}
Jerry Yuc5aef882021-12-23 20:15:02 +08003776
Jerry Yufb4b6472022-01-27 15:03:26 +08003777#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */