blob: 2868a84dc38cc985089390ccbe01a081845ec057 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020026#else
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020028#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010029#define mbedtls_free free
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020030#endif
31
SimonBd5800b72016-04-26 07:43:27 +010032#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010033#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000034#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/debug.h"
36#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010038
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020041#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000042#endif /* MBEDTLS_USE_PSA_CRYPTO */
43
SimonBd5800b72016-04-26 07:43:27 +010044#include <string.h>
45
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020046#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010049#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050053#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020054#endif
55
Gilles Peskineeccd8882020-03-10 12:19:08 +010056#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Ronald Crond491c2d2022-02-19 18:30:46 +010057int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010058{
59 if( conf->psk_identity == NULL ||
60 conf->psk_identity_len == 0 )
61 {
62 return( 0 );
63 }
64
65 if( conf->psk != NULL && conf->psk_len != 0 )
66 return( 1 );
67
68#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020069 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010070 return( 1 );
71#endif /* MBEDTLS_USE_PSA_CRYPTO */
72
73 return( 0 );
74}
Hanno Beckerdfab8e22018-10-23 11:59:34 +010075
76#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker520224e2018-10-26 11:38:07 +010077static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
Hanno Beckerdfab8e22018-10-23 11:59:34 +010078{
79 if( conf->psk_identity == NULL ||
80 conf->psk_identity_len == 0 )
81 {
82 return( 0 );
83 }
84
85 if( conf->psk != NULL && conf->psk_len != 0 )
86 return( 1 );
87
88 return( 0 );
89}
90#endif /* MBEDTLS_USE_PSA_CRYPTO */
91
Gilles Peskineeccd8882020-03-10 12:19:08 +010092#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +010095static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
96 unsigned char *buf,
97 const unsigned char *end,
98 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010099{
100 unsigned char *p = buf;
101
102 *olen = 0;
103
Hanno Becker40f8b512017-10-12 14:58:55 +0100104 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
105 * initial ClientHello, in which case also adding the renegotiation
106 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +0100108 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100109
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100110 MBEDTLS_SSL_DEBUG_MSG( 3,
111 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100112
Hanno Becker261602c2017-04-12 14:54:42 +0100113 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100114
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115 /*
116 * Secure renegotiation
117 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100118 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
119 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120
121 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100122 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
123 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124
125 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
126
127 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100128
129 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100132
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200133#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100134 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100135
Hanno Becker261602c2017-04-12 14:54:42 +0100136static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
137 unsigned char *buf,
138 const unsigned char *end,
139 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100140{
141 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100142 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100143
144 *olen = 0;
145
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100146 MBEDTLS_SSL_DEBUG_MSG( 3,
147 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100148 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100149
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100150 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
151 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152
153 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100154 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200155
156 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100158
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200159 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100160
161 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100162}
Darryl Green11999bb2018-03-13 15:22:58 +0000163#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100164 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100165
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200166#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100167static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
168 unsigned char *buf,
169 const unsigned char *end,
170 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200171{
Janos Follath865b3eb2019-12-16 11:46:15 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200173 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200174 size_t kkpp_len;
175
176 *olen = 0;
177
178 /* Skip costly extension if we can't use EC J-PAKE anyway */
179 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100180 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200181
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100182 MBEDTLS_SSL_DEBUG_MSG( 3,
183 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200184
Hanno Becker261602c2017-04-12 14:54:42 +0100185 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200186
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100187 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
188 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200189
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200190 /*
191 * We may need to send ClientHello multiple times for Hello verification.
192 * We don't want to compute fresh values every time (both for performance
193 * and consistency reasons), so cache the extension content.
194 */
195 if( ssl->handshake->ecjpake_cache == NULL ||
196 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200197 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
199
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200200 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100201 p + 2, end - p - 2, &kkpp_len,
202 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200203 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200204 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100205 MBEDTLS_SSL_DEBUG_RET( 1 ,
206 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100207 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200208 }
209
210 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
211 if( ssl->handshake->ecjpake_cache == NULL )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100214 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200215 }
216
217 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
218 ssl->handshake->ecjpake_cache_len = kkpp_len;
219 }
220 else
221 {
222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
223
224 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100225 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200226
227 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200228 }
229
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100230 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
231 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200232
233 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100234
235 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200236}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200237#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000238
Hanno Beckera0e20d02019-05-15 14:03:01 +0100239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100240static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
241 unsigned char *buf,
242 const unsigned char *end,
243 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100244{
245 unsigned char *p = buf;
246 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100247
248 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100249 * Quoting draft-ietf-tls-dtls-connection-id-05
250 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100251 *
252 * struct {
253 * opaque cid<0..2^8-1>;
254 * } ConnectionId;
255 */
256
257 *olen = 0;
258 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
259 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
260 {
Hanno Becker261602c2017-04-12 14:54:42 +0100261 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100262 }
263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
264
265 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
266 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100267 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100268
269 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100270 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
271 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100272 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100273 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
274 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100275
276 *p++ = (uint8_t) ssl->own_cid_len;
277 memcpy( p, ssl->own_cid, ssl->own_cid_len );
278
279 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100280
281 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100282}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100283#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100286static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
287 unsigned char *buf,
288 const unsigned char *end,
289 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200290{
291 unsigned char *p = buf;
292
Simon Butcher0fc94e92015-09-28 20:52:04 +0100293 *olen = 0;
294
Hanno Becker261602c2017-04-12 14:54:42 +0100295 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
296 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200297
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100298 MBEDTLS_SSL_DEBUG_MSG( 3,
299 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200300
Hanno Becker261602c2017-04-12 14:54:42 +0100301 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100302
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100303 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
304 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200305
306 *p++ = 0x00;
307 *p++ = 1;
308
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200309 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200310
311 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100312
313 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100318static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
319 unsigned char *buf,
320 const unsigned char *end,
321 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322{
323 unsigned char *p = buf;
324
Simon Butcher0fc94e92015-09-28 20:52:04 +0100325 *olen = 0;
326
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100327 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100328 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100329
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100330 MBEDTLS_SSL_DEBUG_MSG( 3,
331 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100332
Hanno Becker261602c2017-04-12 14:54:42 +0100333 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100334
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100335 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
336 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100337
338 *p++ = 0x00;
339 *p++ = 0x00;
340
341 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100342
343 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100348static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
349 unsigned char *buf,
350 const unsigned char *end,
351 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200352{
353 unsigned char *p = buf;
354
Simon Butcher0fc94e92015-09-28 20:52:04 +0100355 *olen = 0;
356
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100357 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100358 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200359
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100360 MBEDTLS_SSL_DEBUG_MSG( 3,
361 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200362
Hanno Becker261602c2017-04-12 14:54:42 +0100363 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100364
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100365 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
366 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200367
368 *p++ = 0x00;
369 *p++ = 0x00;
370
371 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100372
373 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100378static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
379 unsigned char *buf,
380 const unsigned char *end,
381 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200382{
383 unsigned char *p = buf;
384 size_t tlen = ssl->session_negotiate->ticket_len;
385
Simon Butcher0fc94e92015-09-28 20:52:04 +0100386 *olen = 0;
387
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200388 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100389 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200390
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100391 MBEDTLS_SSL_DEBUG_MSG( 3,
392 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
Hanno Becker261602c2017-04-12 14:54:42 +0100394 /* The addition is safe here since the ticket length is 16 bit. */
395 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100396
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100397 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
398 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100400 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
401 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200402
403 *olen = 4;
404
Simon Butchered997662015-09-28 02:14:30 +0100405 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100406 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200407
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100408 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000409 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200410
411 memcpy( p, ssl->session_negotiate->ticket, tlen );
412
413 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100414
415 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200418
Ron Eldora9788042018-12-05 11:04:31 +0200419#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200420static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200421 unsigned char *buf,
422 const unsigned char *end,
423 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100424{
425 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200426 size_t protection_profiles_index = 0, ext_len = 0;
427 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428
429 *olen = 0;
430
Johan Pascalc3ccd982020-10-28 17:18:18 +0100431 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
432 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
433 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100434 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200435 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100436 }
437
Ron Eldora9788042018-12-05 11:04:31 +0200438 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100439 * uint8 SRTPProtectionProfile[2];
440 *
441 * struct {
442 * SRTPProtectionProfiles SRTPProtectionProfiles;
443 * opaque srtp_mki<0..255>;
444 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100445 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100446 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200447 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200448 {
449 mki_len = ssl->dtls_srtp_info.mki_len;
450 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300451 /* Extension length = 2 bytes for profiles length,
452 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
453 * 1 byte for srtp_mki vector length and the mki_len value
454 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200455 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
456
Johan Pascal77696ee2020-09-22 21:49:40 +0200457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
458
459 /* Check there is room in the buffer for the extension + 4 bytes
460 * - the extension tag (2 bytes)
461 * - the extension length (2 bytes)
462 */
463 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
464
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100465 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
466 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200467
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100468 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
469 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100470
Ron Eldor3adb9922017-12-21 10:15:08 +0200471 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200472 /* micro-optimization:
473 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
474 * which is lower than 127, so the upper byte of the length is always 0
475 * For the documentation, the more generic code is left in comments
476 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
477 * >> 8 ) & 0xFF );
478 */
479 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100480 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100481
Ron Eldoref72faf2018-07-12 11:54:20 +0300482 for( protection_profiles_index=0;
483 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
484 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100485 {
Johan Pascal43f94902020-09-22 12:25:52 +0200486 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200487 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200488 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200489 {
490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
491 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100492 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
493 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200494 }
495 else
496 {
497 /*
498 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200499 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200500 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200501 MBEDTLS_SSL_DEBUG_MSG( 3,
502 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200503 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200504 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
505 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200506 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100507 }
508 }
509
Ron Eldor591f1622018-01-22 12:30:04 +0200510 *p++ = mki_len & 0xFF;
511
512 if( mki_len != 0 )
513 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200514 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200515 /*
516 * Increment p to point to the current position.
517 */
518 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300519 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
520 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200521 }
522
Ron Eldoref72faf2018-07-12 11:54:20 +0300523 /*
524 * total extension length: extension type (2 bytes)
525 * + extension length (2 bytes)
526 * + protection profile length (2 bytes)
527 * + 2 * number of protection profiles
528 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200529 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300530 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200531 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200532
533 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100534}
535#endif /* MBEDTLS_SSL_DTLS_SRTP */
536
Ronald Cron4079abc2022-02-20 10:35:26 +0100537int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
538 unsigned char *buf,
539 const unsigned char *end,
540 int uses_ec,
541 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100542{
543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
544 unsigned char *p = buf;
545 size_t ext_len = 0;
546
547 (void) ssl;
548 (void) end;
549 (void) uses_ec;
550 (void) ret;
551 (void) ext_len;
552
553 *out_len = 0;
554
555 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
556 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
557#if defined(MBEDTLS_SSL_RENEGOTIATION)
558 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
559 {
560 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
561 return( ret );
562 }
563 p += ext_len;
564#endif
565
566#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
567 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
568 if( uses_ec )
569 {
570 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
571 &ext_len ) ) != 0 )
572 {
573 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
574 return( ret );
575 }
576 p += ext_len;
577 }
578#endif
579
580#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
581 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
582 {
583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
584 return( ret );
585 }
586 p += ext_len;
587#endif
588
589#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
590 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
591 {
592 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
593 return( ret );
594 }
595 p += ext_len;
596#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
597
598#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
599 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
600 &ext_len ) ) != 0 )
601 {
602 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
603 return( ret );
604 }
605 p += ext_len;
606#endif
607
608#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
609 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
610 {
611 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
612 return( ret );
613 }
614 p += ext_len;
615#endif
616
617#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
618 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
621 return( ret );
622 }
623 p += ext_len;
624#endif
625
626#if defined(MBEDTLS_SSL_DTLS_SRTP)
627 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
628 {
629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
630 return( ret );
631 }
632 p += ext_len;
633#endif
634
635#if defined(MBEDTLS_SSL_SESSION_TICKETS)
636 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
637 {
638 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
639 return( ret );
640 }
641 p += ext_len;
642#endif
643
644 *out_len = p - buf;
645
646 return( 0 );
647}
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200650 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000651 size_t len )
652{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_SSL_RENEGOTIATION)
654 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000655 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100656 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000657 if( len != 1 + ssl->verify_data_len * 2 ||
658 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200659 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100660 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200661 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100662 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100665 mbedtls_ssl_send_alert_message(
666 ssl,
667 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
668 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100669 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000670 }
671 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100674 {
675 if( len != 1 || buf[0] != 0x00 )
676 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200677 MBEDTLS_SSL_DEBUG_MSG( 1,
678 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100679 mbedtls_ssl_send_alert_message(
680 ssl,
681 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
682 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100683 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100684 }
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100687 }
Paul Bakker48916f92012-09-16 19:57:18 +0000688
689 return( 0 );
690}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
693static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200694 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200695 size_t len )
696{
697 /*
698 * server should use the extension only if we did,
699 * and if so the server's value should match ours (and len is always 1)
700 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200701 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200702 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200703 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200704 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100705 MBEDTLS_SSL_DEBUG_MSG( 1,
706 ( "non-matching max fragment length extension" ) );
707 mbedtls_ssl_send_alert_message(
708 ssl,
709 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100710 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
711 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200712 }
713
714 return( 0 );
715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000717
Hanno Beckera0e20d02019-05-15 14:03:01 +0100718#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +0100719static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
720 const unsigned char *buf,
721 size_t len )
722{
723 size_t peer_cid_len;
724
725 if( /* CID extension only makes sense in DTLS */
726 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
727 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100728 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100729 {
Hanno Becker22626482019-05-03 12:46:59 +0100730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100732 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100733 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100734 }
735
736 if( len == 0 )
737 {
738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100742 }
743
744 peer_cid_len = *buf++;
745 len--;
746
747 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
748 {
Hanno Becker22626482019-05-03 12:46:59 +0100749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100750 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100751 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
752 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100753 }
754
755 if( len != peer_cid_len )
756 {
Hanno Becker22626482019-05-03 12:46:59 +0100757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100758 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100759 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
760 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100761 }
762
Hanno Becker5a299902019-05-03 12:47:49 +0100763 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100764 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
765 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
766
767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
768 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
769
Hanno Beckera8373a12019-04-26 15:37:26 +0100770 return( 0 );
771}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100772#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
775static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100776 const unsigned char *buf,
777 size_t len )
778{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200779 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100780 len != 0 )
781 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100782 MBEDTLS_SSL_DEBUG_MSG( 1,
783 ( "non-matching encrypt-then-MAC extension" ) );
784 mbedtls_ssl_send_alert_message(
785 ssl,
786 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100787 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100788 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100789 }
790
791 ((void) buf);
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100794
795 return( 0 );
796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
800static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200801 const unsigned char *buf,
802 size_t len )
803{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200804 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200805 len != 0 )
806 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100807 MBEDTLS_SSL_DEBUG_MSG( 1,
808 ( "non-matching extended master secret extension" ) );
809 mbedtls_ssl_send_alert_message(
810 ssl,
811 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100812 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
813 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200814 }
815
816 ((void) buf);
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200819
820 return( 0 );
821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
825static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200826 const unsigned char *buf,
827 size_t len )
828{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200829 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200830 len != 0 )
831 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100832 MBEDTLS_SSL_DEBUG_MSG( 1,
833 ( "non-matching session ticket extension" ) );
834 mbedtls_ssl_send_alert_message(
835 ssl,
836 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100837 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
838 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200839 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200840
841 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200842
843 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200844
845 return( 0 );
846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200848
Robert Cragie136884c2015-10-02 13:34:31 +0100849#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100850 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200852 const unsigned char *buf,
853 size_t len )
854{
855 size_t list_size;
856 const unsigned char *p;
857
Philippe Antoine747fd532018-05-30 09:13:21 +0200858 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200861 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
862 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100863 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200864 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200865 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200866
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200867 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200868 while( list_size > 0 )
869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
871 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200872 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200873#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
874 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200875 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200876#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
877 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100878#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200879 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
880 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200883 return( 0 );
884 }
885
886 list_size--;
887 p++;
888 }
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200891 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
892 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100893 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200894}
Darryl Green11999bb2018-03-13 15:22:58 +0000895#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100896 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200897
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200898#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
899static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
900 const unsigned char *buf,
901 size_t len )
902{
Janos Follath865b3eb2019-12-16 11:46:15 +0000903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200904
Hanno Beckere694c3e2017-12-27 21:34:08 +0000905 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
907 {
908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
909 return( 0 );
910 }
911
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200912 /* If we got here, we no longer need our cached extension */
913 mbedtls_free( ssl->handshake->ecjpake_cache );
914 ssl->handshake->ecjpake_cache = NULL;
915 ssl->handshake->ecjpake_cache_len = 0;
916
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200917 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
918 buf, len ) ) != 0 )
919 {
920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100921 mbedtls_ssl_send_alert_message(
922 ssl,
923 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
924 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200925 return( ret );
926 }
927
928 return( 0 );
929}
930#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_ALPN)
933static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200934 const unsigned char *buf, size_t len )
935{
936 size_t list_len, name_len;
937 const char **p;
938
939 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200940 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200941 {
942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100943 mbedtls_ssl_send_alert_message(
944 ssl,
945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100946 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
947 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200948 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200949
950 /*
951 * opaque ProtocolName<1..2^8-1>;
952 *
953 * struct {
954 * ProtocolName protocol_name_list<2..2^16-1>
955 * } ProtocolNameList;
956 *
957 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
958 */
959
960 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
961 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200962 {
963 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100965 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200966 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200967
968 list_len = ( buf[0] << 8 ) | buf[1];
969 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200970 {
971 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
972 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100973 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200974 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200975
976 name_len = buf[2];
977 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200978 {
979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100981 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200982 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200983
984 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200985 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200986 {
987 if( name_len == strlen( *p ) &&
988 memcmp( buf + 3, *p, name_len ) == 0 )
989 {
990 ssl->alpn_chosen = *p;
991 return( 0 );
992 }
993 }
994
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200996 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
997 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100998 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001001
Johan Pascalb62bb512015-12-03 21:56:45 +01001002#if defined(MBEDTLS_SSL_DTLS_SRTP)
1003static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001004 const unsigned char *buf,
1005 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001006{
Johan Pascal43f94902020-09-22 12:25:52 +02001007 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001008 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001009 uint16_t server_protection_profile_value = 0;
1010
1011 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001012 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1013 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1014 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001015 return( 0 );
1016
Ron Eldora9788042018-12-05 11:04:31 +02001017 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001018 * uint8 SRTPProtectionProfile[2];
1019 *
1020 * struct {
1021 * SRTPProtectionProfiles SRTPProtectionProfiles;
1022 * opaque srtp_mki<0..255>;
1023 * } UseSRTPData;
1024
1025 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1026 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001027 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001028 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001029 {
1030 mki_len = ssl->dtls_srtp_info.mki_len;
1031 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001032
Ron Eldoref72faf2018-07-12 11:54:20 +03001033 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001034 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1035 * + protection profile (2 bytes)
1036 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001037 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001038 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001039 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001040 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001041
1042 /*
1043 * get the server protection profile
1044 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001045
1046 /*
1047 * protection profile length must be 0x0002 as we must have only
1048 * one protection profile in server Hello
1049 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001050 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001051 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001052
1053 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001054 server_protection = mbedtls_ssl_check_srtp_profile_value(
1055 server_protection_profile_value );
1056 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001057 {
Johan Pascal43f94902020-09-22 12:25:52 +02001058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1059 mbedtls_ssl_get_srtp_profile_as_string(
1060 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001061 }
1062
Johan Pascal43f94902020-09-22 12:25:52 +02001063 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001064
Johan Pascalb62bb512015-12-03 21:56:45 +01001065 /*
1066 * Check we have the server profile in our list
1067 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001068 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001069 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001070 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1071 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001072 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1074 mbedtls_ssl_get_srtp_profile_as_string(
1075 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001076 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001077 }
1078 }
1079
Ron Eldor591f1622018-01-22 12:30:04 +02001080 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001081 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001082 {
1083 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001084 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001085 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001086 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001087
1088 /* If server does not use mki in its reply, make sure the client won't keep
1089 * one as negotiated */
1090 if( len == 5 )
1091 {
1092 ssl->dtls_srtp_info.mki_len = 0;
1093 }
1094
Ron Eldoref72faf2018-07-12 11:54:20 +03001095 /*
1096 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001097 * If the client detects a nonzero-length MKI in the server's response
1098 * that is different than the one the client offered, then the client
1099 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1100 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001101 if( len > 5 && ( buf[4] != mki_len ||
1102 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001103 {
1104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1105 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001106 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001107 }
Ron Eldorb4655392018-07-05 18:25:39 +03001108#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001109 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001110 {
Ron Eldora9788042018-12-05 11:04:31 +02001111 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1112 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001113 }
1114#endif
Ron Eldora9788042018-12-05 11:04:31 +02001115 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001116}
1117#endif /* MBEDTLS_SSL_DTLS_SRTP */
1118
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001119/*
1120 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_SSL_PROTO_DTLS)
1123static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Glenn Strauss83158112022-04-13 14:59:34 -04001126 uint16_t dtls_legacy_version;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001127 unsigned char cookie_len;
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130
Gilles Peskineb64bf062019-09-27 14:02:44 +02001131 /* Check that there is enough room for:
1132 * - 2 bytes of version
1133 * - 1 byte of cookie_len
1134 */
1135 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1136 {
1137 MBEDTLS_SSL_DEBUG_MSG( 1,
1138 ( "incoming HelloVerifyRequest message is too short" ) );
1139 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1140 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001141 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001142 }
1143
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001144 /*
1145 * struct {
1146 * ProtocolVersion server_version;
1147 * opaque cookie<0..2^8-1>;
1148 * } HelloVerifyRequest;
1149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Glenn Strauss83158112022-04-13 14:59:34 -04001151 dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001152 p += 2;
1153
TRodziewicz2d8800e2021-05-13 19:14:19 +02001154 /*
Glenn Strauss83158112022-04-13 14:59:34 -04001155 * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
1156 * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
1157 * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
TRodziewicz2d8800e2021-05-13 19:14:19 +02001158 */
Glenn Strauss83158112022-04-13 14:59:34 -04001159 if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff )
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
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001300 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 );
1301 ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001302 ssl->session_negotiate->tls_version = ssl->tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Glenn Strauss60bfe602022-03-14 19:04:24 -04001304 if( ssl->tls_version < ssl->conf->min_tls_version ||
1305 ssl->tls_version > ssl->conf->max_tls_version )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001306 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001307 MBEDTLS_SSL_DEBUG_MSG( 1,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001308 ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
1309 (unsigned)ssl->conf->min_tls_version,
Glenn Strauss60bfe602022-03-14 19:04:24 -04001310 (unsigned)ssl->tls_version,
Glenn Strauss2dfcea22022-03-14 17:26:42 -04001311 (unsigned)ssl->conf->max_tls_version ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1314 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001315
Hanno Beckerbc000442021-06-24 09:18:19 +01001316 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001317 }
1318
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001320 ( (unsigned long) buf[2] << 24 ) |
1321 ( (unsigned long) buf[3] << 16 ) |
1322 ( (unsigned long) buf[4] << 8 ) |
1323 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001325 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001327 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Paul Bakker48916f92012-09-16 19:57:18 +00001331 if( n > 32 )
1332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1335 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001336 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001337 }
1338
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001339 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001341 ext_len = ( ( buf[38 + n] << 8 )
1342 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Paul Bakker48916f92012-09-16 19:57:18 +00001344 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001348 mbedtls_ssl_send_alert_message(
1349 ssl,
1350 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1351 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001352 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001355 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001356 {
1357 ext_len = 0;
1358 }
1359 else
1360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001362 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1363 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001364 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001367 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001368 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001369
1370 /*
1371 * Read and check compression
1372 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001373 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001375 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001376 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001377 MBEDTLS_SSL_DEBUG_MSG( 1,
1378 ( "server hello, bad compression: %d", comp ) );
1379 mbedtls_ssl_send_alert_message(
1380 ssl,
1381 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1382 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001384 }
1385
Paul Bakker380da532012-04-18 16:10:25 +00001386 /*
1387 * Initialize update checksum functions
1388 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001389 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1390 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001391 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001392 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001393 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1395 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001397 }
Paul Bakker380da532012-04-18 16:10:25 +00001398
Hanno Beckere694c3e2017-12-27 21:34:08 +00001399 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001400
Paul Elliottd48d5c62021-01-07 14:47:05 +00001401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
1404 /*
1405 * Check if the session can be resumed
1406 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001407 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_SSL_RENEGOTIATION)
1409 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001410#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001411 ssl->session_negotiate->ciphersuite != i ||
1412 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001413 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001414 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 {
1416 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001417 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001419 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001420#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001421 ssl->session_negotiate->ciphersuite = i;
1422 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001423 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001424 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 }
1426 else
1427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001433 mbedtls_ssl_send_alert_message(
1434 ssl,
1435 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1436 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001437 return( ret );
1438 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 }
1440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001442 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Paul Elliott3891caf2020-12-17 18:42:40 +00001444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1446 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Andrzej Kurek03bac442018-04-25 05:06:07 -04001448 /*
1449 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001450 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 i = 0;
1452 while( 1 )
1453 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001454 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001457 mbedtls_ssl_send_alert_message(
1458 ssl,
1459 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1460 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001461 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
1463
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001464 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001465 ssl->session_negotiate->ciphersuite )
1466 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 }
1470
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001471 suite_info = mbedtls_ssl_ciphersuite_from_id(
1472 ssl->session_negotiate->ciphersuite );
Glenn Strauss60bfe602022-03-14 19:04:24 -04001473 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version,
1474 ssl->tls_version ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001475 {
1476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001477 mbedtls_ssl_send_alert_message(
1478 ssl,
1479 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001480 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1481 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001482 }
1483
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001484 MBEDTLS_SSL_DEBUG_MSG( 3,
1485 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001486
Gilles Peskineeccd8882020-03-10 12:19:08 +01001487#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001488 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
Glenn Strauss60bfe602022-03-14 19:04:24 -04001489 ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001490 {
1491 ssl->handshake->ecrs_enabled = 1;
1492 }
1493#endif
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00001496 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001499 mbedtls_ssl_send_alert_message(
1500 ssl,
1501 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1502 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001503 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 }
Paul Bakker48916f92012-09-16 19:57:18 +00001505 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001506
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001507 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001508
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001509 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001510 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001511
Paul Bakker48916f92012-09-16 19:57:18 +00001512 while( ext_len )
1513 {
1514 unsigned int ext_id = ( ( ext[0] << 8 )
1515 | ( ext[1] ) );
1516 unsigned int ext_size = ( ( ext[2] << 8 )
1517 | ( ext[3] ) );
1518
1519 if( ext_size + 4 > ext_len )
1520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001522 mbedtls_ssl_send_alert_message(
1523 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1524 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001525 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001526 }
1527
1528 switch( ext_id )
1529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1532#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001533 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001534#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001535
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001536 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1537 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001538 return( ret );
1539
1540 break;
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1543 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001544 MBEDTLS_SSL_DEBUG_MSG( 3,
1545 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001546
1547 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1548 ext + 4, ext_size ) ) != 0 )
1549 {
1550 return( ret );
1551 }
1552
1553 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001555
Hanno Beckera0e20d02019-05-15 14:03:01 +01001556#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001557 case MBEDTLS_TLS_EXT_CID:
1558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1559
1560 if( ( ret = ssl_parse_cid_ext( ssl,
1561 ext + 4,
1562 ext_size ) ) != 0 )
1563 {
1564 return( ret );
1565 }
1566
1567 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001568#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1571 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001573
1574 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1575 ext + 4, ext_size ) ) != 0 )
1576 {
1577 return( ret );
1578 }
1579
1580 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001585 MBEDTLS_SSL_DEBUG_MSG( 3,
1586 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001587
1588 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1589 ext + 4, ext_size ) ) != 0 )
1590 {
1591 return( ret );
1592 }
1593
1594 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1598 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001600
1601 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1602 ext + 4, ext_size ) ) != 0 )
1603 {
1604 return( ret );
1605 }
1606
1607 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001609
Robert Cragie136884c2015-10-02 13:34:31 +01001610#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001611 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001613 MBEDTLS_SSL_DEBUG_MSG( 3,
1614 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001615
1616 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1617 ext + 4, ext_size ) ) != 0 )
1618 {
1619 return( ret );
1620 }
1621
1622 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001623#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1624 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001625
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001626#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1627 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1629
1630 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1631 ext + 4, ext_size ) ) != 0 )
1632 {
1633 return( ret );
1634 }
1635
1636 break;
1637#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_ALPN)
1640 case MBEDTLS_TLS_EXT_ALPN:
1641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001642
Johan Pascal275874b2020-10-27 10:43:53 +01001643 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1644 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001645
1646 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001648
Johan Pascalb62bb512015-12-03 21:56:45 +01001649#if defined(MBEDTLS_SSL_DTLS_SRTP)
1650 case MBEDTLS_TLS_EXT_USE_SRTP:
1651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1652
Johan Pascalc3ccd982020-10-28 17:18:18 +01001653 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1654 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001655
1656 break;
1657#endif /* MBEDTLS_SSL_DTLS_SRTP */
1658
Paul Bakker48916f92012-09-16 19:57:18 +00001659 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001660 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001661 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001662 }
1663
1664 ext_len -= 4 + ext_size;
1665 ext += 4 + ext_size;
1666
1667 if( ext_len > 0 && ext_len < 4 )
1668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001670 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001671 }
1672 }
1673
1674 /*
1675 * Renegotiation security checks
1676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001678 ssl->conf->allow_legacy_renegotiation ==
1679 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001680 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001681 MBEDTLS_SSL_DEBUG_MSG( 1,
1682 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001683 handshake_failure = 1;
1684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_SSL_RENEGOTIATION)
1686 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1687 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001688 renegotiation_info_seen == 0 )
1689 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001690 MBEDTLS_SSL_DEBUG_MSG( 1,
1691 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001692 handshake_failure = 1;
1693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1695 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001696 ssl->conf->allow_legacy_renegotiation ==
1697 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001700 handshake_failure = 1;
1701 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1703 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001704 renegotiation_info_seen == 1 )
1705 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001706 MBEDTLS_SSL_DEBUG_MSG( 1,
1707 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001708 handshake_failure = 1;
1709 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001711
1712 if( handshake_failure == 1 )
1713 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001714 mbedtls_ssl_send_alert_message(
1715 ssl,
1716 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1717 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001718 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001719 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
1723 return( 0 );
1724}
1725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1727 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001728static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1729 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001730 unsigned char *end )
1731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001733 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001734
Paul Bakker29e1f122013-04-16 13:07:56 +02001735 /*
1736 * Ephemeral DH parameters:
1737 *
1738 * struct {
1739 * opaque dh_p<1..2^16-1>;
1740 * opaque dh_g<1..2^16-1>;
1741 * opaque dh_Ys<1..2^16-1>;
1742 * } ServerDHParams;
1743 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001744 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1745 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001748 return( ret );
1749 }
1750
Gilles Peskine487bbf62021-05-27 22:17:07 +02001751 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001752 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001753 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001755 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001756 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001757 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001758 }
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1761 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1762 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001763
1764 return( ret );
1765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1767 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001768
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001769#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1770 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1771 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1772#if defined(MBEDTLS_USE_PSA_CRYPTO)
1773static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001774 unsigned char **p,
1775 unsigned char *end )
1776{
1777 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001778 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001779 uint8_t ecpoint_len;
1780 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1781
1782 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001783 * struct {
1784 * ECParameters curve_params;
1785 * ECPoint public;
1786 * } ServerECDHParams;
1787 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001788 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001789 * 2..3 NamedCurve
1790 * 4 ECPoint.len
1791 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001792 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001793 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001794 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001795
1796 /* First byte is curve_type; only named_curve is handled */
1797 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001798 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001799
1800 /* Next two bytes are the namedcurve value */
1801 tls_id = *(*p)++;
1802 tls_id <<= 8;
1803 tls_id |= *(*p)++;
1804
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001805 /* Check it's a curve we offered */
1806 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001807 {
1808 MBEDTLS_SSL_DEBUG_MSG( 2,
1809 ( "bad server key exchange message (ECDHE curve): %u",
1810 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001811 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001812 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001813
Hanno Beckerbb89e272019-01-08 12:54:37 +00001814 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001815 if( ( handshake->ecdh_psa_type =
1816 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001817 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001818 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001819 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001820 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001821
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001822 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001823 ecpoint_len = *(*p)++;
1824 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001825 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001826
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001827 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1828 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001829
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001830 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1831 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001832 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001833
Hanno Beckerbb89e272019-01-08 12:54:37 +00001834 return( 0 );
1835}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001836#else
Neil Armstrong1f198d82022-04-13 15:02:30 +02001837static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1838{
1839 const mbedtls_ecp_curve_info *curve_info;
1840 mbedtls_ecp_group_id grp_id;
1841#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1842 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1843#else
1844 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1845#endif
Hanno Beckerbb89e272019-01-08 12:54:37 +00001846
Neil Armstrong1f198d82022-04-13 15:02:30 +02001847 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1848 if( curve_info == NULL )
1849 {
1850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1851 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1852 }
1853
1854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1855
1856 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1857 return( -1 );
1858
1859 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1860 MBEDTLS_DEBUG_ECDH_QP );
1861
1862 return( 0 );
1863}
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001866 unsigned char **p,
1867 unsigned char *end )
1868{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001870
Paul Bakker29e1f122013-04-16 13:07:56 +02001871 /*
1872 * Ephemeral ECDH parameters:
1873 *
1874 * struct {
1875 * ECParameters curve_params;
1876 * ECPoint public;
1877 * } ServerECDHParams;
1878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001880 (const unsigned char **) p, end ) ) != 0 )
1881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001883#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001884 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1885 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001886#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001887 return( ret );
1888 }
1889
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001890 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001891 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001892 MBEDTLS_SSL_DEBUG_MSG( 1,
1893 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001894 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001895 }
1896
Paul Bakker29e1f122013-04-16 13:07:56 +02001897 return( ret );
1898}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001899#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1901 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1902 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001903
Gilles Peskineeccd8882020-03-10 12:19:08 +01001904#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001906 unsigned char **p,
1907 unsigned char *end )
1908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001910 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001911 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001912
1913 /*
1914 * PSK parameters:
1915 *
1916 * opaque psk_identity_hint<0..2^16-1>;
1917 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001918 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001919 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001920 MBEDTLS_SSL_DEBUG_MSG( 1,
1921 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001922 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001923 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001924 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001925 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001926
irwir6527bd62019-09-21 18:51:25 +03001927 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001928 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001929 MBEDTLS_SSL_DEBUG_MSG( 1,
1930 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001931 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001932 }
1933
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001934 /*
1935 * Note: we currently ignore the PKS identity hint, as we only allow one
1936 * PSK to be provisionned on the client. This could be changed later if
1937 * someone needs that feature.
1938 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001939 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001940 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941
1942 return( ret );
1943}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001944#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1947 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001948/*
1949 * Generate a pre-master secret and encrypt it with the server's RSA key
1950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001952 size_t offset, size_t *olen,
1953 size_t pms_offset )
1954{
Janos Follath865b3eb2019-12-16 11:46:15 +00001955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001956 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001957 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001958 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001959
Angus Grattond8213d02016-05-25 20:56:48 +10001960 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001961 {
1962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1963 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1964 }
1965
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001966 /*
1967 * Generate (part of) the pre-master as
1968 * struct {
1969 * ProtocolVersion client_version;
1970 * opaque random[46];
1971 * } PreMasterSecret;
1972 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001973 mbedtls_ssl_write_version( p, ssl->conf->transport,
1974 MBEDTLS_SSL_VERSION_TLS1_2 );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001975
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001976 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001979 return( ret );
1980 }
1981
1982 ssl->handshake->pmslen = 48;
1983
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001984#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1985 peer_pk = &ssl->handshake->peer_pubkey;
1986#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001987 if( ssl->session_negotiate->peer_cert == NULL )
1988 {
Hanno Becker8273df82019-02-06 17:37:32 +00001989 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00001991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001992 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001993 peer_pk = &ssl->session_negotiate->peer_cert->pk;
1994#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001995
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001996 /*
1997 * Now write it out, encrypted
1998 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001999 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2002 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002003 }
2004
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002005 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002006 p, ssl->handshake->pmslen,
2007 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002008 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002009 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002012 return( ret );
2013 }
2014
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002015 if( len_bytes == 2 )
2016 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002017 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002018 *olen += 2;
2019 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002020
Hanno Beckerae553dd2019-02-08 14:06:00 +00002021#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2022 /* We don't need the peer's public key anymore. Free it. */
2023 mbedtls_pk_free( peer_pk );
2024#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002025 return( 0 );
2026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2028 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002029
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002030#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2031 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2032 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002034 unsigned char **p,
2035 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 mbedtls_md_type_t *md_alg,
2037 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 *md_alg = MBEDTLS_MD_NONE;
2040 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002041
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002042 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002043 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002044
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002045 /*
2046 * Get hash algorithm
2047 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002048 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2049 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002050 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002051 MBEDTLS_SSL_DEBUG_MSG( 1,
2052 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002053 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002054 }
2055
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002056 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002057 * Get signature algorithm
2058 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002059 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2060 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002061 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002062 MBEDTLS_SSL_DEBUG_MSG( 1,
2063 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002064 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002065 }
2066
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002067 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002068 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002069 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002070 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002071 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002072 MBEDTLS_SSL_DEBUG_MSG( 1,
2073 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002074 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002075 }
2076
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2078 (*p)[1] ) );
2079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2080 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002081 *p += 2;
2082
2083 return( 0 );
2084}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002085#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2086 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2087 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2090 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2091static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002092{
Janos Follath865b3eb2019-12-16 11:46:15 +00002093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002095 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002096
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002097#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2098 peer_pk = &ssl->handshake->peer_pubkey;
2099#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002100 if( ssl->session_negotiate->peer_cert == NULL )
2101 {
Hanno Becker8273df82019-02-06 17:37:32 +00002102 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002105 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002106 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2107#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002108
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002109 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2112 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002113 }
2114
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002115 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002116
Przemek Stekielea4000f2022-03-16 09:49:33 +01002117#if defined(MBEDTLS_USE_PSA_CRYPTO)
2118 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002119 size_t olen = 0;
2120
2121 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2122 {
2123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2124 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2125 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002126
2127 ssl->handshake->ecdh_psa_type =
2128 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2129 &ecdh_bits ) );
2130
2131 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2132 {
2133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2134 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2135 }
2136
2137 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2138
Przemek Stekielea4000f2022-03-16 09:49:33 +01002139 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002140 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2141 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2142 ssl->handshake->ecdh_psa_peerkey,
2143 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002144
Przemek Stekiel561a4232022-03-16 13:16:24 +01002145 if ( ret != 0 )
2146 {
2147 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2148 return( ret );
2149 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002150
Przemek Stekiel561a4232022-03-16 13:16:24 +01002151 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002152#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2154 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002157 return( ret );
2158 }
2159
2160 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002163 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002164 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002165#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002166#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2167 /* We don't need the peer's public key anymore. Free it,
2168 * so that more RAM is available for upcoming expensive
2169 * operations like ECDHE. */
2170 mbedtls_pk_free( peer_pk );
2171#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2172
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002173 return( ret );
2174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2176 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002179{
Janos Follath865b3eb2019-12-16 11:46:15 +00002180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002181 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002182 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002183 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2188 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 ssl->state++;
2192 return( 0 );
2193 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002194 ((void) p);
2195 ((void) end);
2196#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2199 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2200 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2201 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002202 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002203 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002206 mbedtls_ssl_send_alert_message(
2207 ssl,
2208 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2209 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002210 return( ret );
2211 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002214 ssl->state++;
2215 return( 0 );
2216 }
2217 ((void) p);
2218 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2220 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002221
Gilles Peskineeccd8882020-03-10 12:19:08 +01002222#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002223 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002224 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002225 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002226 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002227 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002228#endif
2229
Hanno Becker327c93b2018-08-15 13:56:18 +01002230 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 return( ret );
2234 }
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002239 mbedtls_ssl_send_alert_message(
2240 ssl,
2241 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2242 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
2245
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002246 /*
2247 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2248 * doesn't use a psk_identity_hint
2249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2253 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002254 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002255 /* Current message is probably either
2256 * CertificateRequest or ServerHelloDone */
2257 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002258 goto exit;
2259 }
2260
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002261 MBEDTLS_SSL_DEBUG_MSG( 1,
2262 ( "server key exchange message must not be skipped" ) );
2263 mbedtls_ssl_send_alert_message(
2264 ssl,
2265 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2266 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002267
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
Gilles Peskineeccd8882020-03-10 12:19:08 +01002271#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002272 if( ssl->handshake->ecrs_enabled )
2273 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2274
2275start_processing:
2276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002278 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002280
Gilles Peskineeccd8882020-03-10 12:19:08 +01002281#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2283 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2284 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2285 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002286 {
2287 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002290 mbedtls_ssl_send_alert_message(
2291 ssl,
2292 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002293 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002294 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002295 }
2296 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002297#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2300 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2301 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2302 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002303 ; /* nothing more to do */
2304 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2306 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2307#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2308 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2309 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2310 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002312 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002313 {
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,
2318 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002319 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002320 }
2321 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2324 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002325#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2326 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2328 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2329 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2330 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002331 {
2332 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002335 mbedtls_ssl_send_alert_message(
2336 ssl,
2337 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2338 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002339 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002340 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002341 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002342 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2344 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2345 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002346#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2347 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2348 {
2349 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2350 p, end - p );
2351 if( ret != 0 )
2352 {
2353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002354 mbedtls_ssl_send_alert_message(
2355 ssl,
2356 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002357 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2358 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002359 }
2360 }
2361 else
2362#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2365 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002367
Gilles Peskineeccd8882020-03-10 12:19:08 +01002368#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002369 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002370 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002371 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002372#if defined(MBEDTLS_USE_PSA_CRYPTO)
2373 unsigned char hash[PSA_HASH_MAX_SIZE];
2374#else
2375 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2378 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2379 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002380 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002381 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002382
Hanno Beckera6899bb2019-02-06 18:26:03 +00002383 mbedtls_pk_context * peer_pk;
2384
Paul Bakker29e1f122013-04-16 13:07:56 +02002385 /*
2386 * Handle the digitally-signed structure
2387 */
Ronald Cron90915f22022-03-07 11:11:36 +01002388 if( ssl_parse_signature_algorithm( ssl, &p, end,
2389 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002390 {
Ronald Cron90915f22022-03-07 11:11:36 +01002391 MBEDTLS_SSL_DEBUG_MSG( 1,
2392 ( "bad server key exchange message" ) );
2393 mbedtls_ssl_send_alert_message(
2394 ssl,
2395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2396 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2397 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002398 }
Ronald Cron90915f22022-03-07 11:11:36 +01002399
2400 if( pk_alg !=
2401 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002402 {
Ronald Cron90915f22022-03-07 11:11:36 +01002403 MBEDTLS_SSL_DEBUG_MSG( 1,
2404 ( "bad server key exchange message" ) );
2405 mbedtls_ssl_send_alert_message(
2406 ssl,
2407 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2408 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2409 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002410 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002411
2412 /*
2413 * Read signature
2414 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002415
2416 if( p > end - 2 )
2417 {
2418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002419 mbedtls_ssl_send_alert_message(
2420 ssl,
2421 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2422 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002423 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002424 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002425 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002426 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002427
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002428 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002431 mbedtls_ssl_send_alert_message(
2432 ssl,
2433 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2434 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002435 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002439
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002440 /*
2441 * Compute the hash that has been signed
2442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002444 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002445 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2446 params, params_len,
2447 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002448 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002449 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002450 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002451 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002455 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002456
Gilles Peskineca1d7422018-04-24 11:53:22 +02002457 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002458
Hanno Beckera6899bb2019-02-06 18:26:03 +00002459#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2460 peer_pk = &ssl->handshake->peer_pubkey;
2461#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002462 if( ssl->session_negotiate->peer_cert == NULL )
2463 {
Hanno Becker8273df82019-02-06 17:37:32 +00002464 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002467 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002468 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2469#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002470
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002471 /*
2472 * Verify signature
2473 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002474 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002477 mbedtls_ssl_send_alert_message(
2478 ssl,
2479 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2480 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002482 }
2483
Gilles Peskineeccd8882020-03-10 12:19:08 +01002484#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002485 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002486 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002487#endif
2488
Hanno Beckera6899bb2019-02-06 18:26:03 +00002489 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002490 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002491 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002492#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002493 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2494#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002495 mbedtls_ssl_send_alert_message(
2496 ssl,
2497 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2498 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002500#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002501 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2502 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2503#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002504 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002505 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002506
2507#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2508 /* We don't need the peer's public key anymore. Free it,
2509 * so that more RAM is available for upcoming expensive
2510 * operations like ECDHE. */
2511 mbedtls_pk_free( peer_pk );
2512#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002514#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002516exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 ssl->state++;
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
2521 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522}
2523
Gilles Peskineeccd8882020-03-10 12:19:08 +01002524#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002526{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002528 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002531
Hanno Becker1aa267c2017-04-28 17:08:27 +01002532 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002535 ssl->state++;
2536 return( 0 );
2537 }
2538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002541}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002542#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002544{
Janos Follath865b3eb2019-12-16 11:46:15 +00002545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002546 unsigned char *buf;
2547 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002548 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002549 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002550 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002551 size_t sig_alg_len;
2552#if defined(MBEDTLS_DEBUG_C)
2553 unsigned char *sig_alg;
2554#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Hanno Becker1aa267c2017-04-28 17:08:27 +01002558 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002561 ssl->state++;
2562 return( 0 );
2563 }
2564
Hanno Becker327c93b2018-08-15 13:56:18 +01002565 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2568 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 }
2570
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002571 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2572 {
2573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002574 mbedtls_ssl_send_alert_message(
2575 ssl,
2576 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2577 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002578 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002581 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002582 ssl->handshake->client_auth =
2583 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002586 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Jerry Yufb28b882022-01-28 11:05:58 +08002588 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002589 {
Johan Pascala89ca862020-08-25 10:03:19 +02002590 /* Current message is probably the ServerHelloDone */
2591 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002592 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002593 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002594
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002595 /*
2596 * struct {
2597 * ClientCertificateType certificate_types<1..2^8-1>;
2598 * SignatureAndHashAlgorithm
2599 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2600 * DistinguishedName certificate_authorities<0..2^16-1>;
2601 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002602 *
2603 * Since we only support a single certificate on clients, let's just
2604 * ignore all the information that's supposed to help us pick a
2605 * certificate.
2606 *
2607 * We could check that our certificate matches the request, and bail out
2608 * if it doesn't, but it's simpler to just send the certificate anyway,
2609 * and give the server the opportunity to decide if it should terminate
2610 * the connection when it doesn't like our certificate.
2611 *
2612 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2613 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002614 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002615 *
2616 * However, we still minimally parse the message to check it is at least
2617 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002618 */
Paul Bakker926af752012-11-23 13:38:07 +01002619 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002620
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002621 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002622 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2623 {
2624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2625 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2626 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002627 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002630 n = cert_type_len;
2631
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002632 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002633 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002634 * * the length of the signature algorithms field (if minor version of
2635 * SSL is 3),
2636 * * distinguished name length otherwise.
2637 * Both reach at most the index:
2638 * ...hdr_len + 2 + n,
2639 * therefore the buffer length at this point must be greater than that
2640 * regardless of the actual code path.
2641 */
2642 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002645 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2646 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002647 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002648 }
2649
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002650 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002651 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2652 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2653
2654 /*
2655 * The furthest access in buf is in the loop few lines below:
2656 * sig_alg[i + 1],
2657 * where:
2658 * sig_alg = buf + ...hdr_len + 3 + n,
2659 * max(i) = sig_alg_len - 1.
2660 * Therefore the furthest access is:
2661 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2662 * which reduces to:
2663 * buf[...hdr_len + 3 + n + sig_alg_len],
2664 * which is one less than we need the buf to be.
2665 */
2666 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002667 {
Ronald Cron90915f22022-03-07 11:11:36 +01002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2669 mbedtls_ssl_send_alert_message(
2670 ssl,
2671 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2672 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2673 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002674 }
Paul Bakker926af752012-11-23 13:38:07 +01002675
Ronald Cron90915f22022-03-07 11:11:36 +01002676#if defined(MBEDTLS_DEBUG_C)
2677 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2678 for( size_t i = 0; i < sig_alg_len; i += 2 )
2679 {
2680 MBEDTLS_SSL_DEBUG_MSG( 3,
2681 ( "Supported Signature Algorithm found: %d,%d",
2682 sig_alg[i], sig_alg[i + 1] ) );
2683 }
2684#endif
2685
2686 n += 2 + sig_alg_len;
2687
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002688 /* certificate_authorities */
2689 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2690 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002691
2692 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002693 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002696 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2697 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002698 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002699 }
2700
2701exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
2704 return( 0 );
2705}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002706#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002709{
Janos Follath865b3eb2019-12-16 11:46:15 +00002710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Hanno Becker327c93b2018-08-15 13:56:18 +01002714 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2717 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002719
2720 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2721 {
2722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2723 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2727 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002730 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2731 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002732 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
2735 ssl->state++;
2736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002740#endif
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
2744 return( 0 );
2745}
2746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002748{
Janos Follath865b3eb2019-12-16 11:46:15 +00002749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002750
2751 size_t header_len;
2752 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002753 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002754 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2759 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 /*
2762 * DHM key exchange -- send G^X mod P
2763 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002764 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Joe Subbiani6dd73642021-07-19 11:56:54 +01002766 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002767 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002770 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002771 &ssl->out_msg[header_len], content_len,
2772 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 if( ret != 0 )
2774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 return( ret );
2777 }
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2780 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002783 ssl->handshake->premaster,
2784 MBEDTLS_PREMASTER_SIZE,
2785 &ssl->handshake->pmslen,
2786 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 return( ret );
2790 }
2791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 }
2794 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002796#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2797 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2798 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2799 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002800 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002801 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2802 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2803 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002804 {
Neil Armstrong11d49452022-04-13 15:03:43 +02002805#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kureka0237f82022-02-24 13:24:52 -05002806 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2807 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002808 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002809
2810 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2811
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002812 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002813
Hanno Becker0a94a642019-01-11 14:35:30 +00002814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2815
Hanno Becker4a63ed42019-01-08 11:39:35 +00002816 /*
2817 * Generate EC private key for ECDHE exchange.
2818 */
2819
Hanno Becker4a63ed42019-01-08 11:39:35 +00002820 /* The master secret is obtained from the shared ECDH secret by
2821 * applying the TLS 1.2 PRF with a specific salt and label. While
2822 * the PSA Crypto API encourages combining key agreement schemes
2823 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2824 * yet support the provisioning of salt + label to the KDF.
2825 * For the time being, we therefore need to split the computation
2826 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002827 key_attributes = psa_key_attributes_init();
2828 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002829 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002830 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2831 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002832
2833 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002834 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002835 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002836 if( status != PSA_SUCCESS )
2837 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2838
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002839 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002840 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002841 * but we just need to add a length byte before that. */
2842 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2843 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2844 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2845 size_t own_pubkey_len;
2846
Hanno Becker4a63ed42019-01-08 11:39:35 +00002847 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002848 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002849 &own_pubkey_len );
2850 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002851 {
2852 psa_destroy_key( handshake->ecdh_psa_privkey );
2853 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002854 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002855 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002856
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002857 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2858 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002859
Hanno Becker4a63ed42019-01-08 11:39:35 +00002860 /* The ECDH secret is the premaster secret used for key derivation. */
2861
Janos Follathdf3b0892019-08-08 11:12:24 +01002862 /* Compute ECDH shared secret. */
2863 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2864 handshake->ecdh_psa_privkey,
2865 handshake->ecdh_psa_peerkey,
2866 handshake->ecdh_psa_peerkey_len,
2867 ssl->handshake->premaster,
2868 sizeof( ssl->handshake->premaster ),
2869 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002870
Andrzej Kureka0237f82022-02-24 13:24:52 -05002871 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002872 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002873
2874 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2875 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002876#else
Paul Bakker41c83d32013-03-20 14:39:14 +01002877 /*
2878 * ECDH key exchange -- send client public value
2879 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002880 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002881
Gilles Peskineeccd8882020-03-10 12:19:08 +01002882#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002883 if( ssl->handshake->ecrs_enabled )
2884 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002885 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002886 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002887
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002888 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2889 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002890#endif
2891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002893 &content_len,
2894 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002895 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002896 if( ret != 0 )
2897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002899#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002900 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2901 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2902#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002903 return( ret );
2904 }
2905
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002906 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2907 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002908
Gilles Peskineeccd8882020-03-10 12:19:08 +01002909#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002910 if( ssl->handshake->ecrs_enabled )
2911 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002912 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002913 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002914 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002915
2916ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002917 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002918 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002921 &ssl->handshake->pmslen,
2922 ssl->handshake->premaster,
2923 MBEDTLS_MPI_MAX_SIZE,
2924 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002927#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002928 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2929 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2930#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002931 return( ret );
2932 }
2933
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002934 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2935 MBEDTLS_DEBUG_ECDH_Z );
Neil Armstrong11d49452022-04-13 15:03:43 +02002936#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker41c83d32013-03-20 14:39:14 +01002937 }
2938 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2940 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2941 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2942 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002943#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2944 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2945 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2946 {
2947 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2948 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2949 psa_key_attributes_t key_attributes;
2950
2951 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2952
2953 /*
2954 * opaque psk_identity<0..2^16-1>;
2955 */
2956 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002957 /* We don't offer PSK suites if we don't have a PSK,
2958 * and we check that the server's choice is among the
2959 * ciphersuites we offered, so this should never happen. */
2960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002961
Neil Armstrongfc834f22022-03-23 17:54:38 +01002962 /* uint16 to store content length */
2963 const size_t content_len_size = 2;
2964
Neil Armstrong868af822022-03-09 10:26:25 +01002965 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002966
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002967 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002968 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002969 {
2970 MBEDTLS_SSL_DEBUG_MSG( 1,
2971 ( "psk identity too long or SSL buffer too short" ) );
2972 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2973 }
2974
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002975 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002976
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002977 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
2978 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
2979 header_len += content_len_size;
2980
2981 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01002982 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002983 p += ssl->conf->psk_identity_len;
2984
Neil Armstrong868af822022-03-09 10:26:25 +01002985 header_len += ssl->conf->psk_identity_len;
2986
2987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2988
2989 /*
2990 * Generate EC private key for ECDHE exchange.
2991 */
2992
2993 /* The master secret is obtained from the shared ECDH secret by
2994 * applying the TLS 1.2 PRF with a specific salt and label. While
2995 * the PSA Crypto API encourages combining key agreement schemes
2996 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2997 * yet support the provisioning of salt + label to the KDF.
2998 * For the time being, we therefore need to split the computation
2999 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3000 key_attributes = psa_key_attributes_init();
3001 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3002 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3003 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3004 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3005
3006 /* Generate ECDH private key. */
3007 status = psa_generate_key( &key_attributes,
3008 &handshake->ecdh_psa_privkey );
3009 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003010 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003011
3012 /* Export the public part of the ECDH private key from PSA.
3013 * The export format is an ECPoint structure as expected by TLS,
3014 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003015 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003016 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3017 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003018 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003019
3020 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3021 own_pubkey, own_pubkey_max_len,
3022 &own_pubkey_len );
3023 if( status != PSA_SUCCESS )
3024 {
3025 psa_destroy_key( handshake->ecdh_psa_privkey );
3026 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003027 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003028 }
3029
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003030 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003031 content_len = own_pubkey_len + 1;
3032
Neil Armstrong25400452022-03-23 17:44:07 +01003033 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3034 * - a uint16 containing the length (in octets) of the ECDH computation
3035 * - the octet string produced by the ECDH computation
3036 * - a uint16 containing the length (in octets) of the PSK
3037 * - the PSK itself
3038 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003039 unsigned char *pms = ssl->handshake->premaster;
3040 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003041 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003042 /* uint16 to store length (in octets) of the ECDH computation */
3043 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003044 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003045
Neil Armstrong25400452022-03-23 17:44:07 +01003046 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003047 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3048 handshake->ecdh_psa_privkey,
3049 handshake->ecdh_psa_peerkey,
3050 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003051 pms + zlen_size,
3052 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003053 &zlen );
3054
3055 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3056 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3057
Neil Armstrongc530aa62022-03-23 17:45:01 +01003058 if( status != PSA_SUCCESS )
3059 return( psa_ssl_status_to_mbedtls( status ) );
3060 else if( destruction_status != PSA_SUCCESS )
3061 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003062
Neil Armstrong25400452022-03-23 17:44:07 +01003063 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003064 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3065 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003066
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003067 /* In case of opaque psk skip writting psk to pms.
3068 * Opaque key will be handled later. */
3069 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 1 )
3070 {
3071 const unsigned char *psk = NULL;
3072 size_t psk_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003073
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003074 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
3075 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
3076 /*
3077 * This should never happen because the existence of a PSK is always
3078 * checked before calling this function
3079 */
3080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003081
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003082 /* opaque psk<0..2^16-1>; */
3083 if( (size_t)( pms_end - pms ) < ( 2 + psk_len ) )
3084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstronge18ff952022-04-04 18:34:55 +02003085
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003086 /* Write the PSK length as uint16 */
3087 MBEDTLS_PUT_UINT16_BE( psk_len, pms, 0 );
3088 pms += 2;
Neil Armstrong868af822022-03-09 10:26:25 +01003089
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003090 /* Write the PSK itself */
3091 memcpy( pms, psk, psk_len );
3092 pms += psk_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003093
Przemek Stekiel19b80f82022-04-14 08:29:31 +02003094 ssl->handshake->pmslen = pms - ssl->handshake->premaster;
3095 }
3096 else
3097 {
3098 MBEDTLS_SSL_DEBUG_MSG( 1,
3099 ( "skip PMS generation for opaque ECDHE-PSK" ) );
3100 }
Neil Armstrong868af822022-03-09 10:26:25 +01003101 }
3102 else
3103#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3104 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003105#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003106 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003107 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003108 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003109 * opaque psk_identity<0..2^16-1>;
3110 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003111 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003112 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003113 /* We don't offer PSK suites if we don't have a PSK,
3114 * and we check that the server's choice is among the
3115 * ciphersuites we offered, so this should never happen. */
3116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003117 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003118
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003119 header_len = 4;
3120 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003121
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003122 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003123 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003124 MBEDTLS_SSL_DEBUG_MSG( 1,
3125 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003126 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3127 }
3128
Joe Subbianifbeb6922021-07-16 14:27:50 +01003129 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3130 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003131
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003132 memcpy( ssl->out_msg + header_len,
3133 ssl->conf->psk_identity,
3134 ssl->conf->psk_identity_len );
3135 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3138 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003139 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003140 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003141 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003142 else
3143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3145 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003146 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003147 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3148 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003149 return( ret );
3150 }
3151 else
3152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3154 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003155 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003156#if defined(MBEDTLS_USE_PSA_CRYPTO)
3157 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003158 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003159 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3160#endif /* MBEDTLS_USE_PSA_CRYPTO */
3161
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003162 /*
3163 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3164 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003165 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003166
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003167 if( header_len + 2 + content_len >
3168 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003169 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003170 MBEDTLS_SSL_DEBUG_MSG( 1,
3171 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003172 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3173 }
3174
Joe Subbianifbeb6922021-07-16 14:27:50 +01003175 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3176 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003179 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003180 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003181 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003182 if( ret != 0 )
3183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003185 return( ret );
3186 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003187 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003188 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003190#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3191 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003193 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003194 /*
3195 * ClientECDiffieHellmanPublic public;
3196 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003197 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3198 &content_len,
3199 &ssl->out_msg[header_len],
3200 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003201 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003202 if( ret != 0 )
3203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003205 return( ret );
3206 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003207
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003208 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3209 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003210 }
3211 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003212#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003216 }
3217
Hanno Beckerafd311e2018-10-23 15:26:40 +01003218#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3219 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3220 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003221 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003222 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003223 MBEDTLS_SSL_DEBUG_MSG( 1,
3224 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003225 }
3226 else
Przemek Stekielf2534ba2022-04-05 17:21:14 +02003227 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK &&
3228 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
3229 {
3230 MBEDTLS_SSL_DEBUG_MSG( 1,
3231 ( "skip PMS generation for opaque RSA-PSK" ) );
3232 }
3233 else
Hanno Beckerafd311e2018-10-23 15:26:40 +01003234#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3235 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003237 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003238 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003239 MBEDTLS_SSL_DEBUG_RET( 1,
3240 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003241 return( ret );
3242 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243 }
3244 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003245#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3247 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003249 header_len = 4;
3250 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3251 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003252 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 }
Paul Bakkered27a042013-04-18 22:46:23 +02003254 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003256#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3257 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3258 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003259 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003260
3261 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003262 ssl->out_msg + header_len,
3263 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3264 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003265 ssl->conf->f_rng, ssl->conf->p_rng );
3266 if( ret != 0 )
3267 {
3268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3269 return( ret );
3270 }
3271
3272 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3273 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3274 ssl->conf->f_rng, ssl->conf->p_rng );
3275 if( ret != 0 )
3276 {
3277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3278 return( ret );
3279 }
3280 }
3281 else
3282#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003283 {
3284 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003289 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3291 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
3293 ssl->state++;
3294
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003295 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 return( ret );
3299 }
3300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
3303 return( 0 );
3304}
3305
Gilles Peskineeccd8882020-03-10 12:19:08 +01003306#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003308{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003309 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003310 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003318 return( ret );
3319 }
3320
Hanno Becker77adddc2019-02-07 12:32:43 +00003321 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003324 ssl->state++;
3325 return( 0 );
3326 }
3327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003330}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003331#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003335 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003336 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003337 size_t n = 0, offset = 0;
3338 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003339 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003341 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003342 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003343#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3344 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3345#else
3346 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3347#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003350
Gilles Peskineeccd8882020-03-10 12:19:08 +01003351#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003352 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003353 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003354 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003355 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003356 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003357#endif
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003362 return( ret );
3363 }
3364
Hanno Becker77adddc2019-02-07 12:32:43 +00003365 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003368 ssl->state++;
3369 return( 0 );
3370 }
3371
Jerry Yufb28b882022-01-28 11:05:58 +08003372 if( ssl->handshake->client_auth == 0 ||
3373 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 {
Johan Pascala89ca862020-08-25 10:03:19 +02003375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3376 ssl->state++;
3377 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 }
3379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 }
3385
3386 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003387 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003389#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003390 if( ssl->handshake->ecrs_enabled )
3391 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3392
3393sign:
3394#endif
3395
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003396 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
Ronald Cron90915f22022-03-07 11:11:36 +01003398 /*
3399 * digitally-signed struct {
3400 * opaque handshake_messages[handshake_messages_length];
3401 * };
3402 *
3403 * Taking shortcut here. We assume that the server always allows the
3404 * PRF Hash function and has sent it in the allowed signature
3405 * algorithms list received in the Certificate Request message.
3406 *
3407 * Until we encounter a server that does not, we will take this
3408 * shortcut.
3409 *
3410 * Reason: Otherwise we should have running hashes for SHA512 and
3411 * SHA224 in order to satisfy 'weird' needs from the server
3412 * side.
3413 */
3414 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003415 {
Ronald Cron90915f22022-03-07 11:11:36 +01003416 md_alg = MBEDTLS_MD_SHA384;
3417 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003419 else
Paul Bakker577e0062013-08-28 11:57:20 +02003420 {
Ronald Cron90915f22022-03-07 11:11:36 +01003421 md_alg = MBEDTLS_MD_SHA256;
3422 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003423 }
Ronald Cron90915f22022-03-07 11:11:36 +01003424 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3425
3426 /* Info from md_alg will be used instead */
3427 hashlen = 0;
3428 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003429
Gilles Peskineeccd8882020-03-10 12:19:08 +01003430#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003431 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003432 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003433#endif
3434
3435 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3436 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003437 ssl->out_msg + 6 + offset,
3438 out_buf_len - 6 - offset,
3439 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003440 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003443#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003444 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3445 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3446#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003447 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003448 }
Paul Bakker926af752012-11-23 13:38:07 +01003449
Joe Subbiani6dd73642021-07-19 11:56:54 +01003450 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451
Paul Bakker1ef83d62012-04-11 12:09:53 +00003452 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3454 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003455
3456 ssl->state++;
3457
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003458 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 return( ret );
3462 }
3463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
Paul Bakkered27a042013-04-18 22:46:23 +02003466 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003467}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003468#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3471static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003472{
Janos Follath865b3eb2019-12-16 11:46:15 +00003473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003474 uint32_t lifetime;
3475 size_t ticket_len;
3476 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003477 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003480
Hanno Becker327c93b2018-08-15 13:56:18 +01003481 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003484 return( ret );
3485 }
3486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003490 mbedtls_ssl_send_alert_message(
3491 ssl,
3492 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3493 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003495 }
3496
3497 /*
3498 * struct {
3499 * uint32 ticket_lifetime_hint;
3500 * opaque ticket<0..2^16-1>;
3501 * } NewSessionTicket;
3502 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003503 * 0 . 3 ticket_lifetime_hint
3504 * 4 . 5 ticket_len (n)
3505 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3508 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003511 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3512 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003513 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003514 }
3515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003517
Philippe Antoineb5b25432018-05-11 11:06:29 +02003518 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3519 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003520
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003521 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003526 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3527 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003528 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003529 }
3530
Paul Elliottd48d5c62021-01-07 14:47:05 +00003531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003532
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003533 /* We're not waiting for a NewSessionTicket message any more */
3534 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003536
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003537 /*
3538 * Zero-length ticket means the server changed his mind and doesn't want
3539 * to send a ticket after all, so just forget it
3540 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003541 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003542 return( 0 );
3543
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003544 if( ssl->session != NULL && ssl->session->ticket != NULL )
3545 {
3546 mbedtls_platform_zeroize( ssl->session->ticket,
3547 ssl->session->ticket_len );
3548 mbedtls_free( ssl->session->ticket );
3549 ssl->session->ticket = NULL;
3550 ssl->session->ticket_len = 0;
3551 }
3552
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003553 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3554 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003556 ssl->session_negotiate->ticket = NULL;
3557 ssl->session_negotiate->ticket_len = 0;
3558
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003559 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003560 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003562 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3563 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003565 }
3566
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003567 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003568
3569 ssl->session_negotiate->ticket = ticket;
3570 ssl->session_negotiate->ticket_len = ticket_len;
3571 ssl->session_negotiate->ticket_lifetime = lifetime;
3572
3573 /*
3574 * RFC 5077 section 3.4:
3575 * "If the client receives a session ticket from the server, then it
3576 * discards any Session ID that was sent in the ServerHello."
3577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003579 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003582
3583 return( 0 );
3584}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003586
Paul Bakker5121ce52009-01-03 21:22:43 +00003587/*
Paul Bakker1961b702013-01-25 14:49:24 +01003588 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003591{
3592 int ret = 0;
3593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003595 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3597 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003598 ssl->handshake->new_session_ticket != 0 )
3599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003601 }
3602#endif
3603
Paul Bakker1961b702013-01-25 14:49:24 +01003604 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 case MBEDTLS_SSL_HELLO_REQUEST:
3607 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 break;
3609
Paul Bakker1961b702013-01-25 14:49:24 +01003610 /*
3611 * ==> ClientHello
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003614 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003615 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
Paul Bakker1961b702013-01-25 14:49:24 +01003617 /*
3618 * <== ServerHello
3619 * Certificate
3620 * ( ServerKeyExchange )
3621 * ( CertificateRequest )
3622 * ServerHelloDone
3623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003625 ret = ssl_parse_server_hello( ssl );
3626 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3629 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003630 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003633 ret = ssl_parse_server_key_exchange( ssl );
3634 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003637 ret = ssl_parse_certificate_request( ssl );
3638 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003641 ret = ssl_parse_server_hello_done( ssl );
3642 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Paul Bakker1961b702013-01-25 14:49:24 +01003644 /*
3645 * ==> ( Certificate/Alert )
3646 * ClientKeyExchange
3647 * ( CertificateVerify )
3648 * ChangeCipherSpec
3649 * Finished
3650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3652 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003653 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003656 ret = ssl_write_client_key_exchange( ssl );
3657 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003660 ret = ssl_write_certificate_verify( ssl );
3661 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3664 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003665 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 case MBEDTLS_SSL_CLIENT_FINISHED:
3668 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003669 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003670
Paul Bakker1961b702013-01-25 14:49:24 +01003671 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003672 * <== ( NewSessionTicket )
3673 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003674 * Finished
3675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3677 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003678 ret = ssl_parse_new_session_ticket( ssl );
3679 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003680#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3683 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003684 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686 case MBEDTLS_SSL_SERVER_FINISHED:
3687 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003688 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 case MBEDTLS_SSL_FLUSH_BUFFERS:
3691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3692 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003693 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3696 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003697 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003698
Paul Bakker1961b702013-01-25 14:49:24 +01003699 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003702 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
3704 return( ret );
3705}
Jerry Yuc5aef882021-12-23 20:15:02 +08003706
Jerry Yufb4b6472022-01-27 15:03:26 +08003707#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */