blob: 50d08267fe282a2eb4ce172b02f01bf12e010a2d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020026#else
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020028#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010029#define mbedtls_free free
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020030#endif
31
SimonBd5800b72016-04-26 07:43:27 +010032#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010033#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000034#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/debug.h"
36#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010038
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020041#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000042#endif /* MBEDTLS_USE_PSA_CRYPTO */
43
SimonBd5800b72016-04-26 07:43:27 +010044#include <string.h>
45
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020046#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010049#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050053#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020054#endif
55
Gilles Peskineeccd8882020-03-10 12:19:08 +010056#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Ronald Crond491c2d2022-02-19 18:30:46 +010057int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010058{
59 if( conf->psk_identity == NULL ||
60 conf->psk_identity_len == 0 )
61 {
62 return( 0 );
63 }
64
65 if( conf->psk != NULL && conf->psk_len != 0 )
66 return( 1 );
67
68#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020069 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010070 return( 1 );
71#endif /* MBEDTLS_USE_PSA_CRYPTO */
72
73 return( 0 );
74}
Hanno Beckerdfab8e22018-10-23 11:59:34 +010075
76#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker520224e2018-10-26 11:38:07 +010077static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
Hanno Beckerdfab8e22018-10-23 11:59:34 +010078{
79 if( conf->psk_identity == NULL ||
80 conf->psk_identity_len == 0 )
81 {
82 return( 0 );
83 }
84
85 if( conf->psk != NULL && conf->psk_len != 0 )
86 return( 1 );
87
88 return( 0 );
89}
90#endif /* MBEDTLS_USE_PSA_CRYPTO */
91
Gilles Peskineeccd8882020-03-10 12:19:08 +010092#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +010095static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
96 unsigned char *buf,
97 const unsigned char *end,
98 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010099{
100 unsigned char *p = buf;
101
102 *olen = 0;
103
Hanno Becker40f8b512017-10-12 14:58:55 +0100104 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
105 * initial ClientHello, in which case also adding the renegotiation
106 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +0100108 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100109
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100110 MBEDTLS_SSL_DEBUG_MSG( 3,
111 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100112
Hanno Becker261602c2017-04-12 14:54:42 +0100113 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100114
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115 /*
116 * Secure renegotiation
117 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100118 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
119 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120
121 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100122 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
123 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124
125 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
126
127 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100128
129 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100132
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200133#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100134 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100135
Hanno Becker261602c2017-04-12 14:54:42 +0100136static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
137 unsigned char *buf,
138 const unsigned char *end,
139 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100140{
141 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100142 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100143
144 *olen = 0;
145
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100146 MBEDTLS_SSL_DEBUG_MSG( 3,
147 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100148 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100149
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100150 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
151 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152
153 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100154 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200155
156 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100158
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200159 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100160
161 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100162}
Darryl Green11999bb2018-03-13 15:22:58 +0000163#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100164 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100165
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200166#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100167static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
168 unsigned char *buf,
169 const unsigned char *end,
170 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200171{
Janos Follath865b3eb2019-12-16 11:46:15 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200173 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200174 size_t kkpp_len;
175
176 *olen = 0;
177
178 /* Skip costly extension if we can't use EC J-PAKE anyway */
179 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100180 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200181
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100182 MBEDTLS_SSL_DEBUG_MSG( 3,
183 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200184
Hanno Becker261602c2017-04-12 14:54:42 +0100185 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200186
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100187 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
188 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200189
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200190 /*
191 * We may need to send ClientHello multiple times for Hello verification.
192 * We don't want to compute fresh values every time (both for performance
193 * and consistency reasons), so cache the extension content.
194 */
195 if( ssl->handshake->ecjpake_cache == NULL ||
196 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200197 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
199
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200200 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100201 p + 2, end - p - 2, &kkpp_len,
202 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200203 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200204 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100205 MBEDTLS_SSL_DEBUG_RET( 1 ,
206 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100207 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200208 }
209
210 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
211 if( ssl->handshake->ecjpake_cache == NULL )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100214 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200215 }
216
217 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
218 ssl->handshake->ecjpake_cache_len = kkpp_len;
219 }
220 else
221 {
222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
223
224 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100225 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200226
227 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200228 }
229
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100230 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
231 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200232
233 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100234
235 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200236}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200237#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000238
Hanno Beckera0e20d02019-05-15 14:03:01 +0100239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100240static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
241 unsigned char *buf,
242 const unsigned char *end,
243 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100244{
245 unsigned char *p = buf;
246 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100247
248 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100249 * Quoting draft-ietf-tls-dtls-connection-id-05
250 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100251 *
252 * struct {
253 * opaque cid<0..2^8-1>;
254 * } ConnectionId;
255 */
256
257 *olen = 0;
258 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
259 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
260 {
Hanno Becker261602c2017-04-12 14:54:42 +0100261 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100262 }
263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
264
265 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
266 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100267 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100268
269 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100270 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
271 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100272 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100273 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
274 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100275
276 *p++ = (uint8_t) ssl->own_cid_len;
277 memcpy( p, ssl->own_cid, ssl->own_cid_len );
278
279 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100280
281 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100282}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100283#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100286static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
287 unsigned char *buf,
288 const unsigned char *end,
289 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200290{
291 unsigned char *p = buf;
292
Simon Butcher0fc94e92015-09-28 20:52:04 +0100293 *olen = 0;
294
Hanno Becker261602c2017-04-12 14:54:42 +0100295 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
296 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200297
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100298 MBEDTLS_SSL_DEBUG_MSG( 3,
299 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200300
Hanno Becker261602c2017-04-12 14:54:42 +0100301 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100302
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100303 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
304 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200305
306 *p++ = 0x00;
307 *p++ = 1;
308
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200309 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200310
311 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100312
313 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100318static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
319 unsigned char *buf,
320 const unsigned char *end,
321 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322{
323 unsigned char *p = buf;
324
Simon Butcher0fc94e92015-09-28 20:52:04 +0100325 *olen = 0;
326
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100327 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100328 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100329
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100330 MBEDTLS_SSL_DEBUG_MSG( 3,
331 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100332
Hanno Becker261602c2017-04-12 14:54:42 +0100333 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100334
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100335 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
336 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100337
338 *p++ = 0x00;
339 *p++ = 0x00;
340
341 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100342
343 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100348static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
349 unsigned char *buf,
350 const unsigned char *end,
351 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200352{
353 unsigned char *p = buf;
354
Simon Butcher0fc94e92015-09-28 20:52:04 +0100355 *olen = 0;
356
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100357 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100358 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200359
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100360 MBEDTLS_SSL_DEBUG_MSG( 3,
361 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200362
Hanno Becker261602c2017-04-12 14:54:42 +0100363 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100364
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100365 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
366 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200367
368 *p++ = 0x00;
369 *p++ = 0x00;
370
371 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100372
373 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100378static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
379 unsigned char *buf,
380 const unsigned char *end,
381 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200382{
383 unsigned char *p = buf;
384 size_t tlen = ssl->session_negotiate->ticket_len;
385
Simon Butcher0fc94e92015-09-28 20:52:04 +0100386 *olen = 0;
387
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200388 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100389 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200390
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100391 MBEDTLS_SSL_DEBUG_MSG( 3,
392 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
Hanno Becker261602c2017-04-12 14:54:42 +0100394 /* The addition is safe here since the ticket length is 16 bit. */
395 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100396
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100397 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
398 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100400 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
401 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200402
403 *olen = 4;
404
Simon Butchered997662015-09-28 02:14:30 +0100405 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100406 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200407
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100408 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000409 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200410
411 memcpy( p, ssl->session_negotiate->ticket, tlen );
412
413 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100414
415 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200418
Ron Eldora9788042018-12-05 11:04:31 +0200419#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200420static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200421 unsigned char *buf,
422 const unsigned char *end,
423 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100424{
425 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200426 size_t protection_profiles_index = 0, ext_len = 0;
427 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428
429 *olen = 0;
430
Johan Pascalc3ccd982020-10-28 17:18:18 +0100431 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
432 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
433 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100434 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200435 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100436 }
437
Ron Eldora9788042018-12-05 11:04:31 +0200438 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100439 * uint8 SRTPProtectionProfile[2];
440 *
441 * struct {
442 * SRTPProtectionProfiles SRTPProtectionProfiles;
443 * opaque srtp_mki<0..255>;
444 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100445 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100446 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200447 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200448 {
449 mki_len = ssl->dtls_srtp_info.mki_len;
450 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300451 /* Extension length = 2 bytes for profiles length,
452 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
453 * 1 byte for srtp_mki vector length and the mki_len value
454 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200455 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
456
Johan Pascal77696ee2020-09-22 21:49:40 +0200457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
458
459 /* Check there is room in the buffer for the extension + 4 bytes
460 * - the extension tag (2 bytes)
461 * - the extension length (2 bytes)
462 */
463 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
464
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100465 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
466 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200467
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100468 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
469 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100470
Ron Eldor3adb9922017-12-21 10:15:08 +0200471 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200472 /* micro-optimization:
473 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
474 * which is lower than 127, so the upper byte of the length is always 0
475 * For the documentation, the more generic code is left in comments
476 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
477 * >> 8 ) & 0xFF );
478 */
479 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100480 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100481
Ron Eldoref72faf2018-07-12 11:54:20 +0300482 for( protection_profiles_index=0;
483 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
484 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100485 {
Johan Pascal43f94902020-09-22 12:25:52 +0200486 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200487 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200488 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200489 {
490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
491 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100492 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
493 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200494 }
495 else
496 {
497 /*
498 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200499 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200500 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200501 MBEDTLS_SSL_DEBUG_MSG( 3,
502 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200503 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200504 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
505 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200506 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100507 }
508 }
509
Ron Eldor591f1622018-01-22 12:30:04 +0200510 *p++ = mki_len & 0xFF;
511
512 if( mki_len != 0 )
513 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200514 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200515 /*
516 * Increment p to point to the current position.
517 */
518 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300519 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
520 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200521 }
522
Ron Eldoref72faf2018-07-12 11:54:20 +0300523 /*
524 * total extension length: extension type (2 bytes)
525 * + extension length (2 bytes)
526 * + protection profile length (2 bytes)
527 * + 2 * number of protection profiles
528 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200529 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300530 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200531 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200532
533 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100534}
535#endif /* MBEDTLS_SSL_DTLS_SRTP */
536
Ronald Cron4079abc2022-02-20 10:35:26 +0100537int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
538 unsigned char *buf,
539 const unsigned char *end,
540 int uses_ec,
541 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100542{
543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
544 unsigned char *p = buf;
545 size_t ext_len = 0;
546
547 (void) ssl;
548 (void) end;
549 (void) uses_ec;
550 (void) ret;
551 (void) ext_len;
552
553 *out_len = 0;
554
555 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
556 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
557#if defined(MBEDTLS_SSL_RENEGOTIATION)
558 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
559 {
560 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
561 return( ret );
562 }
563 p += ext_len;
564#endif
565
566#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
567 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
568 if( uses_ec )
569 {
570 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
571 &ext_len ) ) != 0 )
572 {
573 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
574 return( ret );
575 }
576 p += ext_len;
577 }
578#endif
579
580#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
581 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
582 {
583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
584 return( ret );
585 }
586 p += ext_len;
587#endif
588
589#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
590 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
591 {
592 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
593 return( ret );
594 }
595 p += ext_len;
596#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
597
598#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
599 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
600 &ext_len ) ) != 0 )
601 {
602 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
603 return( ret );
604 }
605 p += ext_len;
606#endif
607
608#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
609 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
610 {
611 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
612 return( ret );
613 }
614 p += ext_len;
615#endif
616
617#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
618 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
621 return( ret );
622 }
623 p += ext_len;
624#endif
625
626#if defined(MBEDTLS_SSL_DTLS_SRTP)
627 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
628 {
629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
630 return( ret );
631 }
632 p += ext_len;
633#endif
634
635#if defined(MBEDTLS_SSL_SESSION_TICKETS)
636 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
637 {
638 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
639 return( ret );
640 }
641 p += ext_len;
642#endif
643
644 *out_len = p - buf;
645
646 return( 0 );
647}
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200650 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000651 size_t len )
652{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_SSL_RENEGOTIATION)
654 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000655 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100656 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000657 if( len != 1 + ssl->verify_data_len * 2 ||
658 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200659 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100660 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200661 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100662 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100665 mbedtls_ssl_send_alert_message(
666 ssl,
667 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
668 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100669 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000670 }
671 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100674 {
675 if( len != 1 || buf[0] != 0x00 )
676 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200677 MBEDTLS_SSL_DEBUG_MSG( 1,
678 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100679 mbedtls_ssl_send_alert_message(
680 ssl,
681 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
682 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100683 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100684 }
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100687 }
Paul Bakker48916f92012-09-16 19:57:18 +0000688
689 return( 0 );
690}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
693static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200694 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200695 size_t len )
696{
697 /*
698 * server should use the extension only if we did,
699 * and if so the server's value should match ours (and len is always 1)
700 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200701 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200702 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200703 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200704 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100705 MBEDTLS_SSL_DEBUG_MSG( 1,
706 ( "non-matching max fragment length extension" ) );
707 mbedtls_ssl_send_alert_message(
708 ssl,
709 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100710 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
711 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200712 }
713
714 return( 0 );
715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000717
Hanno Beckera0e20d02019-05-15 14:03:01 +0100718#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +0100719static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
720 const unsigned char *buf,
721 size_t len )
722{
723 size_t peer_cid_len;
724
725 if( /* CID extension only makes sense in DTLS */
726 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
727 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100728 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100729 {
Hanno Becker22626482019-05-03 12:46:59 +0100730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100732 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100733 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100734 }
735
736 if( len == 0 )
737 {
738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100742 }
743
744 peer_cid_len = *buf++;
745 len--;
746
747 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
748 {
Hanno Becker22626482019-05-03 12:46:59 +0100749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100750 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100751 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
752 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100753 }
754
755 if( len != peer_cid_len )
756 {
Hanno Becker22626482019-05-03 12:46:59 +0100757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100758 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100759 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
760 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100761 }
762
Hanno Becker5a299902019-05-03 12:47:49 +0100763 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100764 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
765 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
766
767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
768 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
769
Hanno Beckera8373a12019-04-26 15:37:26 +0100770 return( 0 );
771}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100772#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
775static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100776 const unsigned char *buf,
777 size_t len )
778{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200779 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100780 len != 0 )
781 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100782 MBEDTLS_SSL_DEBUG_MSG( 1,
783 ( "non-matching encrypt-then-MAC extension" ) );
784 mbedtls_ssl_send_alert_message(
785 ssl,
786 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100787 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100788 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100789 }
790
791 ((void) buf);
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100794
795 return( 0 );
796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
800static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200801 const unsigned char *buf,
802 size_t len )
803{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200804 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200805 len != 0 )
806 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100807 MBEDTLS_SSL_DEBUG_MSG( 1,
808 ( "non-matching extended master secret extension" ) );
809 mbedtls_ssl_send_alert_message(
810 ssl,
811 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100812 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
813 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200814 }
815
816 ((void) buf);
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200819
820 return( 0 );
821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
825static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200826 const unsigned char *buf,
827 size_t len )
828{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200829 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200830 len != 0 )
831 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100832 MBEDTLS_SSL_DEBUG_MSG( 1,
833 ( "non-matching session ticket extension" ) );
834 mbedtls_ssl_send_alert_message(
835 ssl,
836 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100837 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
838 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200839 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200840
841 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200842
843 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200844
845 return( 0 );
846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200848
Robert Cragie136884c2015-10-02 13:34:31 +0100849#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100850 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200852 const unsigned char *buf,
853 size_t len )
854{
855 size_t list_size;
856 const unsigned char *p;
857
Philippe Antoine747fd532018-05-30 09:13:21 +0200858 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200861 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
862 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100863 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200864 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200865 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200866
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200867 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200868 while( list_size > 0 )
869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
871 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200872 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200873#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
874 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200875 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200876#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
877 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100878#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200879 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
880 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200883 return( 0 );
884 }
885
886 list_size--;
887 p++;
888 }
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200891 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
892 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100893 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200894}
Darryl Green11999bb2018-03-13 15:22:58 +0000895#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100896 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200897
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200898#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
899static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
900 const unsigned char *buf,
901 size_t len )
902{
Janos Follath865b3eb2019-12-16 11:46:15 +0000903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200904
Hanno Beckere694c3e2017-12-27 21:34:08 +0000905 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
907 {
908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
909 return( 0 );
910 }
911
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200912 /* If we got here, we no longer need our cached extension */
913 mbedtls_free( ssl->handshake->ecjpake_cache );
914 ssl->handshake->ecjpake_cache = NULL;
915 ssl->handshake->ecjpake_cache_len = 0;
916
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200917 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
918 buf, len ) ) != 0 )
919 {
920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100921 mbedtls_ssl_send_alert_message(
922 ssl,
923 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
924 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200925 return( ret );
926 }
927
928 return( 0 );
929}
930#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_ALPN)
933static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200934 const unsigned char *buf, size_t len )
935{
936 size_t list_len, name_len;
937 const char **p;
938
939 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200940 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200941 {
942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100943 mbedtls_ssl_send_alert_message(
944 ssl,
945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100946 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
947 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200948 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200949
950 /*
951 * opaque ProtocolName<1..2^8-1>;
952 *
953 * struct {
954 * ProtocolName protocol_name_list<2..2^16-1>
955 * } ProtocolNameList;
956 *
957 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
958 */
959
960 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
961 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200962 {
963 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100965 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200966 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200967
968 list_len = ( buf[0] << 8 ) | buf[1];
969 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200970 {
971 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
972 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100973 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200974 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200975
976 name_len = buf[2];
977 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200978 {
979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100981 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200982 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200983
984 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200985 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200986 {
987 if( name_len == strlen( *p ) &&
988 memcmp( buf + 3, *p, name_len ) == 0 )
989 {
990 ssl->alpn_chosen = *p;
991 return( 0 );
992 }
993 }
994
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200996 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
997 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100998 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001001
Johan Pascalb62bb512015-12-03 21:56:45 +01001002#if defined(MBEDTLS_SSL_DTLS_SRTP)
1003static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001004 const unsigned char *buf,
1005 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001006{
Johan Pascal43f94902020-09-22 12:25:52 +02001007 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001008 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001009 uint16_t server_protection_profile_value = 0;
1010
1011 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001012 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1013 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1014 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001015 return( 0 );
1016
Ron Eldora9788042018-12-05 11:04:31 +02001017 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001018 * uint8 SRTPProtectionProfile[2];
1019 *
1020 * struct {
1021 * SRTPProtectionProfiles SRTPProtectionProfiles;
1022 * opaque srtp_mki<0..255>;
1023 * } UseSRTPData;
1024
1025 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1026 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001027 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001028 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001029 {
1030 mki_len = ssl->dtls_srtp_info.mki_len;
1031 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001032
Ron Eldoref72faf2018-07-12 11:54:20 +03001033 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001034 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1035 * + protection profile (2 bytes)
1036 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001037 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001038 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001039 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001040 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001041
1042 /*
1043 * get the server protection profile
1044 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001045
1046 /*
1047 * protection profile length must be 0x0002 as we must have only
1048 * one protection profile in server Hello
1049 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001050 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001051 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001052
1053 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001054 server_protection = mbedtls_ssl_check_srtp_profile_value(
1055 server_protection_profile_value );
1056 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001057 {
Johan Pascal43f94902020-09-22 12:25:52 +02001058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1059 mbedtls_ssl_get_srtp_profile_as_string(
1060 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001061 }
1062
Johan Pascal43f94902020-09-22 12:25:52 +02001063 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001064
Johan Pascalb62bb512015-12-03 21:56:45 +01001065 /*
1066 * Check we have the server profile in our list
1067 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001068 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001069 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001070 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1071 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001072 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1074 mbedtls_ssl_get_srtp_profile_as_string(
1075 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001076 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001077 }
1078 }
1079
Ron Eldor591f1622018-01-22 12:30:04 +02001080 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001081 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001082 {
1083 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001084 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001085 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001086 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001087
1088 /* If server does not use mki in its reply, make sure the client won't keep
1089 * one as negotiated */
1090 if( len == 5 )
1091 {
1092 ssl->dtls_srtp_info.mki_len = 0;
1093 }
1094
Ron Eldoref72faf2018-07-12 11:54:20 +03001095 /*
1096 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001097 * If the client detects a nonzero-length MKI in the server's response
1098 * that is different than the one the client offered, then the client
1099 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1100 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001101 if( len > 5 && ( buf[4] != mki_len ||
1102 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001103 {
1104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1105 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001106 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001107 }
Ron Eldorb4655392018-07-05 18:25:39 +03001108#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001109 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001110 {
Ron Eldora9788042018-12-05 11:04:31 +02001111 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1112 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001113 }
1114#endif
Ron Eldora9788042018-12-05 11:04:31 +02001115 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001116}
1117#endif /* MBEDTLS_SSL_DTLS_SRTP */
1118
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001119/*
1120 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_SSL_PROTO_DTLS)
1123static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126 int major_ver, minor_ver;
1127 unsigned char cookie_len;
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130
Gilles Peskineb64bf062019-09-27 14:02:44 +02001131 /* Check that there is enough room for:
1132 * - 2 bytes of version
1133 * - 1 byte of cookie_len
1134 */
1135 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1136 {
1137 MBEDTLS_SSL_DEBUG_MSG( 1,
1138 ( "incoming HelloVerifyRequest message is too short" ) );
1139 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1140 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001141 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001142 }
1143
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001144 /*
1145 * struct {
1146 * ProtocolVersion server_version;
1147 * opaque cookie<0..2^8-1>;
1148 * } HelloVerifyRequest;
1149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001151 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001152 p += 2;
1153
TRodziewicz2d8800e2021-05-13 19:14:19 +02001154 /*
1155 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1156 * even is lower than our min version.
1157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
TRodziewiczb5850c52021-05-13 17:11:23 +02001159 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001160 major_ver > ssl->conf->max_major_ver ||
1161 minor_ver > ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1166 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001167
Hanno Beckerbc000442021-06-24 09:18:19 +01001168 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001169 }
1170
1171 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001172 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1173 {
1174 MBEDTLS_SSL_DEBUG_MSG( 1,
1175 ( "cookie length does not match incoming message size" ) );
1176 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1177 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001178 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001179 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001180 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001181
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001182 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001183
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001184 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1185 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001186 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001188 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001189 }
1190
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001191 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001192 ssl->handshake->verify_cookie_len = cookie_len;
1193
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001194 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1196 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001201
1202 return( 0 );
1203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001207{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001208 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001209 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001210 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001211 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001212 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001214 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001215#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001216 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
Hanno Becker327c93b2018-08-15 13:56:18 +01001221 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001223 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 return( ret );
1226 }
1227
Hanno Becker79594fd2019-05-08 09:38:41 +01001228 buf = ssl->in_msg;
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_RENEGOTIATION)
1233 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001234 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001235 ssl->renego_records_seen++;
1236
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001237 if( ssl->conf->renego_max_records >= 0 &&
1238 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001239 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001240 MBEDTLS_SSL_DEBUG_MSG( 1,
1241 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001243 }
1244
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001245 MBEDTLS_SSL_DEBUG_MSG( 1,
1246 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001247
1248 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001254 mbedtls_ssl_send_alert_message(
1255 ssl,
1256 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1257 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 }
1260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001268 return( ssl_parse_hello_verify_request( ssl ) );
1269 }
1270 else
1271 {
1272 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001273 mbedtls_free( ssl->handshake->cookie );
1274 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001275 ssl->handshake->verify_cookie_len = 0;
1276 }
1277 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1281 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001284 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1285 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001286 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 }
1288
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001289 /*
1290 * 0 . 1 server_version
1291 * 2 . 33 random (maybe including 4 bytes of Unix time)
1292 * 34 . 34 session_id length = n
1293 * 35 . 34+n session_id
1294 * 35+n . 36+n cipher_suite
1295 * 37+n . 37+n compression_method
1296 *
1297 * 38+n . 39+n extensions length (optional)
1298 * 40+n . .. extensions
1299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
1303 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001304 ssl->conf->transport, buf + 0 );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001305 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001307 if( ssl->major_ver < ssl->conf->min_major_ver ||
1308 ssl->minor_ver < ssl->conf->min_minor_ver ||
1309 ssl->major_ver > ssl->conf->max_major_ver ||
1310 ssl->minor_ver > ssl->conf->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001311 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001312 MBEDTLS_SSL_DEBUG_MSG( 1,
1313 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1314 ssl->conf->min_major_ver,
1315 ssl->conf->min_minor_ver,
1316 ssl->major_ver, ssl->minor_ver,
1317 ssl->conf->max_major_ver,
1318 ssl->conf->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1321 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001322
Hanno Beckerbc000442021-06-24 09:18:19 +01001323 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001324 }
1325
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001327 ( (unsigned long) buf[2] << 24 ) |
1328 ( (unsigned long) buf[3] << 16 ) |
1329 ( (unsigned long) buf[4] << 8 ) |
1330 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001332 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001334 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Paul Bakker48916f92012-09-16 19:57:18 +00001338 if( n > 32 )
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001341 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1342 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001343 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001344 }
1345
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001346 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001348 ext_len = ( ( buf[38 + n] << 8 )
1349 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001355 mbedtls_ssl_send_alert_message(
1356 ssl,
1357 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1358 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001359 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001362 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001363 {
1364 ext_len = 0;
1365 }
1366 else
1367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001369 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1370 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001371 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001374 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001375 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001376
1377 /*
1378 * Read and check compression
1379 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001380 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001382 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001383 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001384 MBEDTLS_SSL_DEBUG_MSG( 1,
1385 ( "server hello, bad compression: %d", comp ) );
1386 mbedtls_ssl_send_alert_message(
1387 ssl,
1388 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1389 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001391 }
1392
Paul Bakker380da532012-04-18 16:10:25 +00001393 /*
1394 * Initialize update checksum functions
1395 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001396 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1397 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001398 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001399 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001400 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001401 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1402 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001404 }
Paul Bakker380da532012-04-18 16:10:25 +00001405
Hanno Beckere694c3e2017-12-27 21:34:08 +00001406 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001407
Paul Elliottd48d5c62021-01-07 14:47:05 +00001408 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
1411 /*
1412 * Check if the session can be resumed
1413 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001414 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_SSL_RENEGOTIATION)
1416 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001417#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001418 ssl->session_negotiate->ciphersuite != i ||
1419 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001420 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001421 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 {
1423 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001424 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001426 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001427#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001428 ssl->session_negotiate->ciphersuite = i;
1429 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001430 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001431 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 }
1433 else
1434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001440 mbedtls_ssl_send_alert_message(
1441 ssl,
1442 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1443 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001444 return( ret );
1445 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001449 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Paul Elliott3891caf2020-12-17 18:42:40 +00001451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1453 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Andrzej Kurek03bac442018-04-25 05:06:07 -04001455 /*
1456 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001457 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 i = 0;
1459 while( 1 )
1460 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001461 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001464 mbedtls_ssl_send_alert_message(
1465 ssl,
1466 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1467 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001468 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 }
1470
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001471 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001472 ssl->session_negotiate->ciphersuite )
1473 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 }
1477
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001478 suite_info = mbedtls_ssl_ciphersuite_from_id(
1479 ssl->session_negotiate->ciphersuite );
Ronald Cron8fdad9e2022-03-30 22:04:30 +02001480 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->minor_ver,
1481 ssl->minor_ver ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001482 {
1483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001484 mbedtls_ssl_send_alert_message(
1485 ssl,
1486 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001487 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1488 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001489 }
1490
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001491 MBEDTLS_SSL_DEBUG_MSG( 3,
1492 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001493
Gilles Peskineeccd8882020-03-10 12:19:08 +01001494#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001495 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1496 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1497 {
1498 ssl->handshake->ecrs_enabled = 1;
1499 }
1500#endif
1501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00001503 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001506 mbedtls_ssl_send_alert_message(
1507 ssl,
1508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1509 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001510 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 }
Paul Bakker48916f92012-09-16 19:57:18 +00001512 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001514 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001515
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001516 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001517 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001518
Paul Bakker48916f92012-09-16 19:57:18 +00001519 while( ext_len )
1520 {
1521 unsigned int ext_id = ( ( ext[0] << 8 )
1522 | ( ext[1] ) );
1523 unsigned int ext_size = ( ( ext[2] << 8 )
1524 | ( ext[3] ) );
1525
1526 if( ext_size + 4 > ext_len )
1527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001529 mbedtls_ssl_send_alert_message(
1530 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1531 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001532 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001533 }
1534
1535 switch( ext_id )
1536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1539#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001540 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001541#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001542
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001543 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1544 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001545 return( ret );
1546
1547 break;
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1550 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001551 MBEDTLS_SSL_DEBUG_MSG( 3,
1552 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001553
1554 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1555 ext + 4, ext_size ) ) != 0 )
1556 {
1557 return( ret );
1558 }
1559
1560 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001562
Hanno Beckera0e20d02019-05-15 14:03:01 +01001563#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001564 case MBEDTLS_TLS_EXT_CID:
1565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1566
1567 if( ( ret = ssl_parse_cid_ext( ssl,
1568 ext + 4,
1569 ext_size ) ) != 0 )
1570 {
1571 return( ret );
1572 }
1573
1574 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001575#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1578 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001580
1581 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1582 ext + 4, ext_size ) ) != 0 )
1583 {
1584 return( ret );
1585 }
1586
1587 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1591 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001592 MBEDTLS_SSL_DEBUG_MSG( 3,
1593 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001594
1595 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1596 ext + 4, ext_size ) ) != 0 )
1597 {
1598 return( ret );
1599 }
1600
1601 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1605 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001607
1608 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1609 ext + 4, ext_size ) ) != 0 )
1610 {
1611 return( ret );
1612 }
1613
1614 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001616
Robert Cragie136884c2015-10-02 13:34:31 +01001617#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001618 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001620 MBEDTLS_SSL_DEBUG_MSG( 3,
1621 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001622
1623 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1624 ext + 4, ext_size ) ) != 0 )
1625 {
1626 return( ret );
1627 }
1628
1629 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001630#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1631 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001632
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001633#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1634 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1636
1637 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1638 ext + 4, ext_size ) ) != 0 )
1639 {
1640 return( ret );
1641 }
1642
1643 break;
1644#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#if defined(MBEDTLS_SSL_ALPN)
1647 case MBEDTLS_TLS_EXT_ALPN:
1648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001649
Johan Pascal275874b2020-10-27 10:43:53 +01001650 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1651 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001652
1653 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001655
Johan Pascalb62bb512015-12-03 21:56:45 +01001656#if defined(MBEDTLS_SSL_DTLS_SRTP)
1657 case MBEDTLS_TLS_EXT_USE_SRTP:
1658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1659
Johan Pascalc3ccd982020-10-28 17:18:18 +01001660 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1661 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001662
1663 break;
1664#endif /* MBEDTLS_SSL_DTLS_SRTP */
1665
Paul Bakker48916f92012-09-16 19:57:18 +00001666 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001667 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001668 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001669 }
1670
1671 ext_len -= 4 + ext_size;
1672 ext += 4 + ext_size;
1673
1674 if( ext_len > 0 && ext_len < 4 )
1675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001677 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001678 }
1679 }
1680
1681 /*
1682 * Renegotiation security checks
1683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001685 ssl->conf->allow_legacy_renegotiation ==
1686 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001687 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001688 MBEDTLS_SSL_DEBUG_MSG( 1,
1689 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001690 handshake_failure = 1;
1691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_SSL_RENEGOTIATION)
1693 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1694 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001695 renegotiation_info_seen == 0 )
1696 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001697 MBEDTLS_SSL_DEBUG_MSG( 1,
1698 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001699 handshake_failure = 1;
1700 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1702 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001703 ssl->conf->allow_legacy_renegotiation ==
1704 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001707 handshake_failure = 1;
1708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1710 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001711 renegotiation_info_seen == 1 )
1712 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001713 MBEDTLS_SSL_DEBUG_MSG( 1,
1714 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001715 handshake_failure = 1;
1716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001718
1719 if( handshake_failure == 1 )
1720 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001721 mbedtls_ssl_send_alert_message(
1722 ssl,
1723 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1724 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001725 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
1730 return( 0 );
1731}
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1734 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001735static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1736 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001737 unsigned char *end )
1738{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001740 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001741
Paul Bakker29e1f122013-04-16 13:07:56 +02001742 /*
1743 * Ephemeral DH parameters:
1744 *
1745 * struct {
1746 * opaque dh_p<1..2^16-1>;
1747 * opaque dh_g<1..2^16-1>;
1748 * opaque dh_Ys<1..2^16-1>;
1749 * } ServerDHParams;
1750 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001751 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1752 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001755 return( ret );
1756 }
1757
Gilles Peskine487bbf62021-05-27 22:17:07 +02001758 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001759 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001760 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001762 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001763 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001764 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001765 }
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1768 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1769 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001770
1771 return( ret );
1772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1774 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001775
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001776#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
1777 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1778 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1779 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001783 mbedtls_ecp_group_id grp_id;
1784#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1785 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1786#else
1787 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1788#endif
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001789
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001790 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001791 if( curve_info == NULL )
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001795 }
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001798
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001799 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001800 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001801
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001802 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1803 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001804
1805 return( 0 );
1806}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001807#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
1808 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1809 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1810 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001811
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001812#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1813 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1814 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1815#if defined(MBEDTLS_USE_PSA_CRYPTO)
1816static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001817 unsigned char **p,
1818 unsigned char *end )
1819{
1820 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001821 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001822 uint8_t ecpoint_len;
1823 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1824
1825 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001826 * struct {
1827 * ECParameters curve_params;
1828 * ECPoint public;
1829 * } ServerECDHParams;
1830 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001831 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001832 * 2..3 NamedCurve
1833 * 4 ECPoint.len
1834 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001835 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001836 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001837 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838
1839 /* First byte is curve_type; only named_curve is handled */
1840 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001841 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001842
1843 /* Next two bytes are the namedcurve value */
1844 tls_id = *(*p)++;
1845 tls_id <<= 8;
1846 tls_id |= *(*p)++;
1847
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001848 /* Check it's a curve we offered */
1849 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001850 {
1851 MBEDTLS_SSL_DEBUG_MSG( 2,
1852 ( "bad server key exchange message (ECDHE curve): %u",
1853 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001854 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001855 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001856
Hanno Beckerbb89e272019-01-08 12:54:37 +00001857 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001858 if( ( handshake->ecdh_psa_type =
1859 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001860 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001861 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001862 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001863 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001864
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001865 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001866 ecpoint_len = *(*p)++;
1867 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001868 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001869
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001870 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1871 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001872
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001873 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1874 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001875 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001876
Hanno Beckerbb89e272019-01-08 12:54:37 +00001877 return( 0 );
1878}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001879#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001881 unsigned char **p,
1882 unsigned char *end )
1883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001885
Paul Bakker29e1f122013-04-16 13:07:56 +02001886 /*
1887 * Ephemeral ECDH parameters:
1888 *
1889 * struct {
1890 * ECParameters curve_params;
1891 * ECPoint public;
1892 * } ServerECDHParams;
1893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001895 (const unsigned char **) p, end ) ) != 0 )
1896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001898#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001899 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1900 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001901#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001902 return( ret );
1903 }
1904
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001905 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001906 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001907 MBEDTLS_SSL_DEBUG_MSG( 1,
1908 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001909 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001910 }
1911
Paul Bakker29e1f122013-04-16 13:07:56 +02001912 return( ret );
1913}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001914#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1916 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1917 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001918
Gilles Peskineeccd8882020-03-10 12:19:08 +01001919#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001921 unsigned char **p,
1922 unsigned char *end )
1923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001925 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001926 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001927
1928 /*
1929 * PSK parameters:
1930 *
1931 * opaque psk_identity_hint<0..2^16-1>;
1932 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001933 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001934 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001935 MBEDTLS_SSL_DEBUG_MSG( 1,
1936 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001937 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001938 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001939 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001940 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941
irwir6527bd62019-09-21 18:51:25 +03001942 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001943 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001944 MBEDTLS_SSL_DEBUG_MSG( 1,
1945 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001946 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001947 }
1948
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001949 /*
1950 * Note: we currently ignore the PKS identity hint, as we only allow one
1951 * PSK to be provisionned on the client. This could be changed later if
1952 * someone needs that feature.
1953 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001954 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001955 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001956
1957 return( ret );
1958}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001959#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1962 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001963/*
1964 * Generate a pre-master secret and encrypt it with the server's RSA key
1965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001967 size_t offset, size_t *olen,
1968 size_t pms_offset )
1969{
Janos Follath865b3eb2019-12-16 11:46:15 +00001970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001971 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001972 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001973 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001974
Angus Grattond8213d02016-05-25 20:56:48 +10001975 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001976 {
1977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1978 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1979 }
1980
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001981 /*
1982 * Generate (part of) the pre-master as
1983 * struct {
1984 * ProtocolVersion client_version;
1985 * opaque random[46];
1986 * } PreMasterSecret;
1987 */
Ronald Cron4e263fd2022-03-15 15:54:17 +01001988 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
1989 MBEDTLS_SSL_MINOR_VERSION_3,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001990 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001991
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001992 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001995 return( ret );
1996 }
1997
1998 ssl->handshake->pmslen = 48;
1999
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002000#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2001 peer_pk = &ssl->handshake->peer_pubkey;
2002#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002003 if( ssl->session_negotiate->peer_cert == NULL )
2004 {
Hanno Becker8273df82019-02-06 17:37:32 +00002005 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002008 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002009 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2010#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002011
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002012 /*
2013 * Now write it out, encrypted
2014 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002015 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2018 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002019 }
2020
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002021 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002022 p, ssl->handshake->pmslen,
2023 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002024 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002025 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028 return( ret );
2029 }
2030
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002031 if( len_bytes == 2 )
2032 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002033 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002034 *olen += 2;
2035 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002036
Hanno Beckerae553dd2019-02-08 14:06:00 +00002037#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2038 /* We don't need the peer's public key anymore. Free it. */
2039 mbedtls_pk_free( peer_pk );
2040#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002041 return( 0 );
2042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2044 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002045
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002046#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2047 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2048 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002050 unsigned char **p,
2051 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 mbedtls_md_type_t *md_alg,
2053 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 *md_alg = MBEDTLS_MD_NONE;
2056 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002057
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002058 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002059 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002060
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002061 /*
2062 * Get hash algorithm
2063 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002064 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2065 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002066 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002067 MBEDTLS_SSL_DEBUG_MSG( 1,
2068 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002069 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002070 }
2071
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002072 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002073 * Get signature algorithm
2074 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002075 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2076 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002077 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002078 MBEDTLS_SSL_DEBUG_MSG( 1,
2079 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002080 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002081 }
2082
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002083 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002084 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002085 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002086 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002087 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002088 MBEDTLS_SSL_DEBUG_MSG( 1,
2089 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002090 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002091 }
2092
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2094 (*p)[1] ) );
2095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2096 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002097 *p += 2;
2098
2099 return( 0 );
2100}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002101#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2102 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2103 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2106 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2107static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002108{
Janos Follath865b3eb2019-12-16 11:46:15 +00002109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002111 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002112
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002113#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2114 peer_pk = &ssl->handshake->peer_pubkey;
2115#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002116 if( ssl->session_negotiate->peer_cert == NULL )
2117 {
Hanno Becker8273df82019-02-06 17:37:32 +00002118 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002121 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002122 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2123#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002124
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002125 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2128 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002129 }
2130
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002131 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002132
Przemek Stekielea4000f2022-03-16 09:49:33 +01002133#if defined(MBEDTLS_USE_PSA_CRYPTO)
2134 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002135 size_t olen = 0;
2136
2137 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2138 {
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2140 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2141 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002142
2143 ssl->handshake->ecdh_psa_type =
2144 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2145 &ecdh_bits ) );
2146
2147 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2148 {
2149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2150 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2151 }
2152
2153 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2154
Przemek Stekielea4000f2022-03-16 09:49:33 +01002155 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002156 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2157 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2158 ssl->handshake->ecdh_psa_peerkey,
2159 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002160
Przemek Stekiel561a4232022-03-16 13:16:24 +01002161 if ( ret != 0 )
2162 {
2163 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2164 return( ret );
2165 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002166
Przemek Stekiel561a4232022-03-16 13:16:24 +01002167 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002168#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2170 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002173 return( ret );
2174 }
2175
2176 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002179 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002180 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002181#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002182#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2183 /* We don't need the peer's public key anymore. Free it,
2184 * so that more RAM is available for upcoming expensive
2185 * operations like ECDHE. */
2186 mbedtls_pk_free( peer_pk );
2187#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2188
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002189 return( ret );
2190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2192 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002195{
Janos Follath865b3eb2019-12-16 11:46:15 +00002196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002197 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002198 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002199 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2204 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 ssl->state++;
2208 return( 0 );
2209 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002210 ((void) p);
2211 ((void) end);
2212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2215 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2216 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2217 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002218 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002219 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002222 mbedtls_ssl_send_alert_message(
2223 ssl,
2224 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2225 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002226 return( ret );
2227 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002230 ssl->state++;
2231 return( 0 );
2232 }
2233 ((void) p);
2234 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2236 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002237
Gilles Peskineeccd8882020-03-10 12:19:08 +01002238#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002239 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002240 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002241 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002242 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002243 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002244#endif
2245
Hanno Becker327c93b2018-08-15 13:56:18 +01002246 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 return( ret );
2250 }
2251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002255 mbedtls_ssl_send_alert_message(
2256 ssl,
2257 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2258 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
2261
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002262 /*
2263 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2264 * doesn't use a psk_identity_hint
2265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2269 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002270 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002271 /* Current message is probably either
2272 * CertificateRequest or ServerHelloDone */
2273 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002274 goto exit;
2275 }
2276
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002277 MBEDTLS_SSL_DEBUG_MSG( 1,
2278 ( "server key exchange message must not be skipped" ) );
2279 mbedtls_ssl_send_alert_message(
2280 ssl,
2281 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2282 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 }
2286
Gilles Peskineeccd8882020-03-10 12:19:08 +01002287#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002288 if( ssl->handshake->ecrs_enabled )
2289 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2290
2291start_processing:
2292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002294 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002296
Gilles Peskineeccd8882020-03-10 12:19:08 +01002297#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2299 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2300 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2301 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002302 {
2303 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002306 mbedtls_ssl_send_alert_message(
2307 ssl,
2308 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002309 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002310 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002311 }
2312 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002313#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2316 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2317 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2318 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002319 ; /* nothing more to do */
2320 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2322 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2323#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2324 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2325 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2326 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002328 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002331 mbedtls_ssl_send_alert_message(
2332 ssl,
2333 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2334 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002335 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002336 }
2337 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2340 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002341#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2342 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2344 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2345 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2346 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002347 {
2348 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002351 mbedtls_ssl_send_alert_message(
2352 ssl,
2353 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2354 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002355 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002356 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002357 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2360 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2361 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002362#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2363 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2364 {
2365 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2366 p, end - p );
2367 if( ret != 0 )
2368 {
2369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002370 mbedtls_ssl_send_alert_message(
2371 ssl,
2372 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002373 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2374 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002375 }
2376 }
2377 else
2378#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2381 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002383
Gilles Peskineeccd8882020-03-10 12:19:08 +01002384#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002385 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002386 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002387 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002388#if defined(MBEDTLS_USE_PSA_CRYPTO)
2389 unsigned char hash[PSA_HASH_MAX_SIZE];
2390#else
2391 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2394 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2395 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002396 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002397 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002398
Hanno Beckera6899bb2019-02-06 18:26:03 +00002399 mbedtls_pk_context * peer_pk;
2400
Paul Bakker29e1f122013-04-16 13:07:56 +02002401 /*
2402 * Handle the digitally-signed structure
2403 */
Ronald Cron90915f22022-03-07 11:11:36 +01002404 if( ssl_parse_signature_algorithm( ssl, &p, end,
2405 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002406 {
Ronald Cron90915f22022-03-07 11:11:36 +01002407 MBEDTLS_SSL_DEBUG_MSG( 1,
2408 ( "bad server key exchange message" ) );
2409 mbedtls_ssl_send_alert_message(
2410 ssl,
2411 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2412 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2413 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002414 }
Ronald Cron90915f22022-03-07 11:11:36 +01002415
2416 if( pk_alg !=
2417 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002418 {
Ronald Cron90915f22022-03-07 11:11:36 +01002419 MBEDTLS_SSL_DEBUG_MSG( 1,
2420 ( "bad server key exchange message" ) );
2421 mbedtls_ssl_send_alert_message(
2422 ssl,
2423 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2424 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2425 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002426 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002427
2428 /*
2429 * Read signature
2430 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002431
2432 if( p > end - 2 )
2433 {
2434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002435 mbedtls_ssl_send_alert_message(
2436 ssl,
2437 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2438 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002439 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002440 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002441 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002442 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002443
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002444 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002447 mbedtls_ssl_send_alert_message(
2448 ssl,
2449 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2450 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002451 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002455
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002456 /*
2457 * Compute the hash that has been signed
2458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002460 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002461 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2462 params, params_len,
2463 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002464 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002465 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002466 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002467 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002471 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002472
Gilles Peskineca1d7422018-04-24 11:53:22 +02002473 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002474
Hanno Beckera6899bb2019-02-06 18:26:03 +00002475#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2476 peer_pk = &ssl->handshake->peer_pubkey;
2477#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002478 if( ssl->session_negotiate->peer_cert == NULL )
2479 {
Hanno Becker8273df82019-02-06 17:37:32 +00002480 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002483 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002484 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2485#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002486
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002487 /*
2488 * Verify signature
2489 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002490 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002493 mbedtls_ssl_send_alert_message(
2494 ssl,
2495 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2496 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002498 }
2499
Gilles Peskineeccd8882020-03-10 12:19:08 +01002500#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002501 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002502 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002503#endif
2504
Hanno Beckera6899bb2019-02-06 18:26:03 +00002505 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002506 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002507 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002508#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002509 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2510#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002511 mbedtls_ssl_send_alert_message(
2512 ssl,
2513 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2514 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002516#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002517 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2518 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2519#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002520 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002521 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002522
2523#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2524 /* We don't need the peer's public key anymore. Free it,
2525 * so that more RAM is available for upcoming expensive
2526 * operations like ECDHE. */
2527 mbedtls_pk_free( peer_pk );
2528#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002530#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002532exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 ssl->state++;
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
2537 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538}
2539
Gilles Peskineeccd8882020-03-10 12:19:08 +01002540#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002542{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002543 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002544 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002547
Hanno Becker1aa267c2017-04-28 17:08:27 +01002548 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002551 ssl->state++;
2552 return( 0 );
2553 }
2554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002557}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002558#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002560{
Janos Follath865b3eb2019-12-16 11:46:15 +00002561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002562 unsigned char *buf;
2563 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002564 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002565 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002566 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002567 size_t sig_alg_len;
2568#if defined(MBEDTLS_DEBUG_C)
2569 unsigned char *sig_alg;
2570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Hanno Becker1aa267c2017-04-28 17:08:27 +01002574 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002577 ssl->state++;
2578 return( 0 );
2579 }
2580
Hanno Becker327c93b2018-08-15 13:56:18 +01002581 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2584 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 }
2586
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002587 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2588 {
2589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002590 mbedtls_ssl_send_alert_message(
2591 ssl,
2592 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2593 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002594 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002597 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002598 ssl->handshake->client_auth =
2599 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002602 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Jerry Yufb28b882022-01-28 11:05:58 +08002604 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002605 {
Johan Pascala89ca862020-08-25 10:03:19 +02002606 /* Current message is probably the ServerHelloDone */
2607 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002608 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002609 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002610
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002611 /*
2612 * struct {
2613 * ClientCertificateType certificate_types<1..2^8-1>;
2614 * SignatureAndHashAlgorithm
2615 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2616 * DistinguishedName certificate_authorities<0..2^16-1>;
2617 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002618 *
2619 * Since we only support a single certificate on clients, let's just
2620 * ignore all the information that's supposed to help us pick a
2621 * certificate.
2622 *
2623 * We could check that our certificate matches the request, and bail out
2624 * if it doesn't, but it's simpler to just send the certificate anyway,
2625 * and give the server the opportunity to decide if it should terminate
2626 * the connection when it doesn't like our certificate.
2627 *
2628 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2629 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002630 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002631 *
2632 * However, we still minimally parse the message to check it is at least
2633 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002634 */
Paul Bakker926af752012-11-23 13:38:07 +01002635 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002636
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002637 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002638 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2639 {
2640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2641 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2642 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002643 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002646 n = cert_type_len;
2647
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002648 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002649 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002650 * * the length of the signature algorithms field (if minor version of
2651 * SSL is 3),
2652 * * distinguished name length otherwise.
2653 * Both reach at most the index:
2654 * ...hdr_len + 2 + n,
2655 * therefore the buffer length at this point must be greater than that
2656 * regardless of the actual code path.
2657 */
2658 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2662 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002663 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002664 }
2665
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002666 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002667 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2668 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2669
2670 /*
2671 * The furthest access in buf is in the loop few lines below:
2672 * sig_alg[i + 1],
2673 * where:
2674 * sig_alg = buf + ...hdr_len + 3 + n,
2675 * max(i) = sig_alg_len - 1.
2676 * Therefore the furthest access is:
2677 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2678 * which reduces to:
2679 * buf[...hdr_len + 3 + n + sig_alg_len],
2680 * which is one less than we need the buf to be.
2681 */
2682 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002683 {
Ronald Cron90915f22022-03-07 11:11:36 +01002684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2685 mbedtls_ssl_send_alert_message(
2686 ssl,
2687 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2688 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2689 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002690 }
Paul Bakker926af752012-11-23 13:38:07 +01002691
Ronald Cron90915f22022-03-07 11:11:36 +01002692#if defined(MBEDTLS_DEBUG_C)
2693 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2694 for( size_t i = 0; i < sig_alg_len; i += 2 )
2695 {
2696 MBEDTLS_SSL_DEBUG_MSG( 3,
2697 ( "Supported Signature Algorithm found: %d,%d",
2698 sig_alg[i], sig_alg[i + 1] ) );
2699 }
2700#endif
2701
2702 n += 2 + sig_alg_len;
2703
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002704 /* certificate_authorities */
2705 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2706 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002707
2708 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002709 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002712 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2713 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002714 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002715 }
2716
2717exit:
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002725{
Janos Follath865b3eb2019-12-16 11:46:15 +00002726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Hanno Becker327c93b2018-08-15 13:56:18 +01002730 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2733 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002735
2736 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2737 {
2738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2739 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2743 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002746 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2747 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002748 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 }
2750
2751 ssl->state++;
2752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002756#endif
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
2760 return( 0 );
2761}
2762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002764{
Janos Follath865b3eb2019-12-16 11:46:15 +00002765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002766
2767 size_t header_len;
2768 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002769 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002770 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2775 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 /*
2778 * DHM key exchange -- send G^X mod P
2779 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002780 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Joe Subbiani6dd73642021-07-19 11:56:54 +01002782 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002783 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002786 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002787 &ssl->out_msg[header_len], content_len,
2788 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 if( ret != 0 )
2790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 return( ret );
2793 }
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2796 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002799 ssl->handshake->premaster,
2800 MBEDTLS_PREMASTER_SIZE,
2801 &ssl->handshake->pmslen,
2802 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 return( ret );
2806 }
2807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 }
2810 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002812#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2813 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2814 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2815 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2816#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002817 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002818 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2819 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2820 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002821 {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2823 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002824 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002825
2826 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2827
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002828 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002829
Hanno Becker0a94a642019-01-11 14:35:30 +00002830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2831
Hanno Becker4a63ed42019-01-08 11:39:35 +00002832 /*
2833 * Generate EC private key for ECDHE exchange.
2834 */
2835
Hanno Becker4a63ed42019-01-08 11:39:35 +00002836 /* The master secret is obtained from the shared ECDH secret by
2837 * applying the TLS 1.2 PRF with a specific salt and label. While
2838 * the PSA Crypto API encourages combining key agreement schemes
2839 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2840 * yet support the provisioning of salt + label to the KDF.
2841 * For the time being, we therefore need to split the computation
2842 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002843 key_attributes = psa_key_attributes_init();
2844 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002845 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002846 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2847 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002848
2849 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002850 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002851 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002852 if( status != PSA_SUCCESS )
2853 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2854
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002855 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002856 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002857 * but we just need to add a length byte before that. */
2858 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2859 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2860 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2861 size_t own_pubkey_len;
2862
Hanno Becker4a63ed42019-01-08 11:39:35 +00002863 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002864 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002865 &own_pubkey_len );
2866 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002867 {
2868 psa_destroy_key( handshake->ecdh_psa_privkey );
2869 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002870 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002871 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002872
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002873 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2874 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002875
Hanno Becker4a63ed42019-01-08 11:39:35 +00002876 /* The ECDH secret is the premaster secret used for key derivation. */
2877
Janos Follathdf3b0892019-08-08 11:12:24 +01002878 /* Compute ECDH shared secret. */
2879 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2880 handshake->ecdh_psa_privkey,
2881 handshake->ecdh_psa_peerkey,
2882 handshake->ecdh_psa_peerkey_len,
2883 ssl->handshake->premaster,
2884 sizeof( ssl->handshake->premaster ),
2885 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002886
Andrzej Kureka0237f82022-02-24 13:24:52 -05002887 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002888 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002889
2890 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2891 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002892 }
2893 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002894#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2896 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2897 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2898 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002899 {
2900 /*
2901 * ECDH key exchange -- send client public value
2902 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002903 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002904
Gilles Peskineeccd8882020-03-10 12:19:08 +01002905#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002906 if( ssl->handshake->ecrs_enabled )
2907 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002908 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002909 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002910
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002911 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2912 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002913#endif
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002916 &content_len,
2917 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002918 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002919 if( ret != 0 )
2920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002922#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002923 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2924 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2925#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002926 return( ret );
2927 }
2928
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002929 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2930 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002931
Gilles Peskineeccd8882020-03-10 12:19:08 +01002932#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002933 if( ssl->handshake->ecrs_enabled )
2934 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002935 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002936 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002937 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002938
2939ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002940 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002941 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002944 &ssl->handshake->pmslen,
2945 ssl->handshake->premaster,
2946 MBEDTLS_MPI_MAX_SIZE,
2947 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002950#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002951 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2952 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2953#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002954 return( ret );
2955 }
2956
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002957 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2958 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01002959 }
2960 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002961#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2963 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2964 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2965 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002966#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2967 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2968 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2969 {
2970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2971 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2972 psa_key_attributes_t key_attributes;
2973
2974 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2975
2976 /*
2977 * opaque psk_identity<0..2^16-1>;
2978 */
2979 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002980 /* We don't offer PSK suites if we don't have a PSK,
2981 * and we check that the server's choice is among the
2982 * ciphersuites we offered, so this should never happen. */
2983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002984
2985 /* Opaque PSKs are currently only supported for PSK-only suites. */
2986 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
2987 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2988
Neil Armstrongfc834f22022-03-23 17:54:38 +01002989 /* uint16 to store content length */
2990 const size_t content_len_size = 2;
2991
Neil Armstrong868af822022-03-09 10:26:25 +01002992 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002993
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002994 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002995 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002996 {
2997 MBEDTLS_SSL_DEBUG_MSG( 1,
2998 ( "psk identity too long or SSL buffer too short" ) );
2999 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3000 }
3001
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003002 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003003
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003004 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
3005 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
3006 header_len += content_len_size;
3007
3008 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003009 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003010 p += ssl->conf->psk_identity_len;
3011
Neil Armstrong868af822022-03-09 10:26:25 +01003012 header_len += ssl->conf->psk_identity_len;
3013
3014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3015
3016 /*
3017 * Generate EC private key for ECDHE exchange.
3018 */
3019
3020 /* The master secret is obtained from the shared ECDH secret by
3021 * applying the TLS 1.2 PRF with a specific salt and label. While
3022 * the PSA Crypto API encourages combining key agreement schemes
3023 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3024 * yet support the provisioning of salt + label to the KDF.
3025 * For the time being, we therefore need to split the computation
3026 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3027 key_attributes = psa_key_attributes_init();
3028 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3029 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3030 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3031 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3032
3033 /* Generate ECDH private key. */
3034 status = psa_generate_key( &key_attributes,
3035 &handshake->ecdh_psa_privkey );
3036 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003037 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003038
3039 /* Export the public part of the ECDH private key from PSA.
3040 * The export format is an ECPoint structure as expected by TLS,
3041 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003042 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003043 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3044 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003045 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003046
3047 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3048 own_pubkey, own_pubkey_max_len,
3049 &own_pubkey_len );
3050 if( status != PSA_SUCCESS )
3051 {
3052 psa_destroy_key( handshake->ecdh_psa_privkey );
3053 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003054 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003055 }
3056
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003057 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003058 content_len = own_pubkey_len + 1;
3059
Neil Armstrong25400452022-03-23 17:44:07 +01003060 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3061 * - a uint16 containing the length (in octets) of the ECDH computation
3062 * - the octet string produced by the ECDH computation
3063 * - a uint16 containing the length (in octets) of the PSK
3064 * - the PSK itself
3065 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003066 unsigned char *pms = ssl->handshake->premaster;
3067 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003068 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003069 /* uint16 to store length (in octets) of the ECDH computation */
3070 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003071 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003072
Neil Armstrong25400452022-03-23 17:44:07 +01003073 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003074 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3075 handshake->ecdh_psa_privkey,
3076 handshake->ecdh_psa_peerkey,
3077 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003078 pms + zlen_size,
3079 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003080 &zlen );
3081
3082 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3083 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3084
Neil Armstrongc530aa62022-03-23 17:45:01 +01003085 if( status != PSA_SUCCESS )
3086 return( psa_ssl_status_to_mbedtls( status ) );
3087 else if( destruction_status != PSA_SUCCESS )
3088 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003089
Neil Armstrong25400452022-03-23 17:44:07 +01003090 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003091 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3092 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003093
Neil Armstrong868af822022-03-09 10:26:25 +01003094 const unsigned char *psk = NULL;
3095 size_t psk_len = 0;
3096
3097 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
3098 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Neil Armstrong868af822022-03-09 10:26:25 +01003099 /*
3100 * This should never happen because the existence of a PSK is always
3101 * checked before calling this function
3102 */
3103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003104
Neil Armstronge18ff952022-04-04 18:34:55 +02003105 /* opaque psk<0..2^16-1>; */
3106 if( (size_t)( pms_end - pms ) < ( 2 + psk_len ) )
3107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3108
Neil Armstrong25400452022-03-23 17:44:07 +01003109 /* Write the PSK length as uint16 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003110 MBEDTLS_PUT_UINT16_BE( psk_len, pms, 0 );
3111 pms += 2;
Neil Armstrong868af822022-03-09 10:26:25 +01003112
Neil Armstrong25400452022-03-23 17:44:07 +01003113 /* Write the PSK itself */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003114 memcpy( pms, psk, psk_len );
3115 pms += psk_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003116
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003117 ssl->handshake->pmslen = pms - ssl->handshake->premaster;
Neil Armstrong868af822022-03-09 10:26:25 +01003118 }
3119 else
3120#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3121 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003122#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003123 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003124 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003125 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003126 * opaque psk_identity<0..2^16-1>;
3127 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003128 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003129 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003130 /* We don't offer PSK suites if we don't have a PSK,
3131 * and we check that the server's choice is among the
3132 * ciphersuites we offered, so this should never happen. */
3133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003134 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003135
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003136 header_len = 4;
3137 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003138
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003139 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003140 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003141 MBEDTLS_SSL_DEBUG_MSG( 1,
3142 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003143 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3144 }
3145
Joe Subbianifbeb6922021-07-16 14:27:50 +01003146 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3147 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003148
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003149 memcpy( ssl->out_msg + header_len,
3150 ssl->conf->psk_identity,
3151 ssl->conf->psk_identity_len );
3152 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3155 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003156 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003157 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003158 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003159 else
3160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3162 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003163 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003164#if defined(MBEDTLS_USE_PSA_CRYPTO)
3165 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003166 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003167 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3168#endif /* MBEDTLS_USE_PSA_CRYPTO */
3169
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003170 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3171 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003172 return( ret );
3173 }
3174 else
3175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3177 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003178 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003179#if defined(MBEDTLS_USE_PSA_CRYPTO)
3180 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003181 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003182 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3183#endif /* MBEDTLS_USE_PSA_CRYPTO */
3184
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003185 /*
3186 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3187 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003188 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003189
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003190 if( header_len + 2 + content_len >
3191 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003192 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003193 MBEDTLS_SSL_DEBUG_MSG( 1,
3194 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003195 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3196 }
3197
Joe Subbianifbeb6922021-07-16 14:27:50 +01003198 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3199 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003202 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003203 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003204 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003205 if( ret != 0 )
3206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003208 return( ret );
3209 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003210 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003211 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003213#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3214 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003216 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003217#if defined(MBEDTLS_USE_PSA_CRYPTO)
3218 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003219 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003220 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3221#endif /* MBEDTLS_USE_PSA_CRYPTO */
3222
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003223 /*
3224 * ClientECDiffieHellmanPublic public;
3225 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003226 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3227 &content_len,
3228 &ssl->out_msg[header_len],
3229 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003230 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003231 if( ret != 0 )
3232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003234 return( ret );
3235 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003236
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003237 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3238 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003239 }
3240 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003241#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003245 }
3246
Hanno Beckerafd311e2018-10-23 15:26:40 +01003247#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3248 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3249 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003250 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003251 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003252 MBEDTLS_SSL_DEBUG_MSG( 1,
3253 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003254 }
3255 else
3256#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3257 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003259 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003260 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003261 MBEDTLS_SSL_DEBUG_RET( 1,
3262 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003263 return( ret );
3264 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003265 }
3266 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003267#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3269 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003271 header_len = 4;
3272 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3273 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003274 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 }
Paul Bakkered27a042013-04-18 22:46:23 +02003276 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003278#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3279 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3280 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003281 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003282
3283 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003284 ssl->out_msg + header_len,
3285 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3286 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003287 ssl->conf->f_rng, ssl->conf->p_rng );
3288 if( ret != 0 )
3289 {
3290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3291 return( ret );
3292 }
3293
3294 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3295 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3296 ssl->conf->f_rng, ssl->conf->p_rng );
3297 if( ret != 0 )
3298 {
3299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3300 return( ret );
3301 }
3302 }
3303 else
3304#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003305 {
3306 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003310
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003311 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3313 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
3315 ssl->state++;
3316
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003317 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 return( ret );
3321 }
3322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
3325 return( 0 );
3326}
3327
Gilles Peskineeccd8882020-03-10 12:19:08 +01003328#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003330{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003331 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003332 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003340 return( ret );
3341 }
3342
Hanno Becker77adddc2019-02-07 12:32:43 +00003343 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003346 ssl->state++;
3347 return( 0 );
3348 }
3349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003352}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003353#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003357 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003358 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003359 size_t n = 0, offset = 0;
3360 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003361 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003363 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003364 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003365#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3366 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3367#else
3368 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3369#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003372
Gilles Peskineeccd8882020-03-10 12:19:08 +01003373#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003374 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003375 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003376 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003377 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003378 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003379#endif
3380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003384 return( ret );
3385 }
3386
Hanno Becker77adddc2019-02-07 12:32:43 +00003387 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003390 ssl->state++;
3391 return( 0 );
3392 }
3393
Jerry Yufb28b882022-01-28 11:05:58 +08003394 if( ssl->handshake->client_auth == 0 ||
3395 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 {
Johan Pascala89ca862020-08-25 10:03:19 +02003397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3398 ssl->state++;
3399 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 }
3401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 }
3407
3408 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003409 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003411#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003412 if( ssl->handshake->ecrs_enabled )
3413 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3414
3415sign:
3416#endif
3417
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003418 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419
Ronald Cron90915f22022-03-07 11:11:36 +01003420 /*
3421 * digitally-signed struct {
3422 * opaque handshake_messages[handshake_messages_length];
3423 * };
3424 *
3425 * Taking shortcut here. We assume that the server always allows the
3426 * PRF Hash function and has sent it in the allowed signature
3427 * algorithms list received in the Certificate Request message.
3428 *
3429 * Until we encounter a server that does not, we will take this
3430 * shortcut.
3431 *
3432 * Reason: Otherwise we should have running hashes for SHA512 and
3433 * SHA224 in order to satisfy 'weird' needs from the server
3434 * side.
3435 */
3436 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003437 {
Ronald Cron90915f22022-03-07 11:11:36 +01003438 md_alg = MBEDTLS_MD_SHA384;
3439 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003440 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003441 else
Paul Bakker577e0062013-08-28 11:57:20 +02003442 {
Ronald Cron90915f22022-03-07 11:11:36 +01003443 md_alg = MBEDTLS_MD_SHA256;
3444 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003445 }
Ronald Cron90915f22022-03-07 11:11:36 +01003446 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3447
3448 /* Info from md_alg will be used instead */
3449 hashlen = 0;
3450 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003451
Gilles Peskineeccd8882020-03-10 12:19:08 +01003452#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003453 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003454 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003455#endif
3456
3457 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3458 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003459 ssl->out_msg + 6 + offset,
3460 out_buf_len - 6 - offset,
3461 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003462 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003465#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003466 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3467 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3468#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003469 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003470 }
Paul Bakker926af752012-11-23 13:38:07 +01003471
Joe Subbiani6dd73642021-07-19 11:56:54 +01003472 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003473
Paul Bakker1ef83d62012-04-11 12:09:53 +00003474 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3476 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003477
3478 ssl->state++;
3479
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003480 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 return( ret );
3484 }
3485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
Paul Bakkered27a042013-04-18 22:46:23 +02003488 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003489}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003490#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3493static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003494{
Janos Follath865b3eb2019-12-16 11:46:15 +00003495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003496 uint32_t lifetime;
3497 size_t ticket_len;
3498 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003499 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003502
Hanno Becker327c93b2018-08-15 13:56:18 +01003503 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003506 return( ret );
3507 }
3508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003512 mbedtls_ssl_send_alert_message(
3513 ssl,
3514 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3515 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003517 }
3518
3519 /*
3520 * struct {
3521 * uint32 ticket_lifetime_hint;
3522 * opaque ticket<0..2^16-1>;
3523 * } NewSessionTicket;
3524 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003525 * 0 . 3 ticket_lifetime_hint
3526 * 4 . 5 ticket_len (n)
3527 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3530 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003533 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3534 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003535 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003536 }
3537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003539
Philippe Antoineb5b25432018-05-11 11:06:29 +02003540 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3541 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003542
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003543 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003548 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3549 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003550 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003551 }
3552
Paul Elliottd48d5c62021-01-07 14:47:05 +00003553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003554
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003555 /* We're not waiting for a NewSessionTicket message any more */
3556 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003558
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003559 /*
3560 * Zero-length ticket means the server changed his mind and doesn't want
3561 * to send a ticket after all, so just forget it
3562 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003563 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003564 return( 0 );
3565
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003566 if( ssl->session != NULL && ssl->session->ticket != NULL )
3567 {
3568 mbedtls_platform_zeroize( ssl->session->ticket,
3569 ssl->session->ticket_len );
3570 mbedtls_free( ssl->session->ticket );
3571 ssl->session->ticket = NULL;
3572 ssl->session->ticket_len = 0;
3573 }
3574
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003575 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3576 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003578 ssl->session_negotiate->ticket = NULL;
3579 ssl->session_negotiate->ticket_len = 0;
3580
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003581 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003582 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003584 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3585 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003586 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003587 }
3588
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003589 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003590
3591 ssl->session_negotiate->ticket = ticket;
3592 ssl->session_negotiate->ticket_len = ticket_len;
3593 ssl->session_negotiate->ticket_lifetime = lifetime;
3594
3595 /*
3596 * RFC 5077 section 3.4:
3597 * "If the client receives a session ticket from the server, then it
3598 * discards any Session ID that was sent in the ServerHello."
3599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003601 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003604
3605 return( 0 );
3606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003608
Paul Bakker5121ce52009-01-03 21:22:43 +00003609/*
Paul Bakker1961b702013-01-25 14:49:24 +01003610 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003613{
3614 int ret = 0;
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003617 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3619 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003620 ssl->handshake->new_session_ticket != 0 )
3621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003623 }
3624#endif
3625
Paul Bakker1961b702013-01-25 14:49:24 +01003626 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 case MBEDTLS_SSL_HELLO_REQUEST:
3629 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 break;
3631
Paul Bakker1961b702013-01-25 14:49:24 +01003632 /*
3633 * ==> ClientHello
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003636 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003637 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003638
Paul Bakker1961b702013-01-25 14:49:24 +01003639 /*
3640 * <== ServerHello
3641 * Certificate
3642 * ( ServerKeyExchange )
3643 * ( CertificateRequest )
3644 * ServerHelloDone
3645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003647 ret = ssl_parse_server_hello( ssl );
3648 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3651 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003652 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003655 ret = ssl_parse_server_key_exchange( ssl );
3656 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003659 ret = ssl_parse_certificate_request( ssl );
3660 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003663 ret = ssl_parse_server_hello_done( ssl );
3664 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003665
Paul Bakker1961b702013-01-25 14:49:24 +01003666 /*
3667 * ==> ( Certificate/Alert )
3668 * ClientKeyExchange
3669 * ( CertificateVerify )
3670 * ChangeCipherSpec
3671 * Finished
3672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3674 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003675 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003678 ret = ssl_write_client_key_exchange( ssl );
3679 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003682 ret = ssl_write_certificate_verify( ssl );
3683 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3686 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003687 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 case MBEDTLS_SSL_CLIENT_FINISHED:
3690 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003691 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
Paul Bakker1961b702013-01-25 14:49:24 +01003693 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003694 * <== ( NewSessionTicket )
3695 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003696 * Finished
3697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3699 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003700 ret = ssl_parse_new_session_ticket( ssl );
3701 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003702#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3705 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003706 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 case MBEDTLS_SSL_SERVER_FINISHED:
3709 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003710 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 case MBEDTLS_SSL_FLUSH_BUFFERS:
3713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3714 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003715 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3718 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003719 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003720
Paul Bakker1961b702013-01-25 14:49:24 +01003721 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
3726 return( ret );
3727}
Jerry Yuc5aef882021-12-23 20:15:02 +08003728
Jerry Yufb4b6472022-01-27 15:03:26 +08003729#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */