blob: af8e9ee6f44f100d0809f8691f28fae140ea2b2b [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1775 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1776 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Przemek Stekiel068a6b42022-03-17 07:54:09 +01001777 ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
1778 ( defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1779 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001783 mbedtls_ecp_group_id grp_id;
1784#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1785 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1786#else
1787 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1788#endif
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001789
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001790 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001791 if( curve_info == NULL )
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001795 }
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001798
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001799 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001800 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001801
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001802 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1803 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001804
1805 return( 0 );
1806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1808 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1809 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Przemek Stekiel068a6b42022-03-17 07:54:09 +01001810 ( !MBEDTLS_USE_PSA_CRYPTO &&
1811 ( MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1812 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED ) ) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001813
Hanno Beckerbb89e272019-01-08 12:54:37 +00001814#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1815 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Neil Armstrong868af822022-03-09 10:26:25 +01001816 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Hanno Beckerbb89e272019-01-08 12:54:37 +00001817 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
1818static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl,
1819 unsigned char **p,
1820 unsigned char *end )
1821{
1822 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001823 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001824 uint8_t ecpoint_len;
1825 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1826
1827 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001828 * struct {
1829 * ECParameters curve_params;
1830 * ECPoint public;
1831 * } ServerECDHParams;
1832 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001833 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001834 * 2..3 NamedCurve
1835 * 4 ECPoint.len
1836 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001837 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001838 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001839 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001840
1841 /* First byte is curve_type; only named_curve is handled */
1842 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001843 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001844
1845 /* Next two bytes are the namedcurve value */
1846 tls_id = *(*p)++;
1847 tls_id <<= 8;
1848 tls_id |= *(*p)++;
1849
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001850 /* Check it's a curve we offered */
1851 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001852 {
1853 MBEDTLS_SSL_DEBUG_MSG( 2,
1854 ( "bad server key exchange message (ECDHE curve): %u",
1855 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001856 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001857 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001858
Hanno Beckerbb89e272019-01-08 12:54:37 +00001859 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001860 if( ( handshake->ecdh_psa_type =
1861 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001862 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001863 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001864 }
Gilles Peskine42459802019-12-19 13:31:53 +01001865 if( ecdh_bits > 0xffff )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001866 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Gilles Peskine42459802019-12-19 13:31:53 +01001867 handshake->ecdh_bits = (uint16_t) ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001868
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001869 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001870 ecpoint_len = *(*p)++;
1871 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001872 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001873
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001874 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1875 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001876
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001877 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1878 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001879 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001880
Hanno Beckerbb89e272019-01-08 12:54:37 +00001881 return( 0 );
1882}
1883#endif /* MBEDTLS_USE_PSA_CRYPTO &&
1884 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1885 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
1886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1888 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1889 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1890static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001891 unsigned char **p,
1892 unsigned char *end )
1893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001895
Paul Bakker29e1f122013-04-16 13:07:56 +02001896 /*
1897 * Ephemeral ECDH parameters:
1898 *
1899 * struct {
1900 * ECParameters curve_params;
1901 * ECPoint public;
1902 * } ServerECDHParams;
1903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001905 (const unsigned char **) p, end ) ) != 0 )
1906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001908#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001909 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1910 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001911#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001912 return( ret );
1913 }
1914
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001915 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001916 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001917 MBEDTLS_SSL_DEBUG_MSG( 1,
1918 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001919 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001920 }
1921
Paul Bakker29e1f122013-04-16 13:07:56 +02001922 return( ret );
1923}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1925 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1926 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001927
Gilles Peskineeccd8882020-03-10 12:19:08 +01001928#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001930 unsigned char **p,
1931 unsigned char *end )
1932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001934 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001935 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001936
1937 /*
1938 * PSK parameters:
1939 *
1940 * opaque psk_identity_hint<0..2^16-1>;
1941 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001942 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001943 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001944 MBEDTLS_SSL_DEBUG_MSG( 1,
1945 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001946 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001947 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001948 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001949 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001950
irwir6527bd62019-09-21 18:51:25 +03001951 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001952 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001953 MBEDTLS_SSL_DEBUG_MSG( 1,
1954 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001955 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001956 }
1957
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001958 /*
1959 * Note: we currently ignore the PKS identity hint, as we only allow one
1960 * PSK to be provisionned on the client. This could be changed later if
1961 * someone needs that feature.
1962 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001963 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001964 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001965
1966 return( ret );
1967}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001968#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1971 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001972/*
1973 * Generate a pre-master secret and encrypt it with the server's RSA key
1974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001976 size_t offset, size_t *olen,
1977 size_t pms_offset )
1978{
Janos Follath865b3eb2019-12-16 11:46:15 +00001979 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001980 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001981 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001982 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001983
Angus Grattond8213d02016-05-25 20:56:48 +10001984 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001985 {
1986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1987 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1988 }
1989
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001990 /*
1991 * Generate (part of) the pre-master as
1992 * struct {
1993 * ProtocolVersion client_version;
1994 * opaque random[46];
1995 * } PreMasterSecret;
1996 */
Ronald Cron4e263fd2022-03-15 15:54:17 +01001997 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
1998 MBEDTLS_SSL_MINOR_VERSION_3,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001999 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002000
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002001 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002004 return( ret );
2005 }
2006
2007 ssl->handshake->pmslen = 48;
2008
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002009#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2010 peer_pk = &ssl->handshake->peer_pubkey;
2011#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002012 if( ssl->session_negotiate->peer_cert == NULL )
2013 {
Hanno Becker8273df82019-02-06 17:37:32 +00002014 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002017 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002018 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2019#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002020
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002021 /*
2022 * Now write it out, encrypted
2023 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002024 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2027 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028 }
2029
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002030 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002031 p, ssl->handshake->pmslen,
2032 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002033 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002034 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002037 return( ret );
2038 }
2039
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002040 if( len_bytes == 2 )
2041 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002042 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002043 *olen += 2;
2044 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002045
Hanno Beckerae553dd2019-02-08 14:06:00 +00002046#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2047 /* We don't need the peer's public key anymore. Free it. */
2048 mbedtls_pk_free( peer_pk );
2049#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002050 return( 0 );
2051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2053 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002054
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002055#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2056 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2057 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002059 unsigned char **p,
2060 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 mbedtls_md_type_t *md_alg,
2062 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 *md_alg = MBEDTLS_MD_NONE;
2065 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002066
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002067 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002068 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002069
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002070 /*
2071 * Get hash algorithm
2072 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002073 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2074 == MBEDTLS_MD_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 HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002078 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002079 }
2080
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002081 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002082 * Get signature algorithm
2083 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002084 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2085 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002086 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002087 MBEDTLS_SSL_DEBUG_MSG( 1,
2088 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002089 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002090 }
2091
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002092 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002093 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002094 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002095 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002096 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002097 MBEDTLS_SSL_DEBUG_MSG( 1,
2098 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002099 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002100 }
2101
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2103 (*p)[1] ) );
2104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2105 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002106 *p += 2;
2107
2108 return( 0 );
2109}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002110#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2111 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2112 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2115 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2116static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002117{
Janos Follath865b3eb2019-12-16 11:46:15 +00002118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002120 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002121
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002122#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2123 peer_pk = &ssl->handshake->peer_pubkey;
2124#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002125 if( ssl->session_negotiate->peer_cert == NULL )
2126 {
Hanno Becker8273df82019-02-06 17:37:32 +00002127 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002130 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002131 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2132#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002133
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002134 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2137 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002138 }
2139
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002140 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002141
Przemek Stekielea4000f2022-03-16 09:49:33 +01002142#if defined(MBEDTLS_USE_PSA_CRYPTO)
2143 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002144 size_t olen = 0;
2145
2146 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2147 {
2148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2149 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2150 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002151
2152 ssl->handshake->ecdh_psa_type =
2153 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2154 &ecdh_bits ) );
2155
2156 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2157 {
2158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2159 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2160 }
2161
2162 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2163
Przemek Stekielea4000f2022-03-16 09:49:33 +01002164 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002165 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2166 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2167 ssl->handshake->ecdh_psa_peerkey,
2168 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002169
Przemek Stekiel561a4232022-03-16 13:16:24 +01002170 if ( ret != 0 )
2171 {
2172 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2173 return( ret );
2174 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002175
Przemek Stekiel561a4232022-03-16 13:16:24 +01002176 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002177#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2179 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002182 return( ret );
2183 }
2184
2185 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002188 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002189 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002190#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002191#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2192 /* We don't need the peer's public key anymore. Free it,
2193 * so that more RAM is available for upcoming expensive
2194 * operations like ECDHE. */
2195 mbedtls_pk_free( peer_pk );
2196#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2197
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002198 return( ret );
2199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2201 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002204{
Janos Follath865b3eb2019-12-16 11:46:15 +00002205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002207 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002208 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2213 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 ssl->state++;
2217 return( 0 );
2218 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002219 ((void) p);
2220 ((void) end);
2221#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2224 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2225 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2226 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002227 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002228 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002231 mbedtls_ssl_send_alert_message(
2232 ssl,
2233 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2234 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002235 return( ret );
2236 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002239 ssl->state++;
2240 return( 0 );
2241 }
2242 ((void) p);
2243 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2245 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002246
Gilles Peskineeccd8882020-03-10 12:19:08 +01002247#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002248 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002249 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002250 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002251 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002252 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002253#endif
2254
Hanno Becker327c93b2018-08-15 13:56:18 +01002255 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 return( ret );
2259 }
2260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002264 mbedtls_ssl_send_alert_message(
2265 ssl,
2266 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2267 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 }
2270
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002271 /*
2272 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2273 * doesn't use a psk_identity_hint
2274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2278 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002279 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002280 /* Current message is probably either
2281 * CertificateRequest or ServerHelloDone */
2282 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002283 goto exit;
2284 }
2285
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002286 MBEDTLS_SSL_DEBUG_MSG( 1,
2287 ( "server key exchange message must not be skipped" ) );
2288 mbedtls_ssl_send_alert_message(
2289 ssl,
2290 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2291 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 }
2295
Gilles Peskineeccd8882020-03-10 12:19:08 +01002296#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002297 if( ssl->handshake->ecrs_enabled )
2298 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2299
2300start_processing:
2301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002303 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002305
Gilles Peskineeccd8882020-03-10 12:19:08 +01002306#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2308 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2309 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2310 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002311 {
2312 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002315 mbedtls_ssl_send_alert_message(
2316 ssl,
2317 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002318 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002319 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002320 }
2321 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002322#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2325 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2326 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2327 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002328 ; /* nothing more to do */
2329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2331 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2332#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2333 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2334 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2335 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002337 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002340 mbedtls_ssl_send_alert_message(
2341 ssl,
2342 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2343 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002344 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002345 }
2346 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002347 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2349 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +00002350#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2351 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Neil Armstrong868af822022-03-09 10:26:25 +01002352 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Hanno Beckerbb89e272019-01-08 12:54:37 +00002353 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2354 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Neil Armstrong868af822022-03-09 10:26:25 +01002355 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
Hanno Beckerbb89e272019-01-08 12:54:37 +00002356 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2357 {
2358 if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
2359 {
2360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002361 mbedtls_ssl_send_alert_message(
2362 ssl,
2363 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2364 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01002365 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002366 }
2367 }
2368 else
2369#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2370 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Neil Armstrong868af822022-03-09 10:26:25 +01002371 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Hanno Beckerbb89e272019-01-08 12:54:37 +00002372 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2374 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2375 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2376 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2377 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2378 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002379 {
2380 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002383 mbedtls_ssl_send_alert_message(
2384 ssl,
2385 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2386 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002387 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002388 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002389 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2392 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2393 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002394#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2395 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2396 {
2397 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2398 p, end - p );
2399 if( ret != 0 )
2400 {
2401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002402 mbedtls_ssl_send_alert_message(
2403 ssl,
2404 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002405 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2406 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002407 }
2408 }
2409 else
2410#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002415
Gilles Peskineeccd8882020-03-10 12:19:08 +01002416#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002417 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002418 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002419 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002420#if defined(MBEDTLS_USE_PSA_CRYPTO)
2421 unsigned char hash[PSA_HASH_MAX_SIZE];
2422#else
2423 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2426 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2427 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002428 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002429 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002430
Hanno Beckera6899bb2019-02-06 18:26:03 +00002431 mbedtls_pk_context * peer_pk;
2432
Paul Bakker29e1f122013-04-16 13:07:56 +02002433 /*
2434 * Handle the digitally-signed structure
2435 */
Ronald Cron90915f22022-03-07 11:11:36 +01002436 if( ssl_parse_signature_algorithm( ssl, &p, end,
2437 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002438 {
Ronald Cron90915f22022-03-07 11:11:36 +01002439 MBEDTLS_SSL_DEBUG_MSG( 1,
2440 ( "bad server key exchange message" ) );
2441 mbedtls_ssl_send_alert_message(
2442 ssl,
2443 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2444 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2445 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002446 }
Ronald Cron90915f22022-03-07 11:11:36 +01002447
2448 if( pk_alg !=
2449 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002450 {
Ronald Cron90915f22022-03-07 11:11:36 +01002451 MBEDTLS_SSL_DEBUG_MSG( 1,
2452 ( "bad server key exchange message" ) );
2453 mbedtls_ssl_send_alert_message(
2454 ssl,
2455 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2456 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2457 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002458 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002459
2460 /*
2461 * Read signature
2462 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002463
2464 if( p > end - 2 )
2465 {
2466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002467 mbedtls_ssl_send_alert_message(
2468 ssl,
2469 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2470 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002471 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002472 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002473 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002474 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002475
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002476 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002479 mbedtls_ssl_send_alert_message(
2480 ssl,
2481 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2482 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002483 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002487
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002488 /*
2489 * Compute the hash that has been signed
2490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002492 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002493 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2494 params, params_len,
2495 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002496 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002497 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002498 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002499 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2502 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002503 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002504
Gilles Peskineca1d7422018-04-24 11:53:22 +02002505 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002506
Hanno Beckera6899bb2019-02-06 18:26:03 +00002507#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2508 peer_pk = &ssl->handshake->peer_pubkey;
2509#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002510 if( ssl->session_negotiate->peer_cert == NULL )
2511 {
Hanno Becker8273df82019-02-06 17:37:32 +00002512 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002515 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002516 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2517#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002518
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002519 /*
2520 * Verify signature
2521 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002522 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002525 mbedtls_ssl_send_alert_message(
2526 ssl,
2527 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2528 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002530 }
2531
Gilles Peskineeccd8882020-03-10 12:19:08 +01002532#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002533 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002534 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002535#endif
2536
Hanno Beckera6899bb2019-02-06 18:26:03 +00002537 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002538 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002539 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002540#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002541 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2542#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002543 mbedtls_ssl_send_alert_message(
2544 ssl,
2545 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2546 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002548#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002549 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2550 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2551#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002552 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002553 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002554
2555#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2556 /* We don't need the peer's public key anymore. Free it,
2557 * so that more RAM is available for upcoming expensive
2558 * operations like ECDHE. */
2559 mbedtls_pk_free( peer_pk );
2560#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002562#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002564exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 ssl->state++;
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570}
2571
Gilles Peskineeccd8882020-03-10 12:19:08 +01002572#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002574{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002575 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002576 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002579
Hanno Becker1aa267c2017-04-28 17:08:27 +01002580 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002583 ssl->state++;
2584 return( 0 );
2585 }
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002589}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002590#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592{
Janos Follath865b3eb2019-12-16 11:46:15 +00002593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002594 unsigned char *buf;
2595 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002596 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002597 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002598 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002599 size_t sig_alg_len;
2600#if defined(MBEDTLS_DEBUG_C)
2601 unsigned char *sig_alg;
2602#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Hanno Becker1aa267c2017-04-28 17:08:27 +01002606 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002609 ssl->state++;
2610 return( 0 );
2611 }
2612
Hanno Becker327c93b2018-08-15 13:56:18 +01002613 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2616 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
2618
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002619 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2620 {
2621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002622 mbedtls_ssl_send_alert_message(
2623 ssl,
2624 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2625 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002626 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002629 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002630 ssl->handshake->client_auth =
2631 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002634 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Jerry Yufb28b882022-01-28 11:05:58 +08002636 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002637 {
Johan Pascala89ca862020-08-25 10:03:19 +02002638 /* Current message is probably the ServerHelloDone */
2639 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002640 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002641 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002642
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002643 /*
2644 * struct {
2645 * ClientCertificateType certificate_types<1..2^8-1>;
2646 * SignatureAndHashAlgorithm
2647 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2648 * DistinguishedName certificate_authorities<0..2^16-1>;
2649 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002650 *
2651 * Since we only support a single certificate on clients, let's just
2652 * ignore all the information that's supposed to help us pick a
2653 * certificate.
2654 *
2655 * We could check that our certificate matches the request, and bail out
2656 * if it doesn't, but it's simpler to just send the certificate anyway,
2657 * and give the server the opportunity to decide if it should terminate
2658 * the connection when it doesn't like our certificate.
2659 *
2660 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2661 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002662 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002663 *
2664 * However, we still minimally parse the message to check it is at least
2665 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002666 */
Paul Bakker926af752012-11-23 13:38:07 +01002667 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002668
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002669 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002670 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2671 {
2672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2673 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2674 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002675 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002678 n = cert_type_len;
2679
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002680 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002681 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002682 * * the length of the signature algorithms field (if minor version of
2683 * SSL is 3),
2684 * * distinguished name length otherwise.
2685 * Both reach at most the index:
2686 * ...hdr_len + 2 + n,
2687 * therefore the buffer length at this point must be greater than that
2688 * regardless of the actual code path.
2689 */
2690 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002693 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2694 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002695 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002696 }
2697
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002698 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002699 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2700 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2701
2702 /*
2703 * The furthest access in buf is in the loop few lines below:
2704 * sig_alg[i + 1],
2705 * where:
2706 * sig_alg = buf + ...hdr_len + 3 + n,
2707 * max(i) = sig_alg_len - 1.
2708 * Therefore the furthest access is:
2709 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2710 * which reduces to:
2711 * buf[...hdr_len + 3 + n + sig_alg_len],
2712 * which is one less than we need the buf to be.
2713 */
2714 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002715 {
Ronald Cron90915f22022-03-07 11:11:36 +01002716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2717 mbedtls_ssl_send_alert_message(
2718 ssl,
2719 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2720 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2721 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002722 }
Paul Bakker926af752012-11-23 13:38:07 +01002723
Ronald Cron90915f22022-03-07 11:11:36 +01002724#if defined(MBEDTLS_DEBUG_C)
2725 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2726 for( size_t i = 0; i < sig_alg_len; i += 2 )
2727 {
2728 MBEDTLS_SSL_DEBUG_MSG( 3,
2729 ( "Supported Signature Algorithm found: %d,%d",
2730 sig_alg[i], sig_alg[i + 1] ) );
2731 }
2732#endif
2733
2734 n += 2 + sig_alg_len;
2735
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002736 /* certificate_authorities */
2737 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2738 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002739
2740 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002741 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request 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 Becker5697af02021-06-24 10:33:51 +01002746 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002747 }
2748
2749exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
2752 return( 0 );
2753}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002754#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002757{
Janos Follath865b3eb2019-12-16 11:46:15 +00002758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Hanno Becker327c93b2018-08-15 13:56:18 +01002762 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2765 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002767
2768 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2769 {
2770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2771 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2772 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2775 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002778 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2779 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002780 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 }
2782
2783 ssl->state++;
2784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002786 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002788#endif
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
2792 return( 0 );
2793}
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002796{
Janos Follath865b3eb2019-12-16 11:46:15 +00002797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002798
2799 size_t header_len;
2800 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002801 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002802 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2807 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 /*
2810 * DHM key exchange -- send G^X mod P
2811 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002812 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Joe Subbiani6dd73642021-07-19 11:56:54 +01002814 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002815 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002818 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002819 &ssl->out_msg[header_len], content_len,
2820 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 if( ret != 0 )
2822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 return( ret );
2825 }
2826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2828 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002831 ssl->handshake->premaster,
2832 MBEDTLS_PREMASTER_SIZE,
2833 &ssl->handshake->pmslen,
2834 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 return( ret );
2838 }
2839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 }
2842 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00002844#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2845 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Przemek Stekield905d332022-03-16 09:50:56 +01002846 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2847 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2848 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002849 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002850 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2851 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2852 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002853 {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2855 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002856 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002857
2858 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2859
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002860 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002861
Hanno Becker0a94a642019-01-11 14:35:30 +00002862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2863
Hanno Becker4a63ed42019-01-08 11:39:35 +00002864 /*
2865 * Generate EC private key for ECDHE exchange.
2866 */
2867
Hanno Becker4a63ed42019-01-08 11:39:35 +00002868 /* The master secret is obtained from the shared ECDH secret by
2869 * applying the TLS 1.2 PRF with a specific salt and label. While
2870 * the PSA Crypto API encourages combining key agreement schemes
2871 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2872 * yet support the provisioning of salt + label to the KDF.
2873 * For the time being, we therefore need to split the computation
2874 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002875 key_attributes = psa_key_attributes_init();
2876 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002877 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002878 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2879 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002880
2881 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002882 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002883 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002884 if( status != PSA_SUCCESS )
2885 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2886
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002887 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002888 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002889 * but we just need to add a length byte before that. */
2890 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2891 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2892 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2893 size_t own_pubkey_len;
2894
Hanno Becker4a63ed42019-01-08 11:39:35 +00002895 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002896 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002897 &own_pubkey_len );
2898 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002899 {
2900 psa_destroy_key( handshake->ecdh_psa_privkey );
2901 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002902 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002903 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002904
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002905 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2906 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002907
Hanno Becker4a63ed42019-01-08 11:39:35 +00002908 /* The ECDH secret is the premaster secret used for key derivation. */
2909
Janos Follathdf3b0892019-08-08 11:12:24 +01002910 /* Compute ECDH shared secret. */
2911 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2912 handshake->ecdh_psa_privkey,
2913 handshake->ecdh_psa_peerkey,
2914 handshake->ecdh_psa_peerkey_len,
2915 ssl->handshake->premaster,
2916 sizeof( ssl->handshake->premaster ),
2917 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002918
Andrzej Kureka0237f82022-02-24 13:24:52 -05002919 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002920 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002921
2922 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2923 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002924 }
2925 else
2926#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2927 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Przemek Stekield905d332022-03-16 09:50:56 +01002928 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2929 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2930 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2932 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2933 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2934 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2935 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2936 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2937 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2938 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002939 {
2940 /*
2941 * ECDH key exchange -- send client public value
2942 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002943 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002944
Gilles Peskineeccd8882020-03-10 12:19:08 +01002945#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002946 if( ssl->handshake->ecrs_enabled )
2947 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002948 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002949 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002950
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002951 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2952 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002953#endif
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002956 &content_len,
2957 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002958 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002959 if( ret != 0 )
2960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002962#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002963 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2964 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2965#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002966 return( ret );
2967 }
2968
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002969 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2970 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002971
Gilles Peskineeccd8882020-03-10 12:19:08 +01002972#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002973 if( ssl->handshake->ecrs_enabled )
2974 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002975 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002976 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002977 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002978
2979ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002980 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002981 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002984 &ssl->handshake->pmslen,
2985 ssl->handshake->premaster,
2986 MBEDTLS_MPI_MAX_SIZE,
2987 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002990#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002991 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2992 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2993#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002994 return( ret );
2995 }
2996
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002997 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2998 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01002999 }
3000 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3002 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3003 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3004 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01003005#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3006 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3007 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3008 {
3009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3010 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
3011 psa_key_attributes_t key_attributes;
3012
3013 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3014
3015 /*
3016 * opaque psk_identity<0..2^16-1>;
3017 */
3018 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
3019 {
3020 /* We don't offer PSK suites if we don't have a PSK,
3021 * and we check that the server's choice is among the
3022 * ciphersuites we offered, so this should never happen. */
3023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3024 }
3025
3026 /* Opaque PSKs are currently only supported for PSK-only suites. */
3027 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
3028 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3029
3030 header_len = 4;
3031 content_len = ssl->conf->psk_identity_len;
3032
3033 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
3034 {
3035 MBEDTLS_SSL_DEBUG_MSG( 1,
3036 ( "psk identity too long or SSL buffer too short" ) );
3037 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3038 }
3039
3040 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3041 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
3042
3043 memcpy( ssl->out_msg + header_len,
3044 ssl->conf->psk_identity,
3045 ssl->conf->psk_identity_len );
3046 header_len += ssl->conf->psk_identity_len;
3047
3048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3049
3050 /*
3051 * Generate EC private key for ECDHE exchange.
3052 */
3053
3054 /* The master secret is obtained from the shared ECDH secret by
3055 * applying the TLS 1.2 PRF with a specific salt and label. While
3056 * the PSA Crypto API encourages combining key agreement schemes
3057 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3058 * yet support the provisioning of salt + label to the KDF.
3059 * For the time being, we therefore need to split the computation
3060 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3061 key_attributes = psa_key_attributes_init();
3062 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3063 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3064 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3065 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3066
3067 /* Generate ECDH private key. */
3068 status = psa_generate_key( &key_attributes,
3069 &handshake->ecdh_psa_privkey );
3070 if( status != PSA_SUCCESS )
3071 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3072
3073 /* Export the public part of the ECDH private key from PSA.
3074 * The export format is an ECPoint structure as expected by TLS,
3075 * but we just need to add a length byte before that. */
3076 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
3077 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3078 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003079 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003080
3081 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3082 own_pubkey, own_pubkey_max_len,
3083 &own_pubkey_len );
3084 if( status != PSA_SUCCESS )
3085 {
3086 psa_destroy_key( handshake->ecdh_psa_privkey );
3087 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3088 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3089 }
3090
3091 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
3092 content_len = own_pubkey_len + 1;
3093
3094 /* The ECDH secret is the premaster secret used for key derivation. */
3095 unsigned char *p = ssl->handshake->premaster;
3096 unsigned char *p_end = p + sizeof( ssl->handshake->premaster );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003097 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003098
3099 /* Compute ECDH shared secret. */
3100 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3101 handshake->ecdh_psa_privkey,
3102 handshake->ecdh_psa_peerkey,
3103 handshake->ecdh_psa_peerkey_len,
3104 p + 2,
3105 p_end - ( p + 2 ),
3106 &zlen );
3107
3108 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3109 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3110
3111 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
3112 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3113
3114 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
3115 p += 2 + zlen;
3116
3117 /* opaque psk<0..2^16-1>; */
3118 if( p_end - p < 2 )
3119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3120
3121 const unsigned char *psk = NULL;
3122 size_t psk_len = 0;
3123
3124 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
3125 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
3126 {
3127 /*
3128 * This should never happen because the existence of a PSK is always
3129 * checked before calling this function
3130 */
3131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3132 }
3133
3134 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
3135 p += 2;
3136
3137 if( p_end < p || (size_t)( p_end - p ) < psk_len )
3138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3139
3140 memcpy( p, psk, psk_len );
3141 p += psk_len;
3142
3143 ssl->handshake->pmslen = p - ssl->handshake->premaster;
3144 }
3145 else
3146#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3147 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003148#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003149 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003150 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003151 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003152 * opaque psk_identity<0..2^16-1>;
3153 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003154 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003155 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003156 /* We don't offer PSK suites if we don't have a PSK,
3157 * and we check that the server's choice is among the
3158 * ciphersuites we offered, so this should never happen. */
3159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003160 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003161
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003162 header_len = 4;
3163 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003164
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003165 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003166 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003167 MBEDTLS_SSL_DEBUG_MSG( 1,
3168 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003169 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3170 }
3171
Joe Subbianifbeb6922021-07-16 14:27:50 +01003172 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3173 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003174
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003175 memcpy( ssl->out_msg + header_len,
3176 ssl->conf->psk_identity,
3177 ssl->conf->psk_identity_len );
3178 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3181 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003182 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003183 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003184 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003185 else
3186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3188 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003189 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003190#if defined(MBEDTLS_USE_PSA_CRYPTO)
3191 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003192 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003193 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3194#endif /* MBEDTLS_USE_PSA_CRYPTO */
3195
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003196 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3197 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003198 return( ret );
3199 }
3200 else
3201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3203 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003204 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003205#if defined(MBEDTLS_USE_PSA_CRYPTO)
3206 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003207 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003208 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3209#endif /* MBEDTLS_USE_PSA_CRYPTO */
3210
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003211 /*
3212 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3213 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003214 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003215
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003216 if( header_len + 2 + content_len >
3217 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003218 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003219 MBEDTLS_SSL_DEBUG_MSG( 1,
3220 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003221 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3222 }
3223
Joe Subbianifbeb6922021-07-16 14:27:50 +01003224 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3225 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003228 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003229 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003230 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003231 if( ret != 0 )
3232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003234 return( ret );
3235 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003236 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3239#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3240 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003241 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003242#if defined(MBEDTLS_USE_PSA_CRYPTO)
3243 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003244 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003245 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3246#endif /* MBEDTLS_USE_PSA_CRYPTO */
3247
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003248 /*
3249 * ClientECDiffieHellmanPublic public;
3250 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003251 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3252 &content_len,
3253 &ssl->out_msg[header_len],
3254 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003255 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003256 if( ret != 0 )
3257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003259 return( ret );
3260 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003261
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003262 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3263 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003264 }
3265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003270 }
3271
Hanno Beckerafd311e2018-10-23 15:26:40 +01003272#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3273 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3274 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003275 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003276 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003277 MBEDTLS_SSL_DEBUG_MSG( 1,
3278 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003279 }
3280 else
3281#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3282 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003284 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003285 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003286 MBEDTLS_SSL_DEBUG_RET( 1,
3287 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003288 return( ret );
3289 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003290 }
3291 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003292#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3294 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003296 header_len = 4;
3297 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3298 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003299 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 }
Paul Bakkered27a042013-04-18 22:46:23 +02003301 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003303#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3304 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3305 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003306 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003307
3308 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003309 ssl->out_msg + header_len,
3310 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3311 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003312 ssl->conf->f_rng, ssl->conf->p_rng );
3313 if( ret != 0 )
3314 {
3315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3316 return( ret );
3317 }
3318
3319 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3320 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3321 ssl->conf->f_rng, ssl->conf->p_rng );
3322 if( ret != 0 )
3323 {
3324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3325 return( ret );
3326 }
3327 }
3328 else
3329#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003330 {
3331 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003336 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3338 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
3340 ssl->state++;
3341
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003342 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 return( ret );
3346 }
3347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003349
3350 return( 0 );
3351}
3352
Gilles Peskineeccd8882020-03-10 12:19:08 +01003353#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003355{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003356 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003357 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003365 return( ret );
3366 }
3367
Hanno Becker77adddc2019-02-07 12:32:43 +00003368 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003371 ssl->state++;
3372 return( 0 );
3373 }
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003377}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003378#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003382 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003383 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003384 size_t n = 0, offset = 0;
3385 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003386 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003388 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003389 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003390#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3391 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3392#else
3393 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3394#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003397
Gilles Peskineeccd8882020-03-10 12:19:08 +01003398#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003399 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003400 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003401 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003402 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003403 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003404#endif
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003409 return( ret );
3410 }
3411
Hanno Becker77adddc2019-02-07 12:32:43 +00003412 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003415 ssl->state++;
3416 return( 0 );
3417 }
3418
Jerry Yufb28b882022-01-28 11:05:58 +08003419 if( ssl->handshake->client_auth == 0 ||
3420 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 {
Johan Pascala89ca862020-08-25 10:03:19 +02003422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3423 ssl->state++;
3424 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 }
3426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 }
3432
3433 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003434 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003436#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003437 if( ssl->handshake->ecrs_enabled )
3438 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3439
3440sign:
3441#endif
3442
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003443 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003444
Ronald Cron90915f22022-03-07 11:11:36 +01003445 /*
3446 * digitally-signed struct {
3447 * opaque handshake_messages[handshake_messages_length];
3448 * };
3449 *
3450 * Taking shortcut here. We assume that the server always allows the
3451 * PRF Hash function and has sent it in the allowed signature
3452 * algorithms list received in the Certificate Request message.
3453 *
3454 * Until we encounter a server that does not, we will take this
3455 * shortcut.
3456 *
3457 * Reason: Otherwise we should have running hashes for SHA512 and
3458 * SHA224 in order to satisfy 'weird' needs from the server
3459 * side.
3460 */
3461 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003462 {
Ronald Cron90915f22022-03-07 11:11:36 +01003463 md_alg = MBEDTLS_MD_SHA384;
3464 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003465 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003466 else
Paul Bakker577e0062013-08-28 11:57:20 +02003467 {
Ronald Cron90915f22022-03-07 11:11:36 +01003468 md_alg = MBEDTLS_MD_SHA256;
3469 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003470 }
Ronald Cron90915f22022-03-07 11:11:36 +01003471 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3472
3473 /* Info from md_alg will be used instead */
3474 hashlen = 0;
3475 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003476
Gilles Peskineeccd8882020-03-10 12:19:08 +01003477#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003478 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003479 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003480#endif
3481
3482 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3483 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003484 ssl->out_msg + 6 + offset,
3485 out_buf_len - 6 - offset,
3486 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003487 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003490#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003491 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3492 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3493#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003494 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003495 }
Paul Bakker926af752012-11-23 13:38:07 +01003496
Joe Subbiani6dd73642021-07-19 11:56:54 +01003497 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003498
Paul Bakker1ef83d62012-04-11 12:09:53 +00003499 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3501 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
3503 ssl->state++;
3504
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003505 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 return( ret );
3509 }
3510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
Paul Bakkered27a042013-04-18 22:46:23 +02003513 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003514}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003515#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3518static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003519{
Janos Follath865b3eb2019-12-16 11:46:15 +00003520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003521 uint32_t lifetime;
3522 size_t ticket_len;
3523 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003524 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003527
Hanno Becker327c93b2018-08-15 13:56:18 +01003528 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003531 return( ret );
3532 }
3533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003537 mbedtls_ssl_send_alert_message(
3538 ssl,
3539 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3540 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003542 }
3543
3544 /*
3545 * struct {
3546 * uint32 ticket_lifetime_hint;
3547 * opaque ticket<0..2^16-1>;
3548 * } NewSessionTicket;
3549 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003550 * 0 . 3 ticket_lifetime_hint
3551 * 4 . 5 ticket_len (n)
3552 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3555 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003558 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3559 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003560 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003561 }
3562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003564
Philippe Antoineb5b25432018-05-11 11:06:29 +02003565 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3566 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003567
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003568 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003573 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3574 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003575 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003576 }
3577
Paul Elliottd48d5c62021-01-07 14:47:05 +00003578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003579
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003580 /* We're not waiting for a NewSessionTicket message any more */
3581 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003583
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003584 /*
3585 * Zero-length ticket means the server changed his mind and doesn't want
3586 * to send a ticket after all, so just forget it
3587 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003588 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003589 return( 0 );
3590
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003591 if( ssl->session != NULL && ssl->session->ticket != NULL )
3592 {
3593 mbedtls_platform_zeroize( ssl->session->ticket,
3594 ssl->session->ticket_len );
3595 mbedtls_free( ssl->session->ticket );
3596 ssl->session->ticket = NULL;
3597 ssl->session->ticket_len = 0;
3598 }
3599
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003600 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3601 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003603 ssl->session_negotiate->ticket = NULL;
3604 ssl->session_negotiate->ticket_len = 0;
3605
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003606 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003607 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003609 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3610 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003611 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003612 }
3613
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003614 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003615
3616 ssl->session_negotiate->ticket = ticket;
3617 ssl->session_negotiate->ticket_len = ticket_len;
3618 ssl->session_negotiate->ticket_lifetime = lifetime;
3619
3620 /*
3621 * RFC 5077 section 3.4:
3622 * "If the client receives a session ticket from the server, then it
3623 * discards any Session ID that was sent in the ServerHello."
3624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003626 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003629
3630 return( 0 );
3631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003633
Paul Bakker5121ce52009-01-03 21:22:43 +00003634/*
Paul Bakker1961b702013-01-25 14:49:24 +01003635 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003638{
3639 int ret = 0;
3640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003642 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3644 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003645 ssl->handshake->new_session_ticket != 0 )
3646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003648 }
3649#endif
3650
Paul Bakker1961b702013-01-25 14:49:24 +01003651 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 case MBEDTLS_SSL_HELLO_REQUEST:
3654 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003655 break;
3656
Paul Bakker1961b702013-01-25 14:49:24 +01003657 /*
3658 * ==> ClientHello
3659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003661 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003662 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Paul Bakker1961b702013-01-25 14:49:24 +01003664 /*
3665 * <== ServerHello
3666 * Certificate
3667 * ( ServerKeyExchange )
3668 * ( CertificateRequest )
3669 * ServerHelloDone
3670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003672 ret = ssl_parse_server_hello( ssl );
3673 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3676 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003677 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003680 ret = ssl_parse_server_key_exchange( ssl );
3681 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003684 ret = ssl_parse_certificate_request( ssl );
3685 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003688 ret = ssl_parse_server_hello_done( ssl );
3689 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
Paul Bakker1961b702013-01-25 14:49:24 +01003691 /*
3692 * ==> ( Certificate/Alert )
3693 * ClientKeyExchange
3694 * ( CertificateVerify )
3695 * ChangeCipherSpec
3696 * Finished
3697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3699 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003700 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003703 ret = ssl_write_client_key_exchange( ssl );
3704 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003707 ret = ssl_write_certificate_verify( ssl );
3708 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3711 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003712 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 case MBEDTLS_SSL_CLIENT_FINISHED:
3715 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003716 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
Paul Bakker1961b702013-01-25 14:49:24 +01003718 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003719 * <== ( NewSessionTicket )
3720 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003721 * Finished
3722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3724 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003725 ret = ssl_parse_new_session_ticket( ssl );
3726 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003727#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3730 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003731 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 case MBEDTLS_SSL_SERVER_FINISHED:
3734 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003735 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 case MBEDTLS_SSL_FLUSH_BUFFERS:
3738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3739 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003740 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3743 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003744 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003745
Paul Bakker1961b702013-01-25 14:49:24 +01003746 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003750
3751 return( ret );
3752}
Jerry Yuc5aef882021-12-23 20:15:02 +08003753
Jerry Yufb4b6472022-01-27 15:03:26 +08003754#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */