blob: 7b62e71a55acd64b017be6561744ca275ff23f77 [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
Gilles Peskineeccd8882020-03-10 12:19:08 +010052#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Ronald Crond491c2d2022-02-19 18:30:46 +010053int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010054{
55 if( conf->psk_identity == NULL ||
56 conf->psk_identity_len == 0 )
57 {
58 return( 0 );
59 }
60
Hanno Becker2e4f6162018-10-23 11:54:44 +010061#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020062 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010063 return( 1 );
Neil Armstrong8ecd6682022-05-05 11:40:35 +020064#endif /* MBEDTLS_USE_PSA_CRYPTO */
65
Neil Armstronge952a302022-05-03 10:22:14 +020066 if( conf->psk != NULL && conf->psk_len != 0 )
67 return( 1 );
Hanno Becker2e4f6162018-10-23 11:54:44 +010068
69 return( 0 );
70}
Gilles Peskineeccd8882020-03-10 12:19:08 +010071#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020074MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +010075static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
76 unsigned char *buf,
77 const unsigned char *end,
78 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010079{
80 unsigned char *p = buf;
81
82 *olen = 0;
83
Tom Cosgrovece7f18c2022-07-28 05:50:56 +010084 /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
Hanno Becker40f8b512017-10-12 14:58:55 +010085 * initial ClientHello, in which case also adding the renegotiation
86 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +010088 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +010089
Hanno Beckerb2fff6d2017-05-08 11:06:19 +010090 MBEDTLS_SSL_DEBUG_MSG( 3,
91 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +010092
Hanno Becker261602c2017-04-12 14:54:42 +010093 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +010094
Paul Bakkerd3edc862013-03-20 16:07:17 +010095 /*
96 * Secure renegotiation
97 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +010098 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
99 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100100
101 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100102 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
103 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100104
105 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
106
107 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100108
109 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100112
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200113#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100114 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200116MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100117static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
118 unsigned char *buf,
119 const unsigned char *end,
120 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100121{
122 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100123 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124
125 *olen = 0;
126
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100127 MBEDTLS_SSL_DEBUG_MSG( 3,
128 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100129 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100130
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100131 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
132 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100133
134 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100135 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200136
137 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100139
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200140 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100141
142 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100143}
Darryl Green11999bb2018-03-13 15:22:58 +0000144#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100145 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200147#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200148MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100149static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
150 unsigned char *buf,
151 const unsigned char *end,
152 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200153{
Janos Follath865b3eb2019-12-16 11:46:15 +0000154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200155 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200156 size_t kkpp_len;
157
158 *olen = 0;
159
160 /* Skip costly extension if we can't use EC J-PAKE anyway */
161 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100162 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200163
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100164 MBEDTLS_SSL_DEBUG_MSG( 3,
165 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200166
Hanno Becker261602c2017-04-12 14:54:42 +0100167 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200168
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100169 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
170 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200171
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200172 /*
173 * We may need to send ClientHello multiple times for Hello verification.
174 * We don't want to compute fresh values every time (both for performance
175 * and consistency reasons), so cache the extension content.
176 */
177 if( ssl->handshake->ecjpake_cache == NULL ||
178 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200179 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
181
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200182 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100183 p + 2, end - p - 2, &kkpp_len,
184 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200185 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200186 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100187 MBEDTLS_SSL_DEBUG_RET( 1 ,
188 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100189 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200190 }
191
192 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
193 if( ssl->handshake->ecjpake_cache == NULL )
194 {
195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100196 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200197 }
198
199 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
200 ssl->handshake->ecjpake_cache_len = kkpp_len;
201 }
202 else
203 {
204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
205
206 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100207 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200208
209 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200210 }
211
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100212 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
213 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200214
215 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100216
217 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200218}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200219#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000220
Hanno Beckera0e20d02019-05-15 14:03:01 +0100221#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200222MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100223static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
224 unsigned char *buf,
225 const unsigned char *end,
226 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100227{
228 unsigned char *p = buf;
229 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100230
231 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100232 * Quoting draft-ietf-tls-dtls-connection-id-05
233 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100234 *
235 * struct {
236 * opaque cid<0..2^8-1>;
237 * } ConnectionId;
238 */
239
240 *olen = 0;
241 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
242 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
243 {
Hanno Becker261602c2017-04-12 14:54:42 +0100244 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100245 }
246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
247
248 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
249 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100250 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100251
252 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100253 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
254 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100255 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100256 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
257 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100258
259 *p++ = (uint8_t) ssl->own_cid_len;
260 memcpy( p, ssl->own_cid, ssl->own_cid_len );
261
262 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100263
264 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100265}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100266#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200269MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100270static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
271 unsigned char *buf,
272 const unsigned char *end,
273 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200274{
275 unsigned char *p = buf;
276
Simon Butcher0fc94e92015-09-28 20:52:04 +0100277 *olen = 0;
278
Hanno Becker261602c2017-04-12 14:54:42 +0100279 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
280 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200281
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100282 MBEDTLS_SSL_DEBUG_MSG( 3,
283 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200284
Hanno Becker261602c2017-04-12 14:54:42 +0100285 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100286
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100287 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
288 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200289
290 *p++ = 0x00;
291 *p++ = 1;
292
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200293 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200294
295 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100296
297 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200302MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100303static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
304 unsigned char *buf,
305 const unsigned char *end,
306 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100307{
308 unsigned char *p = buf;
309
Simon Butcher0fc94e92015-09-28 20:52:04 +0100310 *olen = 0;
311
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100312 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100313 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100314
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100315 MBEDTLS_SSL_DEBUG_MSG( 3,
316 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100317
Hanno Becker261602c2017-04-12 14:54:42 +0100318 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100319
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100320 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
321 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322
323 *p++ = 0x00;
324 *p++ = 0x00;
325
326 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100327
328 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200333MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100334static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
335 unsigned char *buf,
336 const unsigned char *end,
337 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200338{
339 unsigned char *p = buf;
340
Simon Butcher0fc94e92015-09-28 20:52:04 +0100341 *olen = 0;
342
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100343 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100344 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200345
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100346 MBEDTLS_SSL_DEBUG_MSG( 3,
347 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200348
Hanno Becker261602c2017-04-12 14:54:42 +0100349 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100350
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100351 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
352 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200353
354 *p++ = 0x00;
355 *p++ = 0x00;
356
357 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100358
359 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200364MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker261602c2017-04-12 14:54:42 +0100365static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
366 unsigned char *buf,
367 const unsigned char *end,
368 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200369{
370 unsigned char *p = buf;
371 size_t tlen = ssl->session_negotiate->ticket_len;
372
Simon Butcher0fc94e92015-09-28 20:52:04 +0100373 *olen = 0;
374
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200375 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100376 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200377
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100378 MBEDTLS_SSL_DEBUG_MSG( 3,
379 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200380
Hanno Becker261602c2017-04-12 14:54:42 +0100381 /* The addition is safe here since the ticket length is 16 bit. */
382 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100383
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100384 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
385 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200386
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100387 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
388 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200389
390 *olen = 4;
391
Simon Butchered997662015-09-28 02:14:30 +0100392 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100393 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200394
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100395 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000396 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200397
398 memcpy( p, ssl->session_negotiate->ticket, tlen );
399
400 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100401
402 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200405
Ron Eldora9788042018-12-05 11:04:31 +0200406#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200407MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascal77696ee2020-09-22 21:49:40 +0200408static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200409 unsigned char *buf,
410 const unsigned char *end,
411 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100412{
413 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200414 size_t protection_profiles_index = 0, ext_len = 0;
415 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100416
417 *olen = 0;
418
Johan Pascalc3ccd982020-10-28 17:18:18 +0100419 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
420 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
421 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100422 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200423 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100424 }
425
Ron Eldora9788042018-12-05 11:04:31 +0200426 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100427 * uint8 SRTPProtectionProfile[2];
428 *
429 * struct {
430 * SRTPProtectionProfiles SRTPProtectionProfiles;
431 * opaque srtp_mki<0..255>;
432 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100433 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100434 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200435 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200436 {
437 mki_len = ssl->dtls_srtp_info.mki_len;
438 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300439 /* Extension length = 2 bytes for profiles length,
440 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
441 * 1 byte for srtp_mki vector length and the mki_len value
442 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200443 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
444
Johan Pascal77696ee2020-09-22 21:49:40 +0200445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
446
447 /* Check there is room in the buffer for the extension + 4 bytes
448 * - the extension tag (2 bytes)
449 * - the extension length (2 bytes)
450 */
451 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
452
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100453 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
454 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200455
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100456 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
457 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100458
Ron Eldor3adb9922017-12-21 10:15:08 +0200459 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200460 /* micro-optimization:
461 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
462 * which is lower than 127, so the upper byte of the length is always 0
463 * For the documentation, the more generic code is left in comments
464 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
465 * >> 8 ) & 0xFF );
466 */
467 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100468 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100469
Ron Eldoref72faf2018-07-12 11:54:20 +0300470 for( protection_profiles_index=0;
471 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
472 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100473 {
Johan Pascal43f94902020-09-22 12:25:52 +0200474 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200475 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200476 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200477 {
478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
479 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100480 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
481 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200482 }
483 else
484 {
485 /*
486 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200487 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200488 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200489 MBEDTLS_SSL_DEBUG_MSG( 3,
490 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200491 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200492 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
493 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200494 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100495 }
496 }
497
Ron Eldor591f1622018-01-22 12:30:04 +0200498 *p++ = mki_len & 0xFF;
499
500 if( mki_len != 0 )
501 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200502 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200503 /*
504 * Increment p to point to the current position.
505 */
506 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300507 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
508 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200509 }
510
Ron Eldoref72faf2018-07-12 11:54:20 +0300511 /*
512 * total extension length: extension type (2 bytes)
513 * + extension length (2 bytes)
514 * + protection profile length (2 bytes)
515 * + 2 * number of protection profiles
516 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200517 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300518 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200519 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200520
521 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100522}
523#endif /* MBEDTLS_SSL_DTLS_SRTP */
524
Ronald Cron4079abc2022-02-20 10:35:26 +0100525int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
526 unsigned char *buf,
527 const unsigned char *end,
528 int uses_ec,
529 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100530{
531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
532 unsigned char *p = buf;
533 size_t ext_len = 0;
534
535 (void) ssl;
536 (void) end;
537 (void) uses_ec;
538 (void) ret;
539 (void) ext_len;
540
541 *out_len = 0;
542
543 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
544 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
545#if defined(MBEDTLS_SSL_RENEGOTIATION)
546 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
547 {
548 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
549 return( ret );
550 }
551 p += ext_len;
552#endif
553
554#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
555 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
556 if( uses_ec )
557 {
558 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
559 &ext_len ) ) != 0 )
560 {
561 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
562 return( ret );
563 }
564 p += ext_len;
565 }
566#endif
567
568#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
569 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
570 {
571 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
572 return( ret );
573 }
574 p += ext_len;
575#endif
576
577#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
578 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
579 {
580 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
581 return( ret );
582 }
583 p += ext_len;
584#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
585
586#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
587 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
588 &ext_len ) ) != 0 )
589 {
590 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
591 return( ret );
592 }
593 p += ext_len;
594#endif
595
596#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
597 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
598 {
599 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
600 return( ret );
601 }
602 p += ext_len;
603#endif
604
605#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
606 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
607 {
608 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
609 return( ret );
610 }
611 p += ext_len;
612#endif
613
614#if defined(MBEDTLS_SSL_DTLS_SRTP)
615 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
616 {
617 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
618 return( ret );
619 }
620 p += ext_len;
621#endif
622
623#if defined(MBEDTLS_SSL_SESSION_TICKETS)
624 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
625 {
626 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
627 return( ret );
628 }
629 p += ext_len;
630#endif
631
632 *out_len = p - buf;
633
634 return( 0 );
635}
636
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200637MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200639 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000640 size_t len )
641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#if defined(MBEDTLS_SSL_RENEGOTIATION)
643 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000644 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100645 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000646 if( len != 1 + ssl->verify_data_len * 2 ||
647 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200648 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100649 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200650 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100651 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100654 mbedtls_ssl_send_alert_message(
655 ssl,
656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
657 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100658 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000659 }
660 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100661 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100663 {
664 if( len != 1 || buf[0] != 0x00 )
665 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200666 MBEDTLS_SSL_DEBUG_MSG( 1,
667 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100668 mbedtls_ssl_send_alert_message(
669 ssl,
670 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
671 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100672 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100673 }
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100676 }
Paul Bakker48916f92012-09-16 19:57:18 +0000677
678 return( 0 );
679}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200682MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200684 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200685 size_t len )
686{
687 /*
688 * server should use the extension only if we did,
689 * and if so the server's value should match ours (and len is always 1)
690 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200691 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200692 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200693 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200694 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100695 MBEDTLS_SSL_DEBUG_MSG( 1,
696 ( "non-matching max fragment length extension" ) );
697 mbedtls_ssl_send_alert_message(
698 ssl,
699 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100700 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
701 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200702 }
703
704 return( 0 );
705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000707
Hanno Beckera0e20d02019-05-15 14:03:01 +0100708#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200709MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera8373a12019-04-26 15:37:26 +0100710static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
711 const unsigned char *buf,
712 size_t len )
713{
714 size_t peer_cid_len;
715
716 if( /* CID extension only makes sense in DTLS */
717 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
718 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100719 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100720 {
Hanno Becker22626482019-05-03 12:46:59 +0100721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100723 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100724 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100725 }
726
727 if( len == 0 )
728 {
729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
730 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100731 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
732 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100733 }
734
735 peer_cid_len = *buf++;
736 len--;
737
738 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
739 {
Hanno Becker22626482019-05-03 12:46:59 +0100740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100741 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100742 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
743 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100744 }
745
746 if( len != peer_cid_len )
747 {
Hanno Becker22626482019-05-03 12:46:59 +0100748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100749 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100750 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
751 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100752 }
753
Hanno Becker5a299902019-05-03 12:47:49 +0100754 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100755 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
756 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
757
758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
759 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
760
Hanno Beckera8373a12019-04-26 15:37:26 +0100761 return( 0 );
762}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100763#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200766MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100768 const unsigned char *buf,
769 size_t len )
770{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200771 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100772 len != 0 )
773 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100774 MBEDTLS_SSL_DEBUG_MSG( 1,
775 ( "non-matching encrypt-then-MAC extension" ) );
776 mbedtls_ssl_send_alert_message(
777 ssl,
778 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100779 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100780 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100781 }
782
783 ((void) buf);
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100786
787 return( 0 );
788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200792MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200794 const unsigned char *buf,
795 size_t len )
796{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200797 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200798 len != 0 )
799 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100800 MBEDTLS_SSL_DEBUG_MSG( 1,
801 ( "non-matching extended master secret extension" ) );
802 mbedtls_ssl_send_alert_message(
803 ssl,
804 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100805 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
806 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200807 }
808
809 ((void) buf);
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200812
813 return( 0 );
814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200818MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200820 const unsigned char *buf,
821 size_t len )
822{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200823 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200824 len != 0 )
825 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100826 MBEDTLS_SSL_DEBUG_MSG( 1,
827 ( "non-matching session ticket extension" ) );
828 mbedtls_ssl_send_alert_message(
829 ssl,
830 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100831 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
832 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200833 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200834
835 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200836
837 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200838
839 return( 0 );
840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200842
Robert Cragie136884c2015-10-02 13:34:31 +0100843#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100844 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200845MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200847 const unsigned char *buf,
848 size_t len )
849{
850 size_t list_size;
851 const unsigned char *p;
852
Philippe Antoine747fd532018-05-30 09:13:21 +0200853 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
857 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100858 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200859 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200860 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200861
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200862 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200863 while( list_size > 0 )
864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
866 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200867 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200868#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
869 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200870 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200871#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
872 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100873#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200874 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
875 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200878 return( 0 );
879 }
880
881 list_size--;
882 p++;
883 }
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200886 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
887 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100888 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200889}
Darryl Green11999bb2018-03-13 15:22:58 +0000890#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100891 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200892
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200893#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200894MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200895static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
896 const unsigned char *buf,
897 size_t len )
898{
Janos Follath865b3eb2019-12-16 11:46:15 +0000899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200900
Hanno Beckere694c3e2017-12-27 21:34:08 +0000901 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200902 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
903 {
904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
905 return( 0 );
906 }
907
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200908 /* If we got here, we no longer need our cached extension */
909 mbedtls_free( ssl->handshake->ecjpake_cache );
910 ssl->handshake->ecjpake_cache = NULL;
911 ssl->handshake->ecjpake_cache_len = 0;
912
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200913 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
914 buf, len ) ) != 0 )
915 {
916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100917 mbedtls_ssl_send_alert_message(
918 ssl,
919 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
920 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200921 return( ret );
922 }
923
924 return( 0 );
925}
926#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200929MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200931 const unsigned char *buf, size_t len )
932{
933 size_t list_len, name_len;
934 const char **p;
935
936 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200937 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200938 {
939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100940 mbedtls_ssl_send_alert_message(
941 ssl,
942 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100943 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
944 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200945 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200946
947 /*
948 * opaque ProtocolName<1..2^8-1>;
949 *
950 * struct {
951 * ProtocolName protocol_name_list<2..2^16-1>
952 * } ProtocolNameList;
953 *
954 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
955 */
956
957 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
958 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200959 {
960 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
961 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100962 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200963 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200964
965 list_len = ( buf[0] << 8 ) | buf[1];
966 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200967 {
968 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
969 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100970 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200971 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200972
973 name_len = buf[2];
974 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200975 {
976 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
977 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100978 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200979 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200980
981 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200982 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200983 {
984 if( name_len == strlen( *p ) &&
985 memcmp( buf + 3, *p, name_len ) == 0 )
986 {
987 ssl->alpn_chosen = *p;
988 return( 0 );
989 }
990 }
991
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
994 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100995 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200998
Johan Pascalb62bb512015-12-03 21:56:45 +0100999#if defined(MBEDTLS_SSL_DTLS_SRTP)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001000MBEDTLS_CHECK_RETURN_CRITICAL
Johan Pascalb62bb512015-12-03 21:56:45 +01001001static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001002 const unsigned char *buf,
1003 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001004{
Johan Pascal43f94902020-09-22 12:25:52 +02001005 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001006 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001007 uint16_t server_protection_profile_value = 0;
1008
1009 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001010 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1011 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1012 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001013 return( 0 );
1014
Ron Eldora9788042018-12-05 11:04:31 +02001015 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001016 * uint8 SRTPProtectionProfile[2];
1017 *
1018 * struct {
1019 * SRTPProtectionProfiles SRTPProtectionProfiles;
1020 * opaque srtp_mki<0..255>;
1021 * } UseSRTPData;
1022
1023 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1024 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001025 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001026 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001027 {
1028 mki_len = ssl->dtls_srtp_info.mki_len;
1029 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001030
Ron Eldoref72faf2018-07-12 11:54:20 +03001031 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001032 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1033 * + protection profile (2 bytes)
1034 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001035 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001036 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001037 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001038 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001039
1040 /*
1041 * get the server protection profile
1042 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001043
1044 /*
1045 * protection profile length must be 0x0002 as we must have only
1046 * one protection profile in server Hello
1047 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001048 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001049 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001050
1051 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001052 server_protection = mbedtls_ssl_check_srtp_profile_value(
1053 server_protection_profile_value );
1054 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001055 {
Johan Pascal43f94902020-09-22 12:25:52 +02001056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1057 mbedtls_ssl_get_srtp_profile_as_string(
1058 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001059 }
1060
Johan Pascal43f94902020-09-22 12:25:52 +02001061 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001062
Johan Pascalb62bb512015-12-03 21:56:45 +01001063 /*
1064 * Check we have the server profile in our list
1065 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001066 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001067 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001068 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1069 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001070 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1072 mbedtls_ssl_get_srtp_profile_as_string(
1073 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001074 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001075 }
1076 }
1077
Ron Eldor591f1622018-01-22 12:30:04 +02001078 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001079 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001080 {
1081 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001082 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001083 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001084 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001085
1086 /* If server does not use mki in its reply, make sure the client won't keep
1087 * one as negotiated */
1088 if( len == 5 )
1089 {
1090 ssl->dtls_srtp_info.mki_len = 0;
1091 }
1092
Ron Eldoref72faf2018-07-12 11:54:20 +03001093 /*
1094 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001095 * If the client detects a nonzero-length MKI in the server's response
1096 * that is different than the one the client offered, then the client
1097 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1098 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001099 if( len > 5 && ( buf[4] != mki_len ||
1100 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001101 {
1102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1103 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001104 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001105 }
Ron Eldorb4655392018-07-05 18:25:39 +03001106#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001107 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001108 {
Ron Eldora9788042018-12-05 11:04:31 +02001109 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1110 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001111 }
1112#endif
Ron Eldora9788042018-12-05 11:04:31 +02001113 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001114}
1115#endif /* MBEDTLS_SSL_DTLS_SRTP */
1116
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001117/*
1118 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001121MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001125 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126 unsigned char cookie_len;
1127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001129
Gilles Peskineb64bf062019-09-27 14:02:44 +02001130 /* Check that there is enough room for:
1131 * - 2 bytes of version
1132 * - 1 byte of cookie_len
1133 */
1134 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1135 {
1136 MBEDTLS_SSL_DEBUG_MSG( 1,
1137 ( "incoming HelloVerifyRequest message is too short" ) );
1138 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1139 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001140 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001141 }
1142
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001143 /*
1144 * struct {
1145 * ProtocolVersion server_version;
1146 * opaque cookie<0..2^8-1>;
1147 * } HelloVerifyRequest;
1148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001150 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001151 p += 2;
1152
TRodziewicz2d8800e2021-05-13 19:14:19 +02001153 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001154 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1155 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1156 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001157 */
Glenn Strauss83158112022-04-13 14:59:34 -04001158 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1163 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
Hanno Beckerbc000442021-06-24 09:18:19 +01001165 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001166 }
1167
1168 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001169 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1170 {
1171 MBEDTLS_SSL_DEBUG_MSG( 1,
1172 ( "cookie length does not match incoming message size" ) );
1173 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1174 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001175 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001176 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001177 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001178
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001179 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001180
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001181 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1182 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001183 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001185 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001186 }
1187
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001188 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001189 ssl->handshake->verify_cookie_len = cookie_len;
1190
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001191 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1193 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001198
1199 return( 0 );
1200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001202
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001203MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001205{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001206 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001207 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001208 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001209 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001210 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001212 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001213#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001214 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Hanno Becker327c93b2018-08-15 13:56:18 +01001219 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001221 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 return( ret );
1224 }
1225
Hanno Becker79594fd2019-05-08 09:38:41 +01001226 buf = ssl->in_msg;
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_RENEGOTIATION)
1231 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001232 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001233 ssl->renego_records_seen++;
1234
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001235 if( ssl->conf->renego_max_records >= 0 &&
1236 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001237 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001238 MBEDTLS_SSL_DEBUG_MSG( 1,
1239 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001241 }
1242
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001243 MBEDTLS_SSL_DEBUG_MSG( 1,
1244 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001245
1246 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001248 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001252 mbedtls_ssl_send_alert_message(
1253 ssl,
1254 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1255 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 }
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001266 return( ssl_parse_hello_verify_request( ssl ) );
1267 }
1268 else
1269 {
1270 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001271 mbedtls_free( ssl->handshake->cookie );
1272 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001273 ssl->handshake->verify_cookie_len = 0;
1274 }
1275 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1279 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1283 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001284 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 }
1286
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001287 /*
1288 * 0 . 1 server_version
1289 * 2 . 33 random (maybe including 4 bytes of Unix time)
1290 * 34 . 34 session_id length = n
1291 * 35 . 34+n session_id
1292 * 35+n . 36+n cipher_suite
1293 * 37+n . 37+n compression_method
1294 *
1295 * 38+n . 39+n extensions length (optional)
1296 * 40+n . .. extensions
1297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001299
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001300 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1301 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001302 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Glenn Strauss60bfe602022-03-14 19:04:24 -04001304 if( ssl->tls_version < ssl->conf->min_tls_version ||
1305 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001306 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001307 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001308 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1309 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001310 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001311 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1314 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001315
Hanno Beckerbc000442021-06-24 09:18:19 +01001316 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001317 }
1318
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001320 ( (unsigned long) buf[2] << 24 ) |
1321 ( (unsigned long) buf[3] << 16 ) |
1322 ( (unsigned long) buf[4] << 8 ) |
1323 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001325 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001327 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Paul Bakker48916f92012-09-16 19:57:18 +00001331 if( n > 32 )
1332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1335 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001336 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001337 }
1338
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001339 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001341 ext_len = ( ( buf[38 + n] << 8 )
1342 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Paul Bakker48916f92012-09-16 19:57:18 +00001344 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001348 mbedtls_ssl_send_alert_message(
1349 ssl,
1350 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1351 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001352 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001355 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001356 {
1357 ext_len = 0;
1358 }
1359 else
1360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001362 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1363 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001364 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001367 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001368 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001369
1370 /*
1371 * Read and check compression
1372 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001373 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001375 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001376 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001377 MBEDTLS_SSL_DEBUG_MSG( 1,
1378 ( "server hello, bad compression: %d", comp ) );
1379 mbedtls_ssl_send_alert_message(
1380 ssl,
1381 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1382 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001384 }
1385
Paul Bakker380da532012-04-18 16:10:25 +00001386 /*
1387 * Initialize update checksum functions
1388 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001389 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1390 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001391 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001392 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001393 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1395 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001397 }
Paul Bakker380da532012-04-18 16:10:25 +00001398
Hanno Beckere694c3e2017-12-27 21:34:08 +00001399 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001400
Paul Elliottd48d5c62021-01-07 14:47:05 +00001401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
1404 /*
1405 * Check if the session can be resumed
1406 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001407 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_RENEGOTIATION)
1409 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001410#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001411 ssl->session_negotiate->ciphersuite != i ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001412 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001413 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 {
1415 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001416 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001418 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001419#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001420 ssl->session_negotiate->ciphersuite = i;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001421 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001422 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 }
1424 else
1425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 }
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001430 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Paul Elliott3891caf2020-12-17 18:42:40 +00001432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1434 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Andrzej Kurek03bac442018-04-25 05:06:07 -04001436 /*
1437 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001438 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 i = 0;
1440 while( 1 )
1441 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001442 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001445 mbedtls_ssl_send_alert_message(
1446 ssl,
1447 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1448 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001449 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 }
1451
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001452 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001453 ssl->session_negotiate->ciphersuite )
1454 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 }
1458
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001459 suite_info = mbedtls_ssl_ciphersuite_from_id(
1460 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001461 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1462 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001463 {
1464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001465 mbedtls_ssl_send_alert_message(
1466 ssl,
1467 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001468 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1469 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001470 }
1471
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001472 MBEDTLS_SSL_DEBUG_MSG( 3,
1473 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001474
Gilles Peskineeccd8882020-03-10 12:19:08 +01001475#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001476 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001477 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001478 {
1479 ssl->handshake->ecrs_enabled = 1;
1480 }
1481#endif
1482
Thomas Daubney20f89a92022-06-20 15:12:19 +01001483 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001486 mbedtls_ssl_send_alert_message(
1487 ssl,
1488 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1489 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001490 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 }
1492
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001493 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001494
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001495 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001496 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001497
Paul Bakker48916f92012-09-16 19:57:18 +00001498 while( ext_len )
1499 {
1500 unsigned int ext_id = ( ( ext[0] << 8 )
1501 | ( ext[1] ) );
1502 unsigned int ext_size = ( ( ext[2] << 8 )
1503 | ( ext[3] ) );
1504
1505 if( ext_size + 4 > ext_len )
1506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001508 mbedtls_ssl_send_alert_message(
1509 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001511 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001512 }
1513
1514 switch( ext_id )
1515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1518#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001519 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001520#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001521
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001522 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1523 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001524 return( ret );
1525
1526 break;
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1529 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001530 MBEDTLS_SSL_DEBUG_MSG( 3,
1531 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001532
1533 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1534 ext + 4, ext_size ) ) != 0 )
1535 {
1536 return( ret );
1537 }
1538
1539 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001541
Hanno Beckera0e20d02019-05-15 14:03:01 +01001542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001543 case MBEDTLS_TLS_EXT_CID:
1544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1545
1546 if( ( ret = ssl_parse_cid_ext( ssl,
1547 ext + 4,
1548 ext_size ) ) != 0 )
1549 {
1550 return( ret );
1551 }
1552
1553 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001554#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1557 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001559
1560 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1561 ext + 4, ext_size ) ) != 0 )
1562 {
1563 return( ret );
1564 }
1565
1566 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1570 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001571 MBEDTLS_SSL_DEBUG_MSG( 3,
1572 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001573
1574 if( ( ret = ssl_parse_extended_ms_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_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1584 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001586
1587 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1588 ext + 4, ext_size ) ) != 0 )
1589 {
1590 return( ret );
1591 }
1592
1593 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001595
Robert Cragie136884c2015-10-02 13:34:31 +01001596#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001597 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001599 MBEDTLS_SSL_DEBUG_MSG( 3,
1600 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001601
1602 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1603 ext + 4, ext_size ) ) != 0 )
1604 {
1605 return( ret );
1606 }
1607
1608 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001609#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1610 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001611
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001612#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1613 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1615
1616 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1617 ext + 4, ext_size ) ) != 0 )
1618 {
1619 return( ret );
1620 }
1621
1622 break;
1623#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#if defined(MBEDTLS_SSL_ALPN)
1626 case MBEDTLS_TLS_EXT_ALPN:
1627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001628
Johan Pascal275874b2020-10-27 10:43:53 +01001629 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1630 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001631
1632 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001634
Johan Pascalb62bb512015-12-03 21:56:45 +01001635#if defined(MBEDTLS_SSL_DTLS_SRTP)
1636 case MBEDTLS_TLS_EXT_USE_SRTP:
1637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1638
Johan Pascalc3ccd982020-10-28 17:18:18 +01001639 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1640 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001641
1642 break;
1643#endif /* MBEDTLS_SSL_DTLS_SRTP */
1644
Paul Bakker48916f92012-09-16 19:57:18 +00001645 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001646 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001647 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001648 }
1649
1650 ext_len -= 4 + ext_size;
1651 ext += 4 + ext_size;
1652
1653 if( ext_len > 0 && ext_len < 4 )
1654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001656 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001657 }
1658 }
1659
1660 /*
Andrzej Kurek21b50802022-07-06 03:26:55 -04001661 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
1662 * extensions. It sets the transform data for the resumed session which in
1663 * case of DTLS includes the server CID extracted from the CID extension.
1664 */
1665 if( ssl->handshake->resume )
Andrzej Kurek7cf87252022-06-14 07:12:33 -04001666 {
1667 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
1668 {
1669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
1670 mbedtls_ssl_send_alert_message(
1671 ssl,
1672 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1673 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
1674 return( ret );
1675 }
1676 }
1677
Paul Bakker48916f92012-09-16 19:57:18 +00001678 /*
1679 * Renegotiation security checks
1680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001682 ssl->conf->allow_legacy_renegotiation ==
1683 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001684 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001685 MBEDTLS_SSL_DEBUG_MSG( 1,
1686 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001687 handshake_failure = 1;
1688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_SSL_RENEGOTIATION)
1690 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1691 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001692 renegotiation_info_seen == 0 )
1693 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001694 MBEDTLS_SSL_DEBUG_MSG( 1,
1695 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001696 handshake_failure = 1;
1697 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1699 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001700 ssl->conf->allow_legacy_renegotiation ==
1701 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001704 handshake_failure = 1;
1705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1707 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001708 renegotiation_info_seen == 1 )
1709 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001710 MBEDTLS_SSL_DEBUG_MSG( 1,
1711 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001712 handshake_failure = 1;
1713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001715
1716 if( handshake_failure == 1 )
1717 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001718 mbedtls_ssl_send_alert_message(
1719 ssl,
1720 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1721 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001722 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
1727 return( 0 );
1728}
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1731 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001732MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001733static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1734 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001735 unsigned char *end )
1736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001738 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001739
Paul Bakker29e1f122013-04-16 13:07:56 +02001740 /*
1741 * Ephemeral DH parameters:
1742 *
1743 * struct {
1744 * opaque dh_p<1..2^16-1>;
1745 * opaque dh_g<1..2^16-1>;
1746 * opaque dh_Ys<1..2^16-1>;
1747 * } ServerDHParams;
1748 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001749 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1750 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001753 return( ret );
1754 }
1755
Gilles Peskine487bbf62021-05-27 22:17:07 +02001756 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001757 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001758 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001760 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001761 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001762 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001763 }
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1766 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1767 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001768
1769 return( ret );
1770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1772 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001773
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001774#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1775 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1776 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1777#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001778MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001779static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001780 unsigned char **p,
1781 unsigned char *end )
1782{
1783 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001784 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001785 uint8_t ecpoint_len;
1786 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1787
1788 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001789 * struct {
1790 * ECParameters curve_params;
1791 * ECPoint public;
1792 * } ServerECDHParams;
1793 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001794 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001795 * 2..3 NamedCurve
1796 * 4 ECPoint.len
1797 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001798 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001799 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001800 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001801
1802 /* First byte is curve_type; only named_curve is handled */
1803 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001804 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001805
1806 /* Next two bytes are the namedcurve value */
1807 tls_id = *(*p)++;
1808 tls_id <<= 8;
1809 tls_id |= *(*p)++;
1810
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001811 /* Check it's a curve we offered */
1812 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001813 {
1814 MBEDTLS_SSL_DEBUG_MSG( 2,
1815 ( "bad server key exchange message (ECDHE curve): %u",
1816 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001817 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001818 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001819
Hanno Beckerbb89e272019-01-08 12:54:37 +00001820 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001821 if( ( handshake->ecdh_psa_type =
1822 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001823 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001824 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001825 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001826 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001827
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001828 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001829 ecpoint_len = *(*p)++;
1830 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001831 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001832
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001833 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1834 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001835
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001836 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1837 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001839
Hanno Beckerbb89e272019-01-08 12:54:37 +00001840 return( 0 );
1841}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001842#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001843MBEDTLS_CHECK_RETURN_CRITICAL
Neil Armstrong1f198d82022-04-13 15:02:30 +02001844static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1845{
1846 const mbedtls_ecp_curve_info *curve_info;
1847 mbedtls_ecp_group_id grp_id;
1848#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1849 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1850#else
1851 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1852#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001853
Neil Armstrong1f198d82022-04-13 15:02:30 +02001854 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1855 if( curve_info == NULL )
1856 {
1857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1859 }
1860
1861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1862
1863 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1864 return( -1 );
1865
1866 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1867 MBEDTLS_DEBUG_ECDH_QP );
1868
1869 return( 0 );
1870}
1871
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001872MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001874 unsigned char **p,
1875 unsigned char *end )
1876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001878
Paul Bakker29e1f122013-04-16 13:07:56 +02001879 /*
1880 * Ephemeral ECDH parameters:
1881 *
1882 * struct {
1883 * ECParameters curve_params;
1884 * ECPoint public;
1885 * } ServerECDHParams;
1886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001888 (const unsigned char **) p, end ) ) != 0 )
1889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001891#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001892 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1893 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001894#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001895 return( ret );
1896 }
1897
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001898 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001899 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001900 MBEDTLS_SSL_DEBUG_MSG( 1,
1901 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001902 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001903 }
1904
Paul Bakker29e1f122013-04-16 13:07:56 +02001905 return( ret );
1906}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001907#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1909 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1910 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001911
Gilles Peskineeccd8882020-03-10 12:19:08 +01001912#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001913MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001915 unsigned char **p,
1916 unsigned char *end )
1917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001919 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001920 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001921
1922 /*
1923 * PSK parameters:
1924 *
1925 * opaque psk_identity_hint<0..2^16-1>;
1926 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001927 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001928 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001929 MBEDTLS_SSL_DEBUG_MSG( 1,
1930 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001931 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001932 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001933 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001934 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001935
irwir6527bd62019-09-21 18:51:25 +03001936 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001937 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001938 MBEDTLS_SSL_DEBUG_MSG( 1,
1939 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001940 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941 }
1942
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001943 /*
1944 * Note: we currently ignore the PKS identity hint, as we only allow one
1945 * PSK to be provisionned on the client. This could be changed later if
1946 * someone needs that feature.
1947 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001948 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001949 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001950
1951 return( ret );
1952}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001953#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1956 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001957/*
1958 * Generate a pre-master secret and encrypt it with the server's RSA key
1959 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001960MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001962 size_t offset, size_t *olen,
1963 size_t pms_offset )
1964{
Janos Follath865b3eb2019-12-16 11:46:15 +00001965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001966 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001968 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001969
Angus Grattond8213d02016-05-25 20:56:48 +10001970 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001971 {
1972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1973 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1974 }
1975
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001976 /*
1977 * Generate (part of) the pre-master as
1978 * struct {
1979 * ProtocolVersion client_version;
1980 * opaque random[46];
1981 * } PreMasterSecret;
1982 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001983 mbedtls_ssl_write_version( p, ssl->conf->transport,
1984 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001985
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001986 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001989 return( ret );
1990 }
1991
1992 ssl->handshake->pmslen = 48;
1993
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001994#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1995 peer_pk = &ssl->handshake->peer_pubkey;
1996#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001997 if( ssl->session_negotiate->peer_cert == NULL )
1998 {
Hanno Becker8273df82019-02-06 17:37:32 +00001999 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002002 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002003 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2004#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002005
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002006 /*
2007 * Now write it out, encrypted
2008 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002009 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2012 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002013 }
2014
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002015 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002016 p, ssl->handshake->pmslen,
2017 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002018 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002019 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002022 return( ret );
2023 }
2024
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002025 if( len_bytes == 2 )
2026 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002027 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028 *olen += 2;
2029 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002030
Hanno Beckerae553dd2019-02-08 14:06:00 +00002031#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2032 /* We don't need the peer's public key anymore. Free it. */
2033 mbedtls_pk_free( peer_pk );
2034#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002035 return( 0 );
2036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2038 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2041 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002042MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002044{
Janos Follath865b3eb2019-12-16 11:46:15 +00002045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002047 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002048
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002049#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2050 peer_pk = &ssl->handshake->peer_pubkey;
2051#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002052 if( ssl->session_negotiate->peer_cert == NULL )
2053 {
Hanno Becker8273df82019-02-06 17:37:32 +00002054 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002057 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002058 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2059#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002060
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02002061 /* This is a public key, so it can't be opaque, so can_do() is a good
2062 * enough check to ensure pk_ec() is safe to use below. */
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002063 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2066 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002067 }
2068
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002069 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002070
Przemek Stekielea4000f2022-03-16 09:49:33 +01002071#if defined(MBEDTLS_USE_PSA_CRYPTO)
2072 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002073 size_t olen = 0;
2074
2075 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2076 {
2077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2078 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2079 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002080
2081 ssl->handshake->ecdh_psa_type =
2082 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2083 &ecdh_bits ) );
2084
2085 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2086 {
2087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2088 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2089 }
2090
2091 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2092
Przemek Stekielea4000f2022-03-16 09:49:33 +01002093 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002094 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2095 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2096 ssl->handshake->ecdh_psa_peerkey,
2097 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002098
Przemek Stekiel561a4232022-03-16 13:16:24 +01002099 if ( ret != 0 )
2100 {
2101 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2102 return( ret );
2103 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002104
Przemek Stekiel561a4232022-03-16 13:16:24 +01002105 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002106#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2108 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002111 return( ret );
2112 }
2113
2114 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002117 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002118 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002119#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002120#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2121 /* We don't need the peer's public key anymore. Free it,
2122 * so that more RAM is available for upcoming expensive
2123 * operations like ECDHE. */
2124 mbedtls_pk_free( peer_pk );
2125#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2126
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002127 return( ret );
2128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2130 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002131
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002132MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002134{
Janos Follath865b3eb2019-12-16 11:46:15 +00002135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002136 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002137 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002138 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2143 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 ssl->state++;
2147 return( 0 );
2148 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002149 ((void) p);
2150 ((void) end);
2151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2154 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2155 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2156 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002157 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002158 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002161 mbedtls_ssl_send_alert_message(
2162 ssl,
2163 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2164 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002165 return( ret );
2166 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002169 ssl->state++;
2170 return( 0 );
2171 }
2172 ((void) p);
2173 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2175 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002176
Gilles Peskineeccd8882020-03-10 12:19:08 +01002177#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002178 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002179 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002180 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002181 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002182 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002183#endif
2184
Hanno Becker327c93b2018-08-15 13:56:18 +01002185 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 return( ret );
2189 }
2190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002194 mbedtls_ssl_send_alert_message(
2195 ssl,
2196 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2197 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002201 /*
2202 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2203 * doesn't use a psk_identity_hint
2204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2208 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002209 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002210 /* Current message is probably either
2211 * CertificateRequest or ServerHelloDone */
2212 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002213 goto exit;
2214 }
2215
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002216 MBEDTLS_SSL_DEBUG_MSG( 1,
2217 ( "server key exchange message must not be skipped" ) );
2218 mbedtls_ssl_send_alert_message(
2219 ssl,
2220 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2221 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 }
2225
Gilles Peskineeccd8882020-03-10 12:19:08 +01002226#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002227 if( ssl->handshake->ecrs_enabled )
2228 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2229
2230start_processing:
2231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002233 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002235
Gilles Peskineeccd8882020-03-10 12:19:08 +01002236#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
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 ||
2239 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2240 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002241 {
2242 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002245 mbedtls_ssl_send_alert_message(
2246 ssl,
2247 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002248 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002249 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002250 }
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002251 } /* FALLTHROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002252#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2255 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2256 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2257 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002258 ; /* nothing more to do */
2259 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2261 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2262#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2263 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2264 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2265 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002267 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002270 mbedtls_ssl_send_alert_message(
2271 ssl,
2272 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2273 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002274 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002275 }
2276 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2279 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002280#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2281 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2283 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2284 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2285 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002286 {
2287 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002290 mbedtls_ssl_send_alert_message(
2291 ssl,
2292 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2293 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002294 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002295 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002296 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002297 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2299 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2300 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002301#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2302 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2303 {
2304 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2305 p, end - p );
2306 if( ret != 0 )
2307 {
2308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002309 mbedtls_ssl_send_alert_message(
2310 ssl,
2311 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002312 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2313 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002314 }
2315 }
2316 else
2317#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002321 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002322
Gilles Peskineeccd8882020-03-10 12:19:08 +01002323#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002324 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002325 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002326 size_t sig_len, hashlen;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002327 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
2328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2330 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2331 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002332 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002333 void *rs_ctx = NULL;
Jerry Yu693a47a2022-06-23 14:02:28 +08002334 uint16_t sig_alg;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002335
Hanno Beckera6899bb2019-02-06 18:26:03 +00002336 mbedtls_pk_context * peer_pk;
2337
Jerry Yu693a47a2022-06-23 14:02:28 +08002338#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2339 peer_pk = &ssl->handshake->peer_pubkey;
2340#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2341 if( ssl->session_negotiate->peer_cert == NULL )
2342 {
2343 /* Should never happen */
2344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2346 }
2347 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2348#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2349
Paul Bakker29e1f122013-04-16 13:07:56 +02002350 /*
2351 * Handle the digitally-signed structure
2352 */
Jerry Yu693a47a2022-06-23 14:02:28 +08002353 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
2354 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Jerry Yu95b743c2022-07-23 11:37:50 +08002355 if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuc3bf7482022-07-29 10:27:17 +08002356 sig_alg, &pk_alg, &md_alg ) != 0 &&
Jerry Yu693a47a2022-06-23 14:02:28 +08002357 ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) &&
2358 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002359 {
Ronald Cron90915f22022-03-07 11:11:36 +01002360 MBEDTLS_SSL_DEBUG_MSG( 1,
2361 ( "bad server key exchange message" ) );
2362 mbedtls_ssl_send_alert_message(
2363 ssl,
2364 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2365 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2366 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002367 }
Jerry Yu693a47a2022-06-23 14:02:28 +08002368 p += 2;
Ronald Cron90915f22022-03-07 11:11:36 +01002369
Jerry Yu693a47a2022-06-23 14:02:28 +08002370 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002371 {
Ronald Cron90915f22022-03-07 11:11:36 +01002372 MBEDTLS_SSL_DEBUG_MSG( 1,
2373 ( "bad server key exchange message" ) );
2374 mbedtls_ssl_send_alert_message(
2375 ssl,
2376 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2377 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2378 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002379 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002380
2381 /*
2382 * Read signature
2383 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002384
2385 if( p > end - 2 )
2386 {
2387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002388 mbedtls_ssl_send_alert_message(
2389 ssl,
2390 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2391 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002392 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002393 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002394 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002395 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002396
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002397 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002400 mbedtls_ssl_send_alert_message(
2401 ssl,
2402 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2403 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002404 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002408
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002409 /*
2410 * Compute the hash that has been signed
2411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002413 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002414 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2415 params, params_len,
2416 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002417 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002418 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002419 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002420 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002424 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002425
Gilles Peskineca1d7422018-04-24 11:53:22 +02002426 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002427
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002428 /*
2429 * Verify signature
2430 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002431 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002434 mbedtls_ssl_send_alert_message(
2435 ssl,
2436 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2437 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002439 }
2440
Gilles Peskineeccd8882020-03-10 12:19:08 +01002441#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002442 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002443 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002444#endif
2445
Jerry Yu693a47a2022-06-23 14:02:28 +08002446#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2447 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
2448 {
Jerry Yu693a47a2022-06-23 14:02:28 +08002449 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
2450 rsassa_pss_options.mgf1_hash_id = md_alg;
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002451 rsassa_pss_options.expected_salt_len =
2452 mbedtls_hash_info_get_size( md_alg );
2453 if( rsassa_pss_options.expected_salt_len == 0 )
Jerry Yu693a47a2022-06-23 14:02:28 +08002454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Andrzej Kurek0ce59212022-08-17 07:54:34 -04002455
Jerry Yu693a47a2022-06-23 14:02:28 +08002456 ret = mbedtls_pk_verify_ext( pk_alg, &rsassa_pss_options,
2457 peer_pk,
2458 md_alg, hash, hashlen,
2459 p, sig_len );
2460 }
2461 else
2462#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2463 ret = mbedtls_pk_verify_restartable( peer_pk,
2464 md_alg, hash, hashlen, p, sig_len, rs_ctx );
2465
2466 if( ret != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002467 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002468#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002469 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2470#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002471 mbedtls_ssl_send_alert_message(
2472 ssl,
2473 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2474 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002476#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002477 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2478 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2479#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002480 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002481 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002482
2483#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2484 /* We don't need the peer's public key anymore. Free it,
2485 * so that more RAM is available for upcoming expensive
2486 * operations like ECDHE. */
2487 mbedtls_pk_free( peer_pk );
2488#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002490#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002492exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 ssl->state++;
2494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498}
2499
Gilles Peskineeccd8882020-03-10 12:19:08 +01002500#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002501MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002503{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002504 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002505 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002508
Hanno Becker1aa267c2017-04-28 17:08:27 +01002509 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002512 ssl->state++;
2513 return( 0 );
2514 }
2515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002518}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002519#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002520MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002522{
Janos Follath865b3eb2019-12-16 11:46:15 +00002523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002524 unsigned char *buf;
2525 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002526 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002528 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002529 size_t sig_alg_len;
2530#if defined(MBEDTLS_DEBUG_C)
2531 unsigned char *sig_alg;
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002532 unsigned char *dn;
Ronald Cron90915f22022-03-07 11:11:36 +01002533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Hanno Becker1aa267c2017-04-28 17:08:27 +01002537 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002540 ssl->state++;
2541 return( 0 );
2542 }
2543
Hanno Becker327c93b2018-08-15 13:56:18 +01002544 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2547 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
2549
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002550 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2551 {
2552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002553 mbedtls_ssl_send_alert_message(
2554 ssl,
2555 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2556 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002557 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002560 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002561 ssl->handshake->client_auth =
2562 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002565 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Jerry Yufb28b882022-01-28 11:05:58 +08002567 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002568 {
Johan Pascala89ca862020-08-25 10:03:19 +02002569 /* Current message is probably the ServerHelloDone */
2570 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002571 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002572 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002573
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002574 /*
2575 * struct {
2576 * ClientCertificateType certificate_types<1..2^8-1>;
2577 * SignatureAndHashAlgorithm
2578 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2579 * DistinguishedName certificate_authorities<0..2^16-1>;
2580 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002581 *
2582 * Since we only support a single certificate on clients, let's just
2583 * ignore all the information that's supposed to help us pick a
2584 * certificate.
2585 *
2586 * We could check that our certificate matches the request, and bail out
2587 * if it doesn't, but it's simpler to just send the certificate anyway,
2588 * and give the server the opportunity to decide if it should terminate
2589 * the connection when it doesn't like our certificate.
2590 *
2591 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2592 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002593 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002594 *
2595 * However, we still minimally parse the message to check it is at least
2596 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002597 */
Paul Bakker926af752012-11-23 13:38:07 +01002598 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002599
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002600 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002601 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2602 {
2603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2604 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2605 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002606 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002609 n = cert_type_len;
2610
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002611 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002612 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002613 * * the length of the signature algorithms field (if minor version of
2614 * SSL is 3),
2615 * * distinguished name length otherwise.
2616 * Both reach at most the index:
2617 * ...hdr_len + 2 + n,
2618 * therefore the buffer length at this point must be greater than that
2619 * regardless of the actual code path.
2620 */
2621 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002624 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2625 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002626 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002627 }
2628
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002629 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002630 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2631 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2632
2633 /*
2634 * The furthest access in buf is in the loop few lines below:
2635 * sig_alg[i + 1],
2636 * where:
2637 * sig_alg = buf + ...hdr_len + 3 + n,
2638 * max(i) = sig_alg_len - 1.
2639 * Therefore the furthest access is:
2640 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2641 * which reduces to:
2642 * buf[...hdr_len + 3 + n + sig_alg_len],
2643 * which is one less than we need the buf to be.
2644 */
2645 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002646 {
Ronald Cron90915f22022-03-07 11:11:36 +01002647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2648 mbedtls_ssl_send_alert_message(
2649 ssl,
2650 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2651 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2652 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002653 }
Paul Bakker926af752012-11-23 13:38:07 +01002654
Ronald Cron90915f22022-03-07 11:11:36 +01002655#if defined(MBEDTLS_DEBUG_C)
2656 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2657 for( size_t i = 0; i < sig_alg_len; i += 2 )
2658 {
2659 MBEDTLS_SSL_DEBUG_MSG( 3,
2660 ( "Supported Signature Algorithm found: %d,%d",
2661 sig_alg[i], sig_alg[i + 1] ) );
2662 }
2663#endif
2664
2665 n += 2 + sig_alg_len;
2666
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002667 /* certificate_authorities */
2668 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2669 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002670
2671 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002672 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002675 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2676 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002677 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002678 }
2679
Glenn Straussbd10c4e2022-06-25 03:15:48 -04002680#if defined(MBEDTLS_DEBUG_C)
2681 dn = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n - dn_len;
2682 for( size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len )
2683 {
2684 unsigned char *p = dn + i + 2;
2685 mbedtls_x509_name name;
2686 mbedtls_x509_name *name_cur, *name_prv;
2687 size_t asn1_len;
2688 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
2689 memset( &name, 0, sizeof( name ) );
2690 dni_len = MBEDTLS_GET_UINT16_BE( dn + i, 0 );
2691 if( dni_len > dn_len - i - 2 ||
2692 mbedtls_asn1_get_tag( &p, p + dni_len, &asn1_len,
2693 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) != 0 ||
2694 mbedtls_x509_get_name( &p, p + asn1_len, &name ) != 0 )
2695 {
2696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2697 mbedtls_ssl_send_alert_message(
2698 ssl,
2699 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2700 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2701 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
2702 }
2703 MBEDTLS_SSL_DEBUG_MSG( 3,
2704 ( "DN hint: %.*s",
2705 mbedtls_x509_dn_gets( s, sizeof(s), &name ), s ) );
2706 name_cur = name.next;
2707 while( name_cur != NULL )
2708 {
2709 name_prv = name_cur;
2710 name_cur = name_cur->next;
2711 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2712 mbedtls_free( name_prv );
2713 }
2714 }
2715#endif
2716
Paul Bakker926af752012-11-23 13:38:07 +01002717exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 return( 0 );
2721}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002722#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002724MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002726{
Janos Follath865b3eb2019-12-16 11:46:15 +00002727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Hanno Becker327c93b2018-08-15 13:56:18 +01002731 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2734 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002736
2737 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2738 {
2739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2740 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2744 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2748 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002749 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 }
2751
2752 ssl->state++;
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002757#endif
2758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
2761 return( 0 );
2762}
2763
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002764MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002766{
Janos Follath865b3eb2019-12-16 11:46:15 +00002767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002768
2769 size_t header_len;
2770 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002772 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2777 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 /*
2780 * DHM key exchange -- send G^X mod P
2781 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002782 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Joe Subbiani6dd73642021-07-19 11:56:54 +01002784 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002785 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002788 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002789 &ssl->out_msg[header_len], content_len,
2790 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 if( ret != 0 )
2792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 return( ret );
2795 }
2796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2798 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002801 ssl->handshake->premaster,
2802 MBEDTLS_PREMASTER_SIZE,
2803 &ssl->handshake->pmslen,
2804 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 return( ret );
2808 }
2809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 }
2812 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002814#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2815 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2816 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2817 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002818 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002819 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2820 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2821 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002822 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002823#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002824 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2825 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002826 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002827
2828 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2829
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002830 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002831
Hanno Becker0a94a642019-01-11 14:35:30 +00002832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2833
Hanno Becker4a63ed42019-01-08 11:39:35 +00002834 /*
2835 * Generate EC private key for ECDHE exchange.
2836 */
2837
Hanno Becker4a63ed42019-01-08 11:39:35 +00002838 /* The master secret is obtained from the shared ECDH secret by
2839 * applying the TLS 1.2 PRF with a specific salt and label. While
2840 * the PSA Crypto API encourages combining key agreement schemes
2841 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2842 * yet support the provisioning of salt + label to the KDF.
2843 * For the time being, we therefore need to split the computation
2844 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002845 key_attributes = psa_key_attributes_init();
2846 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002847 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002848 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2849 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002850
2851 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002852 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002853 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002854 if( status != PSA_SUCCESS )
2855 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2856
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002857 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002858 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002859 * but we just need to add a length byte before that. */
2860 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2861 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2862 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2863 size_t own_pubkey_len;
2864
Hanno Becker4a63ed42019-01-08 11:39:35 +00002865 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002866 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002867 &own_pubkey_len );
2868 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002869 {
2870 psa_destroy_key( handshake->ecdh_psa_privkey );
2871 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002872 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002873 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002874
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002875 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2876 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002877
Hanno Becker4a63ed42019-01-08 11:39:35 +00002878 /* The ECDH secret is the premaster secret used for key derivation. */
2879
Janos Follathdf3b0892019-08-08 11:12:24 +01002880 /* Compute ECDH shared secret. */
2881 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2882 handshake->ecdh_psa_privkey,
2883 handshake->ecdh_psa_peerkey,
2884 handshake->ecdh_psa_peerkey_len,
2885 ssl->handshake->premaster,
2886 sizeof( ssl->handshake->premaster ),
2887 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002888
Andrzej Kureka0237f82022-02-24 13:24:52 -05002889 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002890 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002891
2892 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2893 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002894#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002895 /*
2896 * ECDH key exchange -- send client public value
2897 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002898 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002899
Gilles Peskineeccd8882020-03-10 12:19:08 +01002900#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002901 if( ssl->handshake->ecrs_enabled )
2902 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002903 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002904 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002905
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002906 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2907 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002908#endif
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002911 &content_len,
2912 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002913 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002914 if( ret != 0 )
2915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002917#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002918 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2919 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2920#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002921 return( ret );
2922 }
2923
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002924 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2925 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002926
Gilles Peskineeccd8882020-03-10 12:19:08 +01002927#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002928 if( ssl->handshake->ecrs_enabled )
2929 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002930 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002931 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002932 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002933
2934ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002935 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002936 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002939 &ssl->handshake->pmslen,
2940 ssl->handshake->premaster,
2941 MBEDTLS_MPI_MAX_SIZE,
2942 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002945#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002946 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2947 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2948#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002949 return( ret );
2950 }
2951
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002952 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2953 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02002954#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01002955 }
2956 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2958 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2959 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2960 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002961#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2962 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2963 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2964 {
2965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2966 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2967 psa_key_attributes_t key_attributes;
2968
2969 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2970
2971 /*
2972 * opaque psk_identity<0..2^16-1>;
2973 */
2974 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002975 /* We don't offer PSK suites if we don't have a PSK,
2976 * and we check that the server's choice is among the
2977 * ciphersuites we offered, so this should never happen. */
2978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002979
Neil Armstrongfc834f22022-03-23 17:54:38 +01002980 /* uint16 to store content length */
2981 const size_t content_len_size = 2;
2982
Neil Armstrong868af822022-03-09 10:26:25 +01002983 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002984
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002985 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002986 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002987 {
2988 MBEDTLS_SSL_DEBUG_MSG( 1,
2989 ( "psk identity too long or SSL buffer too short" ) );
2990 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2991 }
2992
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002993 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002994
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002995 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
2996 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
2997 header_len += content_len_size;
2998
2999 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003000 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003001 p += ssl->conf->psk_identity_len;
3002
Neil Armstrong868af822022-03-09 10:26:25 +01003003 header_len += ssl->conf->psk_identity_len;
3004
3005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3006
3007 /*
3008 * Generate EC private key for ECDHE exchange.
3009 */
3010
3011 /* The master secret is obtained from the shared ECDH secret by
3012 * applying the TLS 1.2 PRF with a specific salt and label. While
3013 * the PSA Crypto API encourages combining key agreement schemes
3014 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3015 * yet support the provisioning of salt + label to the KDF.
3016 * For the time being, we therefore need to split the computation
3017 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3018 key_attributes = psa_key_attributes_init();
3019 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3020 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3021 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3022 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3023
3024 /* Generate ECDH private key. */
3025 status = psa_generate_key( &key_attributes,
3026 &handshake->ecdh_psa_privkey );
3027 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003028 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003029
3030 /* Export the public part of the ECDH private key from PSA.
3031 * The export format is an ECPoint structure as expected by TLS,
3032 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003033 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003034 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3035 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003036 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003037
3038 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3039 own_pubkey, own_pubkey_max_len,
3040 &own_pubkey_len );
3041 if( status != PSA_SUCCESS )
3042 {
3043 psa_destroy_key( handshake->ecdh_psa_privkey );
3044 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003045 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003046 }
3047
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003048 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003049 content_len = own_pubkey_len + 1;
3050
Neil Armstrong25400452022-03-23 17:44:07 +01003051 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3052 * - a uint16 containing the length (in octets) of the ECDH computation
3053 * - the octet string produced by the ECDH computation
3054 * - a uint16 containing the length (in octets) of the PSK
3055 * - the PSK itself
3056 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003057 unsigned char *pms = ssl->handshake->premaster;
3058 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003059 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003060 /* uint16 to store length (in octets) of the ECDH computation */
3061 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003062 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003063
Neil Armstrong25400452022-03-23 17:44:07 +01003064 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003065 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3066 handshake->ecdh_psa_privkey,
3067 handshake->ecdh_psa_peerkey,
3068 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003069 pms + zlen_size,
3070 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003071 &zlen );
3072
3073 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3074 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3075
Neil Armstrongc530aa62022-03-23 17:45:01 +01003076 if( status != PSA_SUCCESS )
3077 return( psa_ssl_status_to_mbedtls( status ) );
3078 else if( destruction_status != PSA_SUCCESS )
3079 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003080
Neil Armstrong25400452022-03-23 17:44:07 +01003081 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003082 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3083 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003084 }
3085 else
3086#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3087 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003088#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003089 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003090 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003091 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003092 * opaque psk_identity<0..2^16-1>;
3093 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003094 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003095 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003096 /* We don't offer PSK suites if we don't have a PSK,
3097 * and we check that the server's choice is among the
3098 * ciphersuites we offered, so this should never happen. */
3099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003100 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003101
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003102 header_len = 4;
3103 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003104
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003105 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003106 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003107 MBEDTLS_SSL_DEBUG_MSG( 1,
3108 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003109 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3110 }
3111
Joe Subbianifbeb6922021-07-16 14:27:50 +01003112 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3113 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003114
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003115 memcpy( ssl->out_msg + header_len,
3116 ssl->conf->psk_identity,
3117 ssl->conf->psk_identity_len );
3118 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3121 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003122 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003123 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003124 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003125 else
3126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3128 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003129 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003130 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3131 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003132 return( ret );
3133 }
3134 else
3135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3137 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003138 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003139 /*
3140 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3141 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003142 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003143
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003144 if( header_len + 2 + content_len >
3145 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003146 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003147 MBEDTLS_SSL_DEBUG_MSG( 1,
3148 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003149 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3150 }
3151
Joe Subbianifbeb6922021-07-16 14:27:50 +01003152 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3153 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003156 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003157 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003158 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003159 if( ret != 0 )
3160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003162 return( ret );
3163 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003164
3165#if defined(MBEDTLS_USE_PSA_CRYPTO)
3166 unsigned char *pms = ssl->handshake->premaster;
3167 unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster );
3168 size_t pms_len;
3169
3170 /* Write length only when we know the actual value */
3171 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3172 pms + 2, pms_end - ( pms + 2 ), &pms_len,
3173 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3174 {
3175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3176 return( ret );
3177 }
3178 MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 );
3179 pms += 2 + pms_len;
3180
3181 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3182#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003183 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003184 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003186#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3187 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003189 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003190 /*
3191 * ClientECDiffieHellmanPublic public;
3192 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003193 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3194 &content_len,
3195 &ssl->out_msg[header_len],
3196 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003197 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003198 if( ret != 0 )
3199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003201 return( ret );
3202 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003203
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003204 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3205 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003206 }
3207 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003208#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003212 }
3213
Neil Armstrong80f6f322022-05-03 17:56:38 +02003214#if !defined(MBEDTLS_USE_PSA_CRYPTO)
3215 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3216 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003217 {
Neil Armstrong80f6f322022-05-03 17:56:38 +02003218 MBEDTLS_SSL_DEBUG_RET( 1,
Neil Armstrongcd05f0b2022-05-03 10:28:37 +02003219 "mbedtls_ssl_psk_derive_premaster", ret );
Neil Armstrong80f6f322022-05-03 17:56:38 +02003220 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003221 }
Neil Armstrong80f6f322022-05-03 17:56:38 +02003222#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003223 }
3224 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003225#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3227 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003229 header_len = 4;
3230 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3231 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003232 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 }
Paul Bakkered27a042013-04-18 22:46:23 +02003234 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003236#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3237 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3238 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003239 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003240
3241 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003242 ssl->out_msg + header_len,
3243 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3244 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003245 ssl->conf->f_rng, ssl->conf->p_rng );
3246 if( ret != 0 )
3247 {
3248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3249 return( ret );
3250 }
3251
3252 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3253 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3254 ssl->conf->f_rng, ssl->conf->p_rng );
3255 if( ret != 0 )
3256 {
3257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3258 return( ret );
3259 }
3260 }
3261 else
3262#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003263 {
3264 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003269 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3271 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
3273 ssl->state++;
3274
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003275 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 return( ret );
3279 }
3280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
3283 return( 0 );
3284}
3285
Gilles Peskineeccd8882020-03-10 12:19:08 +01003286#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003287MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003290 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003291 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003299 return( ret );
3300 }
3301
Hanno Becker77adddc2019-02-07 12:32:43 +00003302 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003305 ssl->state++;
3306 return( 0 );
3307 }
3308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003311}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003312#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003313MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003317 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003318 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003319 size_t n = 0, offset = 0;
3320 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003321 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003323 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003324 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003325#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3326 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3327#else
3328 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3329#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003332
Gilles Peskineeccd8882020-03-10 12:19:08 +01003333#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003334 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003335 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003336 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003337 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003338 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003339#endif
3340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003344 return( ret );
3345 }
3346
Hanno Becker77adddc2019-02-07 12:32:43 +00003347 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003350 ssl->state++;
3351 return( 0 );
3352 }
3353
Jerry Yufb28b882022-01-28 11:05:58 +08003354 if( ssl->handshake->client_auth == 0 ||
3355 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 {
Johan Pascala89ca862020-08-25 10:03:19 +02003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3358 ssl->state++;
3359 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 }
3361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 }
3367
3368 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003369 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003371#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003372 if( ssl->handshake->ecrs_enabled )
3373 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3374
3375sign:
3376#endif
3377
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003378 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
Ronald Cron90915f22022-03-07 11:11:36 +01003380 /*
3381 * digitally-signed struct {
3382 * opaque handshake_messages[handshake_messages_length];
3383 * };
3384 *
3385 * Taking shortcut here. We assume that the server always allows the
3386 * PRF Hash function and has sent it in the allowed signature
3387 * algorithms list received in the Certificate Request message.
3388 *
3389 * Until we encounter a server that does not, we will take this
3390 * shortcut.
3391 *
3392 * Reason: Otherwise we should have running hashes for SHA512 and
3393 * SHA224 in order to satisfy 'weird' needs from the server
3394 * side.
3395 */
3396 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003397 {
Ronald Cron90915f22022-03-07 11:11:36 +01003398 md_alg = MBEDTLS_MD_SHA384;
3399 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003400 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003401 else
Paul Bakker577e0062013-08-28 11:57:20 +02003402 {
Ronald Cron90915f22022-03-07 11:11:36 +01003403 md_alg = MBEDTLS_MD_SHA256;
3404 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003405 }
Ronald Cron90915f22022-03-07 11:11:36 +01003406 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3407
3408 /* Info from md_alg will be used instead */
3409 hashlen = 0;
3410 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003411
Gilles Peskineeccd8882020-03-10 12:19:08 +01003412#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003413 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003414 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003415#endif
3416
3417 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3418 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003419 ssl->out_msg + 6 + offset,
3420 out_buf_len - 6 - offset,
3421 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003422 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003425#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003426 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3427 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3428#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003429 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003430 }
Paul Bakker926af752012-11-23 13:38:07 +01003431
Joe Subbiani6dd73642021-07-19 11:56:54 +01003432 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
Paul Bakker1ef83d62012-04-11 12:09:53 +00003434 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3436 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003437
3438 ssl->state++;
3439
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003440 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 return( ret );
3444 }
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
Paul Bakkered27a042013-04-18 22:46:23 +02003448 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003450#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003453MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003455{
Janos Follath865b3eb2019-12-16 11:46:15 +00003456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003457 uint32_t lifetime;
3458 size_t ticket_len;
3459 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003460 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003463
Hanno Becker327c93b2018-08-15 13:56:18 +01003464 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003467 return( ret );
3468 }
3469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003473 mbedtls_ssl_send_alert_message(
3474 ssl,
3475 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3476 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003478 }
3479
3480 /*
3481 * struct {
3482 * uint32 ticket_lifetime_hint;
3483 * opaque ticket<0..2^16-1>;
3484 * } NewSessionTicket;
3485 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003486 * 0 . 3 ticket_lifetime_hint
3487 * 4 . 5 ticket_len (n)
3488 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3491 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003494 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3495 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003496 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003497 }
3498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003500
Philippe Antoineb5b25432018-05-11 11:06:29 +02003501 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3502 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003503
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003504 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003511 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003512 }
3513
Paul Elliottd48d5c62021-01-07 14:47:05 +00003514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003515
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003516 /* We're not waiting for a NewSessionTicket message any more */
3517 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003519
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003520 /*
3521 * Zero-length ticket means the server changed his mind and doesn't want
3522 * to send a ticket after all, so just forget it
3523 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003524 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003525 return( 0 );
3526
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003527 if( ssl->session != NULL && ssl->session->ticket != NULL )
3528 {
3529 mbedtls_platform_zeroize( ssl->session->ticket,
3530 ssl->session->ticket_len );
3531 mbedtls_free( ssl->session->ticket );
3532 ssl->session->ticket = NULL;
3533 ssl->session->ticket_len = 0;
3534 }
3535
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003536 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3537 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003539 ssl->session_negotiate->ticket = NULL;
3540 ssl->session_negotiate->ticket_len = 0;
3541
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003542 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003543 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003545 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3546 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003547 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003548 }
3549
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003550 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003551
3552 ssl->session_negotiate->ticket = ticket;
3553 ssl->session_negotiate->ticket_len = ticket_len;
3554 ssl->session_negotiate->ticket_lifetime = lifetime;
3555
3556 /*
3557 * RFC 5077 section 3.4:
3558 * "If the client receives a session ticket from the server, then it
3559 * discards any Session ID that was sent in the ServerHello."
3560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003562 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003565
3566 return( 0 );
3567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003569
Paul Bakker5121ce52009-01-03 21:22:43 +00003570/*
Paul Bakker1961b702013-01-25 14:49:24 +01003571 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003574{
3575 int ret = 0;
3576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003578 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3580 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003581 ssl->handshake->new_session_ticket != 0 )
3582 {
Jerry Yua357cf42022-07-12 05:36:45 +00003583 ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003584 }
3585#endif
3586
Paul Bakker1961b702013-01-25 14:49:24 +01003587 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 case MBEDTLS_SSL_HELLO_REQUEST:
3590 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 break;
3592
Paul Bakker1961b702013-01-25 14:49:24 +01003593 /*
3594 * ==> ClientHello
3595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003597 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003598 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
Paul Bakker1961b702013-01-25 14:49:24 +01003600 /*
3601 * <== ServerHello
3602 * Certificate
3603 * ( ServerKeyExchange )
3604 * ( CertificateRequest )
3605 * ServerHelloDone
3606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003608 ret = ssl_parse_server_hello( ssl );
3609 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3612 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003613 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003616 ret = ssl_parse_server_key_exchange( ssl );
3617 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003620 ret = ssl_parse_certificate_request( ssl );
3621 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003624 ret = ssl_parse_server_hello_done( ssl );
3625 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
Paul Bakker1961b702013-01-25 14:49:24 +01003627 /*
3628 * ==> ( Certificate/Alert )
3629 * ClientKeyExchange
3630 * ( CertificateVerify )
3631 * ChangeCipherSpec
3632 * Finished
3633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3635 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003636 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003639 ret = ssl_write_client_key_exchange( ssl );
3640 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003643 ret = ssl_write_certificate_verify( ssl );
3644 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3647 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003648 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 case MBEDTLS_SSL_CLIENT_FINISHED:
3651 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003652 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
Paul Bakker1961b702013-01-25 14:49:24 +01003654 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003655 * <== ( NewSessionTicket )
3656 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003657 * Finished
3658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua357cf42022-07-12 05:36:45 +00003660 case MBEDTLS_SSL_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003661 ret = ssl_parse_new_session_ticket( ssl );
3662 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003663#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3666 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003667 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 case MBEDTLS_SSL_SERVER_FINISHED:
3670 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003671 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 case MBEDTLS_SSL_FLUSH_BUFFERS:
3674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3675 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003676 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3679 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003680 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003681
Paul Bakker1961b702013-01-25 14:49:24 +01003682 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
3687 return( ret );
3688}
Jerry Yuc5aef882021-12-23 20:15:02 +08003689
Jerry Yufb4b6472022-01-27 15:03:26 +08003690#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */