blob: 7471cd0a6a0c2e49fab91cb5e8d775391d03f9c0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020026#else
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020028#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010029#define mbedtls_free free
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020030#endif
31
SimonBd5800b72016-04-26 07:43:27 +010032#include "mbedtls/ssl.h"
Ronald Cron7320e642022-03-08 13:34:49 +010033#include "ssl_client.h"
Chris Jones84a773f2021-03-05 18:38:47 +000034#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/debug.h"
36#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020037#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010038
Hanno Beckerbb89e272019-01-08 12:54:37 +000039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020041#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000042#endif /* MBEDTLS_USE_PSA_CRYPTO */
43
SimonBd5800b72016-04-26 07:43:27 +010044#include <string.h>
45
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020046#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010049#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050053#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020054#endif
55
Gilles Peskineeccd8882020-03-10 12:19:08 +010056#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Ronald Crond491c2d2022-02-19 18:30:46 +010057int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010058{
59 if( conf->psk_identity == NULL ||
60 conf->psk_identity_len == 0 )
61 {
62 return( 0 );
63 }
64
65 if( conf->psk != NULL && conf->psk_len != 0 )
66 return( 1 );
67
68#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020069 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010070 return( 1 );
71#endif /* MBEDTLS_USE_PSA_CRYPTO */
72
73 return( 0 );
74}
Hanno Beckerdfab8e22018-10-23 11:59:34 +010075
76#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker520224e2018-10-26 11:38:07 +010077static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
Hanno Beckerdfab8e22018-10-23 11:59:34 +010078{
79 if( conf->psk_identity == NULL ||
80 conf->psk_identity_len == 0 )
81 {
82 return( 0 );
83 }
84
85 if( conf->psk != NULL && conf->psk_len != 0 )
86 return( 1 );
87
88 return( 0 );
89}
90#endif /* MBEDTLS_USE_PSA_CRYPTO */
91
Gilles Peskineeccd8882020-03-10 12:19:08 +010092#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +010095static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
96 unsigned char *buf,
97 const unsigned char *end,
98 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010099{
100 unsigned char *p = buf;
101
102 *olen = 0;
103
Hanno Becker40f8b512017-10-12 14:58:55 +0100104 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
105 * initial ClientHello, in which case also adding the renegotiation
106 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +0100108 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100109
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100110 MBEDTLS_SSL_DEBUG_MSG( 3,
111 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100112
Hanno Becker261602c2017-04-12 14:54:42 +0100113 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100114
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115 /*
116 * Secure renegotiation
117 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100118 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
119 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120
121 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100122 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
123 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124
125 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
126
127 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100128
129 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100132
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200133#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100134 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100135
Hanno Becker261602c2017-04-12 14:54:42 +0100136static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
137 unsigned char *buf,
138 const unsigned char *end,
139 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100140{
141 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100142 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100143
144 *olen = 0;
145
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100146 MBEDTLS_SSL_DEBUG_MSG( 3,
147 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100148 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100149
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100150 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
151 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152
153 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100154 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200155
156 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100158
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200159 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100160
161 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100162}
Darryl Green11999bb2018-03-13 15:22:58 +0000163#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100164 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100165
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200166#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100167static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
168 unsigned char *buf,
169 const unsigned char *end,
170 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200171{
Janos Follath865b3eb2019-12-16 11:46:15 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200173 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200174 size_t kkpp_len;
175
176 *olen = 0;
177
178 /* Skip costly extension if we can't use EC J-PAKE anyway */
179 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100180 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200181
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100182 MBEDTLS_SSL_DEBUG_MSG( 3,
183 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200184
Hanno Becker261602c2017-04-12 14:54:42 +0100185 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200186
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100187 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
188 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200189
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200190 /*
191 * We may need to send ClientHello multiple times for Hello verification.
192 * We don't want to compute fresh values every time (both for performance
193 * and consistency reasons), so cache the extension content.
194 */
195 if( ssl->handshake->ecjpake_cache == NULL ||
196 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200197 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
199
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200200 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100201 p + 2, end - p - 2, &kkpp_len,
202 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200203 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200204 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100205 MBEDTLS_SSL_DEBUG_RET( 1 ,
206 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100207 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200208 }
209
210 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
211 if( ssl->handshake->ecjpake_cache == NULL )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100214 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200215 }
216
217 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
218 ssl->handshake->ecjpake_cache_len = kkpp_len;
219 }
220 else
221 {
222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
223
224 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100225 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200226
227 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200228 }
229
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100230 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
231 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200232
233 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100234
235 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200236}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200237#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000238
Hanno Beckera0e20d02019-05-15 14:03:01 +0100239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100240static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
241 unsigned char *buf,
242 const unsigned char *end,
243 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100244{
245 unsigned char *p = buf;
246 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100247
248 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100249 * Quoting draft-ietf-tls-dtls-connection-id-05
250 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100251 *
252 * struct {
253 * opaque cid<0..2^8-1>;
254 * } ConnectionId;
255 */
256
257 *olen = 0;
258 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
259 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
260 {
Hanno Becker261602c2017-04-12 14:54:42 +0100261 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100262 }
263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
264
265 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
266 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100267 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100268
269 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100270 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
271 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100272 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100273 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
274 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100275
276 *p++ = (uint8_t) ssl->own_cid_len;
277 memcpy( p, ssl->own_cid, ssl->own_cid_len );
278
279 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100280
281 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100282}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100283#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100286static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
287 unsigned char *buf,
288 const unsigned char *end,
289 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200290{
291 unsigned char *p = buf;
292
Simon Butcher0fc94e92015-09-28 20:52:04 +0100293 *olen = 0;
294
Hanno Becker261602c2017-04-12 14:54:42 +0100295 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
296 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200297
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100298 MBEDTLS_SSL_DEBUG_MSG( 3,
299 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200300
Hanno Becker261602c2017-04-12 14:54:42 +0100301 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100302
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100303 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
304 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200305
306 *p++ = 0x00;
307 *p++ = 1;
308
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200309 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200310
311 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100312
313 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100318static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
319 unsigned char *buf,
320 const unsigned char *end,
321 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100322{
323 unsigned char *p = buf;
324
Simon Butcher0fc94e92015-09-28 20:52:04 +0100325 *olen = 0;
326
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100327 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100328 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100329
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100330 MBEDTLS_SSL_DEBUG_MSG( 3,
331 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100332
Hanno Becker261602c2017-04-12 14:54:42 +0100333 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100334
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100335 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
336 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100337
338 *p++ = 0x00;
339 *p++ = 0x00;
340
341 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100342
343 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100348static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
349 unsigned char *buf,
350 const unsigned char *end,
351 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200352{
353 unsigned char *p = buf;
354
Simon Butcher0fc94e92015-09-28 20:52:04 +0100355 *olen = 0;
356
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100357 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100358 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200359
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100360 MBEDTLS_SSL_DEBUG_MSG( 3,
361 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200362
Hanno Becker261602c2017-04-12 14:54:42 +0100363 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100364
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100365 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
366 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200367
368 *p++ = 0x00;
369 *p++ = 0x00;
370
371 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100372
373 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100378static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
379 unsigned char *buf,
380 const unsigned char *end,
381 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200382{
383 unsigned char *p = buf;
384 size_t tlen = ssl->session_negotiate->ticket_len;
385
Simon Butcher0fc94e92015-09-28 20:52:04 +0100386 *olen = 0;
387
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200388 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100389 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200390
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100391 MBEDTLS_SSL_DEBUG_MSG( 3,
392 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200393
Hanno Becker261602c2017-04-12 14:54:42 +0100394 /* The addition is safe here since the ticket length is 16 bit. */
395 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100396
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100397 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
398 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200399
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100400 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
401 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200402
403 *olen = 4;
404
Simon Butchered997662015-09-28 02:14:30 +0100405 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100406 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200407
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100408 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000409 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200410
411 memcpy( p, ssl->session_negotiate->ticket, tlen );
412
413 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100414
415 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200418
Ron Eldora9788042018-12-05 11:04:31 +0200419#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200420static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200421 unsigned char *buf,
422 const unsigned char *end,
423 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100424{
425 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200426 size_t protection_profiles_index = 0, ext_len = 0;
427 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100428
429 *olen = 0;
430
Johan Pascalc3ccd982020-10-28 17:18:18 +0100431 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
432 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
433 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100434 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200435 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100436 }
437
Ron Eldora9788042018-12-05 11:04:31 +0200438 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100439 * uint8 SRTPProtectionProfile[2];
440 *
441 * struct {
442 * SRTPProtectionProfiles SRTPProtectionProfiles;
443 * opaque srtp_mki<0..255>;
444 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100445 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100446 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200447 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200448 {
449 mki_len = ssl->dtls_srtp_info.mki_len;
450 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300451 /* Extension length = 2 bytes for profiles length,
452 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
453 * 1 byte for srtp_mki vector length and the mki_len value
454 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200455 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
456
Johan Pascal77696ee2020-09-22 21:49:40 +0200457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
458
459 /* Check there is room in the buffer for the extension + 4 bytes
460 * - the extension tag (2 bytes)
461 * - the extension length (2 bytes)
462 */
463 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
464
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100465 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
466 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200467
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100468 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
469 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100470
Ron Eldor3adb9922017-12-21 10:15:08 +0200471 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200472 /* micro-optimization:
473 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
474 * which is lower than 127, so the upper byte of the length is always 0
475 * For the documentation, the more generic code is left in comments
476 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
477 * >> 8 ) & 0xFF );
478 */
479 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100480 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100481
Ron Eldoref72faf2018-07-12 11:54:20 +0300482 for( protection_profiles_index=0;
483 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
484 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100485 {
Johan Pascal43f94902020-09-22 12:25:52 +0200486 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200487 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200488 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200489 {
490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
491 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100492 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
493 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200494 }
495 else
496 {
497 /*
498 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200499 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200500 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200501 MBEDTLS_SSL_DEBUG_MSG( 3,
502 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200503 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200504 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
505 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200506 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100507 }
508 }
509
Ron Eldor591f1622018-01-22 12:30:04 +0200510 *p++ = mki_len & 0xFF;
511
512 if( mki_len != 0 )
513 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200514 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200515 /*
516 * Increment p to point to the current position.
517 */
518 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300519 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
520 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200521 }
522
Ron Eldoref72faf2018-07-12 11:54:20 +0300523 /*
524 * total extension length: extension type (2 bytes)
525 * + extension length (2 bytes)
526 * + protection profile length (2 bytes)
527 * + 2 * number of protection profiles
528 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200529 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300530 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200531 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200532
533 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100534}
535#endif /* MBEDTLS_SSL_DTLS_SRTP */
536
Ronald Cron4079abc2022-02-20 10:35:26 +0100537int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
538 unsigned char *buf,
539 const unsigned char *end,
540 int uses_ec,
541 size_t *out_len )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100542{
543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
544 unsigned char *p = buf;
545 size_t ext_len = 0;
546
547 (void) ssl;
548 (void) end;
549 (void) uses_ec;
550 (void) ret;
551 (void) ext_len;
552
553 *out_len = 0;
554
555 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
556 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
557#if defined(MBEDTLS_SSL_RENEGOTIATION)
558 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
559 {
560 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
561 return( ret );
562 }
563 p += ext_len;
564#endif
565
566#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
567 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
568 if( uses_ec )
569 {
570 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
571 &ext_len ) ) != 0 )
572 {
573 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
574 return( ret );
575 }
576 p += ext_len;
577 }
578#endif
579
580#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
581 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
582 {
583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
584 return( ret );
585 }
586 p += ext_len;
587#endif
588
589#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
590 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
591 {
592 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
593 return( ret );
594 }
595 p += ext_len;
596#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
597
598#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
599 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
600 &ext_len ) ) != 0 )
601 {
602 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
603 return( ret );
604 }
605 p += ext_len;
606#endif
607
608#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
609 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
610 {
611 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
612 return( ret );
613 }
614 p += ext_len;
615#endif
616
617#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
618 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
621 return( ret );
622 }
623 p += ext_len;
624#endif
625
626#if defined(MBEDTLS_SSL_DTLS_SRTP)
627 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
628 {
629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
630 return( ret );
631 }
632 p += ext_len;
633#endif
634
635#if defined(MBEDTLS_SSL_SESSION_TICKETS)
636 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
637 {
638 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
639 return( ret );
640 }
641 p += ext_len;
642#endif
643
644 *out_len = p - buf;
645
646 return( 0 );
647}
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200650 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000651 size_t len )
652{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_SSL_RENEGOTIATION)
654 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000655 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100656 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000657 if( len != 1 + ssl->verify_data_len * 2 ||
658 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200659 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100660 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +0200661 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100662 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100665 mbedtls_ssl_send_alert_message(
666 ssl,
667 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
668 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100669 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +0000670 }
671 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100674 {
675 if( len != 1 || buf[0] != 0x00 )
676 {
Ronald Cron5ee57072020-06-11 09:34:06 +0200677 MBEDTLS_SSL_DEBUG_MSG( 1,
678 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100679 mbedtls_ssl_send_alert_message(
680 ssl,
681 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
682 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100683 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100684 }
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100687 }
Paul Bakker48916f92012-09-16 19:57:18 +0000688
689 return( 0 );
690}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
693static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200694 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200695 size_t len )
696{
697 /*
698 * server should use the extension only if we did,
699 * and if so the server's value should match ours (and len is always 1)
700 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200701 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200702 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200703 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200704 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100705 MBEDTLS_SSL_DEBUG_MSG( 1,
706 ( "non-matching max fragment length extension" ) );
707 mbedtls_ssl_send_alert_message(
708 ssl,
709 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100710 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
711 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200712 }
713
714 return( 0 );
715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000717
Hanno Beckera0e20d02019-05-15 14:03:01 +0100718#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +0100719static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
720 const unsigned char *buf,
721 size_t len )
722{
723 size_t peer_cid_len;
724
725 if( /* CID extension only makes sense in DTLS */
726 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
727 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +0100728 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +0100729 {
Hanno Becker22626482019-05-03 12:46:59 +0100730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100732 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100733 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +0100734 }
735
736 if( len == 0 )
737 {
738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100742 }
743
744 peer_cid_len = *buf++;
745 len--;
746
747 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
748 {
Hanno Becker22626482019-05-03 12:46:59 +0100749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100750 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100751 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
752 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +0100753 }
754
755 if( len != peer_cid_len )
756 {
Hanno Becker22626482019-05-03 12:46:59 +0100757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +0100758 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +0100759 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
760 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +0100761 }
762
Hanno Becker5a299902019-05-03 12:47:49 +0100763 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +0100764 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
765 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
766
767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
768 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
769
Hanno Beckera8373a12019-04-26 15:37:26 +0100770 return( 0 );
771}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100772#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +0100773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
775static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100776 const unsigned char *buf,
777 size_t len )
778{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200779 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100780 len != 0 )
781 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100782 MBEDTLS_SSL_DEBUG_MSG( 1,
783 ( "non-matching encrypt-then-MAC extension" ) );
784 mbedtls_ssl_send_alert_message(
785 ssl,
786 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +0100787 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +0100788 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100789 }
790
791 ((void) buf);
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100794
795 return( 0 );
796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
800static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200801 const unsigned char *buf,
802 size_t len )
803{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200804 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200805 len != 0 )
806 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100807 MBEDTLS_SSL_DEBUG_MSG( 1,
808 ( "non-matching extended master secret extension" ) );
809 mbedtls_ssl_send_alert_message(
810 ssl,
811 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100812 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
813 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200814 }
815
816 ((void) buf);
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200819
820 return( 0 );
821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
825static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200826 const unsigned char *buf,
827 size_t len )
828{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200829 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200830 len != 0 )
831 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100832 MBEDTLS_SSL_DEBUG_MSG( 1,
833 ( "non-matching session ticket extension" ) );
834 mbedtls_ssl_send_alert_message(
835 ssl,
836 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100837 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
838 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200839 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200840
841 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200842
843 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200844
845 return( 0 );
846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200848
Robert Cragie136884c2015-10-02 13:34:31 +0100849#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100850 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200852 const unsigned char *buf,
853 size_t len )
854{
855 size_t list_size;
856 const unsigned char *p;
857
Philippe Antoine747fd532018-05-30 09:13:21 +0200858 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200861 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
862 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100863 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200864 }
Philippe Antoine747fd532018-05-30 09:13:21 +0200865 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200866
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200867 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200868 while( list_size > 0 )
869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
871 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200872 {
Neil Armstrong3ea01492022-04-12 14:41:50 +0200873#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
874 ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200875 ssl->handshake->ecdh_ctx.point_format = p[0];
Neil Armstrong3ea01492022-04-12 14:41:50 +0200876#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
877 ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
Robert Cragieae8535d2015-10-06 17:11:18 +0100878#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +0200879 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
880 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +0100881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200883 return( 0 );
884 }
885
886 list_size--;
887 p++;
888 }
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200891 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
892 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100893 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200894}
Darryl Green11999bb2018-03-13 15:22:58 +0000895#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100896 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200897
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200898#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
899static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
900 const unsigned char *buf,
901 size_t len )
902{
Janos Follath865b3eb2019-12-16 11:46:15 +0000903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200904
Hanno Beckere694c3e2017-12-27 21:34:08 +0000905 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200906 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
907 {
908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
909 return( 0 );
910 }
911
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200912 /* If we got here, we no longer need our cached extension */
913 mbedtls_free( ssl->handshake->ecjpake_cache );
914 ssl->handshake->ecjpake_cache = NULL;
915 ssl->handshake->ecjpake_cache_len = 0;
916
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200917 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
918 buf, len ) ) != 0 )
919 {
920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100921 mbedtls_ssl_send_alert_message(
922 ssl,
923 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
924 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +0200925 return( ret );
926 }
927
928 return( 0 );
929}
930#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_ALPN)
933static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200934 const unsigned char *buf, size_t len )
935{
936 size_t list_len, name_len;
937 const char **p;
938
939 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200940 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200941 {
942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100943 mbedtls_ssl_send_alert_message(
944 ssl,
945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +0100946 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
947 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200948 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200949
950 /*
951 * opaque ProtocolName<1..2^8-1>;
952 *
953 * struct {
954 * ProtocolName protocol_name_list<2..2^16-1>
955 * } ProtocolNameList;
956 *
957 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
958 */
959
960 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
961 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200962 {
963 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100965 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200966 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200967
968 list_len = ( buf[0] << 8 ) | buf[1];
969 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200970 {
971 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
972 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100973 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200974 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200975
976 name_len = buf[2];
977 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200978 {
979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100981 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200982 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200983
984 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200985 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200986 {
987 if( name_len == strlen( *p ) &&
988 memcmp( buf + 3, *p, name_len ) == 0 )
989 {
990 ssl->alpn_chosen = *p;
991 return( 0 );
992 }
993 }
994
Gilles Peskine1cc8e342017-05-03 16:28:34 +0200995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +0200996 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
997 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +0100998 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001001
Johan Pascalb62bb512015-12-03 21:56:45 +01001002#if defined(MBEDTLS_SSL_DTLS_SRTP)
1003static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001004 const unsigned char *buf,
1005 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001006{
Johan Pascal43f94902020-09-22 12:25:52 +02001007 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001008 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001009 uint16_t server_protection_profile_value = 0;
1010
1011 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001012 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1013 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1014 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001015 return( 0 );
1016
Ron Eldora9788042018-12-05 11:04:31 +02001017 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001018 * uint8 SRTPProtectionProfile[2];
1019 *
1020 * struct {
1021 * SRTPProtectionProfiles SRTPProtectionProfiles;
1022 * opaque srtp_mki<0..255>;
1023 * } UseSRTPData;
1024
1025 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1026 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001027 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001028 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001029 {
1030 mki_len = ssl->dtls_srtp_info.mki_len;
1031 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001032
Ron Eldoref72faf2018-07-12 11:54:20 +03001033 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001034 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1035 * + protection profile (2 bytes)
1036 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001037 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001038 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001039 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001040 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001041
1042 /*
1043 * get the server protection profile
1044 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001045
1046 /*
1047 * protection profile length must be 0x0002 as we must have only
1048 * one protection profile in server Hello
1049 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001050 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001051 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001052
1053 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001054 server_protection = mbedtls_ssl_check_srtp_profile_value(
1055 server_protection_profile_value );
1056 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001057 {
Johan Pascal43f94902020-09-22 12:25:52 +02001058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1059 mbedtls_ssl_get_srtp_profile_as_string(
1060 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001061 }
1062
Johan Pascal43f94902020-09-22 12:25:52 +02001063 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001064
Johan Pascalb62bb512015-12-03 21:56:45 +01001065 /*
1066 * Check we have the server profile in our list
1067 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001068 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001069 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001070 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1071 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001072 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1074 mbedtls_ssl_get_srtp_profile_as_string(
1075 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001076 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001077 }
1078 }
1079
Ron Eldor591f1622018-01-22 12:30:04 +02001080 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001081 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001082 {
1083 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001084 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001085 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001086 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001087
1088 /* If server does not use mki in its reply, make sure the client won't keep
1089 * one as negotiated */
1090 if( len == 5 )
1091 {
1092 ssl->dtls_srtp_info.mki_len = 0;
1093 }
1094
Ron Eldoref72faf2018-07-12 11:54:20 +03001095 /*
1096 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001097 * If the client detects a nonzero-length MKI in the server's response
1098 * that is different than the one the client offered, then the client
1099 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1100 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001101 if( len > 5 && ( buf[4] != mki_len ||
1102 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001103 {
1104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1105 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001106 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001107 }
Ron Eldorb4655392018-07-05 18:25:39 +03001108#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001109 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001110 {
Ron Eldora9788042018-12-05 11:04:31 +02001111 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1112 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001113 }
1114#endif
Ron Eldora9788042018-12-05 11:04:31 +02001115 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001116}
1117#endif /* MBEDTLS_SSL_DTLS_SRTP */
1118
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001119/*
1120 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_SSL_PROTO_DTLS)
1123static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001126 int major_ver, minor_ver;
1127 unsigned char cookie_len;
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130
Gilles Peskineb64bf062019-09-27 14:02:44 +02001131 /* Check that there is enough room for:
1132 * - 2 bytes of version
1133 * - 1 byte of cookie_len
1134 */
1135 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1136 {
1137 MBEDTLS_SSL_DEBUG_MSG( 1,
1138 ( "incoming HelloVerifyRequest message is too short" ) );
1139 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1140 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001141 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001142 }
1143
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001144 /*
1145 * struct {
1146 * ProtocolVersion server_version;
1147 * opaque cookie<0..2^8-1>;
1148 * } HelloVerifyRequest;
1149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001151 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001152 p += 2;
1153
TRodziewicz2d8800e2021-05-13 19:14:19 +02001154 /*
1155 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1156 * even is lower than our min version.
1157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
TRodziewiczb5850c52021-05-13 17:11:23 +02001159 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001160 major_ver > ssl->conf->max_major_ver ||
1161 minor_ver > ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1166 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001167
Hanno Beckerbc000442021-06-24 09:18:19 +01001168 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001169 }
1170
1171 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001172 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1173 {
1174 MBEDTLS_SSL_DEBUG_MSG( 1,
1175 ( "cookie length does not match incoming message size" ) );
1176 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1177 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001178 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001179 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001180 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001181
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001182 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001183
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001184 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1185 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001186 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001188 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001189 }
1190
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001191 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001192 ssl->handshake->verify_cookie_len = cookie_len;
1193
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001194 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1196 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001201
1202 return( 0 );
1203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001207{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001208 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001209 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001210 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001211 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001212 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001214 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001215#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001216 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
Hanno Becker327c93b2018-08-15 13:56:18 +01001221 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001223 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 return( ret );
1226 }
1227
Hanno Becker79594fd2019-05-08 09:38:41 +01001228 buf = ssl->in_msg;
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_RENEGOTIATION)
1233 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001234 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001235 ssl->renego_records_seen++;
1236
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001237 if( ssl->conf->renego_max_records >= 0 &&
1238 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001239 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001240 MBEDTLS_SSL_DEBUG_MSG( 1,
1241 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001243 }
1244
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001245 MBEDTLS_SSL_DEBUG_MSG( 1,
1246 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001247
1248 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001254 mbedtls_ssl_send_alert_message(
1255 ssl,
1256 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1257 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 }
1260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001268 return( ssl_parse_hello_verify_request( ssl ) );
1269 }
1270 else
1271 {
1272 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001273 mbedtls_free( ssl->handshake->cookie );
1274 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001275 ssl->handshake->verify_cookie_len = 0;
1276 }
1277 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1281 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001284 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1285 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001286 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 }
1288
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001289 /*
1290 * 0 . 1 server_version
1291 * 2 . 33 random (maybe including 4 bytes of Unix time)
1292 * 34 . 34 session_id length = n
1293 * 35 . 34+n session_id
1294 * 35+n . 36+n cipher_suite
1295 * 37+n . 37+n compression_method
1296 *
1297 * 38+n . 39+n extensions length (optional)
1298 * 40+n . .. extensions
1299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
1303 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001304 ssl->conf->transport, buf + 0 );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001305 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001307 if( ssl->major_ver < ssl->conf->min_major_ver ||
1308 ssl->minor_ver < ssl->conf->min_minor_ver ||
1309 ssl->major_ver > ssl->conf->max_major_ver ||
1310 ssl->minor_ver > ssl->conf->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001311 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001312 MBEDTLS_SSL_DEBUG_MSG( 1,
1313 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1314 ssl->conf->min_major_ver,
1315 ssl->conf->min_minor_ver,
1316 ssl->major_ver, ssl->minor_ver,
1317 ssl->conf->max_major_ver,
1318 ssl->conf->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1321 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001322
Hanno Beckerbc000442021-06-24 09:18:19 +01001323 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001324 }
1325
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001327 ( (unsigned long) buf[2] << 24 ) |
1328 ( (unsigned long) buf[3] << 16 ) |
1329 ( (unsigned long) buf[4] << 8 ) |
1330 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001332 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001334 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Paul Bakker48916f92012-09-16 19:57:18 +00001338 if( n > 32 )
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001341 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1342 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001343 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001344 }
1345
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001346 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001348 ext_len = ( ( buf[38 + n] << 8 )
1349 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001355 mbedtls_ssl_send_alert_message(
1356 ssl,
1357 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1358 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001359 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001362 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001363 {
1364 ext_len = 0;
1365 }
1366 else
1367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001369 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1370 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001371 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001374 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001375 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001376
1377 /*
1378 * Read and check compression
1379 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001380 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001382 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001383 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001384 MBEDTLS_SSL_DEBUG_MSG( 1,
1385 ( "server hello, bad compression: %d", comp ) );
1386 mbedtls_ssl_send_alert_message(
1387 ssl,
1388 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1389 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001391 }
1392
Paul Bakker380da532012-04-18 16:10:25 +00001393 /*
1394 * Initialize update checksum functions
1395 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001396 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1397 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001398 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001399 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001400 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001401 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1402 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001404 }
Paul Bakker380da532012-04-18 16:10:25 +00001405
Hanno Beckere694c3e2017-12-27 21:34:08 +00001406 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001407
Paul Elliottd48d5c62021-01-07 14:47:05 +00001408 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
1411 /*
1412 * Check if the session can be resumed
1413 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001414 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_SSL_RENEGOTIATION)
1416 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001417#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001418 ssl->session_negotiate->ciphersuite != i ||
1419 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001420 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001421 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 {
1423 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001424 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001426 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001427#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001428 ssl->session_negotiate->ciphersuite = i;
1429 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001430 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001431 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 }
1433 else
1434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001440 mbedtls_ssl_send_alert_message(
1441 ssl,
1442 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1443 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001444 return( ret );
1445 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001449 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Paul Elliott3891caf2020-12-17 18:42:40 +00001451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1453 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Andrzej Kurek03bac442018-04-25 05:06:07 -04001455 /*
1456 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001457 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 i = 0;
1459 while( 1 )
1460 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001461 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001464 mbedtls_ssl_send_alert_message(
1465 ssl,
1466 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1467 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001468 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 }
1470
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001471 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001472 ssl->session_negotiate->ciphersuite )
1473 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 }
1477
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001478 suite_info = mbedtls_ssl_ciphersuite_from_id(
1479 ssl->session_negotiate->ciphersuite );
Ronald Cron8fdad9e2022-03-30 22:04:30 +02001480 if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->minor_ver,
1481 ssl->minor_ver ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001482 {
1483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001484 mbedtls_ssl_send_alert_message(
1485 ssl,
1486 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001487 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1488 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001489 }
1490
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001491 MBEDTLS_SSL_DEBUG_MSG( 3,
1492 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001493
Gilles Peskineeccd8882020-03-10 12:19:08 +01001494#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001495 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1496 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1497 {
1498 ssl->handshake->ecrs_enabled = 1;
1499 }
1500#endif
1501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00001503 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001506 mbedtls_ssl_send_alert_message(
1507 ssl,
1508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1509 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001510 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 }
Paul Bakker48916f92012-09-16 19:57:18 +00001512 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001514 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001515
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001516 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001517 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001518
Paul Bakker48916f92012-09-16 19:57:18 +00001519 while( ext_len )
1520 {
1521 unsigned int ext_id = ( ( ext[0] << 8 )
1522 | ( ext[1] ) );
1523 unsigned int ext_size = ( ( ext[2] << 8 )
1524 | ( ext[3] ) );
1525
1526 if( ext_size + 4 > ext_len )
1527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001529 mbedtls_ssl_send_alert_message(
1530 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1531 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001532 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001533 }
1534
1535 switch( ext_id )
1536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1539#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001540 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001541#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001542
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001543 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1544 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001545 return( ret );
1546
1547 break;
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1550 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001551 MBEDTLS_SSL_DEBUG_MSG( 3,
1552 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001553
1554 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1555 ext + 4, ext_size ) ) != 0 )
1556 {
1557 return( ret );
1558 }
1559
1560 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001562
Hanno Beckera0e20d02019-05-15 14:03:01 +01001563#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001564 case MBEDTLS_TLS_EXT_CID:
1565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1566
1567 if( ( ret = ssl_parse_cid_ext( ssl,
1568 ext + 4,
1569 ext_size ) ) != 0 )
1570 {
1571 return( ret );
1572 }
1573
1574 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001575#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1578 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001580
1581 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1582 ext + 4, ext_size ) ) != 0 )
1583 {
1584 return( ret );
1585 }
1586
1587 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1591 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001592 MBEDTLS_SSL_DEBUG_MSG( 3,
1593 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001594
1595 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1596 ext + 4, ext_size ) ) != 0 )
1597 {
1598 return( ret );
1599 }
1600
1601 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1605 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001607
1608 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1609 ext + 4, ext_size ) ) != 0 )
1610 {
1611 return( ret );
1612 }
1613
1614 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001616
Robert Cragie136884c2015-10-02 13:34:31 +01001617#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001618 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001620 MBEDTLS_SSL_DEBUG_MSG( 3,
1621 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001622
1623 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1624 ext + 4, ext_size ) ) != 0 )
1625 {
1626 return( ret );
1627 }
1628
1629 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01001630#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1631 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001632
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001633#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1634 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1636
1637 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1638 ext + 4, ext_size ) ) != 0 )
1639 {
1640 return( ret );
1641 }
1642
1643 break;
1644#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#if defined(MBEDTLS_SSL_ALPN)
1647 case MBEDTLS_TLS_EXT_ALPN:
1648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001649
Johan Pascal275874b2020-10-27 10:43:53 +01001650 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1651 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001652
1653 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001655
Johan Pascalb62bb512015-12-03 21:56:45 +01001656#if defined(MBEDTLS_SSL_DTLS_SRTP)
1657 case MBEDTLS_TLS_EXT_USE_SRTP:
1658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
1659
Johan Pascalc3ccd982020-10-28 17:18:18 +01001660 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
1661 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01001662
1663 break;
1664#endif /* MBEDTLS_SSL_DTLS_SRTP */
1665
Paul Bakker48916f92012-09-16 19:57:18 +00001666 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001667 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00001668 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001669 }
1670
1671 ext_len -= 4 + ext_size;
1672 ext += 4 + ext_size;
1673
1674 if( ext_len > 0 && ext_len < 4 )
1675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001677 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001678 }
1679 }
1680
1681 /*
1682 * Renegotiation security checks
1683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001685 ssl->conf->allow_legacy_renegotiation ==
1686 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001687 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001688 MBEDTLS_SSL_DEBUG_MSG( 1,
1689 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001690 handshake_failure = 1;
1691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_SSL_RENEGOTIATION)
1693 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1694 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00001695 renegotiation_info_seen == 0 )
1696 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001697 MBEDTLS_SSL_DEBUG_MSG( 1,
1698 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001699 handshake_failure = 1;
1700 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1702 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001703 ssl->conf->allow_legacy_renegotiation ==
1704 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001707 handshake_failure = 1;
1708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1710 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001711 renegotiation_info_seen == 1 )
1712 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001713 MBEDTLS_SSL_DEBUG_MSG( 1,
1714 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001715 handshake_failure = 1;
1716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001718
1719 if( handshake_failure == 1 )
1720 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001721 mbedtls_ssl_send_alert_message(
1722 ssl,
1723 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1724 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001725 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
1730 return( 0 );
1731}
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1734 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001735static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
1736 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02001737 unsigned char *end )
1738{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001740 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02001741
Paul Bakker29e1f122013-04-16 13:07:56 +02001742 /*
1743 * Ephemeral DH parameters:
1744 *
1745 * struct {
1746 * opaque dh_p<1..2^16-1>;
1747 * opaque dh_g<1..2^16-1>;
1748 * opaque dh_Ys<1..2^16-1>;
1749 * } ServerDHParams;
1750 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001751 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
1752 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001755 return( ret );
1756 }
1757
Gilles Peskine487bbf62021-05-27 22:17:07 +02001758 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001759 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02001760 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01001762 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001763 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001764 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001765 }
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1768 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1769 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001770
1771 return( ret );
1772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1774 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001775
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001776#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1777 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1778 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1779#if defined(MBEDTLS_USE_PSA_CRYPTO)
1780static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Hanno Beckerbb89e272019-01-08 12:54:37 +00001781 unsigned char **p,
1782 unsigned char *end )
1783{
1784 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01001785 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001786 uint8_t ecpoint_len;
1787 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1788
1789 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001790 * struct {
1791 * ECParameters curve_params;
1792 * ECPoint public;
1793 * } ServerECDHParams;
1794 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01001795 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01001796 * 2..3 NamedCurve
1797 * 4 ECPoint.len
1798 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00001799 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001800 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001801 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001802
1803 /* First byte is curve_type; only named_curve is handled */
1804 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01001805 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001806
1807 /* Next two bytes are the namedcurve value */
1808 tls_id = *(*p)++;
1809 tls_id <<= 8;
1810 tls_id |= *(*p)++;
1811
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001812 /* Check it's a curve we offered */
1813 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001814 {
1815 MBEDTLS_SSL_DEBUG_MSG( 2,
1816 ( "bad server key exchange message (ECDHE curve): %u",
1817 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001818 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01001819 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01001820
Hanno Beckerbb89e272019-01-08 12:54:37 +00001821 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01001822 if( ( handshake->ecdh_psa_type =
1823 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00001824 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01001825 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001826 }
Neil Armstrong91477a72022-03-25 15:42:20 +01001827 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001828
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001829 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00001830 ecpoint_len = *(*p)++;
1831 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001832 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001833
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001834 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
1835 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00001836
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001837 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
1838 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00001839 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01001840
Hanno Beckerbb89e272019-01-08 12:54:37 +00001841 return( 0 );
1842}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001843#else
Neil Armstrong1f198d82022-04-13 15:02:30 +02001844static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1845{
1846 const mbedtls_ecp_curve_info *curve_info;
1847 mbedtls_ecp_group_id grp_id;
1848#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1849 grp_id = ssl->handshake->ecdh_ctx.grp.id;
1850#else
1851 grp_id = ssl->handshake->ecdh_ctx.grp_id;
1852#endif
1853
1854 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
1855 if( curve_info == NULL )
1856 {
1857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1859 }
1860
1861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1862
1863 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
1864 return( -1 );
1865
1866 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1867 MBEDTLS_DEBUG_ECDH_QP );
1868
1869 return( 0 );
1870}
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02001873 unsigned char **p,
1874 unsigned char *end )
1875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02001877
Paul Bakker29e1f122013-04-16 13:07:56 +02001878 /*
1879 * Ephemeral ECDH parameters:
1880 *
1881 * struct {
1882 * ECParameters curve_params;
1883 * ECPoint public;
1884 * } ServerECDHParams;
1885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02001887 (const unsigned char **) p, end ) ) != 0 )
1888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01001890#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02001891 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1892 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02001893#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02001894 return( ret );
1895 }
1896
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001897 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001898 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001899 MBEDTLS_SSL_DEBUG_MSG( 1,
1900 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01001901 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001902 }
1903
Paul Bakker29e1f122013-04-16 13:07:56 +02001904 return( ret );
1905}
Neil Armstrongd8419ff2022-04-12 14:39:12 +02001906#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1908 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1909 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001910
Gilles Peskineeccd8882020-03-10 12:19:08 +01001911#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001913 unsigned char **p,
1914 unsigned char *end )
1915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03001917 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001918 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001919
1920 /*
1921 * PSK parameters:
1922 *
1923 * opaque psk_identity_hint<0..2^16-1>;
1924 */
Hanno Becker0c161d12018-10-08 13:40:50 +01001925 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001926 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001927 MBEDTLS_SSL_DEBUG_MSG( 1,
1928 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001929 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01001930 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001931 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001932 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001933
irwir6527bd62019-09-21 18:51:25 +03001934 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001935 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001936 MBEDTLS_SSL_DEBUG_MSG( 1,
1937 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01001938 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001939 }
1940
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01001941 /*
1942 * Note: we currently ignore the PKS identity hint, as we only allow one
1943 * PSK to be provisionned on the client. This could be changed later if
1944 * someone needs that feature.
1945 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001946 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001947 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001948
1949 return( ret );
1950}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001951#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
1954 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001955/*
1956 * Generate a pre-master secret and encrypt it with the server's RSA key
1957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001959 size_t offset, size_t *olen,
1960 size_t pms_offset )
1961{
Janos Follath865b3eb2019-12-16 11:46:15 +00001962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001963 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001964 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001965 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001966
Angus Grattond8213d02016-05-25 20:56:48 +10001967 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001968 {
1969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1970 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1971 }
1972
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001973 /*
1974 * Generate (part of) the pre-master as
1975 * struct {
1976 * ProtocolVersion client_version;
1977 * opaque random[46];
1978 * } PreMasterSecret;
1979 */
Ronald Cron4e263fd2022-03-15 15:54:17 +01001980 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
1981 MBEDTLS_SSL_MINOR_VERSION_3,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001982 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001983
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001984 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001987 return( ret );
1988 }
1989
1990 ssl->handshake->pmslen = 48;
1991
Hanno Beckerc7d7e292019-02-06 16:49:54 +00001992#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1993 peer_pk = &ssl->handshake->peer_pubkey;
1994#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02001995 if( ssl->session_negotiate->peer_cert == NULL )
1996 {
Hanno Becker8273df82019-02-06 17:37:32 +00001997 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00001998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00001999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002000 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002001 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2002#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002003
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002004 /*
2005 * Now write it out, encrypted
2006 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002007 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2010 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002011 }
2012
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002013 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002014 p, ssl->handshake->pmslen,
2015 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002016 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002017 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002020 return( ret );
2021 }
2022
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002023 if( len_bytes == 2 )
2024 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002025 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002026 *olen += 2;
2027 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028
Hanno Beckerae553dd2019-02-08 14:06:00 +00002029#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2030 /* We don't need the peer's public key anymore. Free it. */
2031 mbedtls_pk_free( peer_pk );
2032#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002033 return( 0 );
2034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2036 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002037
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002038#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2039 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2040 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002042 unsigned char **p,
2043 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 mbedtls_md_type_t *md_alg,
2045 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 *md_alg = MBEDTLS_MD_NONE;
2048 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002049
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002050 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002051 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002052
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002053 /*
2054 * Get hash algorithm
2055 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002056 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2057 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002058 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002059 MBEDTLS_SSL_DEBUG_MSG( 1,
2060 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002061 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002062 }
2063
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002064 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002065 * Get signature algorithm
2066 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002067 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2068 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002069 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002070 MBEDTLS_SSL_DEBUG_MSG( 1,
2071 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002072 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002073 }
2074
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002075 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002076 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002077 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002078 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002079 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002080 MBEDTLS_SSL_DEBUG_MSG( 1,
2081 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002082 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002083 }
2084
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2086 (*p)[1] ) );
2087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2088 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002089 *p += 2;
2090
2091 return( 0 );
2092}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002093#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2094 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2095 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2098 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2099static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002100{
Janos Follath865b3eb2019-12-16 11:46:15 +00002101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002103 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002104
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002105#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2106 peer_pk = &ssl->handshake->peer_pubkey;
2107#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002108 if( ssl->session_negotiate->peer_cert == NULL )
2109 {
Hanno Becker8273df82019-02-06 17:37:32 +00002110 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002113 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002114 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2115#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002116
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002117 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2120 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002121 }
2122
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002123 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002124
Przemek Stekielea4000f2022-03-16 09:49:33 +01002125#if defined(MBEDTLS_USE_PSA_CRYPTO)
2126 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002127 size_t olen = 0;
2128
2129 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2130 {
2131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2132 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2133 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002134
2135 ssl->handshake->ecdh_psa_type =
2136 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2137 &ecdh_bits ) );
2138
2139 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2140 {
2141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2142 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2143 }
2144
2145 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2146
Przemek Stekielea4000f2022-03-16 09:49:33 +01002147 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002148 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2149 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2150 ssl->handshake->ecdh_psa_peerkey,
2151 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002152
Przemek Stekiel561a4232022-03-16 13:16:24 +01002153 if ( ret != 0 )
2154 {
2155 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2156 return( ret );
2157 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002158
Przemek Stekiel561a4232022-03-16 13:16:24 +01002159 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002160#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2162 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002165 return( ret );
2166 }
2167
2168 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002171 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002172 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002173#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002174#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2175 /* We don't need the peer's public key anymore. Free it,
2176 * so that more RAM is available for upcoming expensive
2177 * operations like ECDHE. */
2178 mbedtls_pk_free( peer_pk );
2179#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2180
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002181 return( ret );
2182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2184 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002187{
Janos Follath865b3eb2019-12-16 11:46:15 +00002188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002189 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002190 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002191 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2196 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 ssl->state++;
2200 return( 0 );
2201 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002202 ((void) p);
2203 ((void) end);
2204#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2207 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2208 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2209 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002210 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002211 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002214 mbedtls_ssl_send_alert_message(
2215 ssl,
2216 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2217 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002218 return( ret );
2219 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002222 ssl->state++;
2223 return( 0 );
2224 }
2225 ((void) p);
2226 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2228 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002229
Gilles Peskineeccd8882020-03-10 12:19:08 +01002230#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002231 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002232 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002233 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002234 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002235 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002236#endif
2237
Hanno Becker327c93b2018-08-15 13:56:18 +01002238 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 return( ret );
2242 }
2243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002247 mbedtls_ssl_send_alert_message(
2248 ssl,
2249 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2250 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002254 /*
2255 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2256 * doesn't use a psk_identity_hint
2257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2261 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002262 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002263 /* Current message is probably either
2264 * CertificateRequest or ServerHelloDone */
2265 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002266 goto exit;
2267 }
2268
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002269 MBEDTLS_SSL_DEBUG_MSG( 1,
2270 ( "server key exchange message must not be skipped" ) );
2271 mbedtls_ssl_send_alert_message(
2272 ssl,
2273 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2274 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 }
2278
Gilles Peskineeccd8882020-03-10 12:19:08 +01002279#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002280 if( ssl->handshake->ecrs_enabled )
2281 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2282
2283start_processing:
2284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002286 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002288
Gilles Peskineeccd8882020-03-10 12:19:08 +01002289#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2291 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2292 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2293 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002294 {
2295 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002298 mbedtls_ssl_send_alert_message(
2299 ssl,
2300 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002301 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002302 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002303 }
2304 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002305#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2308 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2309 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2310 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002311 ; /* nothing more to do */
2312 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2314 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2315#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2316 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2317 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2318 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002320 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002323 mbedtls_ssl_send_alert_message(
2324 ssl,
2325 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2326 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002327 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002328 }
2329 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002330 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2332 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002333#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2334 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2336 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2337 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2338 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002339 {
2340 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002343 mbedtls_ssl_send_alert_message(
2344 ssl,
2345 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2346 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002347 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002348 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002349 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2352 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2353 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002354#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2355 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2356 {
2357 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2358 p, end - p );
2359 if( ret != 0 )
2360 {
2361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002362 mbedtls_ssl_send_alert_message(
2363 ssl,
2364 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002365 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2366 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002367 }
2368 }
2369 else
2370#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002374 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002375
Gilles Peskineeccd8882020-03-10 12:19:08 +01002376#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002377 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002378 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002379 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002380#if defined(MBEDTLS_USE_PSA_CRYPTO)
2381 unsigned char hash[PSA_HASH_MAX_SIZE];
2382#else
2383 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2386 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2387 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002388 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002389 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002390
Hanno Beckera6899bb2019-02-06 18:26:03 +00002391 mbedtls_pk_context * peer_pk;
2392
Paul Bakker29e1f122013-04-16 13:07:56 +02002393 /*
2394 * Handle the digitally-signed structure
2395 */
Ronald Cron90915f22022-03-07 11:11:36 +01002396 if( ssl_parse_signature_algorithm( ssl, &p, end,
2397 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002398 {
Ronald Cron90915f22022-03-07 11:11:36 +01002399 MBEDTLS_SSL_DEBUG_MSG( 1,
2400 ( "bad server key exchange message" ) );
2401 mbedtls_ssl_send_alert_message(
2402 ssl,
2403 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2404 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2405 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002406 }
Ronald Cron90915f22022-03-07 11:11:36 +01002407
2408 if( pk_alg !=
2409 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002410 {
Ronald Cron90915f22022-03-07 11:11:36 +01002411 MBEDTLS_SSL_DEBUG_MSG( 1,
2412 ( "bad server key exchange message" ) );
2413 mbedtls_ssl_send_alert_message(
2414 ssl,
2415 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2416 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2417 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002418 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002419
2420 /*
2421 * Read signature
2422 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002423
2424 if( p > end - 2 )
2425 {
2426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002427 mbedtls_ssl_send_alert_message(
2428 ssl,
2429 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2430 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002431 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002432 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002433 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002434 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002435
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002436 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002439 mbedtls_ssl_send_alert_message(
2440 ssl,
2441 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2442 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002443 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002447
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002448 /*
2449 * Compute the hash that has been signed
2450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002452 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002453 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2454 params, params_len,
2455 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002456 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002457 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002458 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002459 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002463 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002464
Gilles Peskineca1d7422018-04-24 11:53:22 +02002465 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002466
Hanno Beckera6899bb2019-02-06 18:26:03 +00002467#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2468 peer_pk = &ssl->handshake->peer_pubkey;
2469#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002470 if( ssl->session_negotiate->peer_cert == NULL )
2471 {
Hanno Becker8273df82019-02-06 17:37:32 +00002472 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002475 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00002476 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2477#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002478
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002479 /*
2480 * Verify signature
2481 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00002482 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002485 mbedtls_ssl_send_alert_message(
2486 ssl,
2487 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2488 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002490 }
2491
Gilles Peskineeccd8882020-03-10 12:19:08 +01002492#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002493 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002494 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002495#endif
2496
Hanno Beckera6899bb2019-02-06 18:26:03 +00002497 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002498 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002499 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01002500#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002501 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2502#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002503 mbedtls_ssl_send_alert_message(
2504 ssl,
2505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2506 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002508#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002509 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2510 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2511#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02002512 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002513 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00002514
2515#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2516 /* We don't need the peer's public key anymore. Free it,
2517 * so that more RAM is available for upcoming expensive
2518 * operations like ECDHE. */
2519 mbedtls_pk_free( peer_pk );
2520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01002522#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002524exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 ssl->state++;
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
2529 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530}
2531
Gilles Peskineeccd8882020-03-10 12:19:08 +01002532#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002534{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002535 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002536 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002539
Hanno Becker1aa267c2017-04-28 17:08:27 +01002540 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002543 ssl->state++;
2544 return( 0 );
2545 }
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002549}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002550#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002552{
Janos Follath865b3eb2019-12-16 11:46:15 +00002553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002554 unsigned char *buf;
2555 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002556 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002557 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002558 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01002559 size_t sig_alg_len;
2560#if defined(MBEDTLS_DEBUG_C)
2561 unsigned char *sig_alg;
2562#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Hanno Becker1aa267c2017-04-28 17:08:27 +01002566 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002569 ssl->state++;
2570 return( 0 );
2571 }
2572
Hanno Becker327c93b2018-08-15 13:56:18 +01002573 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002579 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2580 {
2581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002582 mbedtls_ssl_send_alert_message(
2583 ssl,
2584 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2585 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002586 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002589 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08002590 ssl->handshake->client_auth =
2591 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08002594 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Jerry Yufb28b882022-01-28 11:05:58 +08002596 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002597 {
Johan Pascala89ca862020-08-25 10:03:19 +02002598 /* Current message is probably the ServerHelloDone */
2599 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01002600 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002601 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002602
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002603 /*
2604 * struct {
2605 * ClientCertificateType certificate_types<1..2^8-1>;
2606 * SignatureAndHashAlgorithm
2607 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2608 * DistinguishedName certificate_authorities<0..2^16-1>;
2609 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002610 *
2611 * Since we only support a single certificate on clients, let's just
2612 * ignore all the information that's supposed to help us pick a
2613 * certificate.
2614 *
2615 * We could check that our certificate matches the request, and bail out
2616 * if it doesn't, but it's simpler to just send the certificate anyway,
2617 * and give the server the opportunity to decide if it should terminate
2618 * the connection when it doesn't like our certificate.
2619 *
2620 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2621 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00002622 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002623 *
2624 * However, we still minimally parse the message to check it is at least
2625 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002626 */
Paul Bakker926af752012-11-23 13:38:07 +01002627 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002628
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002629 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002630 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
2631 {
2632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2633 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2634 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002635 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02002636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002638 n = cert_type_len;
2639
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002640 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02002641 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01002642 * * the length of the signature algorithms field (if minor version of
2643 * SSL is 3),
2644 * * distinguished name length otherwise.
2645 * Both reach at most the index:
2646 * ...hdr_len + 2 + n,
2647 * therefore the buffer length at this point must be greater than that
2648 * regardless of the actual code path.
2649 */
2650 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002653 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2654 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002655 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002656 }
2657
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002658 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01002659 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2660 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2661
2662 /*
2663 * The furthest access in buf is in the loop few lines below:
2664 * sig_alg[i + 1],
2665 * where:
2666 * sig_alg = buf + ...hdr_len + 3 + n,
2667 * max(i) = sig_alg_len - 1.
2668 * Therefore the furthest access is:
2669 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
2670 * which reduces to:
2671 * buf[...hdr_len + 3 + n + sig_alg_len],
2672 * which is one less than we need the buf to be.
2673 */
2674 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01002675 {
Ronald Cron90915f22022-03-07 11:11:36 +01002676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2677 mbedtls_ssl_send_alert_message(
2678 ssl,
2679 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2680 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2681 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02002682 }
Paul Bakker926af752012-11-23 13:38:07 +01002683
Ronald Cron90915f22022-03-07 11:11:36 +01002684#if defined(MBEDTLS_DEBUG_C)
2685 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2686 for( size_t i = 0; i < sig_alg_len; i += 2 )
2687 {
2688 MBEDTLS_SSL_DEBUG_MSG( 3,
2689 ( "Supported Signature Algorithm found: %d,%d",
2690 sig_alg[i], sig_alg[i + 1] ) );
2691 }
2692#endif
2693
2694 n += 2 + sig_alg_len;
2695
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002696 /* certificate_authorities */
2697 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2698 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002699
2700 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00002701 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2705 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01002706 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01002707 }
2708
2709exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
2712 return( 0 );
2713}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002714#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002717{
Janos Follath865b3eb2019-12-16 11:46:15 +00002718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
Hanno Becker327c93b2018-08-15 13:56:18 +01002722 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2725 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002727
2728 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2729 {
2730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2731 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2732 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2735 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002738 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2739 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01002740 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
2742
2743 ssl->state++;
2744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002746 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002748#endif
2749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
2752 return( 0 );
2753}
2754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002756{
Janos Follath865b3eb2019-12-16 11:46:15 +00002757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002758
2759 size_t header_len;
2760 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002761 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002762 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2767 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 /*
2770 * DHM key exchange -- send G^X mod P
2771 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02002772 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Joe Subbiani6dd73642021-07-19 11:56:54 +01002774 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002775 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02002778 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002779 &ssl->out_msg[header_len], content_len,
2780 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 if( ret != 0 )
2782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 return( ret );
2785 }
2786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2788 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002791 ssl->handshake->premaster,
2792 MBEDTLS_PREMASTER_SIZE,
2793 &ssl->handshake->pmslen,
2794 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 return( ret );
2798 }
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 }
2802 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002804#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2805 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2806 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2807 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2808#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker4a63ed42019-01-08 11:39:35 +00002809 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01002810 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2811 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2812 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00002813 {
Andrzej Kureka0237f82022-02-24 13:24:52 -05002814 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2815 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01002816 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002817
2818 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2819
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002820 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002821
Hanno Becker0a94a642019-01-11 14:35:30 +00002822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
2823
Hanno Becker4a63ed42019-01-08 11:39:35 +00002824 /*
2825 * Generate EC private key for ECDHE exchange.
2826 */
2827
Hanno Becker4a63ed42019-01-08 11:39:35 +00002828 /* The master secret is obtained from the shared ECDH secret by
2829 * applying the TLS 1.2 PRF with a specific salt and label. While
2830 * the PSA Crypto API encourages combining key agreement schemes
2831 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
2832 * yet support the provisioning of salt + label to the KDF.
2833 * For the time being, we therefore need to split the computation
2834 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002835 key_attributes = psa_key_attributes_init();
2836 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01002837 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01002838 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
2839 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002840
2841 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01002842 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01002843 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002844 if( status != PSA_SUCCESS )
2845 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2846
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002847 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01002848 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002849 * but we just need to add a length byte before that. */
2850 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
2851 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2852 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
2853 size_t own_pubkey_len;
2854
Hanno Becker4a63ed42019-01-08 11:39:35 +00002855 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002856 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00002857 &own_pubkey_len );
2858 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05002859 {
2860 psa_destroy_key( handshake->ecdh_psa_privkey );
2861 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002862 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05002863 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00002864
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01002865 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
2866 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00002867
Hanno Becker4a63ed42019-01-08 11:39:35 +00002868 /* The ECDH secret is the premaster secret used for key derivation. */
2869
Janos Follathdf3b0892019-08-08 11:12:24 +01002870 /* Compute ECDH shared secret. */
2871 status = psa_raw_key_agreement( PSA_ALG_ECDH,
2872 handshake->ecdh_psa_privkey,
2873 handshake->ecdh_psa_peerkey,
2874 handshake->ecdh_psa_peerkey_len,
2875 ssl->handshake->premaster,
2876 sizeof( ssl->handshake->premaster ),
2877 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002878
Andrzej Kureka0237f82022-02-24 13:24:52 -05002879 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002880 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05002881
2882 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
2883 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker4a63ed42019-01-08 11:39:35 +00002884 }
2885 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2888 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2889 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2890 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002891 {
2892 /*
2893 * ECDH key exchange -- send client public value
2894 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002895 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01002896
Gilles Peskineeccd8882020-03-10 12:19:08 +01002897#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002898 if( ssl->handshake->ecrs_enabled )
2899 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002900 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002901 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02002902
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002903 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
2904 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002905#endif
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002908 &content_len,
2909 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002910 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01002911 if( ret != 0 )
2912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002914#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002915 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2916 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2917#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002918 return( ret );
2919 }
2920
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002921 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2922 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01002923
Gilles Peskineeccd8882020-03-10 12:19:08 +01002924#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002925 if( ssl->handshake->ecrs_enabled )
2926 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002927 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02002928 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002929 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002930
2931ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002932 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00002933 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02002934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002936 &ssl->handshake->pmslen,
2937 ssl->handshake->premaster,
2938 MBEDTLS_MPI_MAX_SIZE,
2939 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002942#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002943 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2944 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2945#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002946 return( ret );
2947 }
2948
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002949 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2950 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01002951 }
2952 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02002953#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2955 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2956 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2957 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Neil Armstrong868af822022-03-09 10:26:25 +01002958#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2959 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2960 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2961 {
2962 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2963 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
2964 psa_key_attributes_t key_attributes;
2965
2966 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2967
2968 /*
2969 * opaque psk_identity<0..2^16-1>;
2970 */
2971 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Neil Armstrong868af822022-03-09 10:26:25 +01002972 /* We don't offer PSK suites if we don't have a PSK,
2973 * and we check that the server's choice is among the
2974 * ciphersuites we offered, so this should never happen. */
2975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01002976
2977 /* Opaque PSKs are currently only supported for PSK-only suites. */
2978 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
2979 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2980
Neil Armstrongfc834f22022-03-23 17:54:38 +01002981 /* uint16 to store content length */
2982 const size_t content_len_size = 2;
2983
Neil Armstrong868af822022-03-09 10:26:25 +01002984 header_len = 4;
Neil Armstrong868af822022-03-09 10:26:25 +01002985
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002986 if( header_len + content_len_size + ssl->conf->psk_identity_len
Neil Armstrongfc834f22022-03-23 17:54:38 +01002987 > MBEDTLS_SSL_OUT_CONTENT_LEN )
Neil Armstrong868af822022-03-09 10:26:25 +01002988 {
2989 MBEDTLS_SSL_DEBUG_MSG( 1,
2990 ( "psk identity too long or SSL buffer too short" ) );
2991 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2992 }
2993
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002994 unsigned char *p = ssl->out_msg + header_len;
Neil Armstrong868af822022-03-09 10:26:25 +01002995
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02002996 *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len );
2997 *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len );
2998 header_len += content_len_size;
2999
3000 memcpy( p, ssl->conf->psk_identity,
Neil Armstrong868af822022-03-09 10:26:25 +01003001 ssl->conf->psk_identity_len );
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003002 p += ssl->conf->psk_identity_len;
3003
Neil Armstrong868af822022-03-09 10:26:25 +01003004 header_len += ssl->conf->psk_identity_len;
3005
3006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3007
3008 /*
3009 * Generate EC private key for ECDHE exchange.
3010 */
3011
3012 /* The master secret is obtained from the shared ECDH secret by
3013 * applying the TLS 1.2 PRF with a specific salt and label. While
3014 * the PSA Crypto API encourages combining key agreement schemes
3015 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3016 * yet support the provisioning of salt + label to the KDF.
3017 * For the time being, we therefore need to split the computation
3018 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3019 key_attributes = psa_key_attributes_init();
3020 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3021 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3022 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3023 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3024
3025 /* Generate ECDH private key. */
3026 status = psa_generate_key( &key_attributes,
3027 &handshake->ecdh_psa_privkey );
3028 if( status != PSA_SUCCESS )
Neil Armstrongc530aa62022-03-23 17:45:01 +01003029 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003030
3031 /* Export the public part of the ECDH private key from PSA.
3032 * The export format is an ECPoint structure as expected by TLS,
3033 * but we just need to add a length byte before that. */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003034 unsigned char *own_pubkey = p + 1;
Neil Armstrong868af822022-03-09 10:26:25 +01003035 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3036 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003037 size_t own_pubkey_len = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003038
3039 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3040 own_pubkey, own_pubkey_max_len,
3041 &own_pubkey_len );
3042 if( status != PSA_SUCCESS )
3043 {
3044 psa_destroy_key( handshake->ecdh_psa_privkey );
3045 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongc530aa62022-03-23 17:45:01 +01003046 return( psa_ssl_status_to_mbedtls( status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003047 }
3048
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003049 *p = (unsigned char) own_pubkey_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003050 content_len = own_pubkey_len + 1;
3051
Neil Armstrong25400452022-03-23 17:44:07 +01003052 /* As RFC 5489 section 2, the premaster secret is formed as follows:
3053 * - a uint16 containing the length (in octets) of the ECDH computation
3054 * - the octet string produced by the ECDH computation
3055 * - a uint16 containing the length (in octets) of the PSK
3056 * - the PSK itself
3057 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003058 unsigned char *pms = ssl->handshake->premaster;
3059 const unsigned char* const pms_end = pms +
Neil Armstrongd8420ca2022-03-23 17:46:04 +01003060 sizeof( ssl->handshake->premaster );
Neil Armstrong0bdb68a2022-03-23 17:46:32 +01003061 /* uint16 to store length (in octets) of the ECDH computation */
3062 const size_t zlen_size = 2;
Neil Armstrongbc5e8f92022-03-23 17:42:50 +01003063 size_t zlen = 0;
Neil Armstrong868af822022-03-09 10:26:25 +01003064
Neil Armstrong25400452022-03-23 17:44:07 +01003065 /* Perform ECDH computation after the uint16 reserved for the length */
Neil Armstrong868af822022-03-09 10:26:25 +01003066 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3067 handshake->ecdh_psa_privkey,
3068 handshake->ecdh_psa_peerkey,
3069 handshake->ecdh_psa_peerkey_len,
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003070 pms + zlen_size,
3071 pms_end - ( pms + zlen_size ),
Neil Armstrong868af822022-03-09 10:26:25 +01003072 &zlen );
3073
3074 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
3075 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3076
Neil Armstrongc530aa62022-03-23 17:45:01 +01003077 if( status != PSA_SUCCESS )
3078 return( psa_ssl_status_to_mbedtls( status ) );
3079 else if( destruction_status != PSA_SUCCESS )
3080 return( psa_ssl_status_to_mbedtls( destruction_status ) );
Neil Armstrong868af822022-03-09 10:26:25 +01003081
Neil Armstrong25400452022-03-23 17:44:07 +01003082 /* Write the ECDH computation length before the ECDH computation */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003083 MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 );
3084 pms += zlen_size + zlen;
Neil Armstrong868af822022-03-09 10:26:25 +01003085
Neil Armstrong868af822022-03-09 10:26:25 +01003086 const unsigned char *psk = NULL;
3087 size_t psk_len = 0;
3088
3089 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
3090 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Neil Armstrong868af822022-03-09 10:26:25 +01003091 /*
3092 * This should never happen because the existence of a PSK is always
3093 * checked before calling this function
3094 */
3095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Neil Armstrong868af822022-03-09 10:26:25 +01003096
Neil Armstronge18ff952022-04-04 18:34:55 +02003097 /* opaque psk<0..2^16-1>; */
3098 if( (size_t)( pms_end - pms ) < ( 2 + psk_len ) )
3099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3100
Neil Armstrong25400452022-03-23 17:44:07 +01003101 /* Write the PSK length as uint16 */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003102 MBEDTLS_PUT_UINT16_BE( psk_len, pms, 0 );
3103 pms += 2;
Neil Armstrong868af822022-03-09 10:26:25 +01003104
Neil Armstrong25400452022-03-23 17:44:07 +01003105 /* Write the PSK itself */
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003106 memcpy( pms, psk, psk_len );
3107 pms += psk_len;
Neil Armstrong868af822022-03-09 10:26:25 +01003108
Neil Armstrongb7ca76b2022-04-04 18:27:15 +02003109 ssl->handshake->pmslen = pms - ssl->handshake->premaster;
Neil Armstrong868af822022-03-09 10:26:25 +01003110 }
3111 else
3112#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3113 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003114#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003115 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003116 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003117 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003118 * opaque psk_identity<0..2^16-1>;
3119 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003120 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003121 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003122 /* We don't offer PSK suites if we don't have a PSK,
3123 * and we check that the server's choice is among the
3124 * ciphersuites we offered, so this should never happen. */
3125 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003126 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003127
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003128 header_len = 4;
3129 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003130
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003131 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003132 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003133 MBEDTLS_SSL_DEBUG_MSG( 1,
3134 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003135 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3136 }
3137
Joe Subbianifbeb6922021-07-16 14:27:50 +01003138 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3139 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003140
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003141 memcpy( ssl->out_msg + header_len,
3142 ssl->conf->psk_identity,
3143 ssl->conf->psk_identity_len );
3144 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3147 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003148 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003149 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003150 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003151 else
3152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3154 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +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
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003162 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3163 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003164 return( ret );
3165 }
3166 else
3167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3169 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003170 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003171#if defined(MBEDTLS_USE_PSA_CRYPTO)
3172 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003173 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003174 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3175#endif /* MBEDTLS_USE_PSA_CRYPTO */
3176
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003177 /*
3178 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3179 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003180 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003181
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003182 if( header_len + 2 + content_len >
3183 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003184 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003185 MBEDTLS_SSL_DEBUG_MSG( 1,
3186 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003187 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3188 }
3189
Joe Subbianifbeb6922021-07-16 14:27:50 +01003190 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3191 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003194 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003195 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003196 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003197 if( ret != 0 )
3198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003200 return( ret );
3201 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003202 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003203 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003205#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
3206 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003208 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003209#if defined(MBEDTLS_USE_PSA_CRYPTO)
3210 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003211 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003212 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3213#endif /* MBEDTLS_USE_PSA_CRYPTO */
3214
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003215 /*
3216 * ClientECDiffieHellmanPublic public;
3217 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003218 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3219 &content_len,
3220 &ssl->out_msg[header_len],
3221 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003222 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003223 if( ret != 0 )
3224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003226 return( ret );
3227 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003228
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003229 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3230 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003231 }
3232 else
Neil Armstrongd8419ff2022-04-12 14:39:12 +02003233#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003237 }
3238
Hanno Beckerafd311e2018-10-23 15:26:40 +01003239#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3240 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3241 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003242 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003243 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003244 MBEDTLS_SSL_DEBUG_MSG( 1,
3245 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003246 }
3247 else
3248#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3249 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003251 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003252 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003253 MBEDTLS_SSL_DEBUG_RET( 1,
3254 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003255 return( ret );
3256 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003257 }
3258 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003259#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3261 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003263 header_len = 4;
3264 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3265 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003266 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 }
Paul Bakkered27a042013-04-18 22:46:23 +02003268 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003270#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3271 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3272 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003273 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003274
3275 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003276 ssl->out_msg + header_len,
3277 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3278 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003279 ssl->conf->f_rng, ssl->conf->p_rng );
3280 if( ret != 0 )
3281 {
3282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3283 return( ret );
3284 }
3285
3286 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3287 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3288 ssl->conf->f_rng, ssl->conf->p_rng );
3289 if( ret != 0 )
3290 {
3291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3292 return( ret );
3293 }
3294 }
3295 else
3296#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003297 {
3298 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003303 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3305 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
3307 ssl->state++;
3308
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003309 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 return( ret );
3313 }
3314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
3317 return( 0 );
3318}
3319
Gilles Peskineeccd8882020-03-10 12:19:08 +01003320#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003322{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003323 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003324 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003332 return( ret );
3333 }
3334
Hanno Becker77adddc2019-02-07 12:32:43 +00003335 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003338 ssl->state++;
3339 return( 0 );
3340 }
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3343 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003344}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003345#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003349 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003350 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003351 size_t n = 0, offset = 0;
3352 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003353 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003355 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003356 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003357#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3358 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3359#else
3360 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3361#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003364
Gilles Peskineeccd8882020-03-10 12:19:08 +01003365#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003366 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003367 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003368 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003369 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003370 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003371#endif
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003376 return( ret );
3377 }
3378
Hanno Becker77adddc2019-02-07 12:32:43 +00003379 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003382 ssl->state++;
3383 return( 0 );
3384 }
3385
Jerry Yufb28b882022-01-28 11:05:58 +08003386 if( ssl->handshake->client_auth == 0 ||
3387 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 {
Johan Pascala89ca862020-08-25 10:03:19 +02003389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3390 ssl->state++;
3391 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003392 }
3393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003395 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 }
3399
3400 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003401 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003403#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003404 if( ssl->handshake->ecrs_enabled )
3405 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3406
3407sign:
3408#endif
3409
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003410 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003411
Ronald Cron90915f22022-03-07 11:11:36 +01003412 /*
3413 * digitally-signed struct {
3414 * opaque handshake_messages[handshake_messages_length];
3415 * };
3416 *
3417 * Taking shortcut here. We assume that the server always allows the
3418 * PRF Hash function and has sent it in the allowed signature
3419 * algorithms list received in the Certificate Request message.
3420 *
3421 * Until we encounter a server that does not, we will take this
3422 * shortcut.
3423 *
3424 * Reason: Otherwise we should have running hashes for SHA512 and
3425 * SHA224 in order to satisfy 'weird' needs from the server
3426 * side.
3427 */
3428 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003429 {
Ronald Cron90915f22022-03-07 11:11:36 +01003430 md_alg = MBEDTLS_MD_SHA384;
3431 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003433 else
Paul Bakker577e0062013-08-28 11:57:20 +02003434 {
Ronald Cron90915f22022-03-07 11:11:36 +01003435 md_alg = MBEDTLS_MD_SHA256;
3436 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003437 }
Ronald Cron90915f22022-03-07 11:11:36 +01003438 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3439
3440 /* Info from md_alg will be used instead */
3441 hashlen = 0;
3442 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003443
Gilles Peskineeccd8882020-03-10 12:19:08 +01003444#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003445 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003446 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003447#endif
3448
3449 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3450 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003451 ssl->out_msg + 6 + offset,
3452 out_buf_len - 6 - offset,
3453 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003454 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003457#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003458 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3459 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3460#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003461 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003462 }
Paul Bakker926af752012-11-23 13:38:07 +01003463
Joe Subbiani6dd73642021-07-19 11:56:54 +01003464 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
Paul Bakker1ef83d62012-04-11 12:09:53 +00003466 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3468 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003469
3470 ssl->state++;
3471
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003472 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 return( ret );
3476 }
3477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003479
Paul Bakkered27a042013-04-18 22:46:23 +02003480 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003482#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3485static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003486{
Janos Follath865b3eb2019-12-16 11:46:15 +00003487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003488 uint32_t lifetime;
3489 size_t ticket_len;
3490 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003491 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003494
Hanno Becker327c93b2018-08-15 13:56:18 +01003495 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003498 return( ret );
3499 }
3500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003504 mbedtls_ssl_send_alert_message(
3505 ssl,
3506 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3507 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003509 }
3510
3511 /*
3512 * struct {
3513 * uint32 ticket_lifetime_hint;
3514 * opaque ticket<0..2^16-1>;
3515 * } NewSessionTicket;
3516 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003517 * 0 . 3 ticket_lifetime_hint
3518 * 4 . 5 ticket_len (n)
3519 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3522 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003525 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3526 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003527 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003528 }
3529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003531
Philippe Antoineb5b25432018-05-11 11:06:29 +02003532 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3533 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003534
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003535 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003540 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3541 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003542 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003543 }
3544
Paul Elliottd48d5c62021-01-07 14:47:05 +00003545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003546
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003547 /* We're not waiting for a NewSessionTicket message any more */
3548 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003550
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003551 /*
3552 * Zero-length ticket means the server changed his mind and doesn't want
3553 * to send a ticket after all, so just forget it
3554 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003555 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003556 return( 0 );
3557
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003558 if( ssl->session != NULL && ssl->session->ticket != NULL )
3559 {
3560 mbedtls_platform_zeroize( ssl->session->ticket,
3561 ssl->session->ticket_len );
3562 mbedtls_free( ssl->session->ticket );
3563 ssl->session->ticket = NULL;
3564 ssl->session->ticket_len = 0;
3565 }
3566
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003567 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3568 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003570 ssl->session_negotiate->ticket = NULL;
3571 ssl->session_negotiate->ticket_len = 0;
3572
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003573 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003574 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3577 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003578 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003579 }
3580
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003581 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003582
3583 ssl->session_negotiate->ticket = ticket;
3584 ssl->session_negotiate->ticket_len = ticket_len;
3585 ssl->session_negotiate->ticket_lifetime = lifetime;
3586
3587 /*
3588 * RFC 5077 section 3.4:
3589 * "If the client receives a session ticket from the server, then it
3590 * discards any Session ID that was sent in the ServerHello."
3591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003593 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003596
3597 return( 0 );
3598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003600
Paul Bakker5121ce52009-01-03 21:22:43 +00003601/*
Paul Bakker1961b702013-01-25 14:49:24 +01003602 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003605{
3606 int ret = 0;
3607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003609 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3611 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003612 ssl->handshake->new_session_ticket != 0 )
3613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003615 }
3616#endif
3617
Paul Bakker1961b702013-01-25 14:49:24 +01003618 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 case MBEDTLS_SSL_HELLO_REQUEST:
3621 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 break;
3623
Paul Bakker1961b702013-01-25 14:49:24 +01003624 /*
3625 * ==> ClientHello
3626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 case MBEDTLS_SSL_CLIENT_HELLO:
Ronald Cron7320e642022-03-08 13:34:49 +01003628 ret = mbedtls_ssl_write_client_hello( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003629 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630
Paul Bakker1961b702013-01-25 14:49:24 +01003631 /*
3632 * <== ServerHello
3633 * Certificate
3634 * ( ServerKeyExchange )
3635 * ( CertificateRequest )
3636 * ServerHelloDone
3637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01003639 ret = ssl_parse_server_hello( ssl );
3640 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3643 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003644 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003647 ret = ssl_parse_server_key_exchange( ssl );
3648 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01003651 ret = ssl_parse_certificate_request( ssl );
3652 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01003655 ret = ssl_parse_server_hello_done( ssl );
3656 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Paul Bakker1961b702013-01-25 14:49:24 +01003658 /*
3659 * ==> ( Certificate/Alert )
3660 * ClientKeyExchange
3661 * ( CertificateVerify )
3662 * ChangeCipherSpec
3663 * Finished
3664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3666 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003667 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01003670 ret = ssl_write_client_key_exchange( ssl );
3671 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01003674 ret = ssl_write_certificate_verify( ssl );
3675 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3678 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003679 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 case MBEDTLS_SSL_CLIENT_FINISHED:
3682 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003683 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003684
Paul Bakker1961b702013-01-25 14:49:24 +01003685 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003686 * <== ( NewSessionTicket )
3687 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003688 * Finished
3689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3691 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003692 ret = ssl_parse_new_session_ticket( ssl );
3693 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003694#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3697 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003698 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 case MBEDTLS_SSL_SERVER_FINISHED:
3701 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003702 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 case MBEDTLS_SSL_FLUSH_BUFFERS:
3705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3706 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01003707 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3710 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003711 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003712
Paul Bakker1961b702013-01-25 14:49:24 +01003713 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01003716 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
3718 return( ret );
3719}
Jerry Yuc5aef882021-12-23 20:15:02 +08003720
Jerry Yufb4b6472022-01-27 15:03:26 +08003721#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */