blob: 5ff8ab4b84ca21853140c66a7ee94e5fc9d95fd0 [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 Setti6b3dab02022-11-17 17:14:54 +0100167 ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
168 p + 2, end - p - 2, &kkpp_len,
169 MBEDTLS_ECJPAKE_ROUND_ONE );
Valerio Setti02c25b52022-11-15 14:08:42 +0100170 if ( ret != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +0200171 {
Valerio Setti02c25b52022-11-15 14:08:42 +0100172 psa_destroy_key( ssl->handshake->psa_pake_password );
173 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
174 MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret );
175 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200176 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200177#else
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200178 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100179 p + 2, end - p - 2, &kkpp_len,
180 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200181 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200182 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100183 MBEDTLS_SSL_DEBUG_RET( 1 ,
184 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100185 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200186 }
Neil Armstrongca7d5062022-05-31 14:43:23 +0200187#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200188
189 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
190 if( ssl->handshake->ecjpake_cache == NULL )
191 {
192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100193 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200194 }
195
196 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
197 ssl->handshake->ecjpake_cache_len = kkpp_len;
198 }
199 else
200 {
201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
202
203 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100204 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200205
206 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200207 }
208
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100209 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
210 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200211
212 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100213
214 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200215}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200216#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000217
Hanno Beckera0e20d02019-05-15 14:03:01 +0100218#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200219MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100220static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
221 unsigned char *buf,
222 const unsigned char *end,
223 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100224{
225 unsigned char *p = buf;
226 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100227
228 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100229 * Quoting draft-ietf-tls-dtls-connection-id-05
230 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100231 *
232 * struct {
233 * opaque cid<0..2^8-1>;
234 * } ConnectionId;
235 */
236
237 *olen = 0;
238 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
239 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
240 {
Hanno Becker261602c2017-04-12 14:54:42 +0100241 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100242 }
243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
244
245 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
246 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100247 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100248
249 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100250 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
251 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100252 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100253 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
254 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100255
256 *p++ = (uint8_t) ssl->own_cid_len;
257 memcpy( p, ssl->own_cid, ssl->own_cid_len );
258
259 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100260
261 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100262}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100263#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200266MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100267static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
268 unsigned char *buf,
269 const unsigned char *end,
270 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200271{
272 unsigned char *p = buf;
273
Simon Butcher0fc94e92015-09-28 20:52:04 +0100274 *olen = 0;
275
Hanno Becker261602c2017-04-12 14:54:42 +0100276 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
277 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200278
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100279 MBEDTLS_SSL_DEBUG_MSG( 3,
280 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200281
Hanno Becker261602c2017-04-12 14:54:42 +0100282 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100283
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100284 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
285 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200286
287 *p++ = 0x00;
288 *p++ = 1;
289
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200290 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200291
292 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100293
294 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200299MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100300static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
301 unsigned char *buf,
302 const unsigned char *end,
303 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100304{
305 unsigned char *p = buf;
306
Simon Butcher0fc94e92015-09-28 20:52:04 +0100307 *olen = 0;
308
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100309 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100310 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100311
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100312 MBEDTLS_SSL_DEBUG_MSG( 3,
313 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100314
Hanno Becker261602c2017-04-12 14:54:42 +0100315 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100316
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100317 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
318 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100319
320 *p++ = 0x00;
321 *p++ = 0x00;
322
323 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100324
325 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200330MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100331static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
332 unsigned char *buf,
333 const unsigned char *end,
334 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200335{
336 unsigned char *p = buf;
337
Simon Butcher0fc94e92015-09-28 20:52:04 +0100338 *olen = 0;
339
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100340 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100341 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200342
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100343 MBEDTLS_SSL_DEBUG_MSG( 3,
344 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200345
Hanno Becker261602c2017-04-12 14:54:42 +0100346 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100347
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100348 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
349 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200350
351 *p++ = 0x00;
352 *p++ = 0x00;
353
354 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100355
356 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200361MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100362static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
363 unsigned char *buf,
364 const unsigned char *end,
365 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200366{
367 unsigned char *p = buf;
368 size_t tlen = ssl->session_negotiate->ticket_len;
369
Simon Butcher0fc94e92015-09-28 20:52:04 +0100370 *olen = 0;
371
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200372 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100373 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200374
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100375 MBEDTLS_SSL_DEBUG_MSG( 3,
376 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377
Hanno Becker261602c2017-04-12 14:54:42 +0100378 /* The addition is safe here since the ticket length is 16 bit. */
379 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100380
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100381 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
382 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200383
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100384 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
385 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200386
387 *olen = 4;
388
Simon Butchered997662015-09-28 02:14:30 +0100389 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100390 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200391
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100392 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000393 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200394
395 memcpy( p, ssl->session_negotiate->ticket, tlen );
396
397 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100398
399 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200402
Ron Eldora9788042018-12-05 11:04:31 +0200403#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200404MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascal77696ee2020-09-22 21:49:40 +0200405static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200406 unsigned char *buf,
407 const unsigned char *end,
408 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100409{
410 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200411 size_t protection_profiles_index = 0, ext_len = 0;
412 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100413
414 *olen = 0;
415
Johan Pascalc3ccd982020-10-28 17:18:18 +0100416 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
417 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
418 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100419 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200420 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100421 }
422
Ron Eldora9788042018-12-05 11:04:31 +0200423 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100424 * uint8 SRTPProtectionProfile[2];
425 *
426 * struct {
427 * SRTPProtectionProfiles SRTPProtectionProfiles;
428 * opaque srtp_mki<0..255>;
429 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100430 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100431 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200432 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200433 {
434 mki_len = ssl->dtls_srtp_info.mki_len;
435 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300436 /* Extension length = 2 bytes for profiles length,
437 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
438 * 1 byte for srtp_mki vector length and the mki_len value
439 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200440 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
441
Johan Pascal77696ee2020-09-22 21:49:40 +0200442 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
443
444 /* Check there is room in the buffer for the extension + 4 bytes
445 * - the extension tag (2 bytes)
446 * - the extension length (2 bytes)
447 */
448 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
449
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100450 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
451 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200452
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100453 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
454 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100455
Ron Eldor3adb9922017-12-21 10:15:08 +0200456 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200457 /* micro-optimization:
458 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
459 * which is lower than 127, so the upper byte of the length is always 0
460 * For the documentation, the more generic code is left in comments
461 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
462 * >> 8 ) & 0xFF );
463 */
464 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100465 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100466
Ron Eldoref72faf2018-07-12 11:54:20 +0300467 for( protection_profiles_index=0;
468 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
469 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100470 {
Johan Pascal43f94902020-09-22 12:25:52 +0200471 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200472 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200473 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200474 {
475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
476 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100477 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
478 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200479 }
480 else
481 {
482 /*
483 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200484 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200485 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200486 MBEDTLS_SSL_DEBUG_MSG( 3,
487 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200488 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200489 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
490 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200491 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100492 }
493 }
494
Ron Eldor591f1622018-01-22 12:30:04 +0200495 *p++ = mki_len & 0xFF;
496
497 if( mki_len != 0 )
498 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200499 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200500 /*
501 * Increment p to point to the current position.
502 */
503 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300504 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
505 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200506 }
507
Ron Eldoref72faf2018-07-12 11:54:20 +0300508 /*
509 * total extension length: extension type (2 bytes)
510 * + extension length (2 bytes)
511 * + protection profile length (2 bytes)
512 * + 2 * number of protection profiles
513 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200514 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300515 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200516 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200517
518 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100519}
520#endif /* MBEDTLS_SSL_DTLS_SRTP */
521
Ronald Cron4079abc2022-02-20 10:35:26 +0100522int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
523 unsigned char *buf,
524 const unsigned char *end,
525 int uses_ec,
526 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100527{
528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
529 unsigned char *p = buf;
530 size_t ext_len = 0;
531
532 (void) ssl;
533 (void) end;
534 (void) uses_ec;
535 (void) ret;
536 (void) ext_len;
537
538 *out_len = 0;
539
540 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
541 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
542#if defined(MBEDTLS_SSL_RENEGOTIATION)
543 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
544 {
545 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
546 return( ret );
547 }
548 p += ext_len;
549#endif
550
551#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
552 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
553 if( uses_ec )
554 {
555 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
556 &ext_len ) ) != 0 )
557 {
558 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
559 return( ret );
560 }
561 p += ext_len;
562 }
563#endif
564
565#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
566 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
567 {
568 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
569 return( ret );
570 }
571 p += ext_len;
572#endif
573
574#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
575 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
576 {
577 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
578 return( ret );
579 }
580 p += ext_len;
581#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
582
583#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
584 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
585 &ext_len ) ) != 0 )
586 {
587 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
588 return( ret );
589 }
590 p += ext_len;
591#endif
592
593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
594 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
595 {
596 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
597 return( ret );
598 }
599 p += ext_len;
600#endif
601
602#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
603 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
604 {
605 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
606 return( ret );
607 }
608 p += ext_len;
609#endif
610
611#if defined(MBEDTLS_SSL_DTLS_SRTP)
612 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
613 {
614 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
615 return( ret );
616 }
617 p += ext_len;
618#endif
619
620#if defined(MBEDTLS_SSL_SESSION_TICKETS)
621 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
622 {
623 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
624 return( ret );
625 }
626 p += ext_len;
627#endif
628
629 *out_len = p - buf;
630
631 return( 0 );
632}
633
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200634MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200636 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000637 size_t len )
638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_SSL_RENEGOTIATION)
640 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000641 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100642 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000643 if( len != 1 + ssl->verify_data_len * 2 ||
644 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200645 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100646 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200647 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100648 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100651 mbedtls_ssl_send_alert_message(
652 ssl,
653 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
654 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100655 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000656 }
657 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100658 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100660 {
661 if( len != 1 || buf[0] != 0x00 )
662 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200663 MBEDTLS_SSL_DEBUG_MSG( 1,
664 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100665 mbedtls_ssl_send_alert_message(
666 ssl,
667 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
668 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100669 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100670 }
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100673 }
Paul Bakker48916f92012-09-16 19:57:18 +0000674
675 return( 0 );
676}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200679MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200681 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200682 size_t len )
683{
684 /*
685 * server should use the extension only if we did,
686 * and if so the server's value should match ours (and len is always 1)
687 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200688 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200689 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200690 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200691 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100692 MBEDTLS_SSL_DEBUG_MSG( 1,
693 ( "non-matching max fragment length extension" ) );
694 mbedtls_ssl_send_alert_message(
695 ssl,
696 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100697 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
698 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200699 }
700
701 return( 0 );
702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000704
Hanno Beckera0e20d02019-05-15 14:03:01 +0100705#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200706MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera8373a12019-04-26 15:37:26 +0100707static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
708 const unsigned char *buf,
709 size_t len )
710{
711 size_t peer_cid_len;
712
713 if( /* CID extension only makes sense in DTLS */
714 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
715 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100716 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100717 {
Hanno Becker22626482019-05-03 12:46:59 +0100718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100719 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100720 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100721 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100722 }
723
724 if( len == 0 )
725 {
726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
727 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100728 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
729 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100730 }
731
732 peer_cid_len = *buf++;
733 len--;
734
735 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
736 {
Hanno Becker22626482019-05-03 12:46:59 +0100737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100738 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100739 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
740 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100741 }
742
743 if( len != peer_cid_len )
744 {
Hanno Becker22626482019-05-03 12:46:59 +0100745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100746 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100747 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
748 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100749 }
750
Hanno Becker5a299902019-05-03 12:47:49 +0100751 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100752 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
753 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
754
755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
756 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
757
Hanno Beckera8373a12019-04-26 15:37:26 +0100758 return( 0 );
759}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100760#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200763MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100765 const unsigned char *buf,
766 size_t len )
767{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200768 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100769 len != 0 )
770 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100771 MBEDTLS_SSL_DEBUG_MSG( 1,
772 ( "non-matching encrypt-then-MAC extension" ) );
773 mbedtls_ssl_send_alert_message(
774 ssl,
775 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100776 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100777 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100778 }
779
780 ((void) buf);
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100783
784 return( 0 );
785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200789MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200791 const unsigned char *buf,
792 size_t len )
793{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200794 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200795 len != 0 )
796 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100797 MBEDTLS_SSL_DEBUG_MSG( 1,
798 ( "non-matching extended master secret extension" ) );
799 mbedtls_ssl_send_alert_message(
800 ssl,
801 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100802 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
803 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200804 }
805
806 ((void) buf);
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200809
810 return( 0 );
811}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200815MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200817 const unsigned char *buf,
818 size_t len )
819{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200820 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200821 len != 0 )
822 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100823 MBEDTLS_SSL_DEBUG_MSG( 1,
824 ( "non-matching session ticket extension" ) );
825 mbedtls_ssl_send_alert_message(
826 ssl,
827 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100828 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
829 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200830 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200831
832 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200833
834 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200835
836 return( 0 );
837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200839
Robert Cragie136884c2015-10-02 13:34:31 +0100840#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100841 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200842MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200844 const unsigned char *buf,
845 size_t len )
846{
847 size_t list_size;
848 const unsigned char *p;
849
Philippe Antoine747fd532018-05-30 09:13:21 +0200850 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
854 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100855 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200856 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200857 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200858
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200859 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200860 while( list_size > 0 )
861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
863 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200864 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200865#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
866 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200867 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200868#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
869 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200870#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
871 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200872 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
873 p[0] );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200874#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200876 return( 0 );
877 }
878
879 list_size--;
880 p++;
881 }
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200884 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
885 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100886 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200887}
Darryl Green11999bb2018-03-13 15:22:58 +0000888#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100889 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200890
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200891#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200892MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200893static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
894 const unsigned char *buf,
895 size_t len )
896{
Janos Follath865b3eb2019-12-16 11:46:15 +0000897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200898
Hanno Beckere694c3e2017-12-27 21:34:08 +0000899 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200900 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
901 {
902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
903 return( 0 );
904 }
905
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200906 /* If we got here, we no longer need our cached extension */
907 mbedtls_free( ssl->handshake->ecjpake_cache );
908 ssl->handshake->ecjpake_cache = NULL;
909 ssl->handshake->ecjpake_cache_len = 0;
910
Neil Armstrongca7d5062022-05-31 14:43:23 +0200911#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100912 if( ( ret = mbedtls_psa_ecjpake_read_round(
913 &ssl->handshake->psa_pake_ctx, buf, len,
914 MBEDTLS_ECJPAKE_ROUND_ONE ) ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +0200915 {
Valerio Setti02c25b52022-11-15 14:08:42 +0100916 psa_destroy_key( ssl->handshake->psa_pake_password );
917 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200918
Valerio Setti02c25b52022-11-15 14:08:42 +0100919 MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round one", ret );
920 mbedtls_ssl_send_alert_message(
921 ssl,
922 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
923 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
924 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200925 }
926
927 return( 0 );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200928#else
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200929 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
930 buf, len ) ) != 0 )
931 {
932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100933 mbedtls_ssl_send_alert_message(
934 ssl,
935 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
936 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200937 return( ret );
938 }
939
940 return( 0 );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200941#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200942}
943#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200946MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200948 const unsigned char *buf, size_t len )
949{
950 size_t list_len, name_len;
951 const char **p;
952
953 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200954 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200955 {
956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100957 mbedtls_ssl_send_alert_message(
958 ssl,
959 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100960 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
961 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200962 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200963
964 /*
965 * opaque ProtocolName<1..2^8-1>;
966 *
967 * struct {
968 * ProtocolName protocol_name_list<2..2^16-1>
969 * } ProtocolNameList;
970 *
971 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
972 */
973
974 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
975 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200976 {
977 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
978 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100979 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200980 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200981
982 list_len = ( buf[0] << 8 ) | buf[1];
983 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200984 {
985 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
986 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100987 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200988 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200989
990 name_len = buf[2];
991 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200992 {
993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
994 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100995 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200996 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200997
998 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200999 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001000 {
1001 if( name_len == strlen( *p ) &&
1002 memcmp( buf + 3, *p, name_len ) == 0 )
1003 {
1004 ssl->alpn_chosen = *p;
1005 return( 0 );
1006 }
1007 }
1008
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001010 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1011 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001012 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001015
Johan Pascalb62bb512015-12-03 21:56:45 +01001016#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001017MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascalb62bb512015-12-03 21:56:45 +01001018static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001019 const unsigned char *buf,
1020 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001021{
Johan Pascal43f94902020-09-22 12:25:52 +02001022 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001023 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001024 uint16_t server_protection_profile_value = 0;
1025
1026 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001027 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1028 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1029 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001030 return( 0 );
1031
Ron Eldora9788042018-12-05 11:04:31 +02001032 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001033 * uint8 SRTPProtectionProfile[2];
1034 *
1035 * struct {
1036 * SRTPProtectionProfiles SRTPProtectionProfiles;
1037 * opaque srtp_mki<0..255>;
1038 * } UseSRTPData;
1039
1040 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1041 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001042 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001043 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001044 {
1045 mki_len = ssl->dtls_srtp_info.mki_len;
1046 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001047
Ron Eldoref72faf2018-07-12 11:54:20 +03001048 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001049 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1050 * + protection profile (2 bytes)
1051 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001052 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001053 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001054 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001055 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001056
1057 /*
1058 * get the server protection profile
1059 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001060
1061 /*
1062 * protection profile length must be 0x0002 as we must have only
1063 * one protection profile in server Hello
1064 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001065 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001066 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001067
1068 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001069 server_protection = mbedtls_ssl_check_srtp_profile_value(
1070 server_protection_profile_value );
1071 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001072 {
Johan Pascal43f94902020-09-22 12:25:52 +02001073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1074 mbedtls_ssl_get_srtp_profile_as_string(
1075 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001076 }
1077
Johan Pascal43f94902020-09-22 12:25:52 +02001078 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001079
Johan Pascalb62bb512015-12-03 21:56:45 +01001080 /*
1081 * Check we have the server profile in our list
1082 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001083 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001084 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001085 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1086 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001087 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1089 mbedtls_ssl_get_srtp_profile_as_string(
1090 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001091 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001092 }
1093 }
1094
Ron Eldor591f1622018-01-22 12:30:04 +02001095 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001096 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001097 {
1098 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001099 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001100 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001101 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001102
1103 /* If server does not use mki in its reply, make sure the client won't keep
1104 * one as negotiated */
1105 if( len == 5 )
1106 {
1107 ssl->dtls_srtp_info.mki_len = 0;
1108 }
1109
Ron Eldoref72faf2018-07-12 11:54:20 +03001110 /*
1111 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001112 * If the client detects a nonzero-length MKI in the server's response
1113 * that is different than the one the client offered, then the client
1114 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1115 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001116 if( len > 5 && ( buf[4] != mki_len ||
1117 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001118 {
1119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1120 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001121 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001122 }
Ron Eldorb4655392018-07-05 18:25:39 +03001123#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001124 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001125 {
Ron Eldora9788042018-12-05 11:04:31 +02001126 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1127 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001128 }
1129#endif
Ron Eldora9788042018-12-05 11:04:31 +02001130 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001131}
1132#endif /* MBEDTLS_SSL_DTLS_SRTP */
1133
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001134/*
1135 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001138MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001142 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001143 unsigned char cookie_len;
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001146
Gilles Peskineb64bf062019-09-27 14:02:44 +02001147 /* Check that there is enough room for:
1148 * - 2 bytes of version
1149 * - 1 byte of cookie_len
1150 */
1151 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1152 {
1153 MBEDTLS_SSL_DEBUG_MSG( 1,
1154 ( "incoming HelloVerifyRequest message is too short" ) );
1155 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1156 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001157 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001158 }
1159
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160 /*
1161 * struct {
1162 * ProtocolVersion server_version;
1163 * opaque cookie<0..2^8-1>;
1164 * } HelloVerifyRequest;
1165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001167 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001168 p += 2;
1169
TRodziewicz2d8800e2021-05-13 19:14:19 +02001170 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001171 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1172 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1173 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001174 */
Glenn Strauss83158112022-04-13 14:59:34 -04001175 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1180 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001181
Hanno Beckerbc000442021-06-24 09:18:19 +01001182 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001183 }
1184
1185 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001186 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1187 {
1188 MBEDTLS_SSL_DEBUG_MSG( 1,
1189 ( "cookie length does not match incoming message size" ) );
1190 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1191 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001192 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001193 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001194 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001195
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001196 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001197
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001198 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1199 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001200 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001202 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001203 }
1204
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001205 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001206 ssl->handshake->verify_cookie_len = cookie_len;
1207
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001208 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1210 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001215
1216 return( 0 );
1217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001219
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001220MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001223 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001224 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001225 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001226 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001227 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001229 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001230#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001231 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001235
Hanno Becker327c93b2018-08-15 13:56:18 +01001236 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001238 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 return( ret );
1241 }
1242
Hanno Becker79594fd2019-05-08 09:38:41 +01001243 buf = ssl->in_msg;
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_SSL_RENEGOTIATION)
1248 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001249 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001250 ssl->renego_records_seen++;
1251
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001252 if( ssl->conf->renego_max_records >= 0 &&
1253 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001254 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001255 MBEDTLS_SSL_DEBUG_MSG( 1,
1256 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001258 }
1259
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001260 MBEDTLS_SSL_DEBUG_MSG( 1,
1261 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001262
1263 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001269 mbedtls_ssl_send_alert_message(
1270 ssl,
1271 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1272 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 }
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001283 return( ssl_parse_hello_verify_request( ssl ) );
1284 }
1285 else
1286 {
1287 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001288 mbedtls_free( ssl->handshake->cookie );
1289 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001290 ssl->handshake->verify_cookie_len = 0;
1291 }
1292 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1296 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001299 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1300 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001301 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 }
1303
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001304 /*
1305 * 0 . 1 server_version
1306 * 2 . 33 random (maybe including 4 bytes of Unix time)
1307 * 34 . 34 session_id length = n
1308 * 35 . 34+n session_id
1309 * 35+n . 36+n cipher_suite
1310 * 37+n . 37+n compression_method
1311 *
1312 * 38+n . 39+n extensions length (optional)
1313 * 40+n . .. extensions
1314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001316
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001317 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1318 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001319 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Glenn Strauss60bfe602022-03-14 19:04:24 -04001321 if( ssl->tls_version < ssl->conf->min_tls_version ||
1322 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001323 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001324 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001325 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1326 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001327 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001328 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1331 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001332
Hanno Beckerbc000442021-06-24 09:18:19 +01001333 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001334 }
1335
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001337 ( (unsigned long) buf[2] << 24 ) |
1338 ( (unsigned long) buf[3] << 16 ) |
1339 ( (unsigned long) buf[4] << 8 ) |
1340 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001342 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001344 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
Paul Bakker48916f92012-09-16 19:57:18 +00001348 if( n > 32 )
1349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001351 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1352 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001353 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001354 }
1355
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001356 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001358 ext_len = ( ( buf[38 + n] << 8 )
1359 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Paul Bakker48916f92012-09-16 19:57:18 +00001361 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001365 mbedtls_ssl_send_alert_message(
1366 ssl,
1367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1368 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001369 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001372 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001373 {
1374 ext_len = 0;
1375 }
1376 else
1377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001379 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1380 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001381 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001384 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001385 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001386
1387 /*
1388 * Read and check compression
1389 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001390 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001392 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001393 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001394 MBEDTLS_SSL_DEBUG_MSG( 1,
1395 ( "server hello, bad compression: %d", comp ) );
1396 mbedtls_ssl_send_alert_message(
1397 ssl,
1398 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1399 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001401 }
1402
Paul Bakker380da532012-04-18 16:10:25 +00001403 /*
1404 * Initialize update checksum functions
1405 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001406 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1407 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001408 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001409 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001410 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001411 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1412 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001414 }
Paul Bakker380da532012-04-18 16:10:25 +00001415
Hanno Beckere694c3e2017-12-27 21:34:08 +00001416 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001417
Paul Elliottd48d5c62021-01-07 14:47:05 +00001418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
1421 /*
1422 * Check if the session can be resumed
1423 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001424 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_RENEGOTIATION)
1426 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001427#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001428 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001429 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001430 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
1432 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001433 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001435 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001436#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001437 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001438 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001439 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441 else
1442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 }
1445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001447 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Paul Elliott3891caf2020-12-17 18:42:40 +00001449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001450 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1451 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001452
Andrzej Kurek03bac442018-04-25 05:06:07 -04001453 /*
1454 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001455 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 i = 0;
1457 while( 1 )
1458 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001459 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001462 mbedtls_ssl_send_alert_message(
1463 ssl,
1464 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1465 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001466 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 }
1468
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001469 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001470 ssl->session_negotiate->ciphersuite )
1471 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 }
1475
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001476 suite_info = mbedtls_ssl_ciphersuite_from_id(
1477 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001478 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1479 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001480 {
1481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001482 mbedtls_ssl_send_alert_message(
1483 ssl,
1484 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001485 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1486 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001487 }
1488
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001489 MBEDTLS_SSL_DEBUG_MSG( 3,
1490 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001491
Gilles Peskineeccd8882020-03-10 12:19:08 +01001492#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001493 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001494 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001495 {
1496 ssl->handshake->ecrs_enabled = 1;
1497 }
1498#endif
1499
Thomas Daubney20f89a92022-06-20 15:12:19 +01001500 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001503 mbedtls_ssl_send_alert_message(
1504 ssl,
1505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1506 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001507 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 }
1509
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001510 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001511
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001512 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001513 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001514
Paul Bakker48916f92012-09-16 19:57:18 +00001515 while( ext_len )
1516 {
1517 unsigned int ext_id = ( ( ext[0] << 8 )
1518 | ( ext[1] ) );
1519 unsigned int ext_size = ( ( ext[2] << 8 )
1520 | ( ext[3] ) );
1521
1522 if( ext_size + 4 > ext_len )
1523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001525 mbedtls_ssl_send_alert_message(
1526 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1527 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001528 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001529 }
1530
1531 switch( ext_id )
1532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1535#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001536 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001537#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001538
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001539 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1540 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001541 return( ret );
1542
1543 break;
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1546 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001547 MBEDTLS_SSL_DEBUG_MSG( 3,
1548 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001549
1550 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1551 ext + 4, ext_size ) ) != 0 )
1552 {
1553 return( ret );
1554 }
1555
1556 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001558
Hanno Beckera0e20d02019-05-15 14:03:01 +01001559#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001560 case MBEDTLS_TLS_EXT_CID:
1561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1562
1563 if( ( ret = ssl_parse_cid_ext( ssl,
1564 ext + 4,
1565 ext_size ) ) != 0 )
1566 {
1567 return( ret );
1568 }
1569
1570 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001571#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1574 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001576
1577 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1578 ext + 4, ext_size ) ) != 0 )
1579 {
1580 return( ret );
1581 }
1582
1583 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1587 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001588 MBEDTLS_SSL_DEBUG_MSG( 3,
1589 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001590
1591 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1592 ext + 4, ext_size ) ) != 0 )
1593 {
1594 return( ret );
1595 }
1596
1597 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1601 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001603
1604 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1605 ext + 4, ext_size ) ) != 0 )
1606 {
1607 return( ret );
1608 }
1609
1610 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001612
Robert Cragie136884c2015-10-02 13:34:31 +01001613#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001614 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001616 MBEDTLS_SSL_DEBUG_MSG( 3,
1617 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001618
1619 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1620 ext + 4, ext_size ) ) != 0 )
1621 {
1622 return( ret );
1623 }
1624
1625 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001626#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1627 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001628
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001629#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1630 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1632
1633 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1634 ext + 4, ext_size ) ) != 0 )
1635 {
1636 return( ret );
1637 }
1638
1639 break;
1640#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#if defined(MBEDTLS_SSL_ALPN)
1643 case MBEDTLS_TLS_EXT_ALPN:
1644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001645
Johan Pascal275874b2020-10-27 10:43:53 +01001646 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1647 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001648
1649 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001651
Johan Pascalb62bb512015-12-03 21:56:45 +01001652#if defined(MBEDTLS_SSL_DTLS_SRTP)
1653 case MBEDTLS_TLS_EXT_USE_SRTP:
1654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1655
Johan Pascalc3ccd982020-10-28 17:18:18 +01001656 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1657 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001658
1659 break;
1660#endif /* MBEDTLS_SSL_DTLS_SRTP */
1661
Paul Bakker48916f92012-09-16 19:57:18 +00001662 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001663 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001664 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001665 }
1666
1667 ext_len -= 4 + ext_size;
1668 ext += 4 + ext_size;
1669
1670 if( ext_len > 0 && ext_len < 4 )
1671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001673 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001674 }
1675 }
1676
1677 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001678 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1679 * extensions. It sets the transform data for the resumed session which in
1680 * case of DTLS includes the server CID extracted from the CID extension.
1681 */
1682 if( ssl->handshake->resume )
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001683 {
1684 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
1685 {
1686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
1687 mbedtls_ssl_send_alert_message(
1688 ssl,
1689 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1690 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
1691 return( ret );
1692 }
1693 }
1694
Paul Bakker48916f92012-09-16 19:57:18 +00001695 /*
1696 * Renegotiation security checks
1697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001699 ssl->conf->allow_legacy_renegotiation ==
1700 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001701 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001702 MBEDTLS_SSL_DEBUG_MSG( 1,
1703 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001704 handshake_failure = 1;
1705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_RENEGOTIATION)
1707 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1708 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001709 renegotiation_info_seen == 0 )
1710 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001711 MBEDTLS_SSL_DEBUG_MSG( 1,
1712 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001713 handshake_failure = 1;
1714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1716 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001717 ssl->conf->allow_legacy_renegotiation ==
1718 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001721 handshake_failure = 1;
1722 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1724 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001725 renegotiation_info_seen == 1 )
1726 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001727 MBEDTLS_SSL_DEBUG_MSG( 1,
1728 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001729 handshake_failure = 1;
1730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001732
1733 if( handshake_failure == 1 )
1734 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001735 mbedtls_ssl_send_alert_message(
1736 ssl,
1737 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1738 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001739 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
1744 return( 0 );
1745}
1746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1748 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001749MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001750static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1751 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001752 unsigned char *end )
1753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001755 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001756
Paul Bakker29e1f122013-04-16 13:07:56 +02001757 /*
1758 * Ephemeral DH parameters:
1759 *
1760 * struct {
1761 * opaque dh_p<1..2^16-1>;
1762 * opaque dh_g<1..2^16-1>;
1763 * opaque dh_Ys<1..2^16-1>;
1764 * } ServerDHParams;
1765 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001766 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1767 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001770 return( ret );
1771 }
1772
Gilles Peskine487bbf62021-05-27 22:17:07 +02001773 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001774 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001775 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001777 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001778 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001779 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001780 }
1781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1783 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1784 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001785
1786 return( ret );
1787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1789 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001790
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001791#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001792#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1793 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1794 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001795MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001796static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001797 unsigned char **p,
1798 unsigned char *end )
1799{
1800 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001801 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001802 uint8_t ecpoint_len;
1803 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1804
1805 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001806 * struct {
1807 * ECParameters curve_params;
1808 * ECPoint public;
1809 * } ServerECDHParams;
1810 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001811 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001812 * 2..3 NamedCurve
1813 * 4 ECPoint.len
1814 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001815 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001816 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001817 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001818
1819 /* First byte is curve_type; only named_curve is handled */
1820 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001821 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001822
1823 /* Next two bytes are the namedcurve value */
1824 tls_id = *(*p)++;
1825 tls_id <<= 8;
1826 tls_id |= *(*p)++;
1827
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001828 /* Check it's a curve we offered */
1829 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001830 {
1831 MBEDTLS_SSL_DEBUG_MSG( 2,
1832 ( "bad server key exchange message (ECDHE curve): %u",
1833 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001834 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001835 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001836
Hanno Beckerbb89e272019-01-08 12:54:37 +00001837 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001838 if( ( handshake->ecdh_psa_type =
1839 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001840 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001841 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001842 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001843 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001844
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001845 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001846 ecpoint_len = *(*p)++;
1847 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001848 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001849
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001850 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1851 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001852
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001853 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1854 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001855 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001856
Hanno Beckerbb89e272019-01-08 12:54:37 +00001857 return( 0 );
1858}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001859#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1860 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1861 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001862#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001863#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1864 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1865 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1866 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1867 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001868MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrong1f198d82022-04-13 15:02:30 +02001869static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1870{
1871 const mbedtls_ecp_curve_info *curve_info;
1872 mbedtls_ecp_group_id grp_id;
1873#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1874 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1875#else
1876 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1877#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001878
Neil Armstrong1f198d82022-04-13 15:02:30 +02001879 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1880 if( curve_info == NULL )
1881 {
1882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1884 }
1885
1886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1887
1888 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1889 return( -1 );
1890
1891 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1892 MBEDTLS_DEBUG_ECDH_QP );
1893
1894 return( 0 );
1895}
1896
Andrzej Kurek468c5062022-10-24 10:30:14 -04001897#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1898 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1899 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1900 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1901 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1902
1903#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1904 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1905 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001906MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001908 unsigned char **p,
1909 unsigned char *end )
1910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001912
Paul Bakker29e1f122013-04-16 13:07:56 +02001913 /*
1914 * Ephemeral ECDH parameters:
1915 *
1916 * struct {
1917 * ECParameters curve_params;
1918 * ECPoint public;
1919 * } ServerECDHParams;
1920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001922 (const unsigned char **) p, end ) ) != 0 )
1923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001925#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001926 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1927 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001928#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001929 return( ret );
1930 }
1931
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001932 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001933 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001934 MBEDTLS_SSL_DEBUG_MSG( 1,
1935 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001936 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001937 }
1938
Paul Bakker29e1f122013-04-16 13:07:56 +02001939 return( ret );
1940}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001941#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1942 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
1943 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1944#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001945#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001946MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001948 unsigned char **p,
1949 unsigned char *end )
1950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001952 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001953 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001954
1955 /*
1956 * PSK parameters:
1957 *
1958 * opaque psk_identity_hint<0..2^16-1>;
1959 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001960 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001961 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001962 MBEDTLS_SSL_DEBUG_MSG( 1,
1963 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001964 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001965 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001966 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001967 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001968
irwir6527bd62019-09-21 18:51:25 +03001969 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001970 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001971 MBEDTLS_SSL_DEBUG_MSG( 1,
1972 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001973 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001974 }
1975
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001976 /*
1977 * Note: we currently ignore the PKS identity hint, as we only allow one
1978 * PSK to be provisionned on the client. This could be changed later if
1979 * someone needs that feature.
1980 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001981 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001982 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001983
1984 return( ret );
1985}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001986#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1989 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001990/*
1991 * Generate a pre-master secret and encrypt it with the server's RSA key
1992 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001993MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001995 size_t offset, size_t *olen,
1996 size_t pms_offset )
1997{
Janos Follath865b3eb2019-12-16 11:46:15 +00001998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001999 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002000 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002001 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002002
Angus Grattond8213d02016-05-25 20:56:48 +10002003 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002004 {
2005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2006 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2007 }
2008
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002009 /*
2010 * Generate (part of) the pre-master as
2011 * struct {
2012 * ProtocolVersion client_version;
2013 * opaque random[46];
2014 * } PreMasterSecret;
2015 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04002016 mbedtls_ssl_write_version( p, ssl->conf->transport,
2017 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002018
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002019 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002022 return( ret );
2023 }
2024
2025 ssl->handshake->pmslen = 48;
2026
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002027#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2028 peer_pk = &ssl->handshake->peer_pubkey;
2029#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002030 if( ssl->session_negotiate->peer_cert == NULL )
2031 {
Hanno Becker8273df82019-02-06 17:37:32 +00002032 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002035 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002036 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2037#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002038
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002039 /*
2040 * Now write it out, encrypted
2041 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002042 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2045 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002046 }
2047
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002048 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002049 p, ssl->handshake->pmslen,
2050 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002051 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002052 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002055 return( ret );
2056 }
2057
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002058 if( len_bytes == 2 )
2059 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002060 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002061 *olen += 2;
2062 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002063
Hanno Beckerae553dd2019-02-08 14:06:00 +00002064#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2065 /* We don't need the peer's public key anymore. Free it. */
2066 mbedtls_pk_free( peer_pk );
2067#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002068 return( 0 );
2069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2071 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2074 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002075MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002077{
Janos Follath865b3eb2019-12-16 11:46:15 +00002078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002080 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002081
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002082#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2083 peer_pk = &ssl->handshake->peer_pubkey;
2084#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002085 if( ssl->session_negotiate->peer_cert == NULL )
2086 {
Hanno Becker8273df82019-02-06 17:37:32 +00002087 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002089 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002090 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002091 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2092#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002093
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002094 /* This is a public key, so it can't be opaque, so can_do() is a good
2095 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002096 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2099 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002100 }
2101
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002102 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002103
Przemek Stekielea4000f2022-03-16 09:49:33 +01002104#if defined(MBEDTLS_USE_PSA_CRYPTO)
2105 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002106 size_t olen = 0;
2107
2108 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2109 {
2110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2111 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2112 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002113
2114 ssl->handshake->ecdh_psa_type =
2115 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2116 &ecdh_bits ) );
2117
2118 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2119 {
2120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2121 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2122 }
2123
2124 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2125
Przemek Stekielea4000f2022-03-16 09:49:33 +01002126 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002127 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2128 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2129 ssl->handshake->ecdh_psa_peerkey,
2130 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002131
Przemek Stekiel561a4232022-03-16 13:16:24 +01002132 if ( ret != 0 )
2133 {
2134 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2135 return( ret );
2136 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002137
Przemek Stekiel561a4232022-03-16 13:16:24 +01002138 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002139#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2141 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002144 return( ret );
2145 }
2146
2147 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002150 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002151 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002152#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002153#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2154 /* We don't need the peer's public key anymore. Free it,
2155 * so that more RAM is available for upcoming expensive
2156 * operations like ECDHE. */
2157 mbedtls_pk_free( peer_pk );
2158#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2159
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002160 return( ret );
2161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2163 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002164
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002165MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002167{
Janos Follath865b3eb2019-12-16 11:46:15 +00002168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002169 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002170 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002171 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2176 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 ssl->state++;
2180 return( 0 );
2181 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002182 ((void) p);
2183 ((void) end);
2184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2187 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2188 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2189 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002190 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002191 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002194 mbedtls_ssl_send_alert_message(
2195 ssl,
2196 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2197 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002198 return( ret );
2199 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002202 ssl->state++;
2203 return( 0 );
2204 }
2205 ((void) p);
2206 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2208 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002209
Gilles Peskineeccd8882020-03-10 12:19:08 +01002210#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002211 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002212 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002213 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002214 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002215 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002216#endif
2217
Hanno Becker327c93b2018-08-15 13:56:18 +01002218 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 return( ret );
2222 }
2223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002227 mbedtls_ssl_send_alert_message(
2228 ssl,
2229 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2230 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002234 /*
2235 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2236 * doesn't use a psk_identity_hint
2237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2241 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002242 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002243 /* Current message is probably either
2244 * CertificateRequest or ServerHelloDone */
2245 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002246 goto exit;
2247 }
2248
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002249 MBEDTLS_SSL_DEBUG_MSG( 1,
2250 ( "server key exchange message must not be skipped" ) );
2251 mbedtls_ssl_send_alert_message(
2252 ssl,
2253 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2254 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258
Gilles Peskineeccd8882020-03-10 12:19:08 +01002259#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002260 if( ssl->handshake->ecrs_enabled )
2261 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2262
2263start_processing:
2264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002266 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002268
Gilles Peskineeccd8882020-03-10 12:19:08 +01002269#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2271 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2272 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2273 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002274 {
2275 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002278 mbedtls_ssl_send_alert_message(
2279 ssl,
2280 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002282 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002283 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002284 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002285#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2288 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2289 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2290 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002291 ; /* nothing more to do */
2292 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2294 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2295#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2296 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2297 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2298 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002300 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002303 mbedtls_ssl_send_alert_message(
2304 ssl,
2305 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2306 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002307 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002308 }
2309 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002310 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2312 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002313#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2314 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2316 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2317 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2318 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002319 {
2320 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002323 mbedtls_ssl_send_alert_message(
2324 ssl,
2325 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2326 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002327 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002328 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002329 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002330 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2332 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2333 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002334#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2335 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2336 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002337#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002338 /*
2339 * The first 3 bytes are:
2340 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2341 * [1, 2] elliptic curve's TLS ID
2342 *
2343 * However since we only support secp256r1 for now, we check only
2344 * that TLS ID here
2345 */
2346 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE( p, 1 );
2347 const mbedtls_ecp_curve_info *curve_info;
2348
2349 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id(
2350 MBEDTLS_ECP_DP_SECP256R1 ) ) == NULL )
2351 {
2352 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2353 }
2354
2355 if( ( *p != MBEDTLS_ECP_TLS_NAMED_CURVE ) ||
2356 ( read_tls_id != curve_info->tls_id ) )
Valerio Setti5151bdf2022-11-21 14:30:02 +01002357 {
Valerio Setti61ea17d2022-11-18 12:11:00 +01002358 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Valerio Setti5151bdf2022-11-21 14:30:02 +01002359 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002360
2361 p += 3;
2362
Valerio Setti6b3dab02022-11-17 17:14:54 +01002363 if( ( ret = mbedtls_psa_ecjpake_read_round(
2364 &ssl->handshake->psa_pake_ctx, p, end - p,
2365 MBEDTLS_ECJPAKE_ROUND_TWO ) ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02002366 {
2367 psa_destroy_key( ssl->handshake->psa_pake_password );
2368 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
2369
2370 MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round two", ret );
2371 mbedtls_ssl_send_alert_message(
2372 ssl,
2373 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2374 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2375 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2376 }
2377#else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002378 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2379 p, end - p );
2380 if( ret != 0 )
2381 {
2382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002383 mbedtls_ssl_send_alert_message(
2384 ssl,
2385 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002386 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2387 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002388 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002389#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002390 }
2391 else
2392#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002396 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002397
Gilles Peskineeccd8882020-03-10 12:19:08 +01002398#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002399 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002400 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002401 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002402 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2405 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2406 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002407 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002408 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002409 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002410
Hanno Beckera6899bb2019-02-06 18:26:03 +00002411 mbedtls_pk_context * peer_pk;
2412
Jerry Yu693a47a2022-06-23 14:02:28 +08002413#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2414 peer_pk = &ssl->handshake->peer_pubkey;
2415#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2416 if( ssl->session_negotiate->peer_cert == NULL )
2417 {
2418 /* Should never happen */
2419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2421 }
2422 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2423#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2424
Paul Bakker29e1f122013-04-16 13:07:56 +02002425 /*
2426 * Handle the digitally-signed structure
2427 */
Jerry Yu693a47a2022-06-23 14:02:28 +08002428 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2429 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yu95b743c2022-07-23 11:37:50 +08002430 if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuc3bf7482022-07-29 10:27:17 +08002431 sig_alg, &pk_alg, &md_alg ) != 0 &&
Jerry Yu693a47a2022-06-23 14:02:28 +08002432 ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) &&
2433 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002434 {
Ronald Cron90915f22022-03-07 11:11:36 +01002435 MBEDTLS_SSL_DEBUG_MSG( 1,
2436 ( "bad server key exchange message" ) );
2437 mbedtls_ssl_send_alert_message(
2438 ssl,
2439 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2440 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2441 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002442 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002443 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002444
Jerry Yu693a47a2022-06-23 14:02:28 +08002445 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002446 {
Ronald Cron90915f22022-03-07 11:11:36 +01002447 MBEDTLS_SSL_DEBUG_MSG( 1,
2448 ( "bad server key exchange message" ) );
2449 mbedtls_ssl_send_alert_message(
2450 ssl,
2451 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2452 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2453 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002454 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002455
2456 /*
2457 * Read signature
2458 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002459
2460 if( p > end - 2 )
2461 {
2462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002463 mbedtls_ssl_send_alert_message(
2464 ssl,
2465 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2466 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002467 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002468 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002469 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002470 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002471
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002472 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002475 mbedtls_ssl_send_alert_message(
2476 ssl,
2477 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2478 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002479 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002483
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002484 /*
2485 * Compute the hash that has been signed
2486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002488 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002489 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2490 params, params_len,
2491 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002492 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002493 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002495 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002499 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002500
Gilles Peskineca1d7422018-04-24 11:53:22 +02002501 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002502
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002503 /*
2504 * Verify signature
2505 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002506 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002509 mbedtls_ssl_send_alert_message(
2510 ssl,
2511 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2512 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002514 }
2515
Gilles Peskineeccd8882020-03-10 12:19:08 +01002516#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002517 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002518 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002519#endif
2520
Jerry Yu693a47a2022-06-23 14:02:28 +08002521#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2522 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
2523 {
Jerry Yu693a47a2022-06-23 14:02:28 +08002524 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2525 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002526 rsassa_pss_options.expected_salt_len =
2527 mbedtls_hash_info_get_size( md_alg );
2528 if( rsassa_pss_options.expected_salt_len == 0 )
Jerry Yu693a47a2022-06-23 14:02:28 +08002529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002530
Jerry Yu693a47a2022-06-23 14:02:28 +08002531 ret = mbedtls_pk_verify_ext( pk_alg, &rsassa_pss_options,
2532 peer_pk,
2533 md_alg, hash, hashlen,
2534 p, sig_len );
2535 }
2536 else
2537#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2538 ret = mbedtls_pk_verify_restartable( peer_pk,
2539 md_alg, hash, hashlen, p, sig_len, rs_ctx );
2540
2541 if( ret != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002542 {
David Horstmannb21bbef2022-10-06 17:49:31 +01002543 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002544#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
David Horstmannb21bbef2022-10-06 17:49:31 +01002545 send_alert_msg = ( ret != MBEDTLS_ERR_ECP_IN_PROGRESS );
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002546#endif
David Horstmannb21bbef2022-10-06 17:49:31 +01002547 if( send_alert_msg )
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002548 mbedtls_ssl_send_alert_message(
2549 ssl,
2550 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2551 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002553#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002554 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2555 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2556#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002557 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002558 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002559
2560#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2561 /* We don't need the peer's public key anymore. Free it,
2562 * so that more RAM is available for upcoming expensive
2563 * operations like ECDHE. */
2564 mbedtls_pk_free( peer_pk );
2565#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002567#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002569exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 ssl->state++;
2571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
2574 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575}
2576
Gilles Peskineeccd8882020-03-10 12:19:08 +01002577#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002578MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002580{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002581 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002582 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002585
Hanno Becker1aa267c2017-04-28 17:08:27 +01002586 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002589 ssl->state++;
2590 return( 0 );
2591 }
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002595}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002596#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002597MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002599{
Janos Follath865b3eb2019-12-16 11:46:15 +00002600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002601 unsigned char *buf;
2602 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002603 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002604 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002605 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002606 size_t sig_alg_len;
2607#if defined(MBEDTLS_DEBUG_C)
2608 unsigned char *sig_alg;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002609 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002610#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
Hanno Becker1aa267c2017-04-28 17:08:27 +01002614 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002617 ssl->state++;
2618 return( 0 );
2619 }
2620
Hanno Becker327c93b2018-08-15 13:56:18 +01002621 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2624 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002627 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2628 {
2629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002630 mbedtls_ssl_send_alert_message(
2631 ssl,
2632 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2633 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002634 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002637 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002638 ssl->handshake->client_auth =
2639 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002642 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Jerry Yufb28b882022-01-28 11:05:58 +08002644 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002645 {
Johan Pascala89ca862020-08-25 10:03:19 +02002646 /* Current message is probably the ServerHelloDone */
2647 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002648 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002649 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002650
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002651 /*
2652 * struct {
2653 * ClientCertificateType certificate_types<1..2^8-1>;
2654 * SignatureAndHashAlgorithm
2655 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2656 * DistinguishedName certificate_authorities<0..2^16-1>;
2657 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002658 *
2659 * Since we only support a single certificate on clients, let's just
2660 * ignore all the information that's supposed to help us pick a
2661 * certificate.
2662 *
2663 * We could check that our certificate matches the request, and bail out
2664 * if it doesn't, but it's simpler to just send the certificate anyway,
2665 * and give the server the opportunity to decide if it should terminate
2666 * the connection when it doesn't like our certificate.
2667 *
2668 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2669 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002670 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002671 *
2672 * However, we still minimally parse the message to check it is at least
2673 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002674 */
Paul Bakker926af752012-11-23 13:38:07 +01002675 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002676
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002677 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002678 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2679 {
2680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2681 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002683 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002686 n = cert_type_len;
2687
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002688 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002689 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002690 * * the length of the signature algorithms field (if minor version of
2691 * SSL is 3),
2692 * * distinguished name length otherwise.
2693 * Both reach at most the index:
2694 * ...hdr_len + 2 + n,
2695 * therefore the buffer length at this point must be greater than that
2696 * regardless of the actual code path.
2697 */
2698 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002701 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002703 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002704 }
2705
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002706 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002707 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2708 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2709
2710 /*
2711 * The furthest access in buf is in the loop few lines below:
2712 * sig_alg[i + 1],
2713 * where:
2714 * sig_alg = buf + ...hdr_len + 3 + n,
2715 * max(i) = sig_alg_len - 1.
2716 * Therefore the furthest access is:
2717 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2718 * which reduces to:
2719 * buf[...hdr_len + 3 + n + sig_alg_len],
2720 * which is one less than we need the buf to be.
2721 */
2722 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002723 {
Ronald Cron90915f22022-03-07 11:11:36 +01002724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2725 mbedtls_ssl_send_alert_message(
2726 ssl,
2727 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2728 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2729 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002730 }
Paul Bakker926af752012-11-23 13:38:07 +01002731
Ronald Cron90915f22022-03-07 11:11:36 +01002732#if defined(MBEDTLS_DEBUG_C)
2733 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2734 for( size_t i = 0; i < sig_alg_len; i += 2 )
2735 {
2736 MBEDTLS_SSL_DEBUG_MSG( 3,
2737 ( "Supported Signature Algorithm found: %d,%d",
2738 sig_alg[i], sig_alg[i + 1] ) );
2739 }
2740#endif
2741
2742 n += 2 + sig_alg_len;
2743
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002744 /* certificate_authorities */
2745 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2746 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002747
2748 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002749 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2753 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002754 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002755 }
2756
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002757#if defined(MBEDTLS_DEBUG_C)
2758 dn = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n - dn_len;
2759 for( size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len )
2760 {
2761 unsigned char *p = dn + i + 2;
2762 mbedtls_x509_name name;
2763 mbedtls_x509_name *name_cur, *name_prv;
2764 size_t asn1_len;
2765 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
2766 memset( &name, 0, sizeof( name ) );
2767 dni_len = MBEDTLS_GET_UINT16_BE( dn + i, 0 );
2768 if( dni_len > dn_len - i - 2 ||
2769 mbedtls_asn1_get_tag( &p, p + dni_len, &asn1_len,
2770 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) != 0 ||
2771 mbedtls_x509_get_name( &p, p + asn1_len, &name ) != 0 )
2772 {
2773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2774 mbedtls_ssl_send_alert_message(
2775 ssl,
2776 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2777 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2778 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
2779 }
2780 MBEDTLS_SSL_DEBUG_MSG( 3,
2781 ( "DN hint: %.*s",
2782 mbedtls_x509_dn_gets( s, sizeof(s), &name ), s ) );
2783 name_cur = name.next;
2784 while( name_cur != NULL )
2785 {
2786 name_prv = name_cur;
2787 name_cur = name_cur->next;
2788 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2789 mbedtls_free( name_prv );
2790 }
2791 }
2792#endif
2793
Paul Bakker926af752012-11-23 13:38:07 +01002794exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
2797 return( 0 );
2798}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002799#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002801MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002803{
Janos Follath865b3eb2019-12-16 11:46:15 +00002804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
Hanno Becker327c93b2018-08-15 13:56:18 +01002808 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2811 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002813
2814 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2815 {
2816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2817 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2818 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2821 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002824 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2825 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002826 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 }
2828
2829 ssl->state++;
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002834#endif
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
2838 return( 0 );
2839}
2840
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002841MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002843{
Janos Follath865b3eb2019-12-16 11:46:15 +00002844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002845
2846 size_t header_len;
2847 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002848 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002849 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2854 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 /*
2857 * DHM key exchange -- send G^X mod P
2858 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002859 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Joe Subbiani6dd73642021-07-19 11:56:54 +01002861 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002862 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002865 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002866 &ssl->out_msg[header_len], content_len,
2867 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 if( ret != 0 )
2869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 return( ret );
2872 }
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2875 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002878 ssl->handshake->premaster,
2879 MBEDTLS_PREMASTER_SIZE,
2880 &ssl->handshake->pmslen,
2881 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 return( ret );
2885 }
2886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002888 }
2889 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002891#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2892 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2893 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2894 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002895 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002896 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2897 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2898 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002899 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002900#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2902 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002903 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002904
2905 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2906
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002907 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002908
Hanno Becker0a94a642019-01-11 14:35:30 +00002909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2910
Hanno Becker4a63ed42019-01-08 11:39:35 +00002911 /*
2912 * Generate EC private key for ECDHE exchange.
2913 */
2914
Hanno Becker4a63ed42019-01-08 11:39:35 +00002915 /* The master secret is obtained from the shared ECDH secret by
2916 * applying the TLS 1.2 PRF with a specific salt and label. While
2917 * the PSA Crypto API encourages combining key agreement schemes
2918 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2919 * yet support the provisioning of salt + label to the KDF.
2920 * For the time being, we therefore need to split the computation
2921 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002922 key_attributes = psa_key_attributes_init();
2923 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002924 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002925 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2926 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002927
2928 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002929 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002930 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002931 if( status != PSA_SUCCESS )
2932 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2933
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002934 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002935 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002936 * but we just need to add a length byte before that. */
2937 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2938 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2939 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2940 size_t own_pubkey_len;
2941
Hanno Becker4a63ed42019-01-08 11:39:35 +00002942 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002943 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002944 &own_pubkey_len );
2945 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002946 {
2947 psa_destroy_key( handshake->ecdh_psa_privkey );
2948 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002949 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002950 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002951
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002952 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2953 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002954
Hanno Becker4a63ed42019-01-08 11:39:35 +00002955 /* The ECDH secret is the premaster secret used for key derivation. */
2956
Janos Follathdf3b0892019-08-08 11:12:24 +01002957 /* Compute ECDH shared secret. */
2958 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2959 handshake->ecdh_psa_privkey,
2960 handshake->ecdh_psa_peerkey,
2961 handshake->ecdh_psa_peerkey_len,
2962 ssl->handshake->premaster,
2963 sizeof( ssl->handshake->premaster ),
2964 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002965
Andrzej Kureka0237f82022-02-24 13:24:52 -05002966 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002967 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002968
2969 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2970 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002971#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002972 /*
2973 * ECDH key exchange -- send client public value
2974 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002975 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002976
Gilles Peskineeccd8882020-03-10 12:19:08 +01002977#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002978 if( ssl->handshake->ecrs_enabled )
2979 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002980 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002981 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002982
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002983 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2984 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002985#endif
2986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002988 &content_len,
2989 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002990 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002991 if( ret != 0 )
2992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002994#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002995 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2996 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2997#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002998 return( ret );
2999 }
3000
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003001 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3002 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003003
Gilles Peskineeccd8882020-03-10 12:19:08 +01003004#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003005 if( ssl->handshake->ecrs_enabled )
3006 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003007 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003008 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003009 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003010
3011ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003012 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003013 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003016 &ssl->handshake->pmslen,
3017 ssl->handshake->premaster,
3018 MBEDTLS_MPI_MAX_SIZE,
3019 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003022#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003023 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3024 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3025#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003026 return( ret );
3027 }
3028
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003029 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3030 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02003031#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01003032 }
3033 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3035 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3036 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3037 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01003038#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3039 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3040 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3041 {
3042 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3043 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
3044 psa_key_attributes_t key_attributes;
3045
3046 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3047
3048 /*
3049 * opaque psk_identity<0..2^16-1>;
3050 */
3051 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01003052 /* We don't offer PSK suites if we don't have a PSK,
3053 * and we check that the server's choice is among the
3054 * ciphersuites we offered, so this should never happen. */
3055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003056
Neil Armstrongfc834f22022-03-23 17:54:38 +01003057 /* uint16 to store content length */
3058 const size_t content_len_size = 2;
3059
Neil Armstrong868af822022-03-09 10:26:25 +01003060 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01003061
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003062 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01003063 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01003064 {
3065 MBEDTLS_SSL_DEBUG_MSG( 1,
3066 ( "psk identity too long or SSL buffer too short" ) );
3067 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3068 }
3069
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003070 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003071
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003072 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
3073 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
3074 header_len += content_len_size;
3075
3076 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003077 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003078 p += ssl->conf->psk_identity_len;
3079
Neil Armstrong868af822022-03-09 10:26:25 +01003080 header_len += ssl->conf->psk_identity_len;
3081
3082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3083
3084 /*
3085 * Generate EC private key for ECDHE exchange.
3086 */
3087
3088 /* The master secret is obtained from the shared ECDH secret by
3089 * applying the TLS 1.2 PRF with a specific salt and label. While
3090 * the PSA Crypto API encourages combining key agreement schemes
3091 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3092 * yet support the provisioning of salt + label to the KDF.
3093 * For the time being, we therefore need to split the computation
3094 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3095 key_attributes = psa_key_attributes_init();
3096 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3097 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3098 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3099 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3100
3101 /* Generate ECDH private key. */
3102 status = psa_generate_key( &key_attributes,
3103 &handshake->ecdh_psa_privkey );
3104 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003105 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003106
3107 /* Export the public part of the ECDH private key from PSA.
3108 * The export format is an ECPoint structure as expected by TLS,
3109 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003110 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003111 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3112 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003113 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003114
3115 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3116 own_pubkey, own_pubkey_max_len,
3117 &own_pubkey_len );
3118 if( status != PSA_SUCCESS )
3119 {
3120 psa_destroy_key( handshake->ecdh_psa_privkey );
3121 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003122 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003123 }
3124
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003125 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003126 content_len = own_pubkey_len + 1;
3127
Neil Armstrong25400452022-03-23 17:44:07 +01003128 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3129 * - a uint16 containing the length (in octets) of the ECDH computation
3130 * - the octet string produced by the ECDH computation
3131 * - a uint16 containing the length (in octets) of the PSK
3132 * - the PSK itself
3133 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003134 unsigned char *pms = ssl->handshake->premaster;
3135 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003136 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003137 /* uint16 to store length (in octets) of the ECDH computation */
3138 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003139 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003140
Neil Armstrong25400452022-03-23 17:44:07 +01003141 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003142 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3143 handshake->ecdh_psa_privkey,
3144 handshake->ecdh_psa_peerkey,
3145 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003146 pms + zlen_size,
3147 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003148 &zlen );
3149
3150 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3151 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3152
Neil Armstrongc530aa62022-03-23 17:45:01 +01003153 if( status != PSA_SUCCESS )
3154 return( psa_ssl_status_to_mbedtls( status ) );
3155 else if( destruction_status != PSA_SUCCESS )
3156 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003157
Neil Armstrong25400452022-03-23 17:44:07 +01003158 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003159 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3160 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003161 }
3162 else
3163#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3164 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003165#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003166 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003167 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003168 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003169 * opaque psk_identity<0..2^16-1>;
3170 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003171 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003172 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003173 /* We don't offer PSK suites if we don't have a PSK,
3174 * and we check that the server's choice is among the
3175 * ciphersuites we offered, so this should never happen. */
3176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003177 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003178
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003179 header_len = 4;
3180 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003181
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003182 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003183 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003184 MBEDTLS_SSL_DEBUG_MSG( 1,
3185 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003186 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3187 }
3188
Joe Subbianifbeb6922021-07-16 14:27:50 +01003189 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3190 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003191
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003192 memcpy( ssl->out_msg + header_len,
3193 ssl->conf->psk_identity,
3194 ssl->conf->psk_identity_len );
3195 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3198 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003199 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003200 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003201 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003202 else
3203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3205 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003206 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003207 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3208 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003209 return( ret );
3210 }
3211 else
3212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3214 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003215 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003216 /*
3217 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3218 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003219 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003220
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003221 if( header_len + 2 + content_len >
3222 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003223 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003224 MBEDTLS_SSL_DEBUG_MSG( 1,
3225 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003226 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3227 }
3228
Joe Subbianifbeb6922021-07-16 14:27:50 +01003229 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3230 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003233 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003234 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003235 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003236 if( ret != 0 )
3237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003239 return( ret );
3240 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003241
3242#if defined(MBEDTLS_USE_PSA_CRYPTO)
3243 unsigned char *pms = ssl->handshake->premaster;
3244 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3245 size_t pms_len;
3246
3247 /* Write length only when we know the actual value */
3248 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3249 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3250 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3251 {
3252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3253 return( ret );
3254 }
3255 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3256 pms += 2 + pms_len;
3257
3258 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3259#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003260 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003261 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003263#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3264 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003266 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003267 /*
3268 * ClientECDiffieHellmanPublic public;
3269 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003270 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3271 &content_len,
3272 &ssl->out_msg[header_len],
3273 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003274 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003275 if( ret != 0 )
3276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003278 return( ret );
3279 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003280
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003281 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3282 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003283 }
3284 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003285#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003289 }
3290
Neil Armstrong80f6f322022-05-03 17:56:38 +02003291#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3292 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3293 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003294 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003295 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003296 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003297 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003298 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003299#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003300 }
3301 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003302#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3304 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003306 header_len = 4;
3307 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3308 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003309 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 }
Paul Bakkered27a042013-04-18 22:46:23 +02003311 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003313#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3314 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3315 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003316 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003317
Neil Armstrongca7d5062022-05-31 14:43:23 +02003318#if defined(MBEDTLS_USE_PSA_CRYPTO)
3319 unsigned char *out_p = ssl->out_msg + header_len;
3320 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3321 header_len;
Valerio Setti6b3dab02022-11-17 17:14:54 +01003322 ret = mbedtls_psa_ecjpake_write_round( &ssl->handshake->psa_pake_ctx,
3323 out_p, end_p - out_p, &content_len,
3324 MBEDTLS_ECJPAKE_ROUND_TWO );
Valerio Setti02c25b52022-11-15 14:08:42 +01003325 if ( ret != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02003326 {
Valerio Setti02c25b52022-11-15 14:08:42 +01003327 psa_destroy_key( ssl->handshake->psa_pake_password );
3328 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
3329 MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret );
3330 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +02003331 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003332#else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003333 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003334 ssl->out_msg + header_len,
3335 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3336 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003337 ssl->conf->f_rng, ssl->conf->p_rng );
3338 if( ret != 0 )
3339 {
3340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3341 return( ret );
3342 }
3343
3344 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3345 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3346 ssl->conf->f_rng, ssl->conf->p_rng );
3347 if( ret != 0 )
3348 {
3349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3350 return( ret );
3351 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003352#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003353 }
3354 else
3355#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003356 {
3357 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003362 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3364 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
3366 ssl->state++;
3367
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003368 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 return( ret );
3372 }
3373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
3376 return( 0 );
3377}
3378
Gilles Peskineeccd8882020-03-10 12:19:08 +01003379#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003380MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003382{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003383 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003384 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003392 return( ret );
3393 }
3394
Hanno Becker77adddc2019-02-07 12:32:43 +00003395 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003398 ssl->state++;
3399 return( 0 );
3400 }
3401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003404}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003405#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003406MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003411 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003412 size_t n = 0, offset = 0;
3413 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003414 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003416 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003417 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003418#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3419 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3420#else
3421 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3422#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003425
Gilles Peskineeccd8882020-03-10 12:19:08 +01003426#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003427 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003428 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003429 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003430 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003431 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003432#endif
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003437 return( ret );
3438 }
3439
Hanno Becker77adddc2019-02-07 12:32:43 +00003440 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003443 ssl->state++;
3444 return( 0 );
3445 }
3446
Jerry Yufb28b882022-01-28 11:05:58 +08003447 if( ssl->handshake->client_auth == 0 ||
3448 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 {
Johan Pascala89ca862020-08-25 10:03:19 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3451 ssl->state++;
3452 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 }
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460
3461 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003462 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003464#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003465 if( ssl->handshake->ecrs_enabled )
3466 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3467
3468sign:
3469#endif
3470
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003471 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003472
Ronald Cron90915f22022-03-07 11:11:36 +01003473 /*
3474 * digitally-signed struct {
3475 * opaque handshake_messages[handshake_messages_length];
3476 * };
3477 *
3478 * Taking shortcut here. We assume that the server always allows the
3479 * PRF Hash function and has sent it in the allowed signature
3480 * algorithms list received in the Certificate Request message.
3481 *
3482 * Until we encounter a server that does not, we will take this
3483 * shortcut.
3484 *
3485 * Reason: Otherwise we should have running hashes for SHA512 and
3486 * SHA224 in order to satisfy 'weird' needs from the server
3487 * side.
3488 */
3489 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003490 {
Ronald Cron90915f22022-03-07 11:11:36 +01003491 md_alg = MBEDTLS_MD_SHA384;
3492 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003493 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003494 else
Paul Bakker577e0062013-08-28 11:57:20 +02003495 {
Ronald Cron90915f22022-03-07 11:11:36 +01003496 md_alg = MBEDTLS_MD_SHA256;
3497 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003498 }
Ronald Cron90915f22022-03-07 11:11:36 +01003499 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3500
3501 /* Info from md_alg will be used instead */
3502 hashlen = 0;
3503 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003504
Gilles Peskineeccd8882020-03-10 12:19:08 +01003505#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003506 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003507 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003508#endif
3509
3510 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3511 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003512 ssl->out_msg + 6 + offset,
3513 out_buf_len - 6 - offset,
3514 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003515 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003518#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003519 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3520 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3521#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003522 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003523 }
Paul Bakker926af752012-11-23 13:38:07 +01003524
Joe Subbiani6dd73642021-07-19 11:56:54 +01003525 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003526
Paul Bakker1ef83d62012-04-11 12:09:53 +00003527 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3529 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003530
3531 ssl->state++;
3532
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003533 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003536 return( ret );
3537 }
3538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
Paul Bakkered27a042013-04-18 22:46:23 +02003541 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003543#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003546MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003548{
Janos Follath865b3eb2019-12-16 11:46:15 +00003549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003550 uint32_t lifetime;
3551 size_t ticket_len;
3552 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003553 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003556
Hanno Becker327c93b2018-08-15 13:56:18 +01003557 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003560 return( ret );
3561 }
3562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003566 mbedtls_ssl_send_alert_message(
3567 ssl,
3568 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3569 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003571 }
3572
3573 /*
3574 * struct {
3575 * uint32 ticket_lifetime_hint;
3576 * opaque ticket<0..2^16-1>;
3577 * } NewSessionTicket;
3578 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003579 * 0 . 3 ticket_lifetime_hint
3580 * 4 . 5 ticket_len (n)
3581 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3584 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003587 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3588 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003589 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003590 }
3591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003593
Philippe Antoineb5b25432018-05-11 11:06:29 +02003594 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3595 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003596
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003597 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003604 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003605 }
3606
Paul Elliottd48d5c62021-01-07 14:47:05 +00003607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003608
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003609 /* We're not waiting for a NewSessionTicket message any more */
3610 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003612
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003613 /*
3614 * Zero-length ticket means the server changed his mind and doesn't want
3615 * to send a ticket after all, so just forget it
3616 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003617 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003618 return( 0 );
3619
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003620 if( ssl->session != NULL && ssl->session->ticket != NULL )
3621 {
3622 mbedtls_platform_zeroize( ssl->session->ticket,
3623 ssl->session->ticket_len );
3624 mbedtls_free( ssl->session->ticket );
3625 ssl->session->ticket = NULL;
3626 ssl->session->ticket_len = 0;
3627 }
3628
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003629 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3630 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003632 ssl->session_negotiate->ticket = NULL;
3633 ssl->session_negotiate->ticket_len = 0;
3634
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003635 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003636 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003638 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3639 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003640 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003641 }
3642
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003643 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003644
3645 ssl->session_negotiate->ticket = ticket;
3646 ssl->session_negotiate->ticket_len = ticket_len;
3647 ssl->session_negotiate->ticket_lifetime = lifetime;
3648
3649 /*
3650 * RFC 5077 section 3.4:
3651 * "If the client receives a session ticket from the server, then it
3652 * discards any Session ID that was sent in the ServerHello."
3653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003655 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003658
3659 return( 0 );
3660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003662
Paul Bakker5121ce52009-01-03 21:22:43 +00003663/*
Paul Bakker1961b702013-01-25 14:49:24 +01003664 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003667{
3668 int ret = 0;
3669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003671 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3673 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003674 ssl->handshake->new_session_ticket != 0 )
3675 {
Jerry Yua357cf42022-07-12 05:36:45 +00003676 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003677 }
3678#endif
3679
Paul Bakker1961b702013-01-25 14:49:24 +01003680 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 case MBEDTLS_SSL_HELLO_REQUEST:
3683 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003684 break;
3685
Paul Bakker1961b702013-01-25 14:49:24 +01003686 /*
3687 * ==> ClientHello
3688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003690 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003691 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
Paul Bakker1961b702013-01-25 14:49:24 +01003693 /*
3694 * <== ServerHello
3695 * Certificate
3696 * ( ServerKeyExchange )
3697 * ( CertificateRequest )
3698 * ServerHelloDone
3699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003701 ret = ssl_parse_server_hello( ssl );
3702 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3705 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003706 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003709 ret = ssl_parse_server_key_exchange( ssl );
3710 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003713 ret = ssl_parse_certificate_request( ssl );
3714 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003717 ret = ssl_parse_server_hello_done( ssl );
3718 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
Paul Bakker1961b702013-01-25 14:49:24 +01003720 /*
3721 * ==> ( Certificate/Alert )
3722 * ClientKeyExchange
3723 * ( CertificateVerify )
3724 * ChangeCipherSpec
3725 * Finished
3726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3728 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003729 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003732 ret = ssl_write_client_key_exchange( ssl );
3733 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003736 ret = ssl_write_certificate_verify( ssl );
3737 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3740 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003741 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 case MBEDTLS_SSL_CLIENT_FINISHED:
3744 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003745 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003746
Paul Bakker1961b702013-01-25 14:49:24 +01003747 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003748 * <== ( NewSessionTicket )
3749 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003750 * Finished
3751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00003753 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003754 ret = ssl_parse_new_session_ticket( ssl );
3755 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003756#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3759 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003760 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 case MBEDTLS_SSL_SERVER_FINISHED:
3763 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003764 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 case MBEDTLS_SSL_FLUSH_BUFFERS:
3767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3768 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003769 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3772 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003773 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003774
Paul Bakker1961b702013-01-25 14:49:24 +01003775 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003778 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003779
3780 return( ret );
3781}
Jerry Yuc5aef882021-12-23 20:15:02 +08003782
Jerry Yufb4b6472022-01-27 15:03:26 +08003783#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */