blob: 7a1745205c34ed4ae503239c16613a8f7c7f83d3 [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 Becker49770ff2019-04-25 16:55:15 +0100229 * struct {
230 * opaque cid<0..2^8-1>;
231 * } ConnectionId;
232 */
233
234 *olen = 0;
235 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
236 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
237 {
Hanno Becker261602c2017-04-12 14:54:42 +0100238 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100239 }
240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
241
242 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
243 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100244 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100245
246 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100247 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
248 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100249 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100250 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
251 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100252
253 *p++ = (uint8_t) ssl->own_cid_len;
254 memcpy( p, ssl->own_cid, ssl->own_cid_len );
255
256 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100257
258 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100259}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100260#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200263MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100264static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
265 unsigned char *buf,
266 const unsigned char *end,
267 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200268{
269 unsigned char *p = buf;
270
Simon Butcher0fc94e92015-09-28 20:52:04 +0100271 *olen = 0;
272
Hanno Becker261602c2017-04-12 14:54:42 +0100273 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
274 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200275
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100276 MBEDTLS_SSL_DEBUG_MSG( 3,
277 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200278
Hanno Becker261602c2017-04-12 14:54:42 +0100279 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100280
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100281 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
282 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200283
284 *p++ = 0x00;
285 *p++ = 1;
286
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200287 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200288
289 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100290
291 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200296MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100297static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
298 unsigned char *buf,
299 const unsigned char *end,
300 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100301{
302 unsigned char *p = buf;
303
Simon Butcher0fc94e92015-09-28 20:52:04 +0100304 *olen = 0;
305
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100306 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100307 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100308
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100309 MBEDTLS_SSL_DEBUG_MSG( 3,
310 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100311
Hanno Becker261602c2017-04-12 14:54:42 +0100312 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100313
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100314 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
315 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100316
317 *p++ = 0x00;
318 *p++ = 0x00;
319
320 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100321
322 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200327MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100328static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
329 unsigned char *buf,
330 const unsigned char *end,
331 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200332{
333 unsigned char *p = buf;
334
Simon Butcher0fc94e92015-09-28 20:52:04 +0100335 *olen = 0;
336
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100337 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100338 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200339
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100340 MBEDTLS_SSL_DEBUG_MSG( 3,
341 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200342
Hanno Becker261602c2017-04-12 14:54:42 +0100343 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100344
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100345 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
346 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200347
348 *p++ = 0x00;
349 *p++ = 0x00;
350
351 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100352
353 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200358MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100359static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
360 unsigned char *buf,
361 const unsigned char *end,
362 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200363{
364 unsigned char *p = buf;
365 size_t tlen = ssl->session_negotiate->ticket_len;
366
Simon Butcher0fc94e92015-09-28 20:52:04 +0100367 *olen = 0;
368
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200369 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100370 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200371
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100372 MBEDTLS_SSL_DEBUG_MSG( 3,
373 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200374
Hanno Becker261602c2017-04-12 14:54:42 +0100375 /* The addition is safe here since the ticket length is 16 bit. */
376 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100377
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100378 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
379 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200380
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100381 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
382 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200383
384 *olen = 4;
385
Simon Butchered997662015-09-28 02:14:30 +0100386 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100387 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200388
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100389 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000390 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200391
392 memcpy( p, ssl->session_negotiate->ticket, tlen );
393
394 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100395
396 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399
Ron Eldora9788042018-12-05 11:04:31 +0200400#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200401MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascal77696ee2020-09-22 21:49:40 +0200402static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200403 unsigned char *buf,
404 const unsigned char *end,
405 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100406{
407 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200408 size_t protection_profiles_index = 0, ext_len = 0;
409 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100410
411 *olen = 0;
412
Johan Pascalc3ccd982020-10-28 17:18:18 +0100413 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
414 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
415 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100416 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200417 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100418 }
419
Ron Eldora9788042018-12-05 11:04:31 +0200420 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100421 * uint8 SRTPProtectionProfile[2];
422 *
423 * struct {
424 * SRTPProtectionProfiles SRTPProtectionProfiles;
425 * opaque srtp_mki<0..255>;
426 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100427 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200429 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200430 {
431 mki_len = ssl->dtls_srtp_info.mki_len;
432 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300433 /* Extension length = 2 bytes for profiles length,
434 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
435 * 1 byte for srtp_mki vector length and the mki_len value
436 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200437 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
438
Johan Pascal77696ee2020-09-22 21:49:40 +0200439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
440
441 /* Check there is room in the buffer for the extension + 4 bytes
442 * - the extension tag (2 bytes)
443 * - the extension length (2 bytes)
444 */
445 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
446
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100447 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
448 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200449
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100450 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
451 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100452
Ron Eldor3adb9922017-12-21 10:15:08 +0200453 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200454 /* micro-optimization:
455 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
456 * which is lower than 127, so the upper byte of the length is always 0
457 * For the documentation, the more generic code is left in comments
458 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
459 * >> 8 ) & 0xFF );
460 */
461 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100462 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100463
Ron Eldoref72faf2018-07-12 11:54:20 +0300464 for( protection_profiles_index=0;
465 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
466 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100467 {
Johan Pascal43f94902020-09-22 12:25:52 +0200468 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200469 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200470 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200471 {
472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
473 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100474 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
475 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200476 }
477 else
478 {
479 /*
480 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200481 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200482 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200483 MBEDTLS_SSL_DEBUG_MSG( 3,
484 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200485 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200486 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
487 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200488 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100489 }
490 }
491
Ron Eldor591f1622018-01-22 12:30:04 +0200492 *p++ = mki_len & 0xFF;
493
494 if( mki_len != 0 )
495 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200496 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200497 /*
498 * Increment p to point to the current position.
499 */
500 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300501 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
502 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200503 }
504
Ron Eldoref72faf2018-07-12 11:54:20 +0300505 /*
506 * total extension length: extension type (2 bytes)
507 * + extension length (2 bytes)
508 * + protection profile length (2 bytes)
509 * + 2 * number of protection profiles
510 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200511 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300512 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200513 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200514
515 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100516}
517#endif /* MBEDTLS_SSL_DTLS_SRTP */
518
Ronald Cron4079abc2022-02-20 10:35:26 +0100519int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
520 unsigned char *buf,
521 const unsigned char *end,
522 int uses_ec,
523 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100524{
525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
526 unsigned char *p = buf;
527 size_t ext_len = 0;
528
529 (void) ssl;
530 (void) end;
531 (void) uses_ec;
532 (void) ret;
533 (void) ext_len;
534
535 *out_len = 0;
536
537 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
538 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
539#if defined(MBEDTLS_SSL_RENEGOTIATION)
540 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
541 {
542 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
543 return( ret );
544 }
545 p += ext_len;
546#endif
547
548#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
549 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
550 if( uses_ec )
551 {
552 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
553 &ext_len ) ) != 0 )
554 {
555 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
556 return( ret );
557 }
558 p += ext_len;
559 }
560#endif
561
562#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
563 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
564 {
565 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
566 return( ret );
567 }
568 p += ext_len;
569#endif
570
571#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
572 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
573 {
574 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
575 return( ret );
576 }
577 p += ext_len;
578#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
579
580#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
581 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
582 &ext_len ) ) != 0 )
583 {
584 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
585 return( ret );
586 }
587 p += ext_len;
588#endif
589
590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
591 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
592 {
593 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
594 return( ret );
595 }
596 p += ext_len;
597#endif
598
599#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
600 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
601 {
602 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
603 return( ret );
604 }
605 p += ext_len;
606#endif
607
608#if defined(MBEDTLS_SSL_DTLS_SRTP)
609 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
610 {
611 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
612 return( ret );
613 }
614 p += ext_len;
615#endif
616
617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
618 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
621 return( ret );
622 }
623 p += ext_len;
624#endif
625
626 *out_len = p - buf;
627
628 return( 0 );
629}
630
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200631MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200633 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000634 size_t len )
635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#if defined(MBEDTLS_SSL_RENEGOTIATION)
637 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000638 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100639 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000640 if( len != 1 + ssl->verify_data_len * 2 ||
641 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200642 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100643 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200644 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100645 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100648 mbedtls_ssl_send_alert_message(
649 ssl,
650 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
651 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100652 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000653 }
654 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100655 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100657 {
658 if( len != 1 || buf[0] != 0x00 )
659 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200660 MBEDTLS_SSL_DEBUG_MSG( 1,
661 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100662 mbedtls_ssl_send_alert_message(
663 ssl,
664 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
665 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100666 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100667 }
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100670 }
Paul Bakker48916f92012-09-16 19:57:18 +0000671
672 return( 0 );
673}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200676MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200678 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200679 size_t len )
680{
681 /*
682 * server should use the extension only if we did,
683 * and if so the server's value should match ours (and len is always 1)
684 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200685 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200686 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200687 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200688 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100689 MBEDTLS_SSL_DEBUG_MSG( 1,
690 ( "non-matching max fragment length extension" ) );
691 mbedtls_ssl_send_alert_message(
692 ssl,
693 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100694 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
695 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200696 }
697
698 return( 0 );
699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000701
Hanno Beckera0e20d02019-05-15 14:03:01 +0100702#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200703MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera8373a12019-04-26 15:37:26 +0100704static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
705 const unsigned char *buf,
706 size_t len )
707{
708 size_t peer_cid_len;
709
710 if( /* CID extension only makes sense in DTLS */
711 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
712 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100713 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100714 {
Hanno Becker22626482019-05-03 12:46:59 +0100715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100716 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100717 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100718 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100719 }
720
721 if( len == 0 )
722 {
723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
724 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100725 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
726 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100727 }
728
729 peer_cid_len = *buf++;
730 len--;
731
732 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
733 {
Hanno Becker22626482019-05-03 12:46:59 +0100734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100735 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100736 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
737 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100738 }
739
740 if( len != peer_cid_len )
741 {
Hanno Becker22626482019-05-03 12:46:59 +0100742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100743 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100744 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
745 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100746 }
747
Hanno Becker5a299902019-05-03 12:47:49 +0100748 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100749 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
750 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
751
752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
753 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
754
Hanno Beckera8373a12019-04-26 15:37:26 +0100755 return( 0 );
756}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100757#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200760MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100762 const unsigned char *buf,
763 size_t len )
764{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200765 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100766 len != 0 )
767 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100768 MBEDTLS_SSL_DEBUG_MSG( 1,
769 ( "non-matching encrypt-then-MAC extension" ) );
770 mbedtls_ssl_send_alert_message(
771 ssl,
772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100773 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100774 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100775 }
776
777 ((void) buf);
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100780
781 return( 0 );
782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200786MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200788 const unsigned char *buf,
789 size_t len )
790{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200791 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200792 len != 0 )
793 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100794 MBEDTLS_SSL_DEBUG_MSG( 1,
795 ( "non-matching extended master secret extension" ) );
796 mbedtls_ssl_send_alert_message(
797 ssl,
798 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100799 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
800 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200801 }
802
803 ((void) buf);
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200806
807 return( 0 );
808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200812MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200814 const unsigned char *buf,
815 size_t len )
816{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200817 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200818 len != 0 )
819 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100820 MBEDTLS_SSL_DEBUG_MSG( 1,
821 ( "non-matching session ticket extension" ) );
822 mbedtls_ssl_send_alert_message(
823 ssl,
824 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100825 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
826 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200827 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200828
829 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200830
831 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200832
833 return( 0 );
834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200836
Robert Cragie136884c2015-10-02 13:34:31 +0100837#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100838 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200839MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200841 const unsigned char *buf,
842 size_t len )
843{
844 size_t list_size;
845 const unsigned char *p;
846
Philippe Antoine747fd532018-05-30 09:13:21 +0200847 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200850 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
851 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100852 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200853 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200854 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200855
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200856 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200857 while( list_size > 0 )
858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
860 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200861 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200862#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
863 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200864 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200865#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
866 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Neil Armstrongca7d5062022-05-31 14:43:23 +0200867#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
868 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200869 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
870 p[0] );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200871#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200873 return( 0 );
874 }
875
876 list_size--;
877 p++;
878 }
879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200881 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
882 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100883 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200884}
Darryl Green11999bb2018-03-13 15:22:58 +0000885#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100886 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200887
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200888#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200889MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200890static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
891 const unsigned char *buf,
892 size_t len )
893{
Janos Follath865b3eb2019-12-16 11:46:15 +0000894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200895
Hanno Beckere694c3e2017-12-27 21:34:08 +0000896 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200897 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
898 {
899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
900 return( 0 );
901 }
902
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200903 /* If we got here, we no longer need our cached extension */
904 mbedtls_free( ssl->handshake->ecjpake_cache );
905 ssl->handshake->ecjpake_cache = NULL;
906 ssl->handshake->ecjpake_cache_len = 0;
907
Neil Armstrongca7d5062022-05-31 14:43:23 +0200908#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +0100909 if( ( ret = mbedtls_psa_ecjpake_read_round(
910 &ssl->handshake->psa_pake_ctx, buf, len,
911 MBEDTLS_ECJPAKE_ROUND_ONE ) ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +0200912 {
Valerio Setti02c25b52022-11-15 14:08:42 +0100913 psa_destroy_key( ssl->handshake->psa_pake_password );
914 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200915
Valerio Setti02c25b52022-11-15 14:08:42 +0100916 MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round one", ret );
917 mbedtls_ssl_send_alert_message(
918 ssl,
919 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
920 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
921 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200922 }
923
924 return( 0 );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200925#else
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200926 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
927 buf, len ) ) != 0 )
928 {
929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100930 mbedtls_ssl_send_alert_message(
931 ssl,
932 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
933 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200934 return( ret );
935 }
936
937 return( 0 );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200938#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200939}
940#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200943MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200945 const unsigned char *buf, size_t len )
946{
947 size_t list_len, name_len;
948 const char **p;
949
950 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200951 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200952 {
953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100954 mbedtls_ssl_send_alert_message(
955 ssl,
956 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100957 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
958 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200959 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200960
961 /*
962 * opaque ProtocolName<1..2^8-1>;
963 *
964 * struct {
965 * ProtocolName protocol_name_list<2..2^16-1>
966 * } ProtocolNameList;
967 *
968 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
969 */
970
971 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
972 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200973 {
974 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
975 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100976 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200977 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200978
979 list_len = ( buf[0] << 8 ) | buf[1];
980 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200981 {
982 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
983 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100984 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200985 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200986
987 name_len = buf[2];
988 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200989 {
990 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
991 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100992 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200993 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200994
995 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200996 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200997 {
998 if( name_len == strlen( *p ) &&
999 memcmp( buf + 3, *p, name_len ) == 0 )
1000 {
1001 ssl->alpn_chosen = *p;
1002 return( 0 );
1003 }
1004 }
1005
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001007 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1008 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001009 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001012
Johan Pascalb62bb512015-12-03 21:56:45 +01001013#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001014MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascalb62bb512015-12-03 21:56:45 +01001015static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001016 const unsigned char *buf,
1017 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001018{
Johan Pascal43f94902020-09-22 12:25:52 +02001019 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001020 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001021 uint16_t server_protection_profile_value = 0;
1022
1023 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001024 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1025 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1026 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001027 return( 0 );
1028
Ron Eldora9788042018-12-05 11:04:31 +02001029 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001030 * uint8 SRTPProtectionProfile[2];
1031 *
1032 * struct {
1033 * SRTPProtectionProfiles SRTPProtectionProfiles;
1034 * opaque srtp_mki<0..255>;
1035 * } UseSRTPData;
1036
1037 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1038 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001039 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001040 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001041 {
1042 mki_len = ssl->dtls_srtp_info.mki_len;
1043 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001044
Ron Eldoref72faf2018-07-12 11:54:20 +03001045 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001046 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1047 * + protection profile (2 bytes)
1048 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001049 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001050 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001051 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001052 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001053
1054 /*
1055 * get the server protection profile
1056 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001057
1058 /*
1059 * protection profile length must be 0x0002 as we must have only
1060 * one protection profile in server Hello
1061 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001062 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001063 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001064
1065 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001066 server_protection = mbedtls_ssl_check_srtp_profile_value(
1067 server_protection_profile_value );
1068 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001069 {
Johan Pascal43f94902020-09-22 12:25:52 +02001070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1071 mbedtls_ssl_get_srtp_profile_as_string(
1072 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001073 }
1074
Johan Pascal43f94902020-09-22 12:25:52 +02001075 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001076
Johan Pascalb62bb512015-12-03 21:56:45 +01001077 /*
1078 * Check we have the server profile in our list
1079 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001080 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001081 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001082 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1083 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001084 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1086 mbedtls_ssl_get_srtp_profile_as_string(
1087 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001088 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001089 }
1090 }
1091
Ron Eldor591f1622018-01-22 12:30:04 +02001092 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001093 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001094 {
1095 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001096 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001097 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001098 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001099
1100 /* If server does not use mki in its reply, make sure the client won't keep
1101 * one as negotiated */
1102 if( len == 5 )
1103 {
1104 ssl->dtls_srtp_info.mki_len = 0;
1105 }
1106
Ron Eldoref72faf2018-07-12 11:54:20 +03001107 /*
1108 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001109 * If the client detects a nonzero-length MKI in the server's response
1110 * that is different than the one the client offered, then the client
1111 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1112 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001113 if( len > 5 && ( buf[4] != mki_len ||
1114 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001115 {
1116 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1117 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001118 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001119 }
Ron Eldorb4655392018-07-05 18:25:39 +03001120#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001121 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001122 {
Ron Eldora9788042018-12-05 11:04:31 +02001123 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1124 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001125 }
1126#endif
Ron Eldora9788042018-12-05 11:04:31 +02001127 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001128}
1129#endif /* MBEDTLS_SSL_DTLS_SRTP */
1130
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001131/*
1132 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001135MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001139 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001140 unsigned char cookie_len;
1141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001143
Gilles Peskineb64bf062019-09-27 14:02:44 +02001144 /* Check that there is enough room for:
1145 * - 2 bytes of version
1146 * - 1 byte of cookie_len
1147 */
1148 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1149 {
1150 MBEDTLS_SSL_DEBUG_MSG( 1,
1151 ( "incoming HelloVerifyRequest message is too short" ) );
1152 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1153 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001154 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001155 }
1156
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001157 /*
1158 * struct {
1159 * ProtocolVersion server_version;
1160 * opaque cookie<0..2^8-1>;
1161 * } HelloVerifyRequest;
1162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001164 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001165 p += 2;
1166
TRodziewicz2d8800e2021-05-13 19:14:19 +02001167 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001168 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1169 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1170 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001171 */
Glenn Strauss83158112022-04-13 14:59:34 -04001172 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1177 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001178
Hanno Beckerbc000442021-06-24 09:18:19 +01001179 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001180 }
1181
1182 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001183 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1184 {
1185 MBEDTLS_SSL_DEBUG_MSG( 1,
1186 ( "cookie length does not match incoming message size" ) );
1187 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1188 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001189 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001190 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001191 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001192
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001193 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001194
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001195 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1196 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001197 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001199 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001200 }
1201
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001202 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001203 ssl->handshake->verify_cookie_len = cookie_len;
1204
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001205 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1207 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001212
1213 return( 0 );
1214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001216
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001217MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001220 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001221 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001222 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001223 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001224 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001226 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001227#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001228 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Hanno Becker327c93b2018-08-15 13:56:18 +01001233 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001235 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 return( ret );
1238 }
1239
Hanno Becker79594fd2019-05-08 09:38:41 +01001240 buf = ssl->in_msg;
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_SSL_RENEGOTIATION)
1245 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001246 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001247 ssl->renego_records_seen++;
1248
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001249 if( ssl->conf->renego_max_records >= 0 &&
1250 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001251 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001252 MBEDTLS_SSL_DEBUG_MSG( 1,
1253 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001255 }
1256
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001257 MBEDTLS_SSL_DEBUG_MSG( 1,
1258 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001259
1260 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001266 mbedtls_ssl_send_alert_message(
1267 ssl,
1268 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1269 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001280 return( ssl_parse_hello_verify_request( ssl ) );
1281 }
1282 else
1283 {
1284 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001285 mbedtls_free( ssl->handshake->cookie );
1286 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001287 ssl->handshake->verify_cookie_len = 0;
1288 }
1289 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1293 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001296 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1297 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001298 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
1300
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001301 /*
1302 * 0 . 1 server_version
1303 * 2 . 33 random (maybe including 4 bytes of Unix time)
1304 * 34 . 34 session_id length = n
1305 * 35 . 34+n session_id
1306 * 35+n . 36+n cipher_suite
1307 * 37+n . 37+n compression_method
1308 *
1309 * 38+n . 39+n extensions length (optional)
1310 * 40+n . .. extensions
1311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001313
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001314 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1315 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001316 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
Glenn Strauss60bfe602022-03-14 19:04:24 -04001318 if( ssl->tls_version < ssl->conf->min_tls_version ||
1319 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001320 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001321 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001322 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1323 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001324 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001325 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1328 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001329
Hanno Beckerbc000442021-06-24 09:18:19 +01001330 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001331 }
1332
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001334 ( (unsigned long) buf[2] << 24 ) |
1335 ( (unsigned long) buf[3] << 16 ) |
1336 ( (unsigned long) buf[4] << 8 ) |
1337 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001339 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001341 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Paul Bakker48916f92012-09-16 19:57:18 +00001345 if( n > 32 )
1346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001348 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1349 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001350 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001351 }
1352
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001353 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001355 ext_len = ( ( buf[38 + n] << 8 )
1356 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Paul Bakker48916f92012-09-16 19:57:18 +00001358 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001362 mbedtls_ssl_send_alert_message(
1363 ssl,
1364 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1365 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001366 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001369 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001370 {
1371 ext_len = 0;
1372 }
1373 else
1374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001376 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1377 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001378 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001379 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001381 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001382 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001383
1384 /*
1385 * Read and check compression
1386 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001387 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001389 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001390 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001391 MBEDTLS_SSL_DEBUG_MSG( 1,
1392 ( "server hello, bad compression: %d", comp ) );
1393 mbedtls_ssl_send_alert_message(
1394 ssl,
1395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1396 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001398 }
1399
Paul Bakker380da532012-04-18 16:10:25 +00001400 /*
1401 * Initialize update checksum functions
1402 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001403 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1404 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001405 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001406 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001407 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001408 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1409 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001411 }
Paul Bakker380da532012-04-18 16:10:25 +00001412
Hanno Beckere694c3e2017-12-27 21:34:08 +00001413 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001414
Paul Elliottd48d5c62021-01-07 14:47:05 +00001415 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
1418 /*
1419 * Check if the session can be resumed
1420 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001421 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_SSL_RENEGOTIATION)
1423 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001424#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001425 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001426 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001427 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
1429 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001430 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001432 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001433#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001434 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001435 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001436 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 }
1438 else
1439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 }
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001444 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Paul Elliott3891caf2020-12-17 18:42:40 +00001446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1448 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449
Andrzej Kurek03bac442018-04-25 05:06:07 -04001450 /*
1451 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001452 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 i = 0;
1454 while( 1 )
1455 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001456 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001459 mbedtls_ssl_send_alert_message(
1460 ssl,
1461 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1462 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001463 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
1465
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001466 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001467 ssl->session_negotiate->ciphersuite )
1468 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 }
1472
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001473 suite_info = mbedtls_ssl_ciphersuite_from_id(
1474 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001475 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1476 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001477 {
1478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001479 mbedtls_ssl_send_alert_message(
1480 ssl,
1481 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001482 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1483 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001484 }
1485
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001486 MBEDTLS_SSL_DEBUG_MSG( 3,
1487 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001488
Gilles Peskineeccd8882020-03-10 12:19:08 +01001489#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001490 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001491 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001492 {
1493 ssl->handshake->ecrs_enabled = 1;
1494 }
1495#endif
1496
Thomas Daubney20f89a92022-06-20 15:12:19 +01001497 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001500 mbedtls_ssl_send_alert_message(
1501 ssl,
1502 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1503 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001504 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 }
1506
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001507 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001508
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001509 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001510 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001511
Paul Bakker48916f92012-09-16 19:57:18 +00001512 while( ext_len )
1513 {
1514 unsigned int ext_id = ( ( ext[0] << 8 )
1515 | ( ext[1] ) );
1516 unsigned int ext_size = ( ( ext[2] << 8 )
1517 | ( ext[3] ) );
1518
1519 if( ext_size + 4 > ext_len )
1520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001522 mbedtls_ssl_send_alert_message(
1523 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1524 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001525 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001526 }
1527
1528 switch( ext_id )
1529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1532#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001533 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001534#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001535
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001536 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1537 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001538 return( ret );
1539
1540 break;
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1543 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001544 MBEDTLS_SSL_DEBUG_MSG( 3,
1545 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001546
1547 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1548 ext + 4, ext_size ) ) != 0 )
1549 {
1550 return( ret );
1551 }
1552
1553 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001555
Hanno Beckera0e20d02019-05-15 14:03:01 +01001556#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001557 case MBEDTLS_TLS_EXT_CID:
1558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1559
1560 if( ( ret = ssl_parse_cid_ext( ssl,
1561 ext + 4,
1562 ext_size ) ) != 0 )
1563 {
1564 return( ret );
1565 }
1566
1567 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001568#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1571 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001573
1574 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1575 ext + 4, ext_size ) ) != 0 )
1576 {
1577 return( ret );
1578 }
1579
1580 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001585 MBEDTLS_SSL_DEBUG_MSG( 3,
1586 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001587
1588 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1589 ext + 4, ext_size ) ) != 0 )
1590 {
1591 return( ret );
1592 }
1593
1594 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1598 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001600
1601 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1602 ext + 4, ext_size ) ) != 0 )
1603 {
1604 return( ret );
1605 }
1606
1607 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001609
Robert Cragie136884c2015-10-02 13:34:31 +01001610#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001611 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001613 MBEDTLS_SSL_DEBUG_MSG( 3,
1614 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001615
1616 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1617 ext + 4, ext_size ) ) != 0 )
1618 {
1619 return( ret );
1620 }
1621
1622 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001623#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1624 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001625
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001626#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1627 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1629
1630 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1631 ext + 4, ext_size ) ) != 0 )
1632 {
1633 return( ret );
1634 }
1635
1636 break;
1637#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_ALPN)
1640 case MBEDTLS_TLS_EXT_ALPN:
1641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001642
Johan Pascal275874b2020-10-27 10:43:53 +01001643 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1644 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001645
1646 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001648
Johan Pascalb62bb512015-12-03 21:56:45 +01001649#if defined(MBEDTLS_SSL_DTLS_SRTP)
1650 case MBEDTLS_TLS_EXT_USE_SRTP:
1651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1652
Johan Pascalc3ccd982020-10-28 17:18:18 +01001653 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1654 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001655
1656 break;
1657#endif /* MBEDTLS_SSL_DTLS_SRTP */
1658
Paul Bakker48916f92012-09-16 19:57:18 +00001659 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001660 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001661 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001662 }
1663
1664 ext_len -= 4 + ext_size;
1665 ext += 4 + ext_size;
1666
1667 if( ext_len > 0 && ext_len < 4 )
1668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001670 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001671 }
1672 }
1673
1674 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001675 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1676 * extensions. It sets the transform data for the resumed session which in
1677 * case of DTLS includes the server CID extracted from the CID extension.
1678 */
1679 if( ssl->handshake->resume )
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001680 {
1681 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
1682 {
1683 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
1684 mbedtls_ssl_send_alert_message(
1685 ssl,
1686 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1687 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
1688 return( ret );
1689 }
1690 }
1691
Paul Bakker48916f92012-09-16 19:57:18 +00001692 /*
1693 * Renegotiation security checks
1694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001696 ssl->conf->allow_legacy_renegotiation ==
1697 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001698 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001699 MBEDTLS_SSL_DEBUG_MSG( 1,
1700 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001701 handshake_failure = 1;
1702 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_SSL_RENEGOTIATION)
1704 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1705 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001706 renegotiation_info_seen == 0 )
1707 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001708 MBEDTLS_SSL_DEBUG_MSG( 1,
1709 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001710 handshake_failure = 1;
1711 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1713 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001714 ssl->conf->allow_legacy_renegotiation ==
1715 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001718 handshake_failure = 1;
1719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1721 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001722 renegotiation_info_seen == 1 )
1723 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001724 MBEDTLS_SSL_DEBUG_MSG( 1,
1725 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001726 handshake_failure = 1;
1727 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001729
1730 if( handshake_failure == 1 )
1731 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001732 mbedtls_ssl_send_alert_message(
1733 ssl,
1734 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1735 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001736 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001737 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001740
1741 return( 0 );
1742}
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1745 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001746MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001747static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1748 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001749 unsigned char *end )
1750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001752 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001753
Paul Bakker29e1f122013-04-16 13:07:56 +02001754 /*
1755 * Ephemeral DH parameters:
1756 *
1757 * struct {
1758 * opaque dh_p<1..2^16-1>;
1759 * opaque dh_g<1..2^16-1>;
1760 * opaque dh_Ys<1..2^16-1>;
1761 * } ServerDHParams;
1762 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001763 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1764 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001767 return( ret );
1768 }
1769
Gilles Peskine487bbf62021-05-27 22:17:07 +02001770 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001771 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001772 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001774 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001775 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001776 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001777 }
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1780 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1781 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001782
1783 return( ret );
1784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1786 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001787
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001788#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek468c5062022-10-24 10:30:14 -04001789#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1790 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1791 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001792MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001793static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001794 unsigned char **p,
1795 unsigned char *end )
1796{
1797 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001798 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001799 uint8_t ecpoint_len;
1800 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1801
1802 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001803 * struct {
1804 * ECParameters curve_params;
1805 * ECPoint public;
1806 * } ServerECDHParams;
1807 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001808 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001809 * 2..3 NamedCurve
1810 * 4 ECPoint.len
1811 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001812 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001813 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001814 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001815
1816 /* First byte is curve_type; only named_curve is handled */
1817 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001818 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001819
1820 /* Next two bytes are the namedcurve value */
1821 tls_id = *(*p)++;
1822 tls_id <<= 8;
1823 tls_id |= *(*p)++;
1824
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001825 /* Check it's a curve we offered */
1826 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001827 {
1828 MBEDTLS_SSL_DEBUG_MSG( 2,
1829 ( "bad server key exchange message (ECDHE curve): %u",
1830 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001831 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001832 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001833
Hanno Beckerbb89e272019-01-08 12:54:37 +00001834 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001835 if( ( handshake->ecdh_psa_type =
1836 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001837 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001838 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001839 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001840 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001841
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001842 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001843 ecpoint_len = *(*p)++;
1844 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001845 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001846
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001847 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1848 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001849
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001850 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1851 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001852 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001853
Hanno Beckerbb89e272019-01-08 12:54:37 +00001854 return( 0 );
1855}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001856#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1857 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1858 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001859#else
Andrzej Kurek468c5062022-10-24 10:30:14 -04001860#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1861 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1862 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1863 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1864 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001865MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrong1f198d82022-04-13 15:02:30 +02001866static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1867{
1868 const mbedtls_ecp_curve_info *curve_info;
1869 mbedtls_ecp_group_id grp_id;
1870#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1871 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1872#else
1873 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1874#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001875
Neil Armstrong1f198d82022-04-13 15:02:30 +02001876 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1877 if( curve_info == NULL )
1878 {
1879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1881 }
1882
1883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1884
1885 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1886 return( -1 );
1887
1888 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1889 MBEDTLS_DEBUG_ECDH_QP );
1890
1891 return( 0 );
1892}
1893
Andrzej Kurek468c5062022-10-24 10:30:14 -04001894#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1895 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1896 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1897 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1898 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1899
1900#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1901 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1902 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001903MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001905 unsigned char **p,
1906 unsigned char *end )
1907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001909
Paul Bakker29e1f122013-04-16 13:07:56 +02001910 /*
1911 * Ephemeral ECDH parameters:
1912 *
1913 * struct {
1914 * ECParameters curve_params;
1915 * ECPoint public;
1916 * } ServerECDHParams;
1917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001919 (const unsigned char **) p, end ) ) != 0 )
1920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001922#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001923 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1924 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001925#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001926 return( ret );
1927 }
1928
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001929 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001930 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001931 MBEDTLS_SSL_DEBUG_MSG( 1,
1932 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001933 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001934 }
1935
Paul Bakker29e1f122013-04-16 13:07:56 +02001936 return( ret );
1937}
Andrzej Kurek468c5062022-10-24 10:30:14 -04001938#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
1939 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
1940 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1941#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineeccd8882020-03-10 12:19:08 +01001942#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001943MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001945 unsigned char **p,
1946 unsigned char *end )
1947{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001949 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001950 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001951
1952 /*
1953 * PSK parameters:
1954 *
1955 * opaque psk_identity_hint<0..2^16-1>;
1956 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001957 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001958 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001959 MBEDTLS_SSL_DEBUG_MSG( 1,
1960 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001961 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001962 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001963 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001964 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001965
irwir6527bd62019-09-21 18:51:25 +03001966 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001967 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001968 MBEDTLS_SSL_DEBUG_MSG( 1,
1969 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001970 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001971 }
1972
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001973 /*
Tom Cosgroveed4f59e2022-12-05 12:07:50 +00001974 * Note: we currently ignore the PSK identity hint, as we only allow one
Tom Cosgrove1797b052022-12-04 17:19:59 +00001975 * PSK to be provisioned on the client. This could be changed later if
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001976 * someone needs that feature.
1977 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001978 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001979 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001980
1981 return( ret );
1982}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001983#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1986 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001987/*
1988 * Generate a pre-master secret and encrypt it with the server's RSA key
1989 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001990MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001992 size_t offset, size_t *olen,
1993 size_t pms_offset )
1994{
Janos Follath865b3eb2019-12-16 11:46:15 +00001995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001996 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001997 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001998 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001999
Angus Grattond8213d02016-05-25 20:56:48 +10002000 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002001 {
2002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2003 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2004 }
2005
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002006 /*
2007 * Generate (part of) the pre-master as
2008 * struct {
2009 * ProtocolVersion client_version;
2010 * opaque random[46];
2011 * } PreMasterSecret;
2012 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04002013 mbedtls_ssl_write_version( p, ssl->conf->transport,
2014 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002015
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002016 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002019 return( ret );
2020 }
2021
2022 ssl->handshake->pmslen = 48;
2023
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002024#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2025 peer_pk = &ssl->handshake->peer_pubkey;
2026#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002027 if( ssl->session_negotiate->peer_cert == NULL )
2028 {
Hanno Becker8273df82019-02-06 17:37:32 +00002029 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002032 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002033 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2034#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002035
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002036 /*
2037 * Now write it out, encrypted
2038 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002039 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2042 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002043 }
2044
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002045 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002046 p, ssl->handshake->pmslen,
2047 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002048 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002049 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002052 return( ret );
2053 }
2054
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002055 if( len_bytes == 2 )
2056 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002057 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002058 *olen += 2;
2059 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002060
Hanno Beckerae553dd2019-02-08 14:06:00 +00002061#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2062 /* We don't need the peer's public key anymore. Free it. */
2063 mbedtls_pk_free( peer_pk );
2064#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002065 return( 0 );
2066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2068 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2071 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002072MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002074{
Janos Follath865b3eb2019-12-16 11:46:15 +00002075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002077 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002078
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002079#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2080 peer_pk = &ssl->handshake->peer_pubkey;
2081#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002082 if( ssl->session_negotiate->peer_cert == NULL )
2083 {
Hanno Becker8273df82019-02-06 17:37:32 +00002084 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002087 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002088 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2089#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002090
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002091 /* This is a public key, so it can't be opaque, so can_do() is a good
2092 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002093 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2096 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002097 }
2098
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002099 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002100
Przemek Stekielea4000f2022-03-16 09:49:33 +01002101#if defined(MBEDTLS_USE_PSA_CRYPTO)
2102 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002103 size_t olen = 0;
2104
2105 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2106 {
2107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2108 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2109 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002110
2111 ssl->handshake->ecdh_psa_type =
2112 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2113 &ecdh_bits ) );
2114
2115 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2116 {
2117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2118 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2119 }
2120
2121 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2122
Przemek Stekielea4000f2022-03-16 09:49:33 +01002123 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002124 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2125 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2126 ssl->handshake->ecdh_psa_peerkey,
2127 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002128
Przemek Stekiel561a4232022-03-16 13:16:24 +01002129 if ( ret != 0 )
2130 {
2131 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2132 return( ret );
2133 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002134
Przemek Stekiel561a4232022-03-16 13:16:24 +01002135 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002136#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2138 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002141 return( ret );
2142 }
2143
2144 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002147 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002148 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002149#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002150#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2151 /* We don't need the peer's public key anymore. Free it,
2152 * so that more RAM is available for upcoming expensive
2153 * operations like ECDHE. */
2154 mbedtls_pk_free( peer_pk );
2155#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2156
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002157 return( ret );
2158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2160 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002161
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002162MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002164{
Janos Follath865b3eb2019-12-16 11:46:15 +00002165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002166 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002167 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002168 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2173 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 ssl->state++;
2177 return( 0 );
2178 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002179 ((void) p);
2180 ((void) end);
2181#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2184 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2185 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2186 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002187 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002188 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002191 mbedtls_ssl_send_alert_message(
2192 ssl,
2193 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2194 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002195 return( ret );
2196 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002199 ssl->state++;
2200 return( 0 );
2201 }
2202 ((void) p);
2203 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2205 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002206
Gilles Peskineeccd8882020-03-10 12:19:08 +01002207#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002208 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002209 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002210 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002211 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002212 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002213#endif
2214
Hanno Becker327c93b2018-08-15 13:56:18 +01002215 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 return( ret );
2219 }
2220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002224 mbedtls_ssl_send_alert_message(
2225 ssl,
2226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2227 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002231 /*
2232 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2233 * doesn't use a psk_identity_hint
2234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2238 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002239 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002240 /* Current message is probably either
2241 * CertificateRequest or ServerHelloDone */
2242 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002243 goto exit;
2244 }
2245
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002246 MBEDTLS_SSL_DEBUG_MSG( 1,
2247 ( "server key exchange message must not be skipped" ) );
2248 mbedtls_ssl_send_alert_message(
2249 ssl,
2250 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2251 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 }
2255
Gilles Peskineeccd8882020-03-10 12:19:08 +01002256#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002257 if( ssl->handshake->ecrs_enabled )
2258 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2259
2260start_processing:
2261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002263 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002265
Gilles Peskineeccd8882020-03-10 12:19:08 +01002266#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2268 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2269 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2270 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002271 {
2272 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002275 mbedtls_ssl_send_alert_message(
2276 ssl,
2277 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002278 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002279 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002280 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002281 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002282#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2285 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2286 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002288 ; /* nothing more to do */
2289 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2291 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2292#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2293 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2294 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2295 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002297 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002300 mbedtls_ssl_send_alert_message(
2301 ssl,
2302 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2303 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002304 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002305 }
2306 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2309 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002310#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2311 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2313 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2314 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2315 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002316 {
2317 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002320 mbedtls_ssl_send_alert_message(
2321 ssl,
2322 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2323 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002324 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002325 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002326 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2329 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2330 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002331#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2332 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2333 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02002334#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002335 /*
2336 * The first 3 bytes are:
2337 * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2338 * [1, 2] elliptic curve's TLS ID
2339 *
2340 * However since we only support secp256r1 for now, we check only
2341 * that TLS ID here
2342 */
2343 uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE( p, 1 );
2344 const mbedtls_ecp_curve_info *curve_info;
2345
2346 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id(
2347 MBEDTLS_ECP_DP_SECP256R1 ) ) == NULL )
2348 {
2349 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2350 }
2351
2352 if( ( *p != MBEDTLS_ECP_TLS_NAMED_CURVE ) ||
2353 ( read_tls_id != curve_info->tls_id ) )
Valerio Setti5151bdf2022-11-21 14:30:02 +01002354 {
Valerio Setti61ea17d2022-11-18 12:11:00 +01002355 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Valerio Setti5151bdf2022-11-21 14:30:02 +01002356 }
Valerio Setti9bed8ec2022-11-17 16:36:19 +01002357
2358 p += 3;
2359
Valerio Setti6b3dab02022-11-17 17:14:54 +01002360 if( ( ret = mbedtls_psa_ecjpake_read_round(
2361 &ssl->handshake->psa_pake_ctx, p, end - p,
2362 MBEDTLS_ECJPAKE_ROUND_TWO ) ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02002363 {
2364 psa_destroy_key( ssl->handshake->psa_pake_password );
2365 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
2366
2367 MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round two", ret );
2368 mbedtls_ssl_send_alert_message(
2369 ssl,
2370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2371 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2372 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
2373 }
2374#else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002375 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2376 p, end - p );
2377 if( ret != 0 )
2378 {
2379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002380 mbedtls_ssl_send_alert_message(
2381 ssl,
2382 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002383 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2384 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002385 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002386#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002387 }
2388 else
2389#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002394
Gilles Peskineeccd8882020-03-10 12:19:08 +01002395#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002396 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002397 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002398 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002399 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2402 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2403 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002404 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002405 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002406 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002407
Hanno Beckera6899bb2019-02-06 18:26:03 +00002408 mbedtls_pk_context * peer_pk;
2409
Jerry Yu693a47a2022-06-23 14:02:28 +08002410#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2411 peer_pk = &ssl->handshake->peer_pubkey;
2412#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2413 if( ssl->session_negotiate->peer_cert == NULL )
2414 {
2415 /* Should never happen */
2416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2418 }
2419 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2420#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2421
Paul Bakker29e1f122013-04-16 13:07:56 +02002422 /*
2423 * Handle the digitally-signed structure
2424 */
Jerry Yu693a47a2022-06-23 14:02:28 +08002425 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2426 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yu95b743c2022-07-23 11:37:50 +08002427 if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuc3bf7482022-07-29 10:27:17 +08002428 sig_alg, &pk_alg, &md_alg ) != 0 &&
Jerry Yu693a47a2022-06-23 14:02:28 +08002429 ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) &&
2430 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002431 {
Ronald Cron90915f22022-03-07 11:11:36 +01002432 MBEDTLS_SSL_DEBUG_MSG( 1,
2433 ( "bad server key exchange message" ) );
2434 mbedtls_ssl_send_alert_message(
2435 ssl,
2436 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2437 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2438 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002439 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002440 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002441
Jerry Yu693a47a2022-06-23 14:02:28 +08002442 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002443 {
Ronald Cron90915f22022-03-07 11:11:36 +01002444 MBEDTLS_SSL_DEBUG_MSG( 1,
2445 ( "bad server key exchange message" ) );
2446 mbedtls_ssl_send_alert_message(
2447 ssl,
2448 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2449 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2450 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002451 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002452
2453 /*
2454 * Read signature
2455 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002456
2457 if( p > end - 2 )
2458 {
2459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002460 mbedtls_ssl_send_alert_message(
2461 ssl,
2462 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2463 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002464 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002465 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002466 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002467 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002468
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002469 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002472 mbedtls_ssl_send_alert_message(
2473 ssl,
2474 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2475 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002476 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002480
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002481 /*
2482 * Compute the hash that has been signed
2483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002485 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002486 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2487 params, params_len,
2488 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002489 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002490 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002491 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002492 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002496 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002497
Gilles Peskineca1d7422018-04-24 11:53:22 +02002498 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002499
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002500 /*
2501 * Verify signature
2502 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002503 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002506 mbedtls_ssl_send_alert_message(
2507 ssl,
2508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2509 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002511 }
2512
Gilles Peskineeccd8882020-03-10 12:19:08 +01002513#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002514 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002515 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002516#endif
2517
Jerry Yu693a47a2022-06-23 14:02:28 +08002518#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2519 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
2520 {
Jerry Yu693a47a2022-06-23 14:02:28 +08002521 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2522 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002523 rsassa_pss_options.expected_salt_len =
2524 mbedtls_hash_info_get_size( md_alg );
2525 if( rsassa_pss_options.expected_salt_len == 0 )
Jerry Yu693a47a2022-06-23 14:02:28 +08002526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002527
Jerry Yu693a47a2022-06-23 14:02:28 +08002528 ret = mbedtls_pk_verify_ext( pk_alg, &rsassa_pss_options,
2529 peer_pk,
2530 md_alg, hash, hashlen,
2531 p, sig_len );
2532 }
2533 else
2534#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2535 ret = mbedtls_pk_verify_restartable( peer_pk,
2536 md_alg, hash, hashlen, p, sig_len, rs_ctx );
2537
2538 if( ret != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002539 {
David Horstmannb21bbef2022-10-06 17:49:31 +01002540 int send_alert_msg = 1;
Gilles Peskineeccd8882020-03-10 12:19:08 +01002541#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
David Horstmannb21bbef2022-10-06 17:49:31 +01002542 send_alert_msg = ( ret != MBEDTLS_ERR_ECP_IN_PROGRESS );
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002543#endif
David Horstmannb21bbef2022-10-06 17:49:31 +01002544 if( send_alert_msg )
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002545 mbedtls_ssl_send_alert_message(
2546 ssl,
2547 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2548 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002550#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002551 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2552 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2553#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002554 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002555 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002556
2557#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2558 /* We don't need the peer's public key anymore. Free it,
2559 * so that more RAM is available for upcoming expensive
2560 * operations like ECDHE. */
2561 mbedtls_pk_free( peer_pk );
2562#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002564#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002566exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 ssl->state++;
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
2571 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572}
2573
Gilles Peskineeccd8882020-03-10 12:19:08 +01002574#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002575MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002577{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002578 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002579 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002582
Hanno Becker1aa267c2017-04-28 17:08:27 +01002583 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002586 ssl->state++;
2587 return( 0 );
2588 }
2589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002592}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002593#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002594MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596{
Janos Follath865b3eb2019-12-16 11:46:15 +00002597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002598 unsigned char *buf;
2599 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002600 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002601 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002602 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002603 size_t sig_alg_len;
2604#if defined(MBEDTLS_DEBUG_C)
2605 unsigned char *sig_alg;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002606 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002607#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Hanno Becker1aa267c2017-04-28 17:08:27 +01002611 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002614 ssl->state++;
2615 return( 0 );
2616 }
2617
Hanno Becker327c93b2018-08-15 13:56:18 +01002618 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2621 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 }
2623
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002624 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2625 {
2626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002627 mbedtls_ssl_send_alert_message(
2628 ssl,
2629 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2630 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002631 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002634 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002635 ssl->handshake->client_auth =
2636 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002639 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Jerry Yufb28b882022-01-28 11:05:58 +08002641 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002642 {
Johan Pascala89ca862020-08-25 10:03:19 +02002643 /* Current message is probably the ServerHelloDone */
2644 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002645 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002646 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002647
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002648 /*
2649 * struct {
2650 * ClientCertificateType certificate_types<1..2^8-1>;
2651 * SignatureAndHashAlgorithm
2652 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2653 * DistinguishedName certificate_authorities<0..2^16-1>;
2654 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002655 *
2656 * Since we only support a single certificate on clients, let's just
2657 * ignore all the information that's supposed to help us pick a
2658 * certificate.
2659 *
2660 * We could check that our certificate matches the request, and bail out
2661 * if it doesn't, but it's simpler to just send the certificate anyway,
2662 * and give the server the opportunity to decide if it should terminate
2663 * the connection when it doesn't like our certificate.
2664 *
2665 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2666 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002667 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002668 *
2669 * However, we still minimally parse the message to check it is at least
2670 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002671 */
Paul Bakker926af752012-11-23 13:38:07 +01002672 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002673
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002674 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002675 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2676 {
2677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2678 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2679 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002680 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002683 n = cert_type_len;
2684
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002685 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002686 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002687 * * the length of the signature algorithms field (if minor version of
2688 * SSL is 3),
2689 * * distinguished name length otherwise.
2690 * Both reach at most the index:
2691 * ...hdr_len + 2 + n,
2692 * therefore the buffer length at this point must be greater than that
2693 * regardless of the actual code path.
2694 */
2695 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002698 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2699 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002700 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002701 }
2702
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002703 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002704 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2705 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2706
2707 /*
2708 * The furthest access in buf is in the loop few lines below:
2709 * sig_alg[i + 1],
2710 * where:
2711 * sig_alg = buf + ...hdr_len + 3 + n,
2712 * max(i) = sig_alg_len - 1.
2713 * Therefore the furthest access is:
2714 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2715 * which reduces to:
2716 * buf[...hdr_len + 3 + n + sig_alg_len],
2717 * which is one less than we need the buf to be.
2718 */
2719 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002720 {
Ronald Cron90915f22022-03-07 11:11:36 +01002721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2722 mbedtls_ssl_send_alert_message(
2723 ssl,
2724 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2725 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2726 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002727 }
Paul Bakker926af752012-11-23 13:38:07 +01002728
Ronald Cron90915f22022-03-07 11:11:36 +01002729#if defined(MBEDTLS_DEBUG_C)
2730 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2731 for( size_t i = 0; i < sig_alg_len; i += 2 )
2732 {
2733 MBEDTLS_SSL_DEBUG_MSG( 3,
Andrzej Kurekec71b092022-11-15 10:21:50 -05002734 ( "Supported Signature Algorithm found: %02x %02x",
Ronald Cron90915f22022-03-07 11:11:36 +01002735 sig_alg[i], sig_alg[i + 1] ) );
2736 }
2737#endif
2738
2739 n += 2 + sig_alg_len;
2740
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002741 /* certificate_authorities */
2742 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2743 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002744
2745 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002746 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002749 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2750 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002751 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002752 }
2753
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002754#if defined(MBEDTLS_DEBUG_C)
2755 dn = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n - dn_len;
2756 for( size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len )
2757 {
2758 unsigned char *p = dn + i + 2;
2759 mbedtls_x509_name name;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002760 size_t asn1_len;
2761 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
2762 memset( &name, 0, sizeof( name ) );
2763 dni_len = MBEDTLS_GET_UINT16_BE( dn + i, 0 );
2764 if( dni_len > dn_len - i - 2 ||
2765 mbedtls_asn1_get_tag( &p, p + dni_len, &asn1_len,
2766 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) != 0 ||
2767 mbedtls_x509_get_name( &p, p + asn1_len, &name ) != 0 )
2768 {
2769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2770 mbedtls_ssl_send_alert_message(
2771 ssl,
2772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2773 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2774 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
2775 }
2776 MBEDTLS_SSL_DEBUG_MSG( 3,
2777 ( "DN hint: %.*s",
2778 mbedtls_x509_dn_gets( s, sizeof(s), &name ), s ) );
Glenn Straussa4b40412022-06-26 19:32:09 -04002779 mbedtls_asn1_free_named_data_list_shallow( name.next );
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002780 }
2781#endif
2782
Paul Bakker926af752012-11-23 13:38:07 +01002783exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
2786 return( 0 );
2787}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002788#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002790MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002792{
Janos Follath865b3eb2019-12-16 11:46:15 +00002793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
Hanno Becker327c93b2018-08-15 13:56:18 +01002797 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2800 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002802
2803 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2804 {
2805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2806 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2810 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2814 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002815 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 }
2817
2818 ssl->state++;
2819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002821 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002823#endif
2824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
2827 return( 0 );
2828}
2829
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002830MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002832{
Janos Follath865b3eb2019-12-16 11:46:15 +00002833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002834
2835 size_t header_len;
2836 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002837 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002838 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2843 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 /*
2846 * DHM key exchange -- send G^X mod P
2847 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002848 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Joe Subbiani6dd73642021-07-19 11:56:54 +01002850 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002851 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002854 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002855 &ssl->out_msg[header_len], content_len,
2856 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 if( ret != 0 )
2858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 return( ret );
2861 }
2862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2864 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002867 ssl->handshake->premaster,
2868 MBEDTLS_PREMASTER_SIZE,
2869 &ssl->handshake->pmslen,
2870 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 return( ret );
2874 }
2875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 }
2878 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002880#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2881 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2882 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2883 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002884 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002885 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2886 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2887 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002888 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002889#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002890 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2891 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002892 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002893
2894 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2895
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002896 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002897
Hanno Becker0a94a642019-01-11 14:35:30 +00002898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2899
Hanno Becker4a63ed42019-01-08 11:39:35 +00002900 /*
2901 * Generate EC private key for ECDHE exchange.
2902 */
2903
Hanno Becker4a63ed42019-01-08 11:39:35 +00002904 /* The master secret is obtained from the shared ECDH secret by
2905 * applying the TLS 1.2 PRF with a specific salt and label. While
2906 * the PSA Crypto API encourages combining key agreement schemes
2907 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2908 * yet support the provisioning of salt + label to the KDF.
2909 * For the time being, we therefore need to split the computation
2910 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002911 key_attributes = psa_key_attributes_init();
2912 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002913 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002914 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2915 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002916
2917 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002918 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002919 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002920 if( status != PSA_SUCCESS )
2921 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2922
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002923 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002924 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002925 * but we just need to add a length byte before that. */
2926 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2927 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2928 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2929 size_t own_pubkey_len;
2930
Hanno Becker4a63ed42019-01-08 11:39:35 +00002931 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002932 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002933 &own_pubkey_len );
2934 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002935 {
2936 psa_destroy_key( handshake->ecdh_psa_privkey );
2937 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002938 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002939 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002940
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002941 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2942 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002943
Hanno Becker4a63ed42019-01-08 11:39:35 +00002944 /* The ECDH secret is the premaster secret used for key derivation. */
2945
Janos Follathdf3b0892019-08-08 11:12:24 +01002946 /* Compute ECDH shared secret. */
2947 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2948 handshake->ecdh_psa_privkey,
2949 handshake->ecdh_psa_peerkey,
2950 handshake->ecdh_psa_peerkey_len,
2951 ssl->handshake->premaster,
2952 sizeof( ssl->handshake->premaster ),
2953 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002954
Andrzej Kureka0237f82022-02-24 13:24:52 -05002955 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002956 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002957
2958 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2959 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002960#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002961 /*
2962 * ECDH key exchange -- send client public value
2963 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002964 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002965
Gilles Peskineeccd8882020-03-10 12:19:08 +01002966#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002967 if( ssl->handshake->ecrs_enabled )
2968 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002969 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002970 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002971
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002972 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2973 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002974#endif
2975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002977 &content_len,
2978 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002979 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002980 if( ret != 0 )
2981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002983#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002984 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2985 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2986#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002987 return( ret );
2988 }
2989
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002990 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2991 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002992
Gilles Peskineeccd8882020-03-10 12:19:08 +01002993#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002994 if( ssl->handshake->ecrs_enabled )
2995 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002996 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002997 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002998 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002999
3000ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003001 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003002 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003005 &ssl->handshake->pmslen,
3006 ssl->handshake->premaster,
3007 MBEDTLS_MPI_MAX_SIZE,
3008 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003011#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003012 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3013 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3014#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003015 return( ret );
3016 }
3017
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003018 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3019 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02003020#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01003021 }
3022 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3024 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3025 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3026 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01003027#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3028 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3029 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3030 {
3031 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3032 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
3033 psa_key_attributes_t key_attributes;
3034
3035 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3036
3037 /*
3038 * opaque psk_identity<0..2^16-1>;
3039 */
3040 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01003041 /* We don't offer PSK suites if we don't have a PSK,
3042 * and we check that the server's choice is among the
3043 * ciphersuites we offered, so this should never happen. */
3044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003045
Neil Armstrongfc834f22022-03-23 17:54:38 +01003046 /* uint16 to store content length */
3047 const size_t content_len_size = 2;
3048
Neil Armstrong868af822022-03-09 10:26:25 +01003049 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01003050
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003051 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01003052 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01003053 {
3054 MBEDTLS_SSL_DEBUG_MSG( 1,
3055 ( "psk identity too long or SSL buffer too short" ) );
3056 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3057 }
3058
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003059 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003060
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003061 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
3062 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
3063 header_len += content_len_size;
3064
3065 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003066 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003067 p += ssl->conf->psk_identity_len;
3068
Neil Armstrong868af822022-03-09 10:26:25 +01003069 header_len += ssl->conf->psk_identity_len;
3070
3071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3072
3073 /*
3074 * Generate EC private key for ECDHE exchange.
3075 */
3076
3077 /* The master secret is obtained from the shared ECDH secret by
3078 * applying the TLS 1.2 PRF with a specific salt and label. While
3079 * the PSA Crypto API encourages combining key agreement schemes
3080 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3081 * yet support the provisioning of salt + label to the KDF.
3082 * For the time being, we therefore need to split the computation
3083 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3084 key_attributes = psa_key_attributes_init();
3085 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3086 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3087 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3088 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3089
3090 /* Generate ECDH private key. */
3091 status = psa_generate_key( &key_attributes,
3092 &handshake->ecdh_psa_privkey );
3093 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003094 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003095
3096 /* Export the public part of the ECDH private key from PSA.
3097 * The export format is an ECPoint structure as expected by TLS,
3098 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003099 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003100 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3101 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003102 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003103
3104 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3105 own_pubkey, own_pubkey_max_len,
3106 &own_pubkey_len );
3107 if( status != PSA_SUCCESS )
3108 {
3109 psa_destroy_key( handshake->ecdh_psa_privkey );
3110 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003111 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003112 }
3113
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003114 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003115 content_len = own_pubkey_len + 1;
3116
Neil Armstrong25400452022-03-23 17:44:07 +01003117 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3118 * - a uint16 containing the length (in octets) of the ECDH computation
3119 * - the octet string produced by the ECDH computation
3120 * - a uint16 containing the length (in octets) of the PSK
3121 * - the PSK itself
3122 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003123 unsigned char *pms = ssl->handshake->premaster;
3124 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003125 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003126 /* uint16 to store length (in octets) of the ECDH computation */
3127 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003128 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003129
Neil Armstrong25400452022-03-23 17:44:07 +01003130 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003131 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3132 handshake->ecdh_psa_privkey,
3133 handshake->ecdh_psa_peerkey,
3134 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003135 pms + zlen_size,
3136 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003137 &zlen );
3138
3139 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3140 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3141
Neil Armstrongc530aa62022-03-23 17:45:01 +01003142 if( status != PSA_SUCCESS )
3143 return( psa_ssl_status_to_mbedtls( status ) );
3144 else if( destruction_status != PSA_SUCCESS )
3145 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003146
Neil Armstrong25400452022-03-23 17:44:07 +01003147 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003148 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3149 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003150 }
3151 else
3152#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3153 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003154#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003155 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003156 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003157 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003158 * opaque psk_identity<0..2^16-1>;
3159 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003160 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003161 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003162 /* We don't offer PSK suites if we don't have a PSK,
3163 * and we check that the server's choice is among the
3164 * ciphersuites we offered, so this should never happen. */
3165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003166 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003167
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003168 header_len = 4;
3169 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003170
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003171 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003172 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003173 MBEDTLS_SSL_DEBUG_MSG( 1,
3174 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003175 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3176 }
3177
Joe Subbianifbeb6922021-07-16 14:27:50 +01003178 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3179 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003180
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003181 memcpy( ssl->out_msg + header_len,
3182 ssl->conf->psk_identity,
3183 ssl->conf->psk_identity_len );
3184 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3187 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003188 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003189 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003190 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003191 else
3192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3194 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003195 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003196 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3197 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003198 return( ret );
3199 }
3200 else
3201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3203 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003204 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003205 /*
3206 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3207 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003208 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003209
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003210 if( header_len + 2 + content_len >
3211 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003212 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003213 MBEDTLS_SSL_DEBUG_MSG( 1,
3214 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003215 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3216 }
3217
Joe Subbianifbeb6922021-07-16 14:27:50 +01003218 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3219 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003222 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003223 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003224 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003225 if( ret != 0 )
3226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003228 return( ret );
3229 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003230
3231#if defined(MBEDTLS_USE_PSA_CRYPTO)
3232 unsigned char *pms = ssl->handshake->premaster;
3233 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3234 size_t pms_len;
3235
3236 /* Write length only when we know the actual value */
3237 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3238 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3239 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3240 {
3241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3242 return( ret );
3243 }
3244 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3245 pms += 2 + pms_len;
3246
3247 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3248#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003249 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003252#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3253 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003255 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003256 /*
3257 * ClientECDiffieHellmanPublic public;
3258 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003259 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3260 &content_len,
3261 &ssl->out_msg[header_len],
3262 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003263 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003264 if( ret != 0 )
3265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003267 return( ret );
3268 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003269
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003270 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3271 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003272 }
3273 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003274#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003278 }
3279
Neil Armstrong80f6f322022-05-03 17:56:38 +02003280#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3281 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3282 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003283 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003284 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003285 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003286 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003287 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003288#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003289 }
3290 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003291#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3293 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003295 header_len = 4;
3296 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3297 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003298 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 }
Paul Bakkered27a042013-04-18 22:46:23 +02003300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003302#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3303 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3304 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003305 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003306
Neil Armstrongca7d5062022-05-31 14:43:23 +02003307#if defined(MBEDTLS_USE_PSA_CRYPTO)
3308 unsigned char *out_p = ssl->out_msg + header_len;
3309 unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
3310 header_len;
Valerio Setti6b3dab02022-11-17 17:14:54 +01003311 ret = mbedtls_psa_ecjpake_write_round( &ssl->handshake->psa_pake_ctx,
3312 out_p, end_p - out_p, &content_len,
3313 MBEDTLS_ECJPAKE_ROUND_TWO );
Valerio Setti02c25b52022-11-15 14:08:42 +01003314 if ( ret != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02003315 {
Valerio Setti02c25b52022-11-15 14:08:42 +01003316 psa_destroy_key( ssl->handshake->psa_pake_password );
3317 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
3318 MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret );
3319 return( ret );
Neil Armstrongca7d5062022-05-31 14:43:23 +02003320 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003321#else
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003322 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003323 ssl->out_msg + header_len,
3324 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3325 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003326 ssl->conf->f_rng, ssl->conf->p_rng );
3327 if( ret != 0 )
3328 {
3329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3330 return( ret );
3331 }
3332
3333 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3334 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3335 ssl->conf->f_rng, ssl->conf->p_rng );
3336 if( ret != 0 )
3337 {
3338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3339 return( ret );
3340 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02003341#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003342 }
3343 else
3344#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003345 {
3346 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003350
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003351 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3353 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
3355 ssl->state++;
3356
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003357 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 return( ret );
3361 }
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
3365 return( 0 );
3366}
3367
Gilles Peskineeccd8882020-03-10 12:19:08 +01003368#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003369MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003371{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003372 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003373 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003381 return( ret );
3382 }
3383
Hanno Becker77adddc2019-02-07 12:32:43 +00003384 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003387 ssl->state++;
3388 return( 0 );
3389 }
3390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003393}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003394#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003395MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003397{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003399 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003400 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003401 size_t n = 0, offset = 0;
3402 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003403 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003405 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003406 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003407#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3408 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3409#else
3410 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3411#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003414
Gilles Peskineeccd8882020-03-10 12:19:08 +01003415#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003416 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003417 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003418 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003419 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003420 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003421#endif
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003426 return( ret );
3427 }
3428
Hanno Becker77adddc2019-02-07 12:32:43 +00003429 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003432 ssl->state++;
3433 return( 0 );
3434 }
3435
Jerry Yufb28b882022-01-28 11:05:58 +08003436 if( ssl->handshake->client_auth == 0 ||
3437 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 {
Johan Pascala89ca862020-08-25 10:03:19 +02003439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3440 ssl->state++;
3441 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 }
3443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 }
3449
3450 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003451 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003453#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003454 if( ssl->handshake->ecrs_enabled )
3455 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3456
3457sign:
3458#endif
3459
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003460 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003461
Ronald Cron90915f22022-03-07 11:11:36 +01003462 /*
3463 * digitally-signed struct {
3464 * opaque handshake_messages[handshake_messages_length];
3465 * };
3466 *
3467 * Taking shortcut here. We assume that the server always allows the
3468 * PRF Hash function and has sent it in the allowed signature
3469 * algorithms list received in the Certificate Request message.
3470 *
3471 * Until we encounter a server that does not, we will take this
3472 * shortcut.
3473 *
3474 * Reason: Otherwise we should have running hashes for SHA512 and
3475 * SHA224 in order to satisfy 'weird' needs from the server
3476 * side.
3477 */
3478 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003479 {
Ronald Cron90915f22022-03-07 11:11:36 +01003480 md_alg = MBEDTLS_MD_SHA384;
3481 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003482 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003483 else
Paul Bakker577e0062013-08-28 11:57:20 +02003484 {
Ronald Cron90915f22022-03-07 11:11:36 +01003485 md_alg = MBEDTLS_MD_SHA256;
3486 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003487 }
Ronald Cron90915f22022-03-07 11:11:36 +01003488 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3489
3490 /* Info from md_alg will be used instead */
3491 hashlen = 0;
3492 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003493
Gilles Peskineeccd8882020-03-10 12:19:08 +01003494#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003495 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003496 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003497#endif
3498
3499 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3500 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003501 ssl->out_msg + 6 + offset,
3502 out_buf_len - 6 - offset,
3503 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003504 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003507#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003508 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3509 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3510#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003511 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003512 }
Paul Bakker926af752012-11-23 13:38:07 +01003513
Joe Subbiani6dd73642021-07-19 11:56:54 +01003514 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515
Paul Bakker1ef83d62012-04-11 12:09:53 +00003516 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3518 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003519
3520 ssl->state++;
3521
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003522 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 return( ret );
3526 }
3527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Paul Bakkered27a042013-04-18 22:46:23 +02003530 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003531}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003532#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003535MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003537{
Janos Follath865b3eb2019-12-16 11:46:15 +00003538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003539 uint32_t lifetime;
3540 size_t ticket_len;
3541 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003542 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003545
Hanno Becker327c93b2018-08-15 13:56:18 +01003546 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003549 return( ret );
3550 }
3551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003555 mbedtls_ssl_send_alert_message(
3556 ssl,
3557 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3558 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003560 }
3561
3562 /*
3563 * struct {
3564 * uint32 ticket_lifetime_hint;
3565 * opaque ticket<0..2^16-1>;
3566 * } NewSessionTicket;
3567 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003568 * 0 . 3 ticket_lifetime_hint
3569 * 4 . 5 ticket_len (n)
3570 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3573 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3577 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003578 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003579 }
3580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003582
Philippe Antoineb5b25432018-05-11 11:06:29 +02003583 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3584 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003585
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003586 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3592 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003593 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003594 }
3595
Paul Elliottd48d5c62021-01-07 14:47:05 +00003596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003597
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003598 /* We're not waiting for a NewSessionTicket message any more */
3599 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003601
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003602 /*
3603 * Zero-length ticket means the server changed his mind and doesn't want
3604 * to send a ticket after all, so just forget it
3605 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003606 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003607 return( 0 );
3608
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003609 if( ssl->session != NULL && ssl->session->ticket != NULL )
3610 {
3611 mbedtls_platform_zeroize( ssl->session->ticket,
3612 ssl->session->ticket_len );
3613 mbedtls_free( ssl->session->ticket );
3614 ssl->session->ticket = NULL;
3615 ssl->session->ticket_len = 0;
3616 }
3617
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003618 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3619 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003621 ssl->session_negotiate->ticket = NULL;
3622 ssl->session_negotiate->ticket_len = 0;
3623
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003624 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003625 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003627 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3628 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003629 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003630 }
3631
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003632 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003633
3634 ssl->session_negotiate->ticket = ticket;
3635 ssl->session_negotiate->ticket_len = ticket_len;
3636 ssl->session_negotiate->ticket_lifetime = lifetime;
3637
3638 /*
3639 * RFC 5077 section 3.4:
3640 * "If the client receives a session ticket from the server, then it
3641 * discards any Session ID that was sent in the ServerHello."
3642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003644 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003647
3648 return( 0 );
3649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003651
Paul Bakker5121ce52009-01-03 21:22:43 +00003652/*
Paul Bakker1961b702013-01-25 14:49:24 +01003653 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003656{
3657 int ret = 0;
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003660 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3662 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003663 ssl->handshake->new_session_ticket != 0 )
3664 {
Jerry Yua357cf42022-07-12 05:36:45 +00003665 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003666 }
3667#endif
3668
Paul Bakker1961b702013-01-25 14:49:24 +01003669 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 case MBEDTLS_SSL_HELLO_REQUEST:
3672 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 break;
3674
Paul Bakker1961b702013-01-25 14:49:24 +01003675 /*
3676 * ==> ClientHello
3677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003679 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003680 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
Paul Bakker1961b702013-01-25 14:49:24 +01003682 /*
3683 * <== ServerHello
3684 * Certificate
3685 * ( ServerKeyExchange )
3686 * ( CertificateRequest )
3687 * ServerHelloDone
3688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003690 ret = ssl_parse_server_hello( ssl );
3691 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3694 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003695 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003698 ret = ssl_parse_server_key_exchange( ssl );
3699 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003702 ret = ssl_parse_certificate_request( ssl );
3703 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003706 ret = ssl_parse_server_hello_done( ssl );
3707 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003708
Paul Bakker1961b702013-01-25 14:49:24 +01003709 /*
3710 * ==> ( Certificate/Alert )
3711 * ClientKeyExchange
3712 * ( CertificateVerify )
3713 * ChangeCipherSpec
3714 * Finished
3715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3717 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003718 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003721 ret = ssl_write_client_key_exchange( ssl );
3722 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003725 ret = ssl_write_certificate_verify( ssl );
3726 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3729 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003730 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 case MBEDTLS_SSL_CLIENT_FINISHED:
3733 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003734 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003735
Paul Bakker1961b702013-01-25 14:49:24 +01003736 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003737 * <== ( NewSessionTicket )
3738 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003739 * Finished
3740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00003742 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003743 ret = ssl_parse_new_session_ticket( ssl );
3744 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003745#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3748 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003749 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 case MBEDTLS_SSL_SERVER_FINISHED:
3752 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003753 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 case MBEDTLS_SSL_FLUSH_BUFFERS:
3756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3757 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003758 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3761 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003762 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003763
Paul Bakker1961b702013-01-25 14:49:24 +01003764 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003768
3769 return( ret );
3770}
Jerry Yuc5aef882021-12-23 20:15:02 +08003771
Jerry Yufb4b6472022-01-27 15:03:26 +08003772#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */