blob: 6e751793bf78b6b0de5d165622cbbc54db622278 [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 {
Robert Cragie136884c2015-10-02 13:34:31 +0100873#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200874 ssl->handshake->ecdh_ctx.point_format = p[0];
Gilles Peskine064a85c2017-05-10 10:46:40 +0200875#endif
Robert Cragieae8535d2015-10-06 17:11:18 +0100876#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200877 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
878 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200881 return( 0 );
882 }
883
884 list_size--;
885 p++;
886 }
887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200889 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
890 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100891 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200892}
Darryl Green11999bb2018-03-13 15:22:58 +0000893#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100894 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200895
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200896#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
897static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
898 const unsigned char *buf,
899 size_t len )
900{
Janos Follath865b3eb2019-12-16 11:46:15 +0000901 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200902
Hanno Beckere694c3e2017-12-27 21:34:08 +0000903 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200904 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
905 {
906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
907 return( 0 );
908 }
909
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200910 /* If we got here, we no longer need our cached extension */
911 mbedtls_free( ssl->handshake->ecjpake_cache );
912 ssl->handshake->ecjpake_cache = NULL;
913 ssl->handshake->ecjpake_cache_len = 0;
914
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200915 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
916 buf, len ) ) != 0 )
917 {
918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100919 mbedtls_ssl_send_alert_message(
920 ssl,
921 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
922 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200923 return( ret );
924 }
925
926 return( 0 );
927}
928#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#if defined(MBEDTLS_SSL_ALPN)
931static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200932 const unsigned char *buf, size_t len )
933{
934 size_t list_len, name_len;
935 const char **p;
936
937 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200938 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200939 {
940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100941 mbedtls_ssl_send_alert_message(
942 ssl,
943 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100944 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
945 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200946 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200947
948 /*
949 * opaque ProtocolName<1..2^8-1>;
950 *
951 * struct {
952 * ProtocolName protocol_name_list<2..2^16-1>
953 * } ProtocolNameList;
954 *
955 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
956 */
957
958 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
959 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200960 {
961 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
962 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100963 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200964 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200965
966 list_len = ( buf[0] << 8 ) | buf[1];
967 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200968 {
969 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
970 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100971 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200972 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200973
974 name_len = buf[2];
975 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200976 {
977 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
978 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100979 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200980 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200981
982 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200983 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200984 {
985 if( name_len == strlen( *p ) &&
986 memcmp( buf + 3, *p, name_len ) == 0 )
987 {
988 ssl->alpn_chosen = *p;
989 return( 0 );
990 }
991 }
992
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200994 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
995 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100996 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200999
Johan Pascalb62bb512015-12-03 21:56:45 +01001000#if defined(MBEDTLS_SSL_DTLS_SRTP)
1001static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001002 const unsigned char *buf,
1003 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001004{
Johan Pascal43f94902020-09-22 12:25:52 +02001005 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001006 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001007 uint16_t server_protection_profile_value = 0;
1008
1009 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001010 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1011 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1012 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001013 return( 0 );
1014
Ron Eldora9788042018-12-05 11:04:31 +02001015 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001016 * uint8 SRTPProtectionProfile[2];
1017 *
1018 * struct {
1019 * SRTPProtectionProfiles SRTPProtectionProfiles;
1020 * opaque srtp_mki<0..255>;
1021 * } UseSRTPData;
1022
1023 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1024 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001025 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001026 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001027 {
1028 mki_len = ssl->dtls_srtp_info.mki_len;
1029 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001030
Ron Eldoref72faf2018-07-12 11:54:20 +03001031 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001032 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1033 * + protection profile (2 bytes)
1034 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001035 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001036 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001037 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001038 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001039
1040 /*
1041 * get the server protection profile
1042 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001043
1044 /*
1045 * protection profile length must be 0x0002 as we must have only
1046 * one protection profile in server Hello
1047 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001048 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001049 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001050
1051 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001052 server_protection = mbedtls_ssl_check_srtp_profile_value(
1053 server_protection_profile_value );
1054 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001055 {
Johan Pascal43f94902020-09-22 12:25:52 +02001056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1057 mbedtls_ssl_get_srtp_profile_as_string(
1058 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001059 }
1060
Johan Pascal43f94902020-09-22 12:25:52 +02001061 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001062
Johan Pascalb62bb512015-12-03 21:56:45 +01001063 /*
1064 * Check we have the server profile in our list
1065 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001066 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001067 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001068 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1069 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001070 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1072 mbedtls_ssl_get_srtp_profile_as_string(
1073 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001074 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001075 }
1076 }
1077
Ron Eldor591f1622018-01-22 12:30:04 +02001078 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001079 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001080 {
1081 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001082 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001083 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001084 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001085
1086 /* If server does not use mki in its reply, make sure the client won't keep
1087 * one as negotiated */
1088 if( len == 5 )
1089 {
1090 ssl->dtls_srtp_info.mki_len = 0;
1091 }
1092
Ron Eldoref72faf2018-07-12 11:54:20 +03001093 /*
1094 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001095 * If the client detects a nonzero-length MKI in the server's response
1096 * that is different than the one the client offered, then the client
1097 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1098 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001099 if( len > 5 && ( buf[4] != mki_len ||
1100 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001101 {
1102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1103 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001104 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001105 }
Ron Eldorb4655392018-07-05 18:25:39 +03001106#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001107 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001108 {
Ron Eldora9788042018-12-05 11:04:31 +02001109 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1110 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001111 }
1112#endif
Ron Eldora9788042018-12-05 11:04:31 +02001113 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001114}
1115#endif /* MBEDTLS_SSL_DTLS_SRTP */
1116
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001117/*
1118 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120#if defined(MBEDTLS_SSL_PROTO_DTLS)
1121static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001124 int major_ver, minor_ver;
1125 unsigned char cookie_len;
1126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001128
Gilles Peskineb64bf062019-09-27 14:02:44 +02001129 /* Check that there is enough room for:
1130 * - 2 bytes of version
1131 * - 1 byte of cookie_len
1132 */
1133 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1134 {
1135 MBEDTLS_SSL_DEBUG_MSG( 1,
1136 ( "incoming HelloVerifyRequest message is too short" ) );
1137 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1138 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001139 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001140 }
1141
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001142 /*
1143 * struct {
1144 * ProtocolVersion server_version;
1145 * opaque cookie<0..2^8-1>;
1146 * } HelloVerifyRequest;
1147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001149 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001150 p += 2;
1151
TRodziewicz2d8800e2021-05-13 19:14:19 +02001152 /*
1153 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1154 * even is lower than our min version.
1155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
TRodziewiczb5850c52021-05-13 17:11:23 +02001157 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001158 major_ver > ssl->conf->max_major_ver ||
1159 minor_ver > ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1164 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001165
Hanno Beckerbc000442021-06-24 09:18:19 +01001166 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001167 }
1168
1169 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001170 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1171 {
1172 MBEDTLS_SSL_DEBUG_MSG( 1,
1173 ( "cookie length does not match incoming message size" ) );
1174 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1175 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001176 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001177 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001178 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001179
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001180 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001181
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001182 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1183 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001184 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001186 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001187 }
1188
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001189 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001190 ssl->handshake->verify_cookie_len = cookie_len;
1191
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001192 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1194 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001199
1200 return( 0 );
1201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001205{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001206 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001207 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001208 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001209 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001210 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001212 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001213#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001214 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Hanno Becker327c93b2018-08-15 13:56:18 +01001219 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001221 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 return( ret );
1224 }
1225
Hanno Becker79594fd2019-05-08 09:38:41 +01001226 buf = ssl->in_msg;
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_RENEGOTIATION)
1231 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001232 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001233 ssl->renego_records_seen++;
1234
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001235 if( ssl->conf->renego_max_records >= 0 &&
1236 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001237 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001238 MBEDTLS_SSL_DEBUG_MSG( 1,
1239 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001241 }
1242
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001243 MBEDTLS_SSL_DEBUG_MSG( 1,
1244 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001245
1246 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001248 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001252 mbedtls_ssl_send_alert_message(
1253 ssl,
1254 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1255 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 }
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001266 return( ssl_parse_hello_verify_request( ssl ) );
1267 }
1268 else
1269 {
1270 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001271 mbedtls_free( ssl->handshake->cookie );
1272 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001273 ssl->handshake->verify_cookie_len = 0;
1274 }
1275 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1279 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1283 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001284 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 }
1286
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001287 /*
1288 * 0 . 1 server_version
1289 * 2 . 33 random (maybe including 4 bytes of Unix time)
1290 * 34 . 34 session_id length = n
1291 * 35 . 34+n session_id
1292 * 35+n . 36+n cipher_suite
1293 * 37+n . 37+n compression_method
1294 *
1295 * 38+n . 39+n extensions length (optional)
1296 * 40+n . .. extensions
1297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
1301 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001302 ssl->conf->transport, buf + 0 );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001303 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001305 if( ssl->major_ver < ssl->conf->min_major_ver ||
1306 ssl->minor_ver < ssl->conf->min_minor_ver ||
1307 ssl->major_ver > ssl->conf->max_major_ver ||
1308 ssl->minor_ver > ssl->conf->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001309 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001310 MBEDTLS_SSL_DEBUG_MSG( 1,
1311 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1312 ssl->conf->min_major_ver,
1313 ssl->conf->min_minor_ver,
1314 ssl->major_ver, ssl->minor_ver,
1315 ssl->conf->max_major_ver,
1316 ssl->conf->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1319 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001320
Hanno Beckerbc000442021-06-24 09:18:19 +01001321 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001322 }
1323
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001325 ( (unsigned long) buf[2] << 24 ) |
1326 ( (unsigned long) buf[3] << 16 ) |
1327 ( (unsigned long) buf[4] << 8 ) |
1328 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001330 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001332 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Paul Bakker48916f92012-09-16 19:57:18 +00001336 if( n > 32 )
1337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1340 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001341 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001342 }
1343
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001344 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001346 ext_len = ( ( buf[38 + n] << 8 )
1347 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Paul Bakker48916f92012-09-16 19:57:18 +00001349 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001353 mbedtls_ssl_send_alert_message(
1354 ssl,
1355 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1356 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001357 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001360 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001361 {
1362 ext_len = 0;
1363 }
1364 else
1365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001367 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1368 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001369 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001372 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001373 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001374
1375 /*
1376 * Read and check compression
1377 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001378 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001380 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001381 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001382 MBEDTLS_SSL_DEBUG_MSG( 1,
1383 ( "server hello, bad compression: %d", comp ) );
1384 mbedtls_ssl_send_alert_message(
1385 ssl,
1386 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1387 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001389 }
1390
Paul Bakker380da532012-04-18 16:10:25 +00001391 /*
1392 * Initialize update checksum functions
1393 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001394 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1395 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001396 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001397 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001398 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001399 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1400 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001402 }
Paul Bakker380da532012-04-18 16:10:25 +00001403
Hanno Beckere694c3e2017-12-27 21:34:08 +00001404 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001405
Paul Elliottd48d5c62021-01-07 14:47:05 +00001406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
1409 /*
1410 * Check if the session can be resumed
1411 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001412 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_SSL_RENEGOTIATION)
1414 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001415#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001416 ssl->session_negotiate->ciphersuite != i ||
1417 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001418 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001419 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 {
1421 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001422 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001424 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001425#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001426 ssl->session_negotiate->ciphersuite = i;
1427 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001428 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001429 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 }
1431 else
1432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001438 mbedtls_ssl_send_alert_message(
1439 ssl,
1440 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1441 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001442 return( ret );
1443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 }
1445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001447 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Paul Elliott3891caf2020-12-17 18:42:40 +00001449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001450 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1451 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001452
Andrzej Kurek03bac442018-04-25 05:06:07 -04001453 /*
1454 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001455 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 i = 0;
1457 while( 1 )
1458 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001459 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001462 mbedtls_ssl_send_alert_message(
1463 ssl,
1464 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1465 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001466 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 }
1468
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001469 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001470 ssl->session_negotiate->ciphersuite )
1471 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 }
1475
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001476 suite_info = mbedtls_ssl_ciphersuite_from_id(
1477 ssl->session_negotiate->ciphersuite );
Ronald Cron8fdad9e2022-03-30 22:04:30 +02001478 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->minor_ver,
1479 ssl->minor_ver ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001480 {
1481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001482 mbedtls_ssl_send_alert_message(
1483 ssl,
1484 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001485 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1486 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001487 }
1488
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001489 MBEDTLS_SSL_DEBUG_MSG( 3,
1490 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001491
Gilles Peskineeccd8882020-03-10 12:19:08 +01001492#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001493 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1494 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1495 {
1496 ssl->handshake->ecrs_enabled = 1;
1497 }
1498#endif
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00001501 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001504 mbedtls_ssl_send_alert_message(
1505 ssl,
1506 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1507 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001508 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 }
Paul Bakker48916f92012-09-16 19:57:18 +00001510 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001511
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001512 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001513
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001514 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001515 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001516
Paul Bakker48916f92012-09-16 19:57:18 +00001517 while( ext_len )
1518 {
1519 unsigned int ext_id = ( ( ext[0] << 8 )
1520 | ( ext[1] ) );
1521 unsigned int ext_size = ( ( ext[2] << 8 )
1522 | ( ext[3] ) );
1523
1524 if( ext_size + 4 > ext_len )
1525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001527 mbedtls_ssl_send_alert_message(
1528 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1529 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001530 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001531 }
1532
1533 switch( ext_id )
1534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1537#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001538 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001539#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001540
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001541 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1542 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001543 return( ret );
1544
1545 break;
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1548 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001549 MBEDTLS_SSL_DEBUG_MSG( 3,
1550 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001551
1552 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1553 ext + 4, ext_size ) ) != 0 )
1554 {
1555 return( ret );
1556 }
1557
1558 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001560
Hanno Beckera0e20d02019-05-15 14:03:01 +01001561#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001562 case MBEDTLS_TLS_EXT_CID:
1563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1564
1565 if( ( ret = ssl_parse_cid_ext( ssl,
1566 ext + 4,
1567 ext_size ) ) != 0 )
1568 {
1569 return( ret );
1570 }
1571
1572 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001573#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1576 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001578
1579 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1580 ext + 4, ext_size ) ) != 0 )
1581 {
1582 return( ret );
1583 }
1584
1585 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1589 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001590 MBEDTLS_SSL_DEBUG_MSG( 3,
1591 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001592
1593 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1594 ext + 4, ext_size ) ) != 0 )
1595 {
1596 return( ret );
1597 }
1598
1599 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1603 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001605
1606 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1607 ext + 4, ext_size ) ) != 0 )
1608 {
1609 return( ret );
1610 }
1611
1612 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001614
Robert Cragie136884c2015-10-02 13:34:31 +01001615#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001616 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001618 MBEDTLS_SSL_DEBUG_MSG( 3,
1619 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001620
1621 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1622 ext + 4, ext_size ) ) != 0 )
1623 {
1624 return( ret );
1625 }
1626
1627 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001628#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1629 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001630
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001631#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1632 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1634
1635 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1636 ext + 4, ext_size ) ) != 0 )
1637 {
1638 return( ret );
1639 }
1640
1641 break;
1642#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_SSL_ALPN)
1645 case MBEDTLS_TLS_EXT_ALPN:
1646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001647
Johan Pascal275874b2020-10-27 10:43:53 +01001648 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1649 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001650
1651 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001653
Johan Pascalb62bb512015-12-03 21:56:45 +01001654#if defined(MBEDTLS_SSL_DTLS_SRTP)
1655 case MBEDTLS_TLS_EXT_USE_SRTP:
1656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1657
Johan Pascalc3ccd982020-10-28 17:18:18 +01001658 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1659 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001660
1661 break;
1662#endif /* MBEDTLS_SSL_DTLS_SRTP */
1663
Paul Bakker48916f92012-09-16 19:57:18 +00001664 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001665 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001666 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001667 }
1668
1669 ext_len -= 4 + ext_size;
1670 ext += 4 + ext_size;
1671
1672 if( ext_len > 0 && ext_len < 4 )
1673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001675 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001676 }
1677 }
1678
1679 /*
1680 * Renegotiation security checks
1681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001683 ssl->conf->allow_legacy_renegotiation ==
1684 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001685 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001686 MBEDTLS_SSL_DEBUG_MSG( 1,
1687 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001688 handshake_failure = 1;
1689 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_SSL_RENEGOTIATION)
1691 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1692 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001693 renegotiation_info_seen == 0 )
1694 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001695 MBEDTLS_SSL_DEBUG_MSG( 1,
1696 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001697 handshake_failure = 1;
1698 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1700 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001701 ssl->conf->allow_legacy_renegotiation ==
1702 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001705 handshake_failure = 1;
1706 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1708 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001709 renegotiation_info_seen == 1 )
1710 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001711 MBEDTLS_SSL_DEBUG_MSG( 1,
1712 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001713 handshake_failure = 1;
1714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001716
1717 if( handshake_failure == 1 )
1718 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001719 mbedtls_ssl_send_alert_message(
1720 ssl,
1721 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1722 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001723 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
1728 return( 0 );
1729}
1730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1732 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001733static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1734 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001735 unsigned char *end )
1736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001738 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001739
Paul Bakker29e1f122013-04-16 13:07:56 +02001740 /*
1741 * Ephemeral DH parameters:
1742 *
1743 * struct {
1744 * opaque dh_p<1..2^16-1>;
1745 * opaque dh_g<1..2^16-1>;
1746 * opaque dh_Ys<1..2^16-1>;
1747 * } ServerDHParams;
1748 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001749 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1750 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001753 return( ret );
1754 }
1755
Gilles Peskine487bbf62021-05-27 22:17:07 +02001756 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001757 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001758 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001760 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001761 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001762 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001763 }
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1766 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1767 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001768
1769 return( ret );
1770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1772 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001773
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001774#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
1775 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1776 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1777 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001781 mbedtls_ecp_group_id grp_id;
1782#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1783 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1784#else
1785 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1786#endif
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001787
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001788 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001789 if( curve_info == NULL )
1790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001793 }
1794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001796
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001797 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001798 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001799
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001800 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1801 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001802
1803 return( 0 );
1804}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001805#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
1806 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1807 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1808 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001809
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001810#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1811 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1812 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1813#if defined(MBEDTLS_USE_PSA_CRYPTO)
1814static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001815 unsigned char **p,
1816 unsigned char *end )
1817{
1818 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001819 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001820 uint8_t ecpoint_len;
1821 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1822
1823 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001824 * struct {
1825 * ECParameters curve_params;
1826 * ECPoint public;
1827 * } ServerECDHParams;
1828 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001829 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001830 * 2..3 NamedCurve
1831 * 4 ECPoint.len
1832 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001833 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001834 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001835 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001836
1837 /* First byte is curve_type; only named_curve is handled */
1838 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001839 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001840
1841 /* Next two bytes are the namedcurve value */
1842 tls_id = *(*p)++;
1843 tls_id <<= 8;
1844 tls_id |= *(*p)++;
1845
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001846 /* Check it's a curve we offered */
1847 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001848 {
1849 MBEDTLS_SSL_DEBUG_MSG( 2,
1850 ( "bad server key exchange message (ECDHE curve): %u",
1851 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001852 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001853 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001854
Hanno Beckerbb89e272019-01-08 12:54:37 +00001855 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001856 if( ( handshake->ecdh_psa_type =
1857 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001858 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001859 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001860 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001861 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001862
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001863 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001864 ecpoint_len = *(*p)++;
1865 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001866 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001867
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001868 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1869 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001870
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001871 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1872 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001873 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001874
Hanno Beckerbb89e272019-01-08 12:54:37 +00001875 return( 0 );
1876}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001877#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001879 unsigned char **p,
1880 unsigned char *end )
1881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001883
Paul Bakker29e1f122013-04-16 13:07:56 +02001884 /*
1885 * Ephemeral ECDH parameters:
1886 *
1887 * struct {
1888 * ECParameters curve_params;
1889 * ECPoint public;
1890 * } ServerECDHParams;
1891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001893 (const unsigned char **) p, end ) ) != 0 )
1894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001896#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001897 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1898 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001899#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001900 return( ret );
1901 }
1902
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001903 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001904 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001905 MBEDTLS_SSL_DEBUG_MSG( 1,
1906 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001907 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001908 }
1909
Paul Bakker29e1f122013-04-16 13:07:56 +02001910 return( ret );
1911}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001912#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1914 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1915 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001916
Gilles Peskineeccd8882020-03-10 12:19:08 +01001917#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001919 unsigned char **p,
1920 unsigned char *end )
1921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001923 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001924 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001925
1926 /*
1927 * PSK parameters:
1928 *
1929 * opaque psk_identity_hint<0..2^16-1>;
1930 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001931 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001932 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001933 MBEDTLS_SSL_DEBUG_MSG( 1,
1934 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001935 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001936 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001937 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001938 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001939
irwir6527bd62019-09-21 18:51:25 +03001940 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001942 MBEDTLS_SSL_DEBUG_MSG( 1,
1943 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001944 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001945 }
1946
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001947 /*
1948 * Note: we currently ignore the PKS identity hint, as we only allow one
1949 * PSK to be provisionned on the client. This could be changed later if
1950 * someone needs that feature.
1951 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001952 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001953 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001954
1955 return( ret );
1956}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001957#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1960 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001961/*
1962 * Generate a pre-master secret and encrypt it with the server's RSA key
1963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001965 size_t offset, size_t *olen,
1966 size_t pms_offset )
1967{
Janos Follath865b3eb2019-12-16 11:46:15 +00001968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001969 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001970 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001971 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001972
Angus Grattond8213d02016-05-25 20:56:48 +10001973 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001974 {
1975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1976 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1977 }
1978
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001979 /*
1980 * Generate (part of) the pre-master as
1981 * struct {
1982 * ProtocolVersion client_version;
1983 * opaque random[46];
1984 * } PreMasterSecret;
1985 */
Ronald Cron4e263fd2022-03-15 15:54:17 +01001986 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
1987 MBEDTLS_SSL_MINOR_VERSION_3,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001988 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001989
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001990 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001993 return( ret );
1994 }
1995
1996 ssl->handshake->pmslen = 48;
1997
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001998#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1999 peer_pk = &ssl->handshake->peer_pubkey;
2000#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002001 if( ssl->session_negotiate->peer_cert == NULL )
2002 {
Hanno Becker8273df82019-02-06 17:37:32 +00002003 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002006 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002007 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2008#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002009
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002010 /*
2011 * Now write it out, encrypted
2012 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002013 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2016 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002017 }
2018
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002019 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002020 p, ssl->handshake->pmslen,
2021 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002022 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002023 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002026 return( ret );
2027 }
2028
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002029 if( len_bytes == 2 )
2030 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002031 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002032 *olen += 2;
2033 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002034
Hanno Beckerae553dd2019-02-08 14:06:00 +00002035#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2036 /* We don't need the peer's public key anymore. Free it. */
2037 mbedtls_pk_free( peer_pk );
2038#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002039 return( 0 );
2040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2042 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002043
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002044#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2045 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2046 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002048 unsigned char **p,
2049 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 mbedtls_md_type_t *md_alg,
2051 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 *md_alg = MBEDTLS_MD_NONE;
2054 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002055
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002056 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002057 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002058
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002059 /*
2060 * Get hash algorithm
2061 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002062 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2063 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002064 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002065 MBEDTLS_SSL_DEBUG_MSG( 1,
2066 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002067 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002068 }
2069
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002070 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002071 * Get signature algorithm
2072 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002073 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2074 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002075 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002076 MBEDTLS_SSL_DEBUG_MSG( 1,
2077 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002078 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002079 }
2080
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002081 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002082 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002083 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002084 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002085 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002086 MBEDTLS_SSL_DEBUG_MSG( 1,
2087 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002088 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002089 }
2090
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2092 (*p)[1] ) );
2093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2094 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002095 *p += 2;
2096
2097 return( 0 );
2098}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002099#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2100 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2101 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2104 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2105static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002106{
Janos Follath865b3eb2019-12-16 11:46:15 +00002107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002109 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002110
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002111#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2112 peer_pk = &ssl->handshake->peer_pubkey;
2113#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002114 if( ssl->session_negotiate->peer_cert == NULL )
2115 {
Hanno Becker8273df82019-02-06 17:37:32 +00002116 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002118 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002119 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002120 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2121#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002122
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002123 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2126 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002127 }
2128
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002129 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002130
Przemek Stekielea4000f2022-03-16 09:49:33 +01002131#if defined(MBEDTLS_USE_PSA_CRYPTO)
2132 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002133 size_t olen = 0;
2134
2135 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2136 {
2137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2138 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2139 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002140
2141 ssl->handshake->ecdh_psa_type =
2142 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2143 &ecdh_bits ) );
2144
2145 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2146 {
2147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2148 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2149 }
2150
2151 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2152
Przemek Stekielea4000f2022-03-16 09:49:33 +01002153 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002154 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2155 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2156 ssl->handshake->ecdh_psa_peerkey,
2157 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002158
Przemek Stekiel561a4232022-03-16 13:16:24 +01002159 if ( ret != 0 )
2160 {
2161 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2162 return( ret );
2163 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002164
Przemek Stekiel561a4232022-03-16 13:16:24 +01002165 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002166#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2168 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002171 return( ret );
2172 }
2173
2174 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002177 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002178 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002179#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002180#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2181 /* We don't need the peer's public key anymore. Free it,
2182 * so that more RAM is available for upcoming expensive
2183 * operations like ECDHE. */
2184 mbedtls_pk_free( peer_pk );
2185#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2186
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002187 return( ret );
2188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2190 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002193{
Janos Follath865b3eb2019-12-16 11:46:15 +00002194 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002195 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002196 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002197 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2202 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 ssl->state++;
2206 return( 0 );
2207 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002208 ((void) p);
2209 ((void) end);
2210#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2213 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2214 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2215 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002216 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002217 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002220 mbedtls_ssl_send_alert_message(
2221 ssl,
2222 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2223 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002224 return( ret );
2225 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002228 ssl->state++;
2229 return( 0 );
2230 }
2231 ((void) p);
2232 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2234 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002235
Gilles Peskineeccd8882020-03-10 12:19:08 +01002236#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002237 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002238 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002239 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002240 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002241 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002242#endif
2243
Hanno Becker327c93b2018-08-15 13:56:18 +01002244 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 return( ret );
2248 }
2249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002253 mbedtls_ssl_send_alert_message(
2254 ssl,
2255 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2256 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 }
2259
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002260 /*
2261 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2262 * doesn't use a psk_identity_hint
2263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2267 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002268 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002269 /* Current message is probably either
2270 * CertificateRequest or ServerHelloDone */
2271 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002272 goto exit;
2273 }
2274
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002275 MBEDTLS_SSL_DEBUG_MSG( 1,
2276 ( "server key exchange message must not be skipped" ) );
2277 mbedtls_ssl_send_alert_message(
2278 ssl,
2279 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2280 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 }
2284
Gilles Peskineeccd8882020-03-10 12:19:08 +01002285#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002286 if( ssl->handshake->ecrs_enabled )
2287 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2288
2289start_processing:
2290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002292 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002294
Gilles Peskineeccd8882020-03-10 12:19:08 +01002295#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2297 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2298 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2299 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002300 {
2301 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002304 mbedtls_ssl_send_alert_message(
2305 ssl,
2306 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002307 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002308 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002309 }
2310 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002311#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2314 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2315 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2316 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002317 ; /* nothing more to do */
2318 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2320 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2321#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2322 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2323 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2324 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002326 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002329 mbedtls_ssl_send_alert_message(
2330 ssl,
2331 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2332 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002333 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002334 }
2335 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2338 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002339#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2340 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2342 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2343 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2344 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002345 {
2346 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002349 mbedtls_ssl_send_alert_message(
2350 ssl,
2351 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2352 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002353 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002354 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002355 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2358 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2359 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002360#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2361 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2362 {
2363 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2364 p, end - p );
2365 if( ret != 0 )
2366 {
2367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002368 mbedtls_ssl_send_alert_message(
2369 ssl,
2370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002371 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2372 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002373 }
2374 }
2375 else
2376#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2379 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002381
Gilles Peskineeccd8882020-03-10 12:19:08 +01002382#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002383 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002384 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002385 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002386#if defined(MBEDTLS_USE_PSA_CRYPTO)
2387 unsigned char hash[PSA_HASH_MAX_SIZE];
2388#else
2389 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2392 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2393 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002394 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002395 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002396
Hanno Beckera6899bb2019-02-06 18:26:03 +00002397 mbedtls_pk_context * peer_pk;
2398
Paul Bakker29e1f122013-04-16 13:07:56 +02002399 /*
2400 * Handle the digitally-signed structure
2401 */
Ronald Cron90915f22022-03-07 11:11:36 +01002402 if( ssl_parse_signature_algorithm( ssl, &p, end,
2403 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002404 {
Ronald Cron90915f22022-03-07 11:11:36 +01002405 MBEDTLS_SSL_DEBUG_MSG( 1,
2406 ( "bad server key exchange message" ) );
2407 mbedtls_ssl_send_alert_message(
2408 ssl,
2409 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2410 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2411 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002412 }
Ronald Cron90915f22022-03-07 11:11:36 +01002413
2414 if( pk_alg !=
2415 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002416 {
Ronald Cron90915f22022-03-07 11:11:36 +01002417 MBEDTLS_SSL_DEBUG_MSG( 1,
2418 ( "bad server key exchange message" ) );
2419 mbedtls_ssl_send_alert_message(
2420 ssl,
2421 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2422 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2423 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002424 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002425
2426 /*
2427 * Read signature
2428 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002429
2430 if( p > end - 2 )
2431 {
2432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002433 mbedtls_ssl_send_alert_message(
2434 ssl,
2435 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2436 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002437 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002438 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002439 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002440 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002441
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002442 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002445 mbedtls_ssl_send_alert_message(
2446 ssl,
2447 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2448 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002449 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002450 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002453
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002454 /*
2455 * Compute the hash that has been signed
2456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002458 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002459 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2460 params, params_len,
2461 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002462 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002463 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002464 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002465 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002469 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002470
Gilles Peskineca1d7422018-04-24 11:53:22 +02002471 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002472
Hanno Beckera6899bb2019-02-06 18:26:03 +00002473#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2474 peer_pk = &ssl->handshake->peer_pubkey;
2475#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002476 if( ssl->session_negotiate->peer_cert == NULL )
2477 {
Hanno Becker8273df82019-02-06 17:37:32 +00002478 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002481 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002482 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2483#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002484
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002485 /*
2486 * Verify signature
2487 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002488 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002491 mbedtls_ssl_send_alert_message(
2492 ssl,
2493 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2494 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002496 }
2497
Gilles Peskineeccd8882020-03-10 12:19:08 +01002498#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002499 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002500 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002501#endif
2502
Hanno Beckera6899bb2019-02-06 18:26:03 +00002503 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002504 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002505 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002506#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002507 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2508#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002509 mbedtls_ssl_send_alert_message(
2510 ssl,
2511 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2512 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002514#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002515 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2516 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2517#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002518 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002519 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002520
2521#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2522 /* We don't need the peer's public key anymore. Free it,
2523 * so that more RAM is available for upcoming expensive
2524 * operations like ECDHE. */
2525 mbedtls_pk_free( peer_pk );
2526#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002528#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002530exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 ssl->state++;
2532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
2535 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536}
2537
Gilles Peskineeccd8882020-03-10 12:19:08 +01002538#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002540{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002541 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002542 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002545
Hanno Becker1aa267c2017-04-28 17:08:27 +01002546 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002549 ssl->state++;
2550 return( 0 );
2551 }
2552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002555}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002556#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002558{
Janos Follath865b3eb2019-12-16 11:46:15 +00002559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002560 unsigned char *buf;
2561 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002562 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002563 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002564 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002565 size_t sig_alg_len;
2566#if defined(MBEDTLS_DEBUG_C)
2567 unsigned char *sig_alg;
2568#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Hanno Becker1aa267c2017-04-28 17:08:27 +01002572 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002575 ssl->state++;
2576 return( 0 );
2577 }
2578
Hanno Becker327c93b2018-08-15 13:56:18 +01002579 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2582 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
2584
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002585 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2586 {
2587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002588 mbedtls_ssl_send_alert_message(
2589 ssl,
2590 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2591 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002592 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002595 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002596 ssl->handshake->client_auth =
2597 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002600 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Jerry Yufb28b882022-01-28 11:05:58 +08002602 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002603 {
Johan Pascala89ca862020-08-25 10:03:19 +02002604 /* Current message is probably the ServerHelloDone */
2605 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002606 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002607 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002608
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002609 /*
2610 * struct {
2611 * ClientCertificateType certificate_types<1..2^8-1>;
2612 * SignatureAndHashAlgorithm
2613 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2614 * DistinguishedName certificate_authorities<0..2^16-1>;
2615 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002616 *
2617 * Since we only support a single certificate on clients, let's just
2618 * ignore all the information that's supposed to help us pick a
2619 * certificate.
2620 *
2621 * We could check that our certificate matches the request, and bail out
2622 * if it doesn't, but it's simpler to just send the certificate anyway,
2623 * and give the server the opportunity to decide if it should terminate
2624 * the connection when it doesn't like our certificate.
2625 *
2626 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2627 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002628 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002629 *
2630 * However, we still minimally parse the message to check it is at least
2631 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002632 */
Paul Bakker926af752012-11-23 13:38:07 +01002633 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002634
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002635 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002636 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2637 {
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2639 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2640 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002641 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002644 n = cert_type_len;
2645
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002646 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002647 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002648 * * the length of the signature algorithms field (if minor version of
2649 * SSL is 3),
2650 * * distinguished name length otherwise.
2651 * Both reach at most the index:
2652 * ...hdr_len + 2 + n,
2653 * therefore the buffer length at this point must be greater than that
2654 * regardless of the actual code path.
2655 */
2656 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002659 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2660 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002661 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002662 }
2663
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002664 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002665 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2666 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2667
2668 /*
2669 * The furthest access in buf is in the loop few lines below:
2670 * sig_alg[i + 1],
2671 * where:
2672 * sig_alg = buf + ...hdr_len + 3 + n,
2673 * max(i) = sig_alg_len - 1.
2674 * Therefore the furthest access is:
2675 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2676 * which reduces to:
2677 * buf[...hdr_len + 3 + n + sig_alg_len],
2678 * which is one less than we need the buf to be.
2679 */
2680 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002681 {
Ronald Cron90915f22022-03-07 11:11:36 +01002682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2683 mbedtls_ssl_send_alert_message(
2684 ssl,
2685 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2686 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2687 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002688 }
Paul Bakker926af752012-11-23 13:38:07 +01002689
Ronald Cron90915f22022-03-07 11:11:36 +01002690#if defined(MBEDTLS_DEBUG_C)
2691 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2692 for( size_t i = 0; i < sig_alg_len; i += 2 )
2693 {
2694 MBEDTLS_SSL_DEBUG_MSG( 3,
2695 ( "Supported Signature Algorithm found: %d,%d",
2696 sig_alg[i], sig_alg[i + 1] ) );
2697 }
2698#endif
2699
2700 n += 2 + sig_alg_len;
2701
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002702 /* certificate_authorities */
2703 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2704 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002705
2706 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002707 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002710 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2711 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002712 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002713 }
2714
2715exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
2718 return( 0 );
2719}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002720#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723{
Janos Follath865b3eb2019-12-16 11:46:15 +00002724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Hanno Becker327c93b2018-08-15 13:56:18 +01002728 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2731 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002733
2734 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2735 {
2736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2737 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2741 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002744 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2745 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002746 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
2749 ssl->state++;
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002752 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002754#endif
2755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
2758 return( 0 );
2759}
2760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002762{
Janos Follath865b3eb2019-12-16 11:46:15 +00002763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002764
2765 size_t header_len;
2766 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002767 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002768 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2773 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 /*
2776 * DHM key exchange -- send G^X mod P
2777 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002778 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Joe Subbiani6dd73642021-07-19 11:56:54 +01002780 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002781 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002784 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002785 &ssl->out_msg[header_len], content_len,
2786 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 if( ret != 0 )
2788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 return( ret );
2791 }
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2794 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002797 ssl->handshake->premaster,
2798 MBEDTLS_PREMASTER_SIZE,
2799 &ssl->handshake->pmslen,
2800 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 return( ret );
2804 }
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 }
2808 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002810#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2811 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2812 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2813 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2814#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002815 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002816 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2817 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2818 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002819 {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002820 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2821 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002822 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002823
2824 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2825
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002826 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002827
Hanno Becker0a94a642019-01-11 14:35:30 +00002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2829
Hanno Becker4a63ed42019-01-08 11:39:35 +00002830 /*
2831 * Generate EC private key for ECDHE exchange.
2832 */
2833
Hanno Becker4a63ed42019-01-08 11:39:35 +00002834 /* The master secret is obtained from the shared ECDH secret by
2835 * applying the TLS 1.2 PRF with a specific salt and label. While
2836 * the PSA Crypto API encourages combining key agreement schemes
2837 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2838 * yet support the provisioning of salt + label to the KDF.
2839 * For the time being, we therefore need to split the computation
2840 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002841 key_attributes = psa_key_attributes_init();
2842 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002843 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002844 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2845 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002846
2847 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002848 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002849 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002850 if( status != PSA_SUCCESS )
2851 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2852
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002853 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002854 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002855 * but we just need to add a length byte before that. */
2856 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2857 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2858 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2859 size_t own_pubkey_len;
2860
Hanno Becker4a63ed42019-01-08 11:39:35 +00002861 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002862 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002863 &own_pubkey_len );
2864 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002865 {
2866 psa_destroy_key( handshake->ecdh_psa_privkey );
2867 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002868 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002869 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002870
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002871 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2872 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002873
Hanno Becker4a63ed42019-01-08 11:39:35 +00002874 /* The ECDH secret is the premaster secret used for key derivation. */
2875
Janos Follathdf3b0892019-08-08 11:12:24 +01002876 /* Compute ECDH shared secret. */
2877 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2878 handshake->ecdh_psa_privkey,
2879 handshake->ecdh_psa_peerkey,
2880 handshake->ecdh_psa_peerkey_len,
2881 ssl->handshake->premaster,
2882 sizeof( ssl->handshake->premaster ),
2883 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002884
Andrzej Kureka0237f82022-02-24 13:24:52 -05002885 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002886 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002887
2888 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2889 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002890 }
2891 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002892#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2894 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2895 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2896 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002897 {
2898 /*
2899 * ECDH key exchange -- send client public value
2900 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002901 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002902
Gilles Peskineeccd8882020-03-10 12:19:08 +01002903#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002904 if( ssl->handshake->ecrs_enabled )
2905 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002906 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002907 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002908
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002909 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2910 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002911#endif
2912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002914 &content_len,
2915 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002916 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002917 if( ret != 0 )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002920#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002921 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2922 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2923#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002924 return( ret );
2925 }
2926
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002927 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2928 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002929
Gilles Peskineeccd8882020-03-10 12:19:08 +01002930#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002931 if( ssl->handshake->ecrs_enabled )
2932 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002933 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002934 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002935 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002936
2937ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002938 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002939 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002942 &ssl->handshake->pmslen,
2943 ssl->handshake->premaster,
2944 MBEDTLS_MPI_MAX_SIZE,
2945 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002948#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002949 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2950 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2951#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002952 return( ret );
2953 }
2954
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002955 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2956 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01002957 }
2958 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002959#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2961 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2962 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2963 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002964#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2965 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2966 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2967 {
2968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2969 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2970 psa_key_attributes_t key_attributes;
2971
2972 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2973
2974 /*
2975 * opaque psk_identity<0..2^16-1>;
2976 */
2977 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002978 /* We don't offer PSK suites if we don't have a PSK,
2979 * and we check that the server's choice is among the
2980 * ciphersuites we offered, so this should never happen. */
2981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002982
2983 /* Opaque PSKs are currently only supported for PSK-only suites. */
2984 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
2985 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2986
Neil Armstrongfc834f22022-03-23 17:54:38 +01002987 /* uint16 to store content length */
2988 const size_t content_len_size = 2;
2989
Neil Armstrong868af822022-03-09 10:26:25 +01002990 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002991
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002992 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002993 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002994 {
2995 MBEDTLS_SSL_DEBUG_MSG( 1,
2996 ( "psk identity too long or SSL buffer too short" ) );
2997 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2998 }
2999
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003000 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003001
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003002 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
3003 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
3004 header_len += content_len_size;
3005
3006 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003007 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003008 p += ssl->conf->psk_identity_len;
3009
Neil Armstrong868af822022-03-09 10:26:25 +01003010 header_len += ssl->conf->psk_identity_len;
3011
3012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3013
3014 /*
3015 * Generate EC private key for ECDHE exchange.
3016 */
3017
3018 /* The master secret is obtained from the shared ECDH secret by
3019 * applying the TLS 1.2 PRF with a specific salt and label. While
3020 * the PSA Crypto API encourages combining key agreement schemes
3021 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3022 * yet support the provisioning of salt + label to the KDF.
3023 * For the time being, we therefore need to split the computation
3024 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3025 key_attributes = psa_key_attributes_init();
3026 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3027 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3028 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3029 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3030
3031 /* Generate ECDH private key. */
3032 status = psa_generate_key( &key_attributes,
3033 &handshake->ecdh_psa_privkey );
3034 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003035 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003036
3037 /* Export the public part of the ECDH private key from PSA.
3038 * The export format is an ECPoint structure as expected by TLS,
3039 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003040 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003041 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3042 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003043 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003044
3045 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3046 own_pubkey, own_pubkey_max_len,
3047 &own_pubkey_len );
3048 if( status != PSA_SUCCESS )
3049 {
3050 psa_destroy_key( handshake->ecdh_psa_privkey );
3051 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003052 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003053 }
3054
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003055 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003056 content_len = own_pubkey_len + 1;
3057
Neil Armstrong25400452022-03-23 17:44:07 +01003058 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3059 * - a uint16 containing the length (in octets) of the ECDH computation
3060 * - the octet string produced by the ECDH computation
3061 * - a uint16 containing the length (in octets) of the PSK
3062 * - the PSK itself
3063 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003064 unsigned char *pms = ssl->handshake->premaster;
3065 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003066 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003067 /* uint16 to store length (in octets) of the ECDH computation */
3068 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003069 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003070
Neil Armstrong25400452022-03-23 17:44:07 +01003071 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003072 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3073 handshake->ecdh_psa_privkey,
3074 handshake->ecdh_psa_peerkey,
3075 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003076 pms + zlen_size,
3077 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003078 &zlen );
3079
3080 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3081 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3082
Neil Armstrongc530aa62022-03-23 17:45:01 +01003083 if( status != PSA_SUCCESS )
3084 return( psa_ssl_status_to_mbedtls( status ) );
3085 else if( destruction_status != PSA_SUCCESS )
3086 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003087
Neil Armstrong25400452022-03-23 17:44:07 +01003088 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003089 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3090 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003091
Neil Armstrong868af822022-03-09 10:26:25 +01003092 const unsigned char *psk = NULL;
3093 size_t psk_len = 0;
3094
3095 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
3096 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Neil Armstrong868af822022-03-09 10:26:25 +01003097 /*
3098 * This should never happen because the existence of a PSK is always
3099 * checked before calling this function
3100 */
3101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003102
Neil Armstronge18ff952022-04-04 18:34:55 +02003103 /* opaque psk<0..2^16-1>; */
3104 if( (size_t)( pms_end - pms ) < ( 2 + psk_len ) )
3105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3106
Neil Armstrong25400452022-03-23 17:44:07 +01003107 /* Write the PSK length as uint16 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003108 MBEDTLS_PUT_UINT16_BE( psk_len, pms, 0 );
3109 pms += 2;
Neil Armstrong868af822022-03-09 10:26:25 +01003110
Neil Armstrong25400452022-03-23 17:44:07 +01003111 /* Write the PSK itself */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003112 memcpy( pms, psk, psk_len );
3113 pms += psk_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003114
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003115 ssl->handshake->pmslen = pms - ssl->handshake->premaster;
Neil Armstrong868af822022-03-09 10:26:25 +01003116 }
3117 else
3118#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3119 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003120#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003121 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003122 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003123 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003124 * opaque psk_identity<0..2^16-1>;
3125 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003126 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003127 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003128 /* We don't offer PSK suites if we don't have a PSK,
3129 * and we check that the server's choice is among the
3130 * ciphersuites we offered, so this should never happen. */
3131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003132 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003133
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003134 header_len = 4;
3135 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003136
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003137 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003138 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003139 MBEDTLS_SSL_DEBUG_MSG( 1,
3140 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3142 }
3143
Joe Subbianifbeb6922021-07-16 14:27:50 +01003144 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3145 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003146
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003147 memcpy( ssl->out_msg + header_len,
3148 ssl->conf->psk_identity,
3149 ssl->conf->psk_identity_len );
3150 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3153 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003154 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003155 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003156 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003157 else
3158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3160 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003161 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003162#if defined(MBEDTLS_USE_PSA_CRYPTO)
3163 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003164 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003165 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3166#endif /* MBEDTLS_USE_PSA_CRYPTO */
3167
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003168 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3169 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003170 return( ret );
3171 }
3172 else
3173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3175 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003176 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003177#if defined(MBEDTLS_USE_PSA_CRYPTO)
3178 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003179 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003180 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3181#endif /* MBEDTLS_USE_PSA_CRYPTO */
3182
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003183 /*
3184 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3185 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003186 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003187
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003188 if( header_len + 2 + content_len >
3189 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003190 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003191 MBEDTLS_SSL_DEBUG_MSG( 1,
3192 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003193 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3194 }
3195
Joe Subbianifbeb6922021-07-16 14:27:50 +01003196 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3197 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003200 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003201 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003202 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003203 if( ret != 0 )
3204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003206 return( ret );
3207 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003208 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003209 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003211#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3212 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003214 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003215#if defined(MBEDTLS_USE_PSA_CRYPTO)
3216 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003217 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003218 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3219#endif /* MBEDTLS_USE_PSA_CRYPTO */
3220
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003221 /*
3222 * ClientECDiffieHellmanPublic public;
3223 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003224 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3225 &content_len,
3226 &ssl->out_msg[header_len],
3227 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003228 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003229 if( ret != 0 )
3230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003232 return( ret );
3233 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003234
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003235 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3236 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003237 }
3238 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003239#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243 }
3244
Hanno Beckerafd311e2018-10-23 15:26:40 +01003245#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3246 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3247 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003248 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003249 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003250 MBEDTLS_SSL_DEBUG_MSG( 1,
3251 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003252 }
3253 else
3254#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3255 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003257 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003259 MBEDTLS_SSL_DEBUG_RET( 1,
3260 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003261 return( ret );
3262 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003263 }
3264 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003265#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3267 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003269 header_len = 4;
3270 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3271 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003272 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 }
Paul Bakkered27a042013-04-18 22:46:23 +02003274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003276#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3277 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3278 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003279 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003280
3281 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003282 ssl->out_msg + header_len,
3283 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3284 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003285 ssl->conf->f_rng, ssl->conf->p_rng );
3286 if( ret != 0 )
3287 {
3288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3289 return( ret );
3290 }
3291
3292 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3293 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3294 ssl->conf->f_rng, ssl->conf->p_rng );
3295 if( ret != 0 )
3296 {
3297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3298 return( ret );
3299 }
3300 }
3301 else
3302#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003303 {
3304 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003309 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3311 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
3313 ssl->state++;
3314
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003315 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 return( ret );
3319 }
3320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003322
3323 return( 0 );
3324}
3325
Gilles Peskineeccd8882020-03-10 12:19:08 +01003326#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003328{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003329 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003330 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003338 return( ret );
3339 }
3340
Hanno Becker77adddc2019-02-07 12:32:43 +00003341 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003344 ssl->state++;
3345 return( 0 );
3346 }
3347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003350}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003351#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003353{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003355 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003356 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003357 size_t n = 0, offset = 0;
3358 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003359 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003361 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003362 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003363#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3364 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3365#else
3366 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3367#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003370
Gilles Peskineeccd8882020-03-10 12:19:08 +01003371#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003372 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003373 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003374 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003375 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003376 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003377#endif
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003382 return( ret );
3383 }
3384
Hanno Becker77adddc2019-02-07 12:32:43 +00003385 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003388 ssl->state++;
3389 return( 0 );
3390 }
3391
Jerry Yufb28b882022-01-28 11:05:58 +08003392 if( ssl->handshake->client_auth == 0 ||
3393 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 {
Johan Pascala89ca862020-08-25 10:03:19 +02003395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3396 ssl->state++;
3397 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 }
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 }
3405
3406 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003407 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003409#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003410 if( ssl->handshake->ecrs_enabled )
3411 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3412
3413sign:
3414#endif
3415
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003416 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003417
Ronald Cron90915f22022-03-07 11:11:36 +01003418 /*
3419 * digitally-signed struct {
3420 * opaque handshake_messages[handshake_messages_length];
3421 * };
3422 *
3423 * Taking shortcut here. We assume that the server always allows the
3424 * PRF Hash function and has sent it in the allowed signature
3425 * algorithms list received in the Certificate Request message.
3426 *
3427 * Until we encounter a server that does not, we will take this
3428 * shortcut.
3429 *
3430 * Reason: Otherwise we should have running hashes for SHA512 and
3431 * SHA224 in order to satisfy 'weird' needs from the server
3432 * side.
3433 */
3434 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003435 {
Ronald Cron90915f22022-03-07 11:11:36 +01003436 md_alg = MBEDTLS_MD_SHA384;
3437 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003438 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003439 else
Paul Bakker577e0062013-08-28 11:57:20 +02003440 {
Ronald Cron90915f22022-03-07 11:11:36 +01003441 md_alg = MBEDTLS_MD_SHA256;
3442 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003443 }
Ronald Cron90915f22022-03-07 11:11:36 +01003444 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3445
3446 /* Info from md_alg will be used instead */
3447 hashlen = 0;
3448 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003449
Gilles Peskineeccd8882020-03-10 12:19:08 +01003450#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003451 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003452 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003453#endif
3454
3455 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3456 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003457 ssl->out_msg + 6 + offset,
3458 out_buf_len - 6 - offset,
3459 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003460 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003463#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003464 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3465 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3466#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003467 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003468 }
Paul Bakker926af752012-11-23 13:38:07 +01003469
Joe Subbiani6dd73642021-07-19 11:56:54 +01003470 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
Paul Bakker1ef83d62012-04-11 12:09:53 +00003472 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3474 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003475
3476 ssl->state++;
3477
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003478 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003479 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 return( ret );
3482 }
3483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
Paul Bakkered27a042013-04-18 22:46:23 +02003486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003487}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003488#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3491static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003492{
Janos Follath865b3eb2019-12-16 11:46:15 +00003493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003494 uint32_t lifetime;
3495 size_t ticket_len;
3496 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003497 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003500
Hanno Becker327c93b2018-08-15 13:56:18 +01003501 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003504 return( ret );
3505 }
3506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003510 mbedtls_ssl_send_alert_message(
3511 ssl,
3512 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3513 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003515 }
3516
3517 /*
3518 * struct {
3519 * uint32 ticket_lifetime_hint;
3520 * opaque ticket<0..2^16-1>;
3521 * } NewSessionTicket;
3522 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003523 * 0 . 3 ticket_lifetime_hint
3524 * 4 . 5 ticket_len (n)
3525 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3528 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003531 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3532 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003533 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003534 }
3535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003537
Philippe Antoineb5b25432018-05-11 11:06:29 +02003538 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3539 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003540
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003541 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003546 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3547 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003548 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003549 }
3550
Paul Elliottd48d5c62021-01-07 14:47:05 +00003551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003552
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003553 /* We're not waiting for a NewSessionTicket message any more */
3554 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003556
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003557 /*
3558 * Zero-length ticket means the server changed his mind and doesn't want
3559 * to send a ticket after all, so just forget it
3560 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003561 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003562 return( 0 );
3563
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003564 if( ssl->session != NULL && ssl->session->ticket != NULL )
3565 {
3566 mbedtls_platform_zeroize( ssl->session->ticket,
3567 ssl->session->ticket_len );
3568 mbedtls_free( ssl->session->ticket );
3569 ssl->session->ticket = NULL;
3570 ssl->session->ticket_len = 0;
3571 }
3572
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003573 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3574 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003576 ssl->session_negotiate->ticket = NULL;
3577 ssl->session_negotiate->ticket_len = 0;
3578
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003579 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003580 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003582 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3583 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003584 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003585 }
3586
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003587 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003588
3589 ssl->session_negotiate->ticket = ticket;
3590 ssl->session_negotiate->ticket_len = ticket_len;
3591 ssl->session_negotiate->ticket_lifetime = lifetime;
3592
3593 /*
3594 * RFC 5077 section 3.4:
3595 * "If the client receives a session ticket from the server, then it
3596 * discards any Session ID that was sent in the ServerHello."
3597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003599 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003602
3603 return( 0 );
3604}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003606
Paul Bakker5121ce52009-01-03 21:22:43 +00003607/*
Paul Bakker1961b702013-01-25 14:49:24 +01003608 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003611{
3612 int ret = 0;
3613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003615 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3617 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003618 ssl->handshake->new_session_ticket != 0 )
3619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003621 }
3622#endif
3623
Paul Bakker1961b702013-01-25 14:49:24 +01003624 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 case MBEDTLS_SSL_HELLO_REQUEST:
3627 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 break;
3629
Paul Bakker1961b702013-01-25 14:49:24 +01003630 /*
3631 * ==> ClientHello
3632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003634 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003635 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
Paul Bakker1961b702013-01-25 14:49:24 +01003637 /*
3638 * <== ServerHello
3639 * Certificate
3640 * ( ServerKeyExchange )
3641 * ( CertificateRequest )
3642 * ServerHelloDone
3643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003645 ret = ssl_parse_server_hello( ssl );
3646 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3649 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003650 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003653 ret = ssl_parse_server_key_exchange( ssl );
3654 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003657 ret = ssl_parse_certificate_request( ssl );
3658 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003661 ret = ssl_parse_server_hello_done( ssl );
3662 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Paul Bakker1961b702013-01-25 14:49:24 +01003664 /*
3665 * ==> ( Certificate/Alert )
3666 * ClientKeyExchange
3667 * ( CertificateVerify )
3668 * ChangeCipherSpec
3669 * Finished
3670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3672 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003673 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003676 ret = ssl_write_client_key_exchange( ssl );
3677 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003680 ret = ssl_write_certificate_verify( ssl );
3681 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3684 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003685 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 case MBEDTLS_SSL_CLIENT_FINISHED:
3688 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003689 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
Paul Bakker1961b702013-01-25 14:49:24 +01003691 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003692 * <== ( NewSessionTicket )
3693 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003694 * Finished
3695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3697 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003698 ret = ssl_parse_new_session_ticket( ssl );
3699 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003700#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3703 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003704 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 case MBEDTLS_SSL_SERVER_FINISHED:
3707 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003708 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 case MBEDTLS_SSL_FLUSH_BUFFERS:
3711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3712 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003713 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3716 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003717 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003718
Paul Bakker1961b702013-01-25 14:49:24 +01003719 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003722 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
3724 return( ret );
3725}
Jerry Yuc5aef882021-12-23 20:15:02 +08003726
Jerry Yufb4b6472022-01-27 15:03:26 +08003727#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */